-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
71 lines (51 loc) · 1.35 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_CHAR 255
char input[MAX_CHAR];
char backup[MAX_CHAR];
char *command;
char delim[2] = " ";
char *token;
char **args;
char **env;
char *currentDir;
void parser() {
int argCount = 0;
input[strnlen(input, MAX_CHAR) - 1] = 0;
strncpy(backup, input, sizeof(input));
token = strtok(input, delim);
while(token != NULL) { //using this to grab how many tokens there actually are
argCount++;
token = strtok(NULL, delim);
}
token = strtok(backup, delim);
command = token;
args = malloc(argCount * sizeof(char *));
for (int i = 0; i < argCount; i++) {
args[i] = malloc(MAX_CHAR * sizeof(char));
}
for (int j = 0; j < argCount; j++) {
strcpy(args[j], token);
token = strtok(NULL, delim);
}
//code starts here
//code ends here
for (int k = 0; k < argCount; k++) {
free(args[k]);
}
free(args);
}
int main() {
char *username = getenv("USER");
char *home = getenv("HOME");
chdir(home);
currentDir = home;
while (1) {
printf("%s@%s > ", username, currentDir);
fgets(input, MAX_CHAR, stdin);
parser();
}
return 0;
}