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
|
/* Private version of the libc *mntent() routines. */
/* Note slightly different prototypes. */
/* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
* - added Native Language Support
*/
#include <stdio.h>
#include <string.h> /* for index */
#include <ctype.h> /* for isdigit */
#include "mntent.h"
#include "sundries.h" /* for xmalloc */
#include "nls.h"
/* Unfortunately the classical Unix /etc/mtab and /etc/fstab
do not handle directory names containing spaces.
Here we mangle them, replacing a space by \040.
What do other Unices do? */
static unsigned char need_escaping[] = { ' ', '\t', '\n', '\\' };
static char *
mangle(unsigned char *s) {
char *ss, *sp;
int n;
n = strlen(s);
ss = sp = xmalloc(4*n+1);
while(1) {
for (n = 0; n < sizeof(need_escaping); n++) {
if (*s == need_escaping[n]) {
*sp++ = '\\';
*sp++ = '0' + ((*s & 0300) >> 6);
*sp++ = '0' + ((*s & 070) >> 3);
*sp++ = '0' + (*s & 07);
goto next;
}
}
*sp++ = *s;
if (*s == 0)
break;
next:
s++;
}
return ss;
}
static int
is_space_or_tab (char c) {
return (c == ' ' || c == '\t');
}
static char *
skip_spaces(char *s) {
while (is_space_or_tab(*s))
s++;
return s;
}
static char *
skip_nonspaces(char *s) {
while (*s && !is_space_or_tab(*s))
s++;
return s;
}
#define isoctal(a) (((a) & ~7) == '0')
/* returns malloced pointer - no more strdup required */
static char *
unmangle(char *s) {
char *ret, *ss, *sp;
ss = skip_nonspaces(s);
ret = sp = xmalloc(ss-s+1);
while(s != ss) {
if (*s == '\\' && isoctal(s[1]) && isoctal(s[2]) && isoctal(s[3])) {
*sp++ = 64*(s[1] & 7) + 8*(s[2] & 7) + (s[3] & 7);
s += 4;
} else
*sp++ = *s++;
}
*sp = 0;
return ret;
}
/*
* fstat'ing the file and allocating a buffer holding all of it
* may be a bad idea: if the file is /proc/mounts, the stat
* returns 0.
* (On the other hand, mangling and unmangling is meaningless
* for /proc/mounts.)
*/
mntFILE *
my_setmntent (const char *file, char *mode) {
mntFILE *mfp = xmalloc(sizeof(*mfp));
mfp->mntent_fp = fopen (file, mode);
mfp->mntent_file = xstrdup(file);
mfp->mntent_errs = (mfp->mntent_fp == NULL);
mfp->mntent_softerrs = 0;
mfp->mntent_lineno = 0;
return mfp;
}
void
my_endmntent (mntFILE *mfp) {
if (mfp) {
if (mfp->mntent_fp)
fclose(mfp->mntent_fp);
if (mfp->mntent_file)
free(mfp->mntent_file);
free(mfp);
}
}
int
my_addmntent (mntFILE *mfp, struct mntent *mnt) {
char *m1, *m2, *m3, *m4;
int res;
if (fseek (mfp->mntent_fp, 0, SEEK_END))
return 1; /* failure */
m1 = mangle(mnt->mnt_fsname);
m2 = mangle(mnt->mnt_dir);
m3 = mangle(mnt->mnt_type);
m4 = mangle(mnt->mnt_opts);
res = ((fprintf (mfp->mntent_fp, "%s %s %s %s %d %d\n",
m1, m2, m3, m4, mnt->mnt_freq, mnt->mnt_passno)
< 0) ? 1 : 0);
free(m1);
free(m2);
free(m3);
free(m4);
return res;
}
/* Read the next entry from the file fp. Stop reading at an incorrect entry. */
struct mntent *
my_getmntent (mntFILE *mfp) {
static char buf[4096];
static struct mntent me;
char *s;
again:
if (mfp->mntent_errs || mfp->mntent_softerrs >= ERR_MAX)
return NULL;
/* read the next non-blank non-comment line */
do {
if (fgets (buf, sizeof(buf), mfp->mntent_fp) == NULL)
return NULL;
mfp->mntent_lineno++;
s = index (buf, '\n');
if (s == NULL) {
/* Missing final newline? Otherwise extremely */
/* long line - assume file was corrupted */
if (feof(mfp->mntent_fp)) {
fprintf(stderr, _("[mntent]: warning: no final "
"newline at the end of %s\n"),
mfp->mntent_file);
s = index (buf, 0);
} else {
mfp->mntent_errs = 1;
goto err;
}
}
*s = 0;
if (--s >= buf && *s == '\r')
*s = 0;
s = skip_spaces(buf);
} while (*s == '\0' || *s == '#');
me.mnt_fsname = unmangle(s);
s = skip_nonspaces(s);
s = skip_spaces(s);
me.mnt_dir = unmangle(s);
s = skip_nonspaces(s);
s = skip_spaces(s);
me.mnt_type = unmangle(s);
s = skip_nonspaces(s);
s = skip_spaces(s);
me.mnt_opts = unmangle(s);
s = skip_nonspaces(s);
s = skip_spaces(s);
if(isdigit(*s)) {
me.mnt_freq = atoi(s);
while(isdigit(*s)) s++;
} else
me.mnt_freq = 0;
if(*s && !is_space_or_tab(*s))
goto err;
s = skip_spaces(s);
if(isdigit(*s)) {
me.mnt_passno = atoi(s);
while(isdigit(*s)) s++;
} else
me.mnt_passno = 0;
if(*s && !is_space_or_tab(*s))
goto err;
/* allow more stuff, e.g. comments, on this line */
return &me;
err:
mfp->mntent_softerrs++;
fprintf(stderr, _("[mntent]: line %d in %s is bad%s\n"),
mfp->mntent_lineno, mfp->mntent_file,
(mfp->mntent_errs || mfp->mntent_softerrs >= ERR_MAX) ?
_("; rest of file ignored") : "");
goto again;
}
|