Advertisement
ionutHulub

Tema 1 retele

Nov 10th, 2012
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.30 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. /*Programul are nevoie de un fisier numit "config.txt" care sa contina o lista cu username-urile valide pentru a functiona */
  8. void executeCmd(char* cmd) {
  9.   int pfd[2];
  10.   char mesaj[500000];
  11.   memset(mesaj, '\0', sizeof(mesaj));
  12.   /* cream pipe-ul */
  13.   if (pipe (pfd) == -1)
  14.     {
  15.       fprintf (stderr, "pipe\n");
  16.       exit (1);
  17.     }
  18.  
  19.   /* cream primul copil */
  20.   switch (fork ())
  21.     {
  22.     case -1:
  23.       fprintf (stderr, "fork - 1\n");
  24.       exit (1);
  25.     case 0:
  26.        close (1);
  27.        /*duplicam descriptorul de scriere al pipe-ului
  28.          la iesirea standard (1)*/
  29.       if (dup (pfd[1]) != 1)
  30.         {
  31.           fprintf (stderr, "dup - 1\n");
  32.           exit (1);
  33.         }
  34.       /* putem inchide descriptorii pipe-ului,
  35.          din moment ce am realizat duplicarea */
  36.       close (pfd[0]);
  37.       close (pfd[1]);
  38.  
  39.       char* index;
  40.       char* comanda[10];
  41.       int start = 0, i=0;
  42.       index = strstr(cmd, " ");
  43.       while(index != NULL) {
  44.         index[0] = '\0';
  45.         comanda[i++] = &cmd[start];
  46.         start += strlen(&cmd[start]) + 1;
  47.         index = strstr(&cmd[start], " ");
  48.       }
  49.       comanda[i] = &cmd[start];
  50.       comanda[i+1] = NULL;
  51.       execvp(comanda[0], comanda);
  52.       fprintf (stderr, "exec - 1 this\n");
  53.       exit(1);
  54.     }
  55.   /* cream al doilea copil */
  56.   switch (fork ())
  57.     {
  58.     case -1:
  59.       fprintf (stderr, "fork - 2\n");
  60.       exit (1);
  61.     case 0:
  62.       ;
  63.       char raspuns[500000];
  64.       read(pfd[0], raspuns, 499999);
  65.       char* ptr; int cnt = 0;
  66.       ptr = strstr(raspuns, "\n");
  67.       while(ptr != NULL) {
  68.         ptr = strstr(ptr+1, "\n");
  69.         cnt++;
  70.       }
  71.       cmd[4] = '\0';
  72.       if(strcmp(cmd, "grep") == 0)        sprintf(raspuns, "Numarul de linii relevante: %d", cnt);
  73.       int lungimeRaspuns = strlen(raspuns);
  74.       write(pfd[1], &lungimeRaspuns, sizeof(int));
  75.       write(pfd[1], raspuns, strlen(raspuns));
  76.       close(pfd[1]);
  77.       close(pfd[0]);
  78.       exit (1);
  79.     }
  80.  
  81.   /* parintele */
  82.   while (wait(NULL) != -1);
  83.   int lungimeRaspuns;
  84.   read(pfd[0], &lungimeRaspuns, sizeof(int));
  85.   read(pfd[0], mesaj, lungimeRaspuns);
  86.   printf("Raspunsul are %d bytes.\nRaspuns:\n%s", lungimeRaspuns, mesaj);
  87.   close (pfd[0]);
  88.   close (pfd[1]);
  89.   memset(cmd, '\0', 500);
  90.   return;
  91. }
  92.  
  93.  
  94.  
  95. int main() {
  96. char name[50];
  97. char users[100][50];
  98. FILE *fisier = fopen("config.txt", "r");
  99. int i = 0, j, gasit = 0;
  100.  
  101. while (fgets(users[i], sizeof(users[i]), fisier) != NULL) {
  102.   users[i][strlen(users[i])-1] = '\0'; i++;
  103. }
  104.  
  105. printf("\nUsername: ");
  106. fgets(name, 49, stdin);
  107. name[strlen(name)-1] = '\0';
  108. while(strcmp(name, "quit") != 0) {
  109.   gasit = 0;
  110.   for(j = 0; j < i; j++) {
  111.     if(strcmp(name, users[j]) == 0)  gasit = 1;
  112.   }
  113.   if(gasit) break;
  114.   printf("Usernameul nu este valid.\n");
  115.   printf("\nUsername: ");
  116.   fgets(name, 49, stdin); name[strlen(name)-1] = '\0';
  117. }
  118.  
  119. if(strcmp(name, "quit") == 0) return 0;
  120.  
  121. printf("Ai fost autentificat cu succes.\n");
  122.  
  123. char cmd[500];
  124. printf("\nComanda: ");
  125. fgets(cmd, 499, stdin);
  126. cmd[strlen(cmd)-1] = '\0';
  127. while (strcmp(cmd, "quit") != 0) {
  128.   executeCmd(cmd); printf("\nComanda: ");
  129.   fgets(cmd, 499, stdin); cmd[strlen(cmd)-1] = '\0';
  130. }
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement