-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.c
81 lines (62 loc) · 1.98 KB
/
check.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Function to trim leading and trailing spaces
char *trim(char *str) {
char *end;
// Trim leading space
while (isspace((unsigned char)*str)) str++;
if (*str == 0) return str; // All spaces?
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) end--;
// Write new null terminator
*(end + 1) = 0;
return str;
}
// Function to split a string by a delimiter and handle multiple spaces
char **split_string(const char *str, const char delimiter, int *num_tokens) {
int count = 0;
const char *temp = str;
// Count the number of delimiters
while (*temp) {
if (*temp == delimiter) count++;
temp++;
}
// Allocate memory for tokens
char **tokens = malloc((count + 1) * sizeof(char *));
if (tokens == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
int index = 0;
char *start = strdup(str);
char *end = strchr(start, delimiter);
while (end != NULL) {
*end = '\0';
tokens[index++] = trim(start);
start = end + 1;
end = strchr(start, delimiter);
}
tokens[index++] = trim(start);
*num_tokens = index;
return tokens;
}
int main() {
char command[] = "bar -c | alayof -d | p -c | t -r";
int num_tokens;
char **commands = split_string(command, '|', &num_tokens);
for (int i = 0; i < num_tokens; i++) {
printf("Command %d: %s\n", i + 1, commands[i]);
// Tokenize each command by spaces
int num_subtokens;
char **subtokens = split_string(commands[i], ' ', &num_subtokens);
for (int j = 0; j < num_subtokens; j++) {
printf(" Subtoken %d: %s\n", j + 1, subtokens[j]);
}
free(subtokens); // Free the memory allocated for subtokens
}
free(commands); // Free the memory allocated for commands
return 0;
}