/
shell.c
203 lines (180 loc) · 4.53 KB
/
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
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
//
// Created by abdullah on 4/17/16.
//
#include "shell.h"
// The print_color functions print text in their respective colors.
void print_red(char * string)
{
printf(ANSI_COLOR_RED "%s" ANSI_COLOR_RESET, string);
}
void print_green(char * string)
{
printf(ANSI_COLOR_GREEN "%s" ANSI_COLOR_RESET, string);
}
void print_yellow(char * string)
{
printf(ANSI_COLOR_YELLOW "%s" ANSI_COLOR_RESET, string);
}
void print_blue(char * string)
{
printf(ANSI_COLOR_BLUE "%s" ANSI_COLOR_RESET, string);
}
void print_magenta(char * string)
{
printf(ANSI_COLOR_MAGENTA "%s" ANSI_COLOR_RESET, string);
}
void print_cyan(char * string)
{
printf(ANSI_COLOR_CYAN "%s" ANSI_COLOR_RESET, string);
}
// Prints user@host prompt.
void print_userhost(char * username, char * hostname, char * current_dir)
{
printf(ANSI_COLOR_CYAN "[" ANSI_COLOR_RESET);
print_blue(username);
printf(ANSI_COLOR_BLUE "@" ANSI_COLOR_RESET);
print_blue(hostname);
printf(" ");
print_green(current_dir);
printf(ANSI_COLOR_CYAN "]" ANSI_COLOR_RESET);
printf(" %% ");
}
// Our shell main loop
void sh_loop(void)
{
char * line;
char ** args;
int status;
do
{
char hostname[HOST_NAME_MAX + 1];
char pathname[PATH_MAX + 1];
struct passwd * p = getpwuid(getuid());
if(p == NULL) // If we can't get the user_id we should quit.
{
fprintf(stderr, "Could not get username.\n");
exit(EXIT_FAILURE);
}
// Set process effective ID to user ID.
setreuid(getuid(), getuid());
gethostname(hostname, sizeof(hostname)); // Get the hostname.
char * username = p->pw_name;
char * current_dir = getcwd(pathname, sizeof(pathname));
/* Print prompt in the format:
* [username@hostname current_working_directory] %
*/
// If the current working directory is the user's home directory
// we should just print ~ to let them know they are home.
if(strcmp(current_dir, p->pw_dir) == 0)
{
print_userhost(username, hostname, "~");
}
else { print_userhost(username, hostname, current_dir); }
line = sh_get_cmd();
args = sh_tokenize_cmd(line);
status = sh_launch(args);
// Free the memory we used.
free(line);
free(args);
} while(status);
}
// Implement sh_get_cmd
/*
Problem: I don't know how much text a user will enter.
Strategy: Use getline from stdio.h to allocate the proper
amount for me.
*/
char * sh_get_cmd(void)
{
char * line = NULL;
size_t bufsize = 0; // getline will change this.
// Get the line from user input.
getline(&line, &bufsize, stdin);
return line;
}
char ** sh_tokenize_cmd(char * cmd)
{
int bufsize = TOKEN_BUFSIZE;
int pos = 0;
char ** tokens = malloc(bufsize * sizeof(char*));
char * token;
// Check for allocation error.
if(!tokens)
{
fprintf(stderr, "Allocation error.\n");
exit(EXIT_FAILURE);
}
token = strtok(cmd, TOKEN_DELIMS);
while(token != NULL)
{
tokens[pos] = token;
pos++;
// Reallocate if tokens don't fit.
if(pos >= bufsize)
{
bufsize += TOKEN_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char*));
// Check for allocation error.
if(!tokens)
{
fprintf(stderr, "Allocation error.\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, TOKEN_DELIMS);
}
tokens[pos] = NULL;
return tokens;
}
// Finally, launching a program.
int sh_launch(char ** args)
{
pid_t pid, wpid;
int status;
// Catch ^C so it doesn't break the program.
if(args[0] == NULL) { return 1; }
// exit shell.
if(strcmp(args[0], "exit") == 0)
{
return 0;
}
pid = fork();
if(pid == 0) // Child process.
{
if(execvp(args[0], args) == -1)
{
perror("yash");
}
exit(EXIT_FAILURE);
}
else if (pid < 0) // fork() error
{
perror("yash");
}
else if(strcmp(args[0], "cd") == 0)
{
if(args[1] == NULL)
{
struct passwd * p = getpwuid(getuid());
chdir(p->pw_dir);
}
else { chdir(args[1]); }
return 1;
}
else // Parent process.
{
do
{
wpid = waitpid(pid, &status, WUNTRACED);
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
int sh_execute(char ** args)
{
if(args[0] == NULL) // Empty command.
{
return 1;
}
return sh_launch(args);
}