-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.c
255 lines (233 loc) · 8.25 KB
/
utility.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include "myshell.h"
void print_cmd_error()
{
char error_message[30] = "An error has occured\n";
write(STDERR_FILENO, error_message, strlen(error_message));
}
int list_directory_contents(char *directory)
{
// list contents in directory
DIR *directory_pointer;
struct dirent *directory_block;
directory_pointer = opendir(directory); // opens directory
if (directory_pointer == NULL)
{
print_cmd_error();
printf("dir error: could not find directory '%s'", directory);
return -1;
}
while ((directory_block = readdir(directory_pointer)) != NULL)
{ // read directory to block struct
printf("%s\t", directory_block->d_name); // print contents to stdout
}
printf("\n");
return 0;
}
int list_directory_contents_output_redirect(char *file_name, char *directory)
{
DIR *directory_pointer;
struct dirent *directory_block;
directory_pointer = opendir(directory); // opens directory
if (directory_pointer == NULL)
{
print_cmd_error();
printf("dir error: could not find directory '%s'", directory);
return -1;
}
int out_file_fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); //O_WRONLY | O_CREAT | O_APPEND in case of appending, O_RDONLY for input redirection
while ((directory_block = readdir(directory_pointer)) != NULL)
{ // read directory to block struct
write(out_file_fd, strcat(directory_block->d_name, "\n"), strlen(directory_block->d_name));
}
write(out_file_fd, "\n", 1); // newline for clarity
return 0;
}
int list_directory_contents_output_append_redirect(char *file_name, char *directory)
{
DIR *directory_pointer;
struct dirent *directory_block;
directory_pointer = opendir(directory); // opens directory
if (directory_pointer == NULL)
{
print_cmd_error();
printf("dir error: could not find directory '%s'", directory);
return -1;
}
int out_file_fd = open(file_name, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRWXG | S_IRWXO); //opens file in append mode, or creates file if doesnt exist
while ((directory_block = readdir(directory_pointer)) != NULL)
{ // read directory to block struct
write(out_file_fd, strcat(directory_block->d_name, "\n"), strlen(directory_block->d_name));
}
write(out_file_fd, "\n", 1); // newline for clarity
return 0;
}
void list_environ_variables(char **environment)
{
// prints all environment variables by looping through the env array
while (*environment != NULL)
printf("%s\n", *environment++);
}
void list_environ_variables_output_redirect(char *file_name, char **environment)
{
// writes all environment variables to a file
int out_file_fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); //O_WRONLY | O_CREAT | O_APPEND in case of appending, O_RDONLY for input redirection
while (*environment != NULL)
{
write(out_file_fd, *environment, strlen(*environment));
environment++;
}
write(out_file_fd, "\n", 1); // newline for clarity
close(out_file_fd);
}
void list_environ_variables_output_append_redirect(char *file_name, char **environment)
{
// appends all environment variables to a file
int out_file_fd = open(file_name, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRWXG | S_IRWXO); //opens file in append mode, or creates file if doesnt exist
while (*environment != NULL)
{
write(out_file_fd, *environment, strlen(*environment));
environment++;
}
write(out_file_fd, "\n", 1); // newline for clarity
close(out_file_fd);
}
void echo(char **arguments)
{
//print user comment and newline (multiple spaces/tabs reduced to one)
arguments++; //skips user command for echo
while (*arguments != NULL)
printf("%s ", *arguments++);
printf("\n");
}
void echo_output_redirect(int output_redir_symbol_index, char *file_name, char **relevant_arguments)
{
int out_file_fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); //O_WRONLY | O_CREAT | O_APPEND in case of appending, O_RDONLY for input redirection
relevant_arguments++; //skips user command for echo
while (*relevant_arguments != NULL)
{
write(out_file_fd, strcat(*relevant_arguments, " "), strlen(*relevant_arguments));
relevant_arguments++;
}
write(out_file_fd, "\n", 1); // newline for clarity
close(out_file_fd);
}
void echo_output_append_redirect(int output_redir_append_symbol_index, char *file_name, char **relevant_arguments)
{
int out_file_fd = open(file_name, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRWXG | S_IRWXO); //opens file in append mode, or creates file if doesnt exist
relevant_arguments++; //skips user command for echo
while (*relevant_arguments != NULL)
{
write(out_file_fd, strcat(*relevant_arguments, " "), strlen(*relevant_arguments));
relevant_arguments++;
}
write(out_file_fd, "\n", 1); // newline for clarity
close(out_file_fd);
}
void help()
{
//Open help file by running more filter
pid_t pid;
char *args[] = {"more", "README.md", NULL};
if ((pid = Fork()) == 0) //child executes
{
if (execvp("more", args) == -1)
{
unix_error("Execvpe error");
}
exit(0);
}
else
{
int status = 0;
wait(&status);
}
}
void help_output_redirect(int output_redir_symbol_index, char *file_name)
{
pid_t pid;
if ((pid = Fork()) == 0) //child executes
{
int out_file_fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); //O_WRONLY | O_CREAT | O_APPEND in case of appending, O_RDONLY for input redirection
dup2(out_file_fd, STDOUT_FILENO); //the file discriptor represents the stdout now, so result of exec will output to this file //replace stdout with newly created file
close(out_file_fd);
char *args[] = {"more", "README.md", NULL};
if (execvp("more", args) == -1)
{
unix_error("Execvpe error");
}
exit(0);
}
else
{
//Parent process waits for child to finish
int status = 0;
wait(&status);
}
}
void help_output_append_redirect(int output_redir_append_symbol_index, char *file_name)
{
pid_t pid;
if ((pid = Fork()) == 0) //child executes
{
int out_file_fd = open(file_name, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRWXG | S_IRWXO); //opens file in append mode, or creates file if doesnt exist
dup2(out_file_fd, STDOUT_FILENO);
close(out_file_fd);
char *args[] = {"more", "README.md", NULL};
if (execvp("more", args) == -1)
{
unix_error("Execvpe error");
}
exit(0);
}
else
{
//Parent process waits for child to finish
int status = 0;
wait(&status);
}
}
void quit()
{
// exit the shell prompt
exit(0);
}
int change_current_directory(char *directory)
{
if (directory == NULL)
{
printf("please provide directory to change to as an argument\n");
printf("current directory: %s\n", getenv("PWD"));
}
if (chdir(directory) == -1)
{
print_cmd_error();
printf("cd error: could not find directory '%s'\n", directory);
return -1;
}
//change PWD environment variable after successful call
char wd[strlen(getenv("PWD")) + strlen(directory) - 2];
strcpy(wd, getenv("PWD"));
strcat(wd, "/");
strcat(wd, directory);
setenv("PWD", wd, 1); //resets PWD
return 0;
}
void pause_input()
{
//pause shell operation until "enter" is pressed
printf("Paused: press Enter key to continue\n");
getchar();
}
void clear_screen()
{
//////////being weird
//terminal gets cleared with cursor positioned at the beginning
printf("\033[H\033[J");
}