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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/*
* Support functions. Exported functions are prototyped in sundries.h.
* sundries.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
*
* added fcntl locking by Kjetil T. (kjetilho@math.uio.no) - aeb, 950927
*
* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
* - added Native Language Support
*
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <mntent.h> /* for MNTTYPE_SWAP */
#include "fstab.h"
#include "sundries.h"
#include "realpath.h"
#include "xmalloc.h"
#include "nls.h"
char *
xstrndup (const char *s, int n) {
char *t;
if (s == NULL)
die (EX_SOFTWARE, _("bug in xstrndup call"));
t = xmalloc(n+1);
strncpy(t,s,n);
t[n] = 0;
return t;
}
/* reallocates its first arg - typical use: s = xstrconcat3(s,t,u); */
char *
xstrconcat3 (char *s, const char *t, const char *u) {
size_t len = 0;
len = (s ? strlen(s) : 0) + (t ? strlen(t) : 0) + (u ? strlen(u) : 0);
if (!len)
return NULL;
if (!s) {
s = xmalloc(len + 1);
*s = '\0';
}
else
s = xrealloc(s, len + 1);
if (t)
strcat(s, t);
if (u)
strcat(s, u);
return s;
}
/* frees its first arg - typical use: s = xstrconcat4(s,t,u,v); */
char *
xstrconcat4 (char *s, const char *t, const char *u, const char *v) {
size_t len = 0;
len = (s ? strlen(s) : 0) + (t ? strlen(t) : 0) +
(u ? strlen(u) : 0) + (v ? strlen(v) : 0);
if (!len)
return NULL;
if (!s) {
s = xmalloc(len + 1);
*s = '\0';
}
else
s = xrealloc(s, len + 1);
if (t)
strcat(s, t);
if (u)
strcat(s, u);
if (v)
strcat(s, v);
return s;
}
/* Call this with SIG_BLOCK to block and SIG_UNBLOCK to unblock. */
void
block_signals (int how) {
sigset_t sigs;
sigfillset (&sigs);
sigdelset(&sigs, SIGTRAP);
sigdelset(&sigs, SIGSEGV);
sigprocmask (how, &sigs, (sigset_t *) 0);
}
/* Non-fatal error. Print message and return. */
/* (print the message in a single printf, in an attempt
to avoid mixing output of several threads) */
void
error (const char *fmt, ...) {
va_list args;
if (mount_quiet)
return;
va_start (args, fmt);
vfprintf (stderr, fmt, args);
va_end (args);
fputc('\n', stderr);
}
/* True if fstypes match. Null *TYPES means match anything,
except that swap types always return false. */
/* Accept nonfs,proc,devpts and nonfs,noproc,nodevpts
with the same meaning. */
int
matching_type (const char *type, const char *types) {
int no; /* negated types list */
int len;
const char *p;
if (streq (type, MNTTYPE_SWAP))
return 0;
if (types == NULL)
return 1;
no = 0;
if (!strncmp(types, "no", 2)) {
no = 1;
types += 2;
}
/* Does type occur in types, separated by commas? */
len = strlen(type);
p = types;
while(1) {
if (!strncmp(p, "no", 2) && !strncmp(p+2, type, len) &&
(p[len+2] == 0 || p[len+2] == ','))
return 0;
if (strncmp(p, type, len) == 0 &&
(p[len] == 0 || p[len] == ','))
return !no;
p = index(p,',');
if (!p)
break;
p++;
}
return no;
}
/* Returns 1 if needle found or noneedle not found in haystack
* Otherwise returns 0
*/
static int
check_option(const char *haystack, const char *needle) {
const char *p, *r;
int len, needle_len, this_len;
int no;
no = 0;
if (!strncmp(needle, "no", 2)) {
no = 1;
needle += 2;
}
needle_len = strlen(needle);
len = strlen(haystack);
for (p = haystack; p < haystack+len; p++) {
r = strchr(p, ',');
if (r) {
this_len = r-p;
} else {
this_len = strlen(p);
}
if (this_len != needle_len) {
p += this_len;
continue;
}
if (strncmp(p, needle, this_len) == 0)
return !no; /* foo or nofoo was found */
p += this_len;
}
return no; /* foo or nofoo was not found */
}
/* Returns 1 if each of the test_opts options agrees with the entire
* list of options.
* Returns 0 if any noopt is found in test_opts and opt is found in options.
* Returns 0 if any opt is found in test_opts but is not found in options.
* Unlike fs type matching, nonetdev,user and nonetdev,nouser have
* DIFFERENT meanings; each option is matched explicitly as specified.
*/
int
matching_opts (const char *options, const char *test_opts) {
const char *p, *r;
char *q;
int len, this_len;
if (test_opts == NULL)
return 1;
len = strlen(test_opts);
q = alloca(len+1);
if (q == NULL)
die (EX_SYSERR, _("not enough memory"));
for (p = test_opts; p < test_opts+len; p++) {
r = strchr(p, ',');
if (r) {
this_len = r-p;
} else {
this_len = strlen(p);
}
if (!this_len) continue; /* if two ',' appear in a row */
strncpy(q, p, this_len);
q[this_len] = '\0';
if (!check_option(options, q))
return 0; /* any match failure means failure */
p += this_len;
}
/* no match failures in list means success */
return 1;
}
/* Make a canonical pathname from PATH. Returns a freshly malloced string.
It is up the *caller* to ensure that the PATH is sensible. i.e.
canonicalize ("/dev/fd0/.") returns "/dev/fd0" even though ``/dev/fd0/.''
is not a legal pathname for ``/dev/fd0''. Anything we cannot parse
we return unmodified. */
char *
canonicalize (const char *path) {
char canonical[PATH_MAX+2];
if (path == NULL)
return NULL;
#if 1
if (streq(path, "none") ||
streq(path, "proc") ||
streq(path, "devpts"))
return xstrdup(path);
#endif
if (myrealpath (path, canonical, PATH_MAX+1))
return xstrdup(canonical);
return xstrdup(path);
}
/*
* Parses NAME=value, returns -1 on parse error, 0 success. The success is also
* when the 'spec' doesn't contain name=value pair (because the spec could be
* a devname too). In particular case the pointer 'name' is set to NULL.
* The result is a new allocated string (the 'name' pointer).
*/
int
parse_spec(const char *spec, char **name, char **value)
{
char *vl, *tk, *cp;
*name = NULL;
*value = NULL;
if (!(cp = strchr(spec, '=')))
return 0; /* no name= */
tk = xstrdup(spec);
vl = tk + (cp - spec);
*vl++ = '\0';
if (*vl == '"' || *vl == '\'') {
if (!(cp = strrchr(vl+1, *vl))) {
free(tk);
return -1; /* parse error */
}
vl++;
*cp = '\0';
}
*name = tk;
*value = vl;
return 0;
}
|