From a11bf5d643b0e48054e13c6e9bc35281c1df35aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cchenyue=E2=80=9D?= <10195501438@stu.ecnu.edu.cn> Date: Wed, 10 Mar 2021 17:28:38 +0800 Subject: [PATCH] init --- fun.h | 12 ++++++ fun_1.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fun_2.c | 23 ++++++++++++ main.c | 17 +++++++++ 4 files changed, 181 insertions(+) create mode 100644 fun.h create mode 100644 fun_1.c create mode 100644 fun_2.c diff --git a/fun.h b/fun.h new file mode 100644 index 0000000..0f4f5ee --- /dev/null +++ b/fun.h @@ -0,0 +1,12 @@ +#ifndef FUN_H_INCLUDED +#define FUN_H_INCLUDED + +#define MAXLINE (80) + +void history(char *cmdline); +void print_his(char *argv); + +void eval(char *cmdline); +int parseline(const char *cmdline, char **argv); + +#endif // FUN_H_INCLUDED diff --git a/fun_1.c b/fun_1.c new file mode 100644 index 0000000..9640173 --- /dev/null +++ b/fun_1.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include "fun.h" + + +void eval(char *cmdline) +{ + char *argv[MAXLINE]; + char buf[MAXLINE]; + int isBg; + int pid; + + strcpy(buf,cmdline); + isBg = parseline(cmdline,argv); + + if(argv[0] == NULL) + { + return; + } + + if(!builtin_cmd(argv)) + { +/* + if((pid=fork())==0) + { + setpgid(0,0); + if(execvp(argv[0],argv)<0) + { + printf("%s:Command not found.\n",argv[0]); + exit(0); + } + } + + if(!isBg) + { + waitfg(pid); + } + else + { + printf("[%d] (%d) %s",pid2jid(pid),pid,cmdline); + } +*/ + } + return; +} + +int parseline(const char *cmdline, char **argv) +{ + static char array[MAXLINE]; + char *buf = array; + char *delim; + int argc; + int bg; + + strcpy(buf, cmdline); + buf[strlen(buf)-1] = ' '; + while (*buf && (*buf == ' ')) + buf++; + + argc = 0; + if (*buf == '\'') + { + buf++; + delim = strchr(buf, '\''); + } + else + { + delim = strchr(buf, ' '); + } + + while (delim) + { + argv[argc++] = buf; + *delim = '\0'; + buf = delim + 1; + while (*buf && (*buf == ' ')) + { + buf++; + } + + if (*buf == '\'') + { + buf++; + delim = strchr(buf, '\''); + } + else + { + delim = strchr(buf, ' '); + } + } + argv[argc] = NULL; + + if (argc == 0) + return 1; + + if ((bg = (*argv[argc-1] == '&')) != 0) + { + argv[--argc] = NULL; + } + return bg; +} + +int builtin_cmd(char **argv) +{ + if(!strcmp(argv[0],"quit")||!strcmp(argv[0],"exit")) + { + exit(0); + } + if(!strcmp(argv[0],"history")) + { + print_his(argv[1]); + return 1; + } + if(!strcmp(argv[0],"jobs")) + { + return 1; + } + if(!strcmp(argv[0],"bg")||!strcmp(argv[0],"fg")) + { + return 1; + } + if(!strcmp(argv[0], "&")) + { + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/fun_2.c b/fun_2.c new file mode 100644 index 0000000..b7d0d71 --- /dev/null +++ b/fun_2.c @@ -0,0 +1,23 @@ +#include +#include "fun.h" + +static int num = 0; +static char storage[MAXLINE][MAXLINE]; + +void history(char *cmdline) +{ + strcpy(storage[num], cmdline); + num++; +} + +void print_his(char *argv) +{ + + int i = num - atoi(argv); + if(i < 0) + { + i = 0; + } + for(; i +#include +#include "fun.h" + +int main() +{ + char cmdline[MAXLINE]; + + while(1) + { + printf("COMMAND->"); + fgets(cmdline, MAXLINE, stdin); + eval(cmdline); + history(cmdline); + } + return 0; +}