forked from TuxForge/archie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
44 lines (39 loc) · 1.13 KB
/
utils.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
#include "archium.h"
void handle_signal(int signal) {
if (signal == SIGINT) {
printf("\nInterrupt received. Exiting gracefully.\n");
exit(EXIT_SUCCESS);
}
}
void get_input(char *input, const char *prompt) {
char *line = readline(prompt);
if (line) {
strcpy(input, line);
free(line);
} else {
input[0] = '\0';
}
char *start = input;
while (isspace((unsigned char)*start)) start++;
memmove(input, start, strlen(start) + 1);
char *end = input + strlen(input) - 1;
while (end > input && isspace((unsigned char)*end)) end--;
end[1] = '\0';
if (strlen(input) == 0) {
get_input(input, prompt);
}
}
int is_valid_command(const char *command) {
const char *valid_commands[] = {
"u", "i", "r", "p", "c", "o", "s", "h", "q",
"l", "?", "cu", "dt", "cc", "lo"
};
int num_commands = sizeof(valid_commands) / sizeof(valid_commands[0]);
for (int i = 0; i < num_commands; i++) {
if (strcmp(command, valid_commands[i]) == 0 ||
strncmp(command, "u ", 2) == 0) {
return 1;
}
}
return 0;
}