Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <string.h>
- #include <stdlib.h>
- /*Programul are nevoie de un fisier numit "config.txt" care sa contina o lista cu username-urile valide pentru a functiona */
- void executeCmd(char* cmd) {
- int pfd[2];
- char mesaj[500000];
- memset(mesaj, '\0', sizeof(mesaj));
- /* cream pipe-ul */
- if (pipe (pfd) == -1)
- {
- fprintf (stderr, "pipe\n");
- exit (1);
- }
- /* cream primul copil */
- switch (fork ())
- {
- case -1:
- fprintf (stderr, "fork - 1\n");
- exit (1);
- case 0:
- close (1);
- /*duplicam descriptorul de scriere al pipe-ului
- la iesirea standard (1)*/
- if (dup (pfd[1]) != 1)
- {
- fprintf (stderr, "dup - 1\n");
- exit (1);
- }
- /* putem inchide descriptorii pipe-ului,
- din moment ce am realizat duplicarea */
- close (pfd[0]);
- close (pfd[1]);
- char* index;
- char* comanda[10];
- int start = 0, i=0;
- index = strstr(cmd, " ");
- while(index != NULL) {
- index[0] = '\0';
- comanda[i++] = &cmd[start];
- start += strlen(&cmd[start]) + 1;
- index = strstr(&cmd[start], " ");
- }
- comanda[i] = &cmd[start];
- comanda[i+1] = NULL;
- execvp(comanda[0], comanda);
- fprintf (stderr, "exec - 1 this\n");
- exit(1);
- }
- /* cream al doilea copil */
- switch (fork ())
- {
- case -1:
- fprintf (stderr, "fork - 2\n");
- exit (1);
- case 0:
- ;
- char raspuns[500000];
- read(pfd[0], raspuns, 499999);
- char* ptr; int cnt = 0;
- ptr = strstr(raspuns, "\n");
- while(ptr != NULL) {
- ptr = strstr(ptr+1, "\n");
- cnt++;
- }
- cmd[4] = '\0';
- if(strcmp(cmd, "grep") == 0) sprintf(raspuns, "Numarul de linii relevante: %d", cnt);
- int lungimeRaspuns = strlen(raspuns);
- write(pfd[1], &lungimeRaspuns, sizeof(int));
- write(pfd[1], raspuns, strlen(raspuns));
- close(pfd[1]);
- close(pfd[0]);
- exit (1);
- }
- /* parintele */
- while (wait(NULL) != -1);
- int lungimeRaspuns;
- read(pfd[0], &lungimeRaspuns, sizeof(int));
- read(pfd[0], mesaj, lungimeRaspuns);
- printf("Raspunsul are %d bytes.\nRaspuns:\n%s", lungimeRaspuns, mesaj);
- close (pfd[0]);
- close (pfd[1]);
- memset(cmd, '\0', 500);
- return;
- }
- int main() {
- char name[50];
- char users[100][50];
- FILE *fisier = fopen("config.txt", "r");
- int i = 0, j, gasit = 0;
- while (fgets(users[i], sizeof(users[i]), fisier) != NULL) {
- users[i][strlen(users[i])-1] = '\0'; i++;
- }
- printf("\nUsername: ");
- fgets(name, 49, stdin);
- name[strlen(name)-1] = '\0';
- while(strcmp(name, "quit") != 0) {
- gasit = 0;
- for(j = 0; j < i; j++) {
- if(strcmp(name, users[j]) == 0) gasit = 1;
- }
- if(gasit) break;
- printf("Usernameul nu este valid.\n");
- printf("\nUsername: ");
- fgets(name, 49, stdin); name[strlen(name)-1] = '\0';
- }
- if(strcmp(name, "quit") == 0) return 0;
- printf("Ai fost autentificat cu succes.\n");
- char cmd[500];
- printf("\nComanda: ");
- fgets(cmd, 499, stdin);
- cmd[strlen(cmd)-1] = '\0';
- while (strcmp(cmd, "quit") != 0) {
- executeCmd(cmd); printf("\nComanda: ");
- fgets(cmd, 499, stdin); cmd[strlen(cmd)-1] = '\0';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement