// author OBLiQUE // copyleft 2009 // simple unix shell skeleton // p0wnbox.com #include #include #include #include #include #include #include #include #include #define MAX_STRING 0xfff #define MAX_ARGS 30 const char *shell_commands[] = {"cd","exit","\0"}; void print_prompt(); int chk_sh_cmds(char *str, char **tok); void sig_int(const int sig_num); int main(int argc, char *arg[]) { char *tok, buf[MAX_STRING], *args[MAX_ARGS+1]; int readlen, sh_cmd_num, arg_num, status; pid_t pid; // register SIGINT signal handler to disable ctrl+c if (signal(SIGINT,sig_int)==SIG_ERR) fprintf(stderr,"signal(): cannot register signal handler\n"); print_prompt(); while (readlen=read(STDIN_FILENO,buf,MAX_STRING)) { // read the line and return the length buf[readlen-1]='\0'; // remove \n from the end if (sh_cmd_num=chk_sh_cmds(buf,&tok)) { // check if its a shell command switch (sh_cmd_num) { case 1: // cd if (chdir(tok)<0) // change directory fprintf(stderr,"chdir(): %s\n",strerror(errno)); break; case 2: // exit printf("exit\n"); exit(EXIT_SUCCESS); } } else { // if it's not a shell command tok=strtok(buf," "); // replace the first space with '\0' and take the tokken arg_num=0; // initiliaze the arg_num while (tok!=NULL && arg_num