changed formating and added cd and exit
This commit is contained in:
@@ -3,6 +3,15 @@
|
||||
#include<sys/wait.h>
|
||||
#include<unistd.h>
|
||||
#include<string.h>
|
||||
int handleBuiltins(char **args) {
|
||||
if(strcmp(args[0], "cd") == 0) {
|
||||
chdir(args[1] == NULL ? getenv("HOME") : args[1]);
|
||||
return 1;
|
||||
}
|
||||
else if(strcmp(args[0], "exit") == 0) {
|
||||
exit(0);
|
||||
} else { return 0;}
|
||||
}
|
||||
char **parse(char *line) {
|
||||
char **args= malloc(64 * sizeof(char *));
|
||||
int i = 0;
|
||||
@@ -18,12 +27,18 @@ char **parse(char *line) {
|
||||
int main() {
|
||||
char line[1024];
|
||||
while(1) {
|
||||
printf("$");
|
||||
char *user = getenv("USER");
|
||||
char dir[1024];
|
||||
getcwd(dir, sizeof(dir));
|
||||
char host[1024];
|
||||
gethostname(host, sizeof(host));
|
||||
printf("%s@%s %s$", user, host, dir);
|
||||
fflush(stdout);
|
||||
char *result = fgets(line, sizeof(line), stdin);
|
||||
if(result == NULL) return 0;
|
||||
char **parsed = parse(line);
|
||||
if(parsed[0] == NULL) { free(parsed); continue; }
|
||||
if(handleBuiltins(parsed)) { free(parsed); continue; }
|
||||
pid_t pid = fork();
|
||||
if(pid == 0) {
|
||||
if(execvp(parsed[0], parsed) == -1 ) {
|
||||
|
||||
Reference in New Issue
Block a user