-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKashish_102103490_Code.c
More file actions
executable file
·276 lines (240 loc) · 5.22 KB
/
Kashish_102103490_Code.c
File metadata and controls
executable file
·276 lines (240 loc) · 5.22 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
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
// Shell - Kashish
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdbool.h>
#include <errno.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
// Exit/Bye Function
void k_exit(char **agr)
{
exit(0);
}
// Bye
void k_bye(char **agr)
{
system("eog bye.png");
exit(0);
}
// Date func
void k_date(char **agr)
{
time_t t;
t = time(NULL);
struct tm tm = *localtime(&t);
printf("Date: %d-%d-%d\n", tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
}
// Time Func
void k_time(char **agr)
{
time_t t;
t = time(NULL);
struct tm tm = *localtime(&t);
if (tm.tm_hour >= 12)
{
if (tm.tm_hour == 12)
printf("12");
else
printf("%d", tm.tm_hour - 12);
printf(":%d:%d PM\n", tm.tm_min, tm.tm_sec);
}
else
printf("%d:%d:%d AM\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
}
// info
void k_info(char **agr)
{
printf("This is a basic shell programmed in C Language\n for Course Practical Computing\n");
}
// pwd
void k_pwd(char **agr)
{
char *buf;
buf = (char *)malloc(100 * sizeof(char));
getcwd(buf, 100);
printf("\n %s \n", buf);
}
// current username
void k_user(char *agr)
{
char *buf;
buf = (char *)malloc(10 * sizeof(char));
buf = getlogin();
printf("\n %s \n", buf);
}
// flip
void k_flip(char **agr)
{
int f = rand() % 10;
if (f % 2 == 0)
{
printf("Tails\n");
}
else if (f % 2 != 0)
{
printf("Heads\n");
}
}
// Cd function
void k_cd(char **agr)
{
if (agr[1] == NULL)
{
fprintf(stderr, "Kashish: cd: missing argument\n");
}
else
{
if (chdir(agr[1]) != 0)
{
perror("kashish: cd");
}
}
}
// Clear func
void k_clear(char **agr)
{
system("clear");
}
// Help function
void k_help(char **agr)
{
char *txt =
"Kashish - KASHISH AGGARWAL 102103490 SHELL\n"
"info - display basic info about this project\n"
"The followng commands are built in this\n"
"cd - change working dir\n"
"exit - exit the shell\n"
"bye - bye bye from the shell\n"
"help - prints this text\n"
"date - display current date\n"
"time - display current time in 12hr format\n"
"pwd - displays pesent working directory\n"
"clear - clear the screen/terminal window\n"
"flip - flips a coin\n"
"user - displays name of the user\n"
"ls - list all contents\n";
printf("%s", txt);
}
// It helps in assigning the cmd name to the handler func
struct bults
{
char *name;
void (*func)(char **agr);
};
// List of built cmds
struct bults bult[] =
{
{"help", k_help},
{"exit", k_exit},
{"bye", k_bye},
{"cd", k_cd},
{"date", k_date},
{"time", k_time},
{"pwd", k_pwd},
{"clear", k_clear},
{"info", k_info},
{"flip", k_flip},
{"user", k_user},
};
// Other Functions
int k_num_bult()
{
return sizeof(bult) / sizeof(struct bults);
}
// Executer Bit
void k_exe(char **agr)
{
for (int i = 0; i < k_num_bult(); i++)
{
if (strcmp(agr[0], bult[i].name) == 0)
{
bult[i].func(agr);
return;
}
}
pid_t child_pid = fork();
if (child_pid == 0)
{
execvp(agr[0], agr);
perror("Kashish");
exit(1);
}
else if (child_pid > 0)
{
int stat;
do
{
waitpid(child_pid, &stat, WUNTRACED);
} while (!WIFEXITED(stat) && !WIFEXITED(stat));
}
else
{
perror("Kashish");
};
}
// Line Splitter and Praizer
char **k_split(char *line)
{
int len = 0;
int cap = 16;
char **tokens = malloc(cap * sizeof(char *));
if (!tokens)
{
perror("Kashish");
exit(1);
}
char *delmiters = "\t\r\n";
char *token = strtok(line, delmiters);
while (token != NULL)
{
tokens[len] = token;
len++;
if (len >= cap)
{
cap = (int)(cap * 1.6);
tokens = realloc(tokens, cap * sizeof(char *));
if (!tokens)
{
perror("Kashish");
exit(1);
}
}
token = strtok(NULL, delmiters);
}
tokens[len = NULL];
return tokens;
}
// Reading the Line using getline
char *k_read_line()
{
char *line = NULL;
size_t buflen = 0;
errno = 0;
ssize_t stlen = getline(&line, &buflen, stdin);
if (stlen < 0)
{
if (errno)
{
perror("Kashish");
}
exit(1);
}
return line;
}
int main() // Main Function handling the loop to read and break the line to understand the commands
{
while (true)
{
printf(" > ");
char *line = k_read_line();
char **tokens = k_split(line);
if (tokens[0] != NULL)
{
k_exe(tokens);
}
free(tokens);
free(line);
}
}