From c642a88fe64593799d35db3bbc55ab5659ec6734 Mon Sep 17 00:00:00 2001 From: shockrah Date: Sun, 15 Sep 2019 23:45:47 -0700 Subject: [PATCH] base for h3 --- 334/homework/3/chap5.txt | 14 +++++++++++++ 334/homework/3/chap6.txt | 14 +++++++++++++ 334/homework/3/msh.c | 43 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 334/homework/3/chap5.txt create mode 100644 334/homework/3/chap6.txt create mode 100644 334/homework/3/msh.c diff --git a/334/homework/3/chap5.txt b/334/homework/3/chap5.txt new file mode 100644 index 0000000..a98d582 --- /dev/null +++ b/334/homework/3/chap5.txt @@ -0,0 +1,14 @@ +# +# Problem lines begin with "#@". +# For each problem line, enter your answer on the following line. +# Do not modify this file in any way except to enter your answers. +# + +#@ 1 -- enter 'a', 'b', or 'c' (don't use quotes in any answer) +b +#@ 2 -- enter 'a', 'b', or 'c' +a +#@ 3 -- enter 'a', 'b', or 'c' +b +#@ 4 -- enter 'a', 'b', or 'c' +a diff --git a/334/homework/3/chap6.txt b/334/homework/3/chap6.txt new file mode 100644 index 0000000..c874cce --- /dev/null +++ b/334/homework/3/chap6.txt @@ -0,0 +1,14 @@ +# +# Problem lines begin with "#@". +# For each problem line, enter your answer on the following line. +# Do not modify this file in any way except to enter your answers. +# + +#@ 1 -- enter 'a' or 'b' (don't use quotes in any answer) +b +#@ 2 -- enter 'a', 'b', or 'c' +a +#@ 3 -- enter 'T' or 'F' +F +#@ 4 -- enter 'T' or 'F' +F diff --git a/334/homework/3/msh.c b/334/homework/3/msh.c new file mode 100644 index 0000000..956d3de --- /dev/null +++ b/334/homework/3/msh.c @@ -0,0 +1,43 @@ +#include +#include +#include + +#define PROMPT "msh> " +#define EXIT_CMD "exit" +#define MAX_BUF 121 + +void echo(const char*); +void remove_newline(char*); +void exit_branch(const char*); + +int main(void) { + char buffer[MAX_BUF]; + while(1) { + printf("%s", PROMPT); + fgets(buffer, MAX_BUF, stdin); + // process the input + remove_newline(buffer); + exit_branch(buffer); + echo(buffer); + } + return 0; +} + +void echo(const char* buf) { + printf("%s\n", buf); +} + + +void remove_newline(char* buf) { + char* c = buf; + while(*c != '\n') { c++; } + *c='\0'; +} + +void exit_branch(const char* buf) { + if(strcmp(buf, EXIT_CMD)) { + return; + } + exit(0); + return; // just for gcc but exit shouldn't fail +}