-
Notifications
You must be signed in to change notification settings - Fork 0
/
shellFunctions.c
169 lines (138 loc) · 2.89 KB
/
shellFunctions.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "packages.h"
#include "shellPrototypes.h"
//prints working directory
void printWD(){
char buffer[PATH_MAX+1];
char* str = getcwd(buffer, PATH_MAX+1);
printf("%s\n", buffer);
}
//changes directory given relative filepath
void chwdRelative(char* input){
errno = 0;
char buffer[PATH_MAX+1];
char* str = getcwd(buffer, PATH_MAX+1);
strcat(buffer, "/");
strcat(buffer, input);
chdir(buffer);
if(errno != 0){
fprintf(stderr, "Error: %s\n", strerror(errno));
return;
}
}
//changes directory given absolute filepath
void chwdAbsolute(char* input){
errno = 0;
chdir(input);
if(errno != 0){
fprintf(stderr, "Error: %s\n", strerror(errno));
return;
}
}
//changes directory
void chwd(char* input){
char buffer[PATH_MAX+1];
strcpy(buffer, input);
if(buffer[0]=='/'){
chwdAbsolute(buffer);
}
else{
chwdRelative(buffer);
}
}
//ends shell
void quitShell(){
printf("Goodbye.\n");
exit(0);
}
//forks and executes list
void shellList(char* input, char* startingPath){
pid_t child;
int cstatus;
pid_t c;
char listLocation[PATH_MAX+1];
strcpy(listLocation, startingPath);
strcat(listLocation, "/list");
if((child = fork()) == 0){
char buffer[strlen(input)+1];
strcpy(buffer, input);
char arguments[5][strlen(input)+1];
int i = 0;
char* token = strtok(buffer, " \n");
while(token!=NULL){
strcpy(arguments[i], token);
token = strtok(NULL, " \n");
i++;
}
if(i == 1){
execl(listLocation, "list", NULL);
}
else if(i == 2){
execl(listLocation, "list", arguments[1], NULL);
}
else if(i == 3){
execl(listLocation, "list", arguments[1], arguments[2], NULL);
}
else{
fprintf(stderr, "Incorrect number of arguments\n");
return;
}
}
else{
if(child == (pid_t)(-1)){
fprintf(stderr, "Error: forking error\n");
}
else{
c = wait(&cstatus);
}
}
}
//forks and excutes create
void shellCreate(char* input, char* startingPath){
pid_t child;
int cstatus;
pid_t c;
char createLocation[PATH_MAX+1];
strcpy(createLocation, startingPath);
strcat(createLocation, "/create");
if((child = fork()) == 0){
char buffer[strlen(input)+1];
strcpy(buffer, input);
char arguments[5][strlen(input)+1];
int i = 0;
char* token = strtok(buffer, " \n");
while(token!=NULL){
strcpy(arguments[i], token);
token = strtok(NULL, " \n");
i++;
}
if(i == 3){
execl(createLocation, "create", arguments[1], arguments[2], NULL);
}
else if(i == 4){
execl(createLocation, "create", arguments[1], arguments[2], arguments[3], NULL);
}
else{
fprintf(stderr, "Incorrect number of arguments\n");
return;
}
}
else{
if(child == (pid_t)(-1)){
fprintf(stderr, "Error: forking error\n");
}
else{
c = wait(&cstatus);
}
}
}
int getNumberOfArgs(char* input){
char buffer[strlen(input) + 1];
int counter = 1;
int i;
for(i = 0; i <strlen(input); i++){
if(buffer[i] == ' '){
counter++;
}
}
return counter;
}