-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.c
440 lines (407 loc) · 9.8 KB
/
builtin.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
// PROTOTYPES
static void exitProgram(char **args, int argcp);
static void cd(char **args, int argcp);
static void pwd(char **args, int argcp);
static void cmdA_ls(char **args, int argc);
static void cmdA_cp(char **args, int argc);
static void cmdA_env(char **args, int argc);
// Helper functions for ls
static void print_file_info(struct stat *fileStat, const struct dirent *dp);
static void format_time(time_t modTime, char *timebuf, size_t bufSize);
static void get_permissions(mode_t mode, char *perm);
/*
* builtIn
*
* Checks if a command is a built-in shell command and executes it if so.
*
* Args:
* args: array of strings containing command and arguments
* argcp: number of elements in args array
*
* Returns:
* 1 if the command was a built-in, 0 otherwise.
*
* Built-in commands are executed directly by the shell process rather than
* being forked to a new process. This function compares the given command
* to each of the built-ins (exit, pwd, cd, and ls/cp/env or stat/tail/touch
* depending on group). If a match is found, the corresponding function is called.
*
* Hint: Refer to checklist for group specific examples
*/
int builtIn(char **args, int argcp)
{
if (args == NULL || argcp == 0)
return 0;
if (strcmp(args[0], "exit") == 0)
{
exitProgram(args, argcp);
return 1;
}
else if (strcmp(args[0], "pwd") == 0)
{
pwd(args, argcp);
return 1;
}
else if (strcmp(args[0], "cd") == 0)
{
cd(args, argcp);
return 1;
}
else if (strcmp(args[0], "ls") == 0)
{
cmdA_ls(args, argcp);
return 1;
}
else if (strcmp(args[0], "cp") == 0)
{
cmdA_cp(args, argcp);
return 1;
}
else if (strcmp(args[0], "env") == 0)
{
cmdA_env(args, argcp);
return 1;
}
return 0;
}
/*
* exitProgram
*
* Terminates the shell with a given exit status.
* If no exit status is provided, exits with status 0.
* This function should use the exit(3) library call.
*
* Args:
* args: array of strings containing "exit" and optionally an exit status
* argcp: number of elements in args array
*/
static void exitProgram(char **args, int argcp)
{
if (argcp > 2)
{
fprintf(stderr, "exit: too many arguments\n");
return;
}
if (argcp == 1)
{
exit(0);
}
else
{
exit(atoi(args[1]));
}
}
/*
* pwd
*
* Prints the current working directory.
*
* Args:
* args: array of strings containing "pwd"
* argcp: number of elements in args array, should be 1
*
* Example Usage:
* Command: $ pwd
* Output: /some/path/to/directory
*/
static void pwd(char **args, int argcp)
{
if (argcp != 1 || args == NULL)
{
fprintf(stderr, "pwd: no arguments required\n");
return;
}
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL)
{
printf("%s\n", cwd);
}
else
{
perror("getcwd");
}
}
/*
* cd
*
* Changes the current working directory.
* When no parameters are provided, changes to the home directory.
* Supports . (current directory) and .. (parent directory).
*
* Args:
* args: array of strings containing "cd" and optionally a directory path
* argcp: number of elements in args array
*
* Example Usage:
* Command: $ pwd
* Output: /some/path/to/directory
* Command: $ cd ..
* Command: $ pwd
* Output: /some/path/to
*
* Hint: Read the man page for chdir(2)
*/
static void cd(char **args, int argcp)
{
if (argcp > 2)
{
fprintf(stderr, "cd: too many arguments\n");
return;
}
const char *path = (argcp == 1) ? getenv("HOME") : args[1];
if (path == NULL)
{
fprintf(stderr, "cd: HOME environment variable not set\n");
return;
}
if (chdir(path) != 0)
{
perror("cd");
}
}
/*
* cmdA_ls
*
* Lists the files and directories in the current directory.
* If the -l flag is provided, the long format is used.
*
* Args:
* args: array of strings containing the command and arguments
* argc: number of arguments in the args array
*
* Note: If the -l flag is provided, the long format is used.
*/
static void cmdA_ls(char **args, int argc)
{
DIR *dir = opendir("."); // Open current directory
if (dir == NULL)
{
perror("opendir");
return;
}
int long_format = (argc == 2 && strcmp(args[1], "-l") == 0);
struct dirent *dp; // Directory entry pointer
struct stat file_stat; // File status structure
int total_blocks = 0;
// First pass: Calculate total blocks for -l format
if (long_format)
{
while ((dp = readdir(dir)) != NULL)
{
if (stat(dp->d_name, &file_stat) == 0)
{
total_blocks += file_stat.st_blocks;
}
}
printf("total %d\n", total_blocks / 2); // 512-byte block units
rewinddir(dir); // Reset directory stream for the second pass
}
// Second pass: Print file details or names
while ((dp = readdir(dir)) != NULL)
{
// Skip `.` and `..` entries
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
{
continue;
}
if (stat(dp->d_name, &file_stat) < 0) // Get file status
{
perror("stat");
continue;
}
if (long_format)
{
print_file_info(&file_stat, dp); // Print detailed info
}
else
{
printf("%s\n", dp->d_name); // Print only the name (if not -l)
}
}
if (closedir(dir) < 0)
{
perror("closedir");
}
}
// Helper function for ls: Generate permissions string
static void get_permissions(mode_t mode, char *perm)
{
perm[0] = S_ISDIR(mode) ? 'd' : '-';
perm[1] = (mode & S_IRUSR) ? 'r' : '-';
perm[2] = (mode & S_IWUSR) ? 'w' : '-';
perm[3] = (mode & S_IXUSR) ? 'x' : '-';
perm[4] = (mode & S_IRGRP) ? 'r' : '-';
perm[5] = (mode & S_IWGRP) ? 'w' : '-';
perm[6] = (mode & S_IXGRP) ? 'x' : '-';
perm[7] = (mode & S_IROTH) ? 'r' : '-';
perm[8] = (mode & S_IWOTH) ? 'w' : '-';
perm[9] = (mode & S_IXOTH) ? 'x' : '-';
perm[10] = '\0'; // Null-terminate the string
}
// Helper function for ls: Format the modification time
static void format_time(time_t modTime, char *timebuf, size_t bufSize)
{
strftime(timebuf, bufSize, "%b %d %H:%M", localtime(&modTime));
}
// Helper function for ls: Print file information for -l format
static void print_file_info(struct stat *fileStat, const struct dirent *dp)
{
char perm[11], timebuf[80];
get_permissions(fileStat->st_mode, perm);
format_time(fileStat->st_mtime, timebuf, sizeof(timebuf));
printf("%s %ld %s %s %5ld %s %s\n",
perm,
fileStat->st_nlink,
getpwuid(fileStat->st_uid)->pw_name,
getgrgid(fileStat->st_gid)->gr_name,
fileStat->st_size,
timebuf,
dp->d_name);
}
/*
* cmdA_cp
*
* Copies the contents of one file to another.
*
* Args:
* args: array of strings containing the command and arguments
* argc: number of arguments in the args array
*
* Note: The cp command requires exactly two arguments.
*/
static void cmdA_cp(char **args, int argc)
{
if (argc != 3)
{
fprintf(stderr, "cp: invalid number of arguments\n");
return;
}
struct stat file_stat;
if (stat(args[1], &file_stat) < 0)
{
perror("stat");
return;
}
if (S_ISDIR(file_stat.st_mode)) // Check if the source is a directory
{
fprintf(stderr, "cp: '%s' is a directory\n", args[1]);
return;
}
int src_file = open(args[1], O_RDONLY); // Open source file to read from
if (src_file == -1)
{
perror("open");
return;
}
int dest_file = open(args[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); // Open destination file to write to
if (dest_file == -1)
{
perror("open");
if (close(src_file) == -1)
{
perror("close");
}
return;
}
char buffer[4096];
ssize_t bytes_read;
while ((bytes_read = read(src_file, buffer, sizeof(buffer))) > 0)
{
if (write(dest_file, buffer, bytes_read) != bytes_read) // Write to destination file
{
perror("write");
if (close(src_file) == -1)
{
perror("close");
}
if (close(dest_file) == -1)
{
perror("close");
}
return;
}
}
if (bytes_read == -1)
{
perror("read");
}
if (close(src_file) == -1)
{
perror("close");
}
if (close(dest_file) == -1)
{
perror("close");
}
}
/*
* cmdA_env
*
* Prints the environment variables or sets a new environment variable.
*
* Args:
* args: array of strings containing the command and arguments
* argc: number of arguments in the args array
*
* Note: If no arguments are provided, all environment variables are printed.
* If one argument is provided in the format NAME=VALUE, the environment variable is set.
*/
/*
* cmdA_env
*
* Prints all environment variables if no arguments are passed.
* If a NAME=VALUE argument is provided, it sets the environment variable.
*
* Args:
* args: array of strings containing the command and arguments
* argc: number of arguments in the args array
*/
static void cmdA_env(char **args, int argc)
{
if (argc > 2)
{
fprintf(stderr, "env: too many arguments\n");
return;
}
// If no arguments are provided, print all environment variables
if (argc == 1)
{
extern char **environ;
for (char **env = environ; *env != NULL; env++)
{
printf("%s\n", *env);
}
return;
}
// Set environment variable using putenv for NAME=VALUE format
if (strchr(args[1], '=') == NULL)
{
fprintf(stderr, "env: invalid format, expected NAME=VALUE\n");
return;
}
// Attempt to set environment variable
if (putenv(args[1]) != 0)
{
perror("putenv");
}
// Print all environment variables after modification
extern char **environ;
for (char **env = environ; *env != NULL; env++)
{
printf("%s\n", *env);
}
}