I have a simple C shell that recognizes change directory and exit commands. Any other commands are printed as is to the command line. I need to simulate the linux 'history' command using an array of stucts. Can anyone help me get started on this? I'm having trouble with how to do this. I believe I need to malloc size for this but not sure how to and how much to..
Code:
struct cmd{
char * cmd;
}
This is my simple shell:
Code:
#define MAX_LENGTH 4096
#define DELIMS " \t\r\n"
int main(int argc, char *argv[]) {
char buffer[MAX_LENGTH];
while (1) {
fprintf(stderr, "$");
//Exit if not reading from stdin
if (!fgets(buffer, MAX_LENGTH, stdin)) {
break;
}
char* token = strtok(buffer, DELIMS); //store each token
if(token != NULL) {
//exit
if(strcmp(token, "exit") == 0) {
ExitSmash();
}
//cd
else if (strcmp(token, "cd") == 0) {
char* args = strtok(0, DELIMS);
ChangeDirectory(args);
}
//tokenize & print to command line
else {
int i = 0;
while(token != NULL) {
printf ("[%d] %s\n",i++, token);
token = strtok (NULL, DELIMS);
}
}
}
}
return 0;
}