From f256d2b80d14bb43df2861f4eaebf9b4308cd9fa Mon Sep 17 00:00:00 2001 From: prnmxfqg4 Date: Thu, 7 Jul 2022 12:51:32 +0800 Subject: [PATCH] ADD file via upload --- mysh.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 mysh.c diff --git a/mysh.c b/mysh.c new file mode 100644 index 0000000..afd8cad --- /dev/null +++ b/mysh.c @@ -0,0 +1,104 @@ +#define __LIBRARY__ +#include +#include +#include +#include +#include +#include +#include +#define PATH_MAX 1024 +#define ENOEXEC 8 +#define MAX_NUMBER_BYTES 1024 +char tmp_path[PATH_MAX+1]; +static char * envp[] = { "HOME=/", NULL, NULL }; +char ** environ; +FILE __stdin; +FILE __stdout; +_syscall3(pid_t ,waitpid,pid_t ,pid,int * ,wait_stat,int, options) +_syscall0(int,fork) +_syscall0(int,pause) +_syscall0(int,sync) +_syscall1(int,chdir,const char *, filename) +_syscall3(int,write,int ,fd,const char *, buf, off_t ,count) +_syscall3(int, execve,const char *, filename, char **, argv, char ** ,envp) +_syscall2(int, getcwd,char *, buf, size_t, size) +_syscall2(int ,access,const char *, filename, mode_t ,mode) +_syscall3(int,read,int ,fildes,char *, buf, off_t ,count) +_syscall2(int, stat,const char * ,filename, struct stat *, stat_buf) + + + +int printf(const char *fmt,...) +{ + write(2,fmt,strlen(fmt)); +} + +pid_t wait(int * wait_stat) +{ + return waitpid(-1,wait_stat,0); +} + + +void main(int argc, char *argv[]) +{ + int pid; + int status; + char dirname[80]; + char cmdstr[255]; + int argc_cmd; + char *argv_cmd[10]; + char *p; + char a[50]; + int num; + char k[80]="usr/root"; + chdir("/usr/root"); + while(1) + { + printf(k); + printf("#"); + memset(cmdstr, 0, 255); + num=read(0,cmdstr, 255); + argc_cmd = 0; + cmdstr[num-1]='\0'; + p = strtok(cmdstr, " \n\t"); + while (p && argc_cmd < 9){ + argv_cmd[argc_cmd] = p; + p = strtok(NULL, " \n\t"); + argc_cmd++; + } + argv_cmd[argc_cmd] = NULL; + if (argc_cmd == 0) + continue; + if (strcmp(argv_cmd[0], "exit") == 0) + break; + if (strcmp(argv_cmd[0], "sync") == 0){ + sync(); + continue; + } + if (strcmp(argv_cmd[0], "cd") == 0){ + if ((argc_cmd == 1) || (strcmp(argv_cmd[1], "~") == 0)) + argv_cmd[1] = "/"; + if (chdir(argv_cmd[1]) < 0) + printf("cd error\n"); + continue; + } + + if (!(pid = fork())) + { + strcpy(a,"/usr/root/"); + strcat(a,argv_cmd[0]); + if (execve(a, argv_cmd,envp) == -1) + {printf("execve error\n");} + exit(1); + } + while (1) + if (pid == wait(&status)) + break; + + } + exit(0); + + + +} +