/
shell.c
45 lines (39 loc) · 973 Bytes
/
shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include<sys/types.h>
main(){
char buffer[256];
char cwd[1024];
char* token;
char* username;
username = getlogin();
getcwd(cwd, sizeof(cwd));
printf("Hello %s, your current directory is: %s. \n", username, cwd);
for(;;){
if(fgets(buffer, 256, stdin) == NULL || (strcmp(buffer, "exit\n") == 0)){
return(0);
}
//strips the newline character at end of input
size_t ln = strlen(buffer) - 1;
if(buffer[ln] == '\n') buffer[ln] = '\0';
if(strlen(buffer) > 0){
token = strtok(buffer, " ,.");
if(strcmp(token, "repeat") == 0){
token = strtok(NULL, " ,.");
while(token != NULL){
printf("%s ", token);
token = strtok(NULL, " ,.");
}
printf("\n");
}
else if(strcmp(token, "welcome") == 0){
token = strtok(NULL, " ,.");
printf("Hello, %s\n", token);
}
else if(strcmp(token, "junk") == 0){
printf("junk is here\n");
}
}
}
}