-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsh.c
152 lines (125 loc) · 2.93 KB
/
rsh.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
#include <stdio.h>
#include <stdlib.h>
#include <spawn.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#define N 12
extern char **environ;
char *allowed[N] = {"cp","touch","mkdir","ls","pwd","cat","grep","chmod","diff","cd","exit","help"};
// deallocates argv elements
void freeMemory(char** argv) {
for (int i = 0; argv[i] != NULL; i++) {
if (i > 22) break;
if (argv[i] == NULL) continue;
free(argv[i]);
argv[i] = NULL;
}
}
int toSpawn(char* cmd) {
for (int i = 0; i < 9; i++) {
if (strcmp(cmd, allowed[i]) == 0) {
return 1;
}
}
return 0;
}
void help() {
printf("The allowed commands are:\n");
for (int i = 0; i < N; i++) {
printf("%d: %s\n", i + 1, allowed[i]);
}
}
int main() {
char line[256];
char **argv = (char**)malloc(22 * sizeof(char*));
while (1) {
fprintf(stderr,"rsh>");
// skip empty lines
if (fgets(line, 256, stdin)==NULL) continue;
if (strcmp(line, "\n")==0) continue;
unsigned int len = strlen(line);
line[len - 1] = '\0';
// parse line into cmd and arguents
argv[0] = (char*)malloc(len * sizeof(char));
int currentArg = 0; // argc = currentArg + 1
int currentPos = 0;
for (int i = 0; i < len; i++) {
if (line[i] == '\0') break;
if (line[i] == '\n') continue;
if (line[i] == ' ') {
if (currentPos == 0) continue;
argv[currentArg][currentPos] = '\0';
currentArg++;
currentPos = 0;
argv[currentArg] = (char*)malloc((len - i) * sizeof(char));
continue;
}
argv[currentArg][currentPos] = line[i];
currentPos++;
}
argv[currentArg][currentPos] = '\0';
if (argv[currentArg][0] == '\0') {
free(argv[currentArg]);
argv[currentArg] = NULL;
} else {
argv[currentArg + 1] = NULL;
}
// print argv debugging
/* printf("cmd: %s\n", argv[0]);
for (int i = 1; i < currentArg + 2; i++) {
printf("argv[%d]: %s\n", i, argv[i]);
}
printf("\n"); */
// 1-9
if (toSpawn(argv[0])) {
pid_t pid;
int status;
posix_spawnattr_t attr;
posix_spawnattr_init(&attr);
// get command path
char path[16] = "/usr/bin/";
int i;
for (i = 0; argv[0][i] != '\0'; i++) {
path[i + 9] = argv[0][i];
}
path[i + 9] = '\0';
if (posix_spawnp(&pid, path, NULL, &attr, argv, environ) != 0) {
perror("spawn failed\n");
}
if (waitpid(pid, &status, 0) == -1) {
perror("waitpid failed\n");
}
if (WIFEXITED(status)) {
// fprintf(stderr, "Process exited with status %d\n", WEXITSTATUS(status));
}
freeMemory(argv);
continue;
}
// cd
if (strcmp(argv[0], "cd") == 0) {
if (currentArg > 1) {
printf("-rsh: cd: too many arguments\n");
} else if (currentArg == 1) {
chdir(argv[1]);
}
freeMemory(argv);
continue;
}
// exit
if (strcmp(argv[0], "exit") == 0) {
freeMemory(argv);
break;
}
// help
if (strcmp(argv[0], "help") == 0) {
help();
freeMemory(argv);
continue;
}
printf("NOT ALLOWED!\n");
freeMemory(argv);
}
free(argv);
return 0;
}