streams are broken but manual input is correct for test4
This commit is contained in:
parent
c02a7905b2
commit
39bfd0e7ff
@ -2,6 +2,7 @@
|
||||
object=homework/msh/msh.c
|
||||
cc=gcc
|
||||
output=msh
|
||||
tmps=*txt
|
||||
|
||||
default:
|
||||
$(cc) $(object) -o $(output)
|
||||
@ -12,3 +13,4 @@ run:
|
||||
|
||||
clean:
|
||||
rm -f $(output)
|
||||
rm -f $(tmps)
|
||||
|
@ -15,16 +15,23 @@
|
||||
#define RESP_DATE 2
|
||||
#define RESP_ECHO 3
|
||||
#define RESP_SHELL 4
|
||||
#define RESP_CD_FAIL -1
|
||||
#define RESP_CD_SUCCESS 6
|
||||
|
||||
// Sub statuses that are generally nice to have
|
||||
#define CD_NOP 0x1
|
||||
|
||||
void echo(const char*);
|
||||
void remove_newline(char*);
|
||||
void exit_branch(const char*);
|
||||
int builtin_response(char*);
|
||||
int cd_handler(char**, const int);
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
FILE* file = NULL;
|
||||
if(argc == 2) {
|
||||
file = fopen(argv[1], "r");
|
||||
if(!file) { return 1; }
|
||||
}
|
||||
char buffer[MAX_BUF];
|
||||
while(1) {
|
||||
@ -35,6 +42,7 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
else {
|
||||
fgets(buffer, MAX_BUF, file);
|
||||
printf("buffer: %s\n", buffer);
|
||||
}
|
||||
|
||||
// Deal with EOF input
|
||||
@ -51,6 +59,8 @@ int main(int argc, char** argv) {
|
||||
echo(buffer);
|
||||
case RESP_SHELL: // shell command handler was used
|
||||
continue;
|
||||
case RESP_CD_FAIL:
|
||||
printf("msh: cd: No such file or directory\n");
|
||||
}
|
||||
memset(buffer, 0x00, MAX_BUF); // reset buffer after each usage
|
||||
}
|
||||
@ -92,6 +102,9 @@ int builtin_response(char* buffer) {
|
||||
return RESP_DATE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Populate tokens
|
||||
char *tokens[64];
|
||||
int idx = 0;
|
||||
for(char* cur = strtok(buffer, STRING_DELIMITER); cur != NULL; cur=strtok(NULL, STRING_DELIMITER)) {
|
||||
@ -102,6 +115,12 @@ int builtin_response(char* buffer) {
|
||||
idx++;
|
||||
}
|
||||
|
||||
// Handling director changes
|
||||
int cd_result = cd_handler(tokens, idx);
|
||||
if(cd_result != CD_NOP) {
|
||||
return cd_result;
|
||||
}
|
||||
|
||||
// Execute child process
|
||||
pid_t child = fork();
|
||||
if(child != 0) {
|
||||
@ -120,3 +139,13 @@ int builtin_response(char* buffer) {
|
||||
|
||||
}
|
||||
|
||||
int cd_handler(char** tokens, const int t_count) {
|
||||
if(!strcmp(tokens[0], "cd")) {
|
||||
if(t_count < 2) {
|
||||
int ret = chdir(getenv("HOME"));
|
||||
return ret;
|
||||
}
|
||||
return chdir(tokens[1]);
|
||||
}
|
||||
return CD_NOP;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user