-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
112 lines (96 loc) · 2.91 KB
/
main.c
File metadata and controls
112 lines (96 loc) · 2.91 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include "cd.h"
#include "echo.h"
#include "pwd.h"
#include "ls.h"
#include "pinfo.h"
#include "syscommands.h"
#include "repeat.h"
int main(int argc, char *argv[]) {
// Prompt display requirement
char user[40], hostname[HOST_NAME_MAX + 1];
char home_path[PATH_MAX];
char *prompt = (char*) malloc(PATH_MAX + HOST_NAME_MAX + 45);
prompt[0] = '<';
prompt[1] = '\0';
char *cwd = (char*) malloc(PATH_MAX);
if (cwd == NULL) {
printf("Memory full\n");
return 1;
}
if (getcwd(cwd, PATH_MAX) == NULL) {
perror("getcwd() error");
return 1;
} else {
strcpy(home_path, cwd);
}
char *prwd = (char*) malloc(PATH_MAX);
if (prwd == NULL) {
printf("Memory full\n");
return 1;
}
strcpy(prwd, home_path);
getlogin_r(user, 40);
strcat(prompt, user);
strcat(prompt, "@");
gethostname(hostname, HOST_NAME_MAX + 1);
strcat(prompt, hostname);
strcat(prompt, ":~>");
// User input
char input[101];
char *token;
char *command;
while (1) {
printf("%s ", prompt);
fgets(input, 100, stdin);
// Tokenize for multiple commands
char *copy = input;
command = strtok_r(copy, ";\n", ©);
while (command != NULL) {
// Tokenize to remove whitespace
token = strtok(command, " \t\n");
if (!token) {
command = strtok_r(copy, ";\n", ©);
if (command == NULL) {
break;
} else {
continue;
}
}
// Compare first word of user input and call appropriate function
if (strcmp(token, "pwd") == 0) {
pwd(&cwd);
token = strtok(NULL, " \t\n");
} else if (strcmp(token, "echo") == 0) {
echo(&token);
} else if (strcmp(token, "cd") == 0) {
token = strtok(NULL, " \t\n");
cd(&token, &prompt, &cwd, &prwd, home_path);
} else if (strcmp(token, "ls") == 0) {
token = strtok(NULL, " \t\n");
ls(&token, &cwd, home_path);
} else if (strcmp(token, "pinfo") == 0) {
token = strtok(NULL, " \t\n");
pinfo(&token, &cwd);
} else if (strcmp(token, "repeat") == 0) {
token = strtok(NULL, " \t\n");
repeat(&token, &prompt, &cwd, &prwd, home_path, input);
} else if (strcmp(token, "exit") == 0) {
goto end;
} else {
syscommands(&token, token);
}
command = strtok_r(copy, ";\n", ©);
}
}
// Fix memory leaks
end:
free(prwd);
free(cwd);
free(prompt);
return EXIT_SUCCESS;
}