msh in its own long standing directory now
This commit is contained in:
parent
1fdd54e80f
commit
a85dffe4da
@ -1,10 +1,14 @@
|
||||
# Single make file for everything becauz lazy
|
||||
object=hw.c
|
||||
object=homework/msh/msh.c
|
||||
cc=gcc
|
||||
output=bin
|
||||
|
||||
default:
|
||||
$(cc) $(object) -o $(output)
|
||||
|
||||
run:
|
||||
./bin
|
||||
|
||||
|
||||
clean:
|
||||
rm -f $(output)
|
||||
|
@ -1,46 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
if(!strlen(buffer)) {
|
||||
return 0;
|
||||
}
|
||||
// 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user