int main() { char* cmd = malloc(256*sizeof(char)); char* cmd_arg[10]; char* path = "user"; int cmdlen,i,j,tag; if(chdir("/home/user")<0) { perror("chdir err"); return -1; } do { memset(cmd,0,256); printf("-=Mini Shell %s=-$",path); fgets(cmd,256,stdin); cmdlen = strlen(cmd); cmdlen--; cmd[cmdlen] = '\0'; i=0;j=0;tag =0; memset(cmd_arg,0,sizeof(cmd_arg)); while(i<cmdlen && j <10) { if(cmd[i] == ' ') { cmd[i] = '\0'; tag =0; } else { if(tag == 0) cmd_arg[j++] = cmd+i; tag =1; } i++; } if(j>=10 && i< cmdlen) { printf("Too Many Arguments"); continue; } /// quit command if(strcmp(cmd_arg[0],"quit") == 0) break; if(strcmp(cmd_arg[0],"cd") == 0) { path = do_cd(cmd_arg); continue; } // new execute new_exec(cmd_arg); }while(1); }
/* * We have started or attached to a process. Set the appropriate flags, and * print a banner showing that we are now tracing it. */ static void new_proc(struct trace_proc * proc, int follow_fork) { int fl; /* Set the desired tracing options. */ fl = TO_ALTEXEC; if (follow_fork) fl |= TO_TRACEFORK; (void)ptrace(T_SETOPT, proc->pid, 0, fl); /* * When attaching to an arbitrary process, this process might be in the * middle of an execve(). Now that we have enabled TO_ALTEXEC, we may * now get a SIGSTOP signal next. Guard against this by marking the * first system call as a possible execve(). */ if ((proc->trace_flags & (TF_ATTACH | TF_STOPPING)) == TF_ATTACH) proc->trace_flags |= TF_EXEC; new_exec(proc); }