Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<sys/types.h>
- #include<string.h>
- #include<sys/wait.h>
- char* linux_command(char query[]) {
- FILE *pf;
- char line[100];
- char *data = (char *) malloc(sizeof(char) * 512);
- // Setup our pipe for reading and execute our command.
- pf = popen(query,"r");
- // Error handling
- // Get the data from the process execution
- while(fgets(line, 100 , pf))
- strcat(data, line);
- // the data is now in 'data'
- if (pclose(pf) != 0)
- fprintf(stderr," Error: Failed to close command stream \n");
- return data;
- }
- int main() {
- int fd1[2], fd2[2];
- if (pipe(fd1)==-1) {
- fprintf(stderr, "Pipe Failed" );
- return 1;
- }
- if (pipe(fd2)==-1) {
- fprintf(stderr, "Pipe Failed" );
- return 1;
- }
- pid_t p = fork();
- if(p > 0) {
- char nume[100];
- printf("Introduceti numele utilizatorului: ");
- scanf("%s", nume);
- close(fd1[0]);
- write(fd1[1], nume, strlen(nume)+1);
- close(fd1[1]);
- wait(NULL);
- close(fd2[1]);
- char informations[300];
- read(fd2[0], informations, 300);
- printf("Informatii despre utilizatorul ales:\n%s\n", informations);
- close(fd2[0]);
- }
- else if (p == 0) {
- close(fd1[1]);
- char query[200] = "finger ";
- char nume[100];
- read(fd1[0], nume, 100);
- strcat(query, nume);
- close(fd1[0]);
- close(fd2[0]);
- char *response = linux_command(query);
- write(fd2[1], response, strlen(response)+1);
- close(fd2[1]);
- exit(0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement