-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
115 lines (104 loc) · 2.36 KB
/
shell.c
File metadata and controls
115 lines (104 loc) · 2.36 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
char **read_user_input();
int main(int argc, const char *argv[])
{
printf("Welcome to my shell!\n");
for (;;)
{
printf("# ");
char **args = read_user_input();
if (args == NULL)
{
continue;
}
// Run command in a seperate process
int ret_val = fork();
if (ret_val < 0)
{
fprintf(stderr, "Fork Failed.\n");
}
else if (ret_val == 0)
{
// Child process
int ret_code = execvp(args[0], args); // Execute given command.
if (ret_code == -1)
{
fprintf(stderr, "Command not found.\n");
}
}
else
{
// Parent process
int wstatus;
int terminated_pid = wait(&wstatus);
if (terminated_pid == -1)
{
fprintf(stderr, "Wait Failed.\n");
}
// Free the allocated memory for arguments
for (int i = 0; args[i] != NULL; ++i)
{
free(args[i]);
}
free(args);
}
}
return 0;
}
/**
* @brief Read user input.
*
* @return char**
*/
char **read_user_input()
{
char *line = NULL;
size_t size;
size_t line_len = getline(&line, &size, stdin);
if (line_len <= 0)
{
free(line);
return NULL;
}
*(line + line_len - 1) = '\0'; // Remove break
// Split input string into tokens based on \s
char *token = strtok(line, "\t\n\r\f\v");
int arg_count = 0;
while (token != NULL)
{
arg_count++;
token = strtok(NULL, "\t\n\r\f\v");
}
// Allocate mem for arg array
char **args = malloc((arg_count + 1) * sizeof(char *));
if (args == NULL)
{
free(line);
return NULL;
}
token = strtok(line, "\t\n\r\f\v");
int i = 0;
while (token != NULL)
{
args[i] = strdup(token);
if (args[i] == NULL)
{
for (int j = 0; j < i; ++j)
{
free(args[j]);
}
free(args);
free(line);
return NULL;
}
i++;
token = strtok(NULL, "\t\n\r\f\v");
}
args[i] = NULL;
free(line);
return args;
}