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
|
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "util-common.h"
const char *mbasename(const char *path)
{
const char *last = strrchr(path, '/');
if(last) {
return last + 1;
}
return path;
}
char *mdirname(const char *path)
{
char *ret, *last;
if(path == NULL || *path == '\0') {
return strdup(".");
}
if((ret = strdup(path)) == NULL) {
return NULL;
}
last = strrchr(ret, '/');
if(last != NULL) {
if(last == ret) {
last++;
}
*last = '\0';
return ret;
}
free(ret);
return strdup(".");
}
int llstat(char *path, struct stat *buf)
{
int ret;
char *c = NULL;
size_t len = strlen(path);
while(len > 1 && path[len - 1] == '/') {
--len;
c = path + len;
}
if(c) {
*c = '\0';
ret = lstat(path, buf);
*c = '/';
} else {
ret = lstat(path, buf);
}
return ret;
}
char *safe_fgets(char *s, int size, FILE *stream)
{
char *ret;
int errno_save = errno, ferror_save = ferror(stream);
while((ret = fgets(s, size, stream)) == NULL && !feof(stream)) {
if(errno == EINTR) {
errno = errno_save;
if(!ferror_save) {
clearerr(stream);
}
} else {
break;
}
}
return ret;
}
#ifndef HAVE_STRNDUP
static size_t strnlen(const char *s, size_t max)
{
register const char *p;
for(p = s; *p && max--; ++p);
return (p - s);
}
char *strndup(const char *s, size_t n)
{
size_t len = strnlen(s, n);
char *new = (char *) malloc(len + 1);
if(new == NULL) {
return NULL;
}
new[len] = '\0';
return (char *)memcpy(new, s, len);
}
#endif
|