Facebook
From Colossal Ostrich, 3 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 122
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. int find_user (char username[100],char pass [100])
  9. {
  10.     char *line = NULL;
  11.     size_t length = 0;
  12.     FILE *open_status = fopen ("user","r");
  13.     if(open_status==NULL)
  14.     {
  15.         printf ("Acest fisier nu poate fi deschis.\n");
  16.         return 0;
  17.     }
  18.     char *token = NULL;
  19.     while (getline(&line,&length,open_status)!=-1)
  20.     {
  21.         token = strtok(line, ".");
  22.         if( strcmp(token,username) == 0)
  23.         {
  24.             token=strtok(NULL,".");
  25.             if(strcmp(token,pass)==0)
  26.             {
  27.                
  28.                 free(line);
  29.                 return 1;
  30.             }
  31.  
  32.             free(line);
  33.             return 0;
  34.             }
  35.         }
  36.    
  37.  
  38.     if ( line)
  39.         free(line);
  40.     if(token)
  41.         free(token);
  42.  
  43.     fclose(open_status);
  44.     return 0;
  45. }
  46.  
  47. int main() {
  48.         char comanda[100];
  49.         char username[100];
  50.         char pass[100];
  51.         pid_t pid;
  52.         int fduserparent[2];
  53.         int fdpassparent[2];
  54.         int verification_pipe[2];
  55.        
  56.  
  57.       do
  58.       {
  59.         scanf("%s",comanda);
  60.         if(strcmp(comanda,"login")==0)
  61.                 {
  62.                         pid=fork();
  63.                         if(pid==-1)
  64.  
  65.                         {
  66.                                 printf ("Fail on fork!\n");
  67.                                 return 1;
  68.                         }
  69.                         else if(pid>0)
  70.                         {
  71.                                 int size;
  72.                                 //ne aflam in procesul parinte
  73.                                 printf("Introduceti username-ul:");
  74.                                 scanf("%s", username);
  75.                                 printf("Introduceti parola:");
  76.                                 scanf("%s", pass);
  77.                                 close(fduserparent[0]);
  78.                                 write(fduserparent[1],username,strlen(username)+1);
  79.                                 close(fdpassparent[0]);
  80.                                 write(fdpassparent[1],pass,strlen(pass)+1);
  81.                                 close(fduserparent[1]);
  82.                                 close(fdpassparent[1]);
  83.                                 wait(NULL);
  84.                                 printf("Ne am intors in parinte:\n");
  85.                                 close(verification_pipe[1]);
  86.                                 read(verification_pipe[0],&size,sizeof(int));
  87.                                 char *verification_message=malloc(size*sizeof(char));
  88.                                 read(verification_pipe[0],verification_message,size+1);
  89.                     printf("%s\n",verification_message );
  90.  
  91.                                 return 1;
  92.  
  93.                         }
  94.                         else if(pid==0)
  95.                         {
  96.                                 printf("Ne aflam in procesul copil:\n");
  97.                                 char nume[100];
  98.                                 char parola[100];
  99.                                 int verificare;
  100.                                 //ne aflam in procesul copil unde vom verifica validitatea datelor
  101.                     close (fduserparent[1]);
  102.                     close(fdpassparent[1]);
  103.                     read(fduserparent[0],nume,100);
  104.                     read(fdpassparent[0],parola,100);
  105.                     verificare=find_user(nume,parola);//deocamdata poate fi 0 sau 1
  106.                    
  107.                     //acum trebuie sa transmitem inapoi catre parinte
  108.                     close (fduserparent[0]);
  109.                     close (fdpassparent[0]);
  110.                     close (verification_pipe[0]);
  111.                     if(verificare==1)
  112.                     {
  113.                         int size=strlen("autentificare cu succes!");
  114.                         write (verification_pipe[1],&size,4);
  115.                         write (verification_pipe[1],"autentificare cu succes!",size+1);
  116.                         close(verification_pipe[1]);
  117.                         exit(0);
  118.                     }
  119.                     else if(verificare==0)
  120.                     {
  121.                         int size=strlen("autentificare fara succes!");
  122.                         write (verification_pipe[1],&size,4);
  123.                         write (verification_pipe[1],"autentificare fara succes!",size+1);
  124.                         close(verification_pipe[1]);
  125.                         exit(0);
  126.                     }  
  127.  
  128.                         }
  129.                 }
  130.  
  131.  
  132.       }while(1);
  133.        return 0;
  134. }