From 036d731bf29f1a371e4f18245a7498c5a7dcaf73 Mon Sep 17 00:00:00 2001 From: prnmxfqg4 Date: Mon, 4 Jul 2022 21:04:22 +0800 Subject: [PATCH] ADD file via upload --- mysh.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 mysh.c diff --git a/mysh.c b/mysh.c new file mode 100644 index 0000000..a25c070 --- /dev/null +++ b/mysh.c @@ -0,0 +1,103 @@ +#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=/usr/root", 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]; + + while(1) + { + + memset(cmdstr, 0, 255); + read(0,cmdstr, strlen(cmdstr)); + argc_cmd = 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/bin/"); + 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); + + + +} +