-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
99 lines (96 loc) · 2.11 KB
/
parse.c
File metadata and controls
99 lines (96 loc) · 2.11 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <termios.h>
#include "switch.h"
int manageEnviron(char *argv[], int args){
/*
runs the appropriate function from the command outline
input: array of strings and lenfth of the array
returns: 1 or 0
*/
if(!strcmp(argv[0],"cd")){
changeDirectory(argv,args);
}
else if(!strcmp(argv[0],"mkdir")){
makeDirectory(argv,args);
} else if(!strcmp(argv[0],"rm")){
removeFile(args,argv);
} else if(!strcmp(argv[0],"ls")){
list(args,argv);
} else if(!strcmp(argv[0],"exit")){
printf("BOOYAKASHA!!!\n");
} else {
printf("No function called %s.\n", argv[0]);
}
}
char **inputToCommand(char *input, int *len){
/*
splits the input into commands by spaces
input: string that contains commands
returns an array of strings that are the commands
*/
char **command = malloc(8 * sizeof(char *));
char *token;
int index = 0;
token = strtok(input," ");
while(token != NULL){
command[index] = token;
index++;
(*len)++;
token = strtok(NULL," ");
}
return command;
}
int getCommands(char **commands){
/*
TODO: make this be a switch that calls the correct one
*/
if(strcmp(commands[0], "exit")){
return -1;
}
else {
return 0;
}
}
int main(){
printf("COWABUNGA DUDES!\n");
char** command;
char input[50];
pid_t child;
int randomVariable;
int commandVal = -1;
while(1){
char *currentDirectory = (char*) calloc(1024, sizeof(char));
char hostn[1204] = "";
gethostname(hostn, sizeof(hostn));
printf("%s@%s %s > ", getenv("LOGNAME"), hostn, getcwd(currentDirectory, 1024));
int len=0;
fgets(input, 50, stdin);
strtok(input, "\n");
command = inputToCommand(input,&len);
if(!strcmp(command[0],"cd")){
changeDirectory(command,len);
continue;
}
child = fork();
if(child == 0){
manageEnviron(command, len);
if(commandVal == -1){
break;
}
}
else{
if(strcmp(command[0],"exit") == 0){
break;
}
waitpid(child, &randomVariable, WUNTRACED);
}
free(command);
}
}