Add main.c
This commit is contained in:
parent
bd74b43ea0
commit
ed0851eeca
1 changed files with 28 additions and 0 deletions
28
main.c
Normal file
28
main.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(void) {
|
||||
char buf[256];
|
||||
char *args[64];
|
||||
|
||||
while (1) {
|
||||
printf("%s ", getuid() ? "$" : "#");
|
||||
fflush(stdout);
|
||||
|
||||
if (!fgets(buf, sizeof(buf), stdin)) break;
|
||||
buf[strcspn(buf, "\n")] = 0;
|
||||
if (!buf[0]) continue;
|
||||
if (!strcmp(buf, "exit")) break;
|
||||
|
||||
int i = 0;
|
||||
char *tok = strtok(buf, " ");
|
||||
while (tok && i < 63) args[i++] = tok, tok = strtok(NULL, " ");
|
||||
args[i] = NULL;
|
||||
|
||||
if (fork() == 0) execvp(args[0], args), perror("exec"), exit(1);
|
||||
else wait(NULL);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue