-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
126 lines (105 loc) · 2.59 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
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
//
// Created by gosha on 15.02.2021.
//
int error(const char *msg)
{
printf("Error: %s\n", msg);
return -1;
}
int parse(const char *cmd, char **args)
{
const char *p = cmd;
int count = 0;
for (;;) {
while (isspace(*p)) p++;
if (count >= 3) {
return count;
}
if (*p == '\0') break;
if (*p == '"' || *p == '\'') {
int quote = *p++;
const char *begin = p;
while (*p && *p != quote) p++;
if (*p == '\0') return error("Unmachted quote");
strncpy(args[count], begin, p-begin);
count++;
p++;
continue;
}
if (strchr("<>()|", *p)) {
args[count] = calloc(1, 256);
strncpy(args[count], p, 1);
count++;
p++;
continue;
}
if (isalnum(*p) || *p == '.' || *p == '/') {
const char *begin = p;
while (isalnum(*p) || *p == '.' || *p == '/') p++;
strncpy(args[count], begin, p-begin);
count++;
continue;
}
return error("Illegal character");
}
return count;
}
int check_directory(const char *path) {
DIR *dir = opendir(path);
if (dir) return 1;
else return 0;
}
int startsWith(const char *str, const char *pre) {
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? 0 : memcmp(pre, str, lenpre) == 0;
}
void append_path_part(char *path, const char *part) {
strcat(path, "/");
strcat(path, part);
}
char *get_line(void) {
char *line = malloc(100), *linep = line;
size_t lenmax = 100, len = lenmax;
int c;
if (line == NULL)
return NULL;
for (;;) {
c = fgetc(stdin);
if (c == EOF)
break;
if (--len == 0) {
len = lenmax;
char *linen = realloc(linep, lenmax *= 2);
if (linen == NULL) {
free(linep);
return NULL;
}
line = linen + (line - linep);
linep = linen;
}
if ((*line++ = c) == '\n')
break;
}
*line = '\0';
return linep;
}
void remove_ending_symbol(char *str, char sym) {
for (int i = 0;; i++) {
if (str[i] == sym)
str[i] = 0;
if (str[i] == 0)
break;
}
}
void remove_until(char *str, char sym) {
size_t len = strlen(str);
for (size_t i = len; i >= 0; i--) {
if (str[i] != sym)
str[i] = 0;
else {
str[i] = 0;
break;
}
}
}