-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsh.c
More file actions
214 lines (194 loc) · 4.91 KB
/
sh.c
File metadata and controls
214 lines (194 loc) · 4.91 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include "sh.h"
#define MAXLINE 128
int sh( int argc, char **argv, char **envp )
{
char *prompt = calloc(PROMPTMAX, sizeof(char));
char *commandline = calloc(MAX_CANON, sizeof(char));
char *command, *arg, *commandpath, *p, *pwd, *owd;
char **args = calloc(MAXARGS, sizeof(char*));
int uid, i, status, argsct, go = 1;
struct passwd *password_entry;
char *homedir;
struct pathelement *pathlist;
char buf[MAXLINE];
uid = getuid();
password_entry = getpwuid(uid); /* get passwd info */
homedir = password_entry->pw_dir; /* Home directory to start
out with*/
if ( (pwd = getcwd(NULL, PATH_MAX+1)) == NULL )
{
perror("getcwd");
exit(2);
}
owd = calloc(strlen(pwd) + 1, sizeof(char));
memcpy(owd, pwd, strlen(pwd));
prompt[0] = ' '; prompt[1] = '\0';
/* Put PATH into a linked list */
//pathlist = get_path();
while ( go )
{
pathlist=get_path();
/* print your prompt*/
printf("%s[%s]>", prompt, pwd);
/* get command line and process */
if(fgets(buf, MAXLINE, stdin) != NULL){
/*printf("^D\n");
free(pathlist->element);
listFree(pathlist);
}
else if((strcmp(buf, "\n") == 0)){ //Enter Key command
(free(pathlist->element);
listFree(pathlist);
free(buf);
continue;
}
else{ */
if(buf[strlen(buf)-1]=='\n'){
buf[strlen(buf)-1]=0;
}
strcpy(commandline, buf);
char *arr[6];
int i=0;
arr[i] = strtok(commandline, " ");
int arrctr=0;
while(arr[i]!=NULL){
arr[++i]=strtok(NULL," ");
arrctr++;
}
/*
arg[(strlen(arg))-1]=0;
args = stringToArray(arg);
i=0;
int argsctr=0;
//char *tmp;
//tmp = &args[0][0];
while(args[i]!=NULL){
argsctr++;
i++;
}*/
/*if(buf) == NULL){
continue;
}*/
/* check for each built in command and implement */
if (strcmp(arr[0], "pwd") == 0) { /* built-in command pwd */
printf("check");
char *ptr = getcwd(NULL, 0);
printf("[%s]\n", ptr);
free(ptr);
}
else if (strcmp(arr[0], "exit")==0) { /* built-in command pwd */
printf("EXITING PROGRAM");
go=0;
}
else if (strcmp(commandline, "prompt")==0) {
if(arrctr >= 3){
printf("Too many arguments given\n");
}
else if(arr[1] == NULL){
printf("input prompt prefix: ");
if(!fgets(prompt + 1, MAXARGS, stdin)){
printf("input error\n");
}
else if(prompt[strlen(prompt)-1] == '\n'){
prompt[strlen(prompt)-1] = 0;
}
}
else{
sprintf(prompt, " %s", arr[1]);
}
}
else if (strcmp(arr[0], "which") == 0){
if (arrctr == 1){
fprintf(stderr, "Not enough args\n");
}
else{
int i = 1;
while (i < arrctr+1 && i < MAXARGS){
char *cmd = which(arr[i], pathlist);
printf("%s", cmd);
i++;
free(cmd);
}
}
}
else if(strcmp(arr[0], "list") ==0)
{
if(arrctr==1){
listPrint(pwd);
}
else{
int counter=1; //starts at 1 since first arr[0] is list command
while(counter<arrctr && counter<5) //capping amount of directories that can be listed at 5
listPrint(arr[counter]);
printf("\n");
counter++;
}
}
/* else program to exec */
/* find it */
/* do fork(), execve() and waitpid() */
/* else */
/* fprintf(stderr, "%s: Command not found.\n", args[0]); */
}
}
return 0;
}
char *which(char *command, struct pathelement *pathlist )
{
char *cmd = malloc(64);
int found = 0;
while (pathlist) {
sprintf(cmd, "%s/%s", pathlist->element, command);
if (access(cmd, X_OK) == 0) {
sprintf(cmd, "%s/%s\n", pathlist->element, command);
found = 1;
break;
}
pathlist = pathlist->next;
}
if (found == 0){
sprintf(cmd, "%s: not found.\n", command);
return cmd;
}
return cmd;
} /* which() */
char *where(char *command, struct pathelement *pathlist )
{
/* similarly loop through finding all locations of command */
} /* where() */
void listPrint(char *d)
{
DIR *curdir;
struct dirent *f;
if((curdir=opendir(d))==NULL){
printf("%s does not exist", d);
}
else{
printf("%s", d);
while((f = readdir(curdir))!=NULL){
printf("%s\n", f->d_name);
}
printf("\n");
free(f);
}
free(curdir);
}
/*void listFree(struct pathelement *head){
struct pathelement* tmp = NULL;
while(head!=NULL){
tmp=head;
head=head->next;
free(tmp);
}
}*/