| 1 | {{{#!c |
| 2 | |
| 3 | int printf(char*, ...); |
| 4 | int scanf(char*, ...); |
| 5 | int strcmp(char*, char*); |
| 6 | int fork(); |
| 7 | int waitpid(int, int*, int); |
| 8 | void exit(int); |
| 9 | int execve(char*, char**, char**); |
| 10 | |
| 11 | #define NULL (void*)0 |
| 12 | |
| 13 | int main(){ |
| 14 | char command[4096]; |
| 15 | // Sia argv che envp DEVONO terminare con un elemento NULL |
| 16 | char* argv[] = {"/bin/ls", NULL}; |
| 17 | char* envp[] = {NULL}; |
| 18 | |
| 19 | while (1){ |
| 20 | int pid, status; |
| 21 | |
| 22 | printf("MYSHELL$ "); |
| 23 | scanf("%s", command); |
| 24 | if (strcmp(command, "exit") == 0) |
| 25 | break; |
| 26 | pid = fork(); |
| 27 | if (pid != 0) { |
| 28 | waitpid(pid, &status, 0); |
| 29 | } else { |
| 30 | argv[0] = command; |
| 31 | execve(argv[0], argv, envp); |
| 32 | printf("Da qui in poi esegue solo se l'execve e` fallita\n"); |
| 33 | exit(0); |
| 34 | } |
| 35 | } |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | }}} |