-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
187 lines (172 loc) · 4.63 KB
/
utils.c
File metadata and controls
187 lines (172 loc) · 4.63 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
#include "utils.h"
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
int msleep(long msec) {
struct timespec ts;
int res;
if (msec < 0) {
errno = EINVAL;
return -1;
}
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);
return res;
}
char *search_command(const char *command) {
char *path = getenv("PATH");
/* From man getenv:
should not modify the string pointed to by
the getenv() function.
*/
char *path_copy = strdup(path); // Duplicate the string pointed to by path
char *path_token = strtok(path_copy, ":");
char *command_path =
calloc(1, MAX_PATH_LENGTH); // Allocate memory for the command path
while (path_token != NULL) {
path_token = strtok(NULL, ":");
if (path_token == NULL) {
break;
}
snprintf(command_path, MAX_PATH_LENGTH, "%s/%s", path_token, command);
if (access(command_path, X_OK) ==
0) { // Check if the file exists and is executable
free(path_copy);
return command_path;
}
}
free(path_copy);
free(command_path);
// search in current directory, and root directory
if (access(command, X_OK) == 0) {
return strdup(command);
}
return NULL;
}
int exec_command(char **argv, int background, char *std_out, int mode,
User *user) {
char *path = search_command(argv[0]);
if (path != NULL) {
pid_t pid = fork();
if (pid < 0) {
return 1;
} else if (pid == 0) { // child process
// freopen(std_out, mode == 0 ? "w" : "a", stdout);
int fd = open(std_out, mode == 0 ? O_WRONLY | O_CREAT | O_TRUNC : O_WRONLY | O_CREAT | O_APPEND, 0666);
dup2(fd, STDOUT_FILENO);
close(fd);
execv(path, argv);
return 3;
} else { // parent process
if (!background)
wait(NULL);
else {
user->add_bg_process(user, pid);
msleep(100);
}
}
free(path);
return 0;
}
return 2;
}
/*
* Utility function to write the given buffer to the given file in reverse order.
* Arguments:
* - char *file: the file name to write to
* If the file does not exist, it will be created.
* - char *buffer: the buffer to read data from
* - size_t count: the size of the buffer
* If the buffer ends with a newline character, it will be ignored.
* Returns:
* - int status: the exit status of the command
* 0: success
* 3: fopen failed
*
*/
int write_in_reverse(char *file, char *buffer, size_t count) {
FILE *fp = fopen(file, "a");
if (fp == NULL) {
return 3;
}
if (count >= 1 && buffer[count - 1] == '\n') {
count--;
}
// print in reverse order
for (ssize_t i = count - 1; i >= 0; i--) {
fputc(buffer[i], fp);
}
fputc('\n', fp);
fclose(fp);
return 0;
}
/*
* Utility for >>> redirection.
* Arguments:
* - char *file: the file name to write to
* - char **argv: null-terminated array of strings
* - char *path: the path to the command to execute
* - int *status: the exit status of the command
* 0: success
* 2: Fork failed
* 3: getdelim failed
* 4: write_in_reverse failed
*
*/
void handle_rappend(char *file, char **argv, char *path, int *status) {
int fd[2];
pipe(fd);
pid_t pid = fork();
if (pid < 0) {
*status = 2;
return;
} else if (pid == 0) {
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
execv(path, argv);
} else {
close(fd[1]);
char *buffer = NULL;
size_t size = 0;
ssize_t count = getdelim(&buffer, &size, '\0', fdopen(fd[0], "r"));
if (count == -1) {
*status = 3;
}
else if (write_in_reverse(file, buffer, count) != 0) {
*status = 4;
}
if (buffer != NULL)
free(buffer);
close(fd[0]);
wait(NULL);
}
status = 0;
}
int exec_with_pipe(char **argv, char *file, int bg, User *user) {
char *_path = search_command(argv[0]);
if (_path == NULL) {
return 1;
}
size_t len = strlen(_path);
char path[len + 1];
strncpy(path, _path, len);
path[len] = '\0';
int status = 0;
pid_t pid = -1;
if (bg) {
pid = fork();
}
if (pid > 0) {
user->add_bg_process(user, pid);
return 0;
}
handle_rappend(file, argv, path, &status);
if (bg){
exit(status);
}
return status;
}