-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
71 lines (66 loc) · 1.02 KB
/
util.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
#include <stdlib.h>
#include <string.h>
char *dupstr(const char *s)
{
size_t n = strlen(s) + 1;
char *t = malloc(n);
if (t)
memcpy(t, s, n);
return t;
}
char *trim(char s[])
{
char c, *r, *t;
char lc = ' ';
r = t = s;
while ((c = *s++) && c != CMNT_MAC) {
if (isspace(c))
c = ' ';
if (c != ' ' || lc != ' ')
*t++ = lc = c;
}
if (lc == ' ' && t > r)
--t;
*t = '\0';
return r;
}
char *copystr(char *s, char *t, char c)
{
while ((*s = *t++)) // -Wparen
++s;
if (c) {
*s++ = c;
*s = '\0';
}
return s;
}
char *concat(char *lines[], unsigned n)
{
char *s, *t;
int len, i;
if (n == 1)
return lines[0];
for (len = i = 0; i < n; ++i)
if (*lines[i])
len += strlen(lines[i]) + 1;
s = malloc(len + 1);
if (!s)
return NULL;
for (t = s, i = 0; i < n; ++i)
if (*lines[i])
t = copystr(t, lines[i], ' ');
*--t = '\0';
return s;
}
int oc_diff(const char s[])
{
char c;
int i = 0;
while ((c = *s++)) { // -Wparen
if (c == LIST_BEG)
++i;
else if (c == LIST_END)
--i;
}
return i;
}