-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyShell.c
181 lines (157 loc) · 2.81 KB
/
myShell.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define delimiter " \n\r\t"
#define line "======================================\n"
void allocationError()
{
fprintf(stderr, "Allocation Error!");
exit(EXIT_FAILURE);
}
char *getUsername()
{
char *username = getenv("USERNAME");
return username;
}
int exitShell()
{
return 0;
}
char *promptOption()
{
printf("Please choose/customise prompt: \n");
char *username = getUsername();
printf("[1] %s> \n", username);
printf("[2] Create your own...\n%s", line);
char option[1024];
char *prompt = malloc(20 * sizeof(char));
fgets(option, 1024, stdin);
if (atoi(option) == 1)
{
prompt = strcat(username, "> ");
printf("%sPrompt set to: %s\n%s", line, prompt, line);
}
else if (atoi(option) == 2)
{
printf("%sWrite your prompt below followed by [ENTER]...\n", line);
fgets(prompt, 20, stdin);
prompt[strcspn(prompt, "\n")] = 0;
printf("%sPrompt set to: %s\n%s", line, prompt, line);
}
else
{
printf("%sIncorrect Option Selected\n%s", line, line);
prompt = promptOption();
}
return prompt;
}
char *readLine()
{
int buffSize = 1024, position = 0;
char *buffer = malloc(buffSize * sizeof(char));
char c;
if (!buffer)
{
allocationError();
}
while (1)
{
c = getchar();
if (c == EOF || c == '\n')
{
buffer[position] = '\0';
return buffer;
}
else
{
buffer[position] = c;
}
position++;
}
if (position >= buffSize)
{
buffSize += 1024;
buffer = realloc(buffer, buffSize);
if (!buffer)
{
allocationError();
}
}
}
int runse(char **args)
{
pid_t pid;
int status;
if (strcmp(args[0], "exit") == 0)
{
return exitShell();
}
pid = fork();
if (pid < 0)
{
printf("COULD NOT FORK SUCCESSFULLY...\n");
}
else if (pid == 0)
{
if (execvp(args[0], args) < 0)
{
printf("COMMAND NOT FOUND...\n");
exit(EXIT_FAILURE);
}
}
else
{
waitpid(pid, &status, WUNTRACED);
}
}
char **tokenize(char *input)
{
int buffSize = 1024, position = 0;
char **tokens = malloc(buffSize * sizeof(char *));
char *token;
if (!tokens)
{
allocationError();
}
token = strtok(input, delimiter);
while (token != NULL)
{
tokens[position] = token;
position++;
if (position >= buffSize)
{
buffSize += 1024;
tokens = realloc(tokens, buffSize * sizeof(char *));
if (!tokens)
{
allocationError();
}
}
tokens[position] = NULL;
return tokens;
}
}
void welcome()
{
printf("%s WELCOME TO myShell \n%s",line, line);
}
int main(void)
{
welcome();
printf("USERNAME = %s\n", getUsername());
char *prompt = promptOption();
char *input;
char **args;
int status = 1;
do
{
printf("%s", prompt);
input = readLine();
args = tokenize(input);
status = runse(args);
free(input);
free(args);
} while (status);
}