Last active
August 29, 2015 14:12
-
-
Save ntddk/114601ae343709726874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <unistd.h> | |
| #include <string.h> | |
| #include <errno.h> | |
| #include <sys/types.h> | |
| void split(char *buf, char** args, size_t sargs, size_t *nargs) | |
| { | |
| char *bargs[sargs], **cp, *pbuf; | |
| int i, j; | |
| pbuf = bargs[0] = args[0] = buf; | |
| for(cp = bargs; (*cp = strsep(&pbuf, " \n\t")) != NULL;) if ((*cp != '\0') || (++cp >= &bargs[sargs])) break; | |
| for (i = j++; bargs[i] != NULL;) if(strlen(bargs[j]) > 0) args[i++]=bargs[j]; | |
| *nargs = i; | |
| args[*nargs] = NULL; | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| char buf[1<<16], *args[1<<16]; | |
| int *status; | |
| size_t nargs; | |
| pid_t pid; | |
| while(1) | |
| { | |
| printf("$ "); | |
| fgets(buf, sizeof(buf), stdin); | |
| split(buf, args, sizeof(*args), &nargs); | |
| if (nargs == 0) continue; | |
| if (!strcmp(args[0], "exit")) exit(0); | |
| if (pid = fork()) pid = wait(status); else if( execvp(args[0], args)) exit(2); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment