1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21: #include <alloca.h>
22: #include <mntent.h>
23: #include <stdio.h>
24: #include <stdio_ext.h>
25: #include <string.h>
26: #include <sys/types.h>
27:
28: #ifdef USE_IN_LIBIO
29: # define flockfile(s) _IO_flockfile (s)
30: # define funlockfile(s) _IO_funlockfile (s)
31: #endif
32:
33: #undef __setmntent
34: #undef __endmntent
35: #undef __getmntent_r
36:
37:
38:
39: FILE *
40: __setmntent (const char *file, const char *mode)
41: {
42:
43:
44: size_t modelen = strlen (mode);
45: char newmode[modelen + 2];
46: memcpy (mempcpy (newmode, mode, modelen), "c", 2);
47: FILE *result = fopen (file, newmode);
48:
49: if (result != NULL)
50:
51: __fsetlocking (result, FSETLOCKING_BYCALLER);
52:
53: return result;
54: }
55: INTDEF(__setmntent)
56: weak_alias (__setmntent, setmntent)
57:
58:
59:
60: int
61: __endmntent (FILE *stream)
62: {
63: if (stream)
64: fclose (stream);
65: return 1;
66: }
67: INTDEF(__endmntent)
68: weak_alias (__endmntent, endmntent)
69:
70:
71:
72:
73:
74:
75: static char *
76: decode_name (char *buf)
77: {
78: char *rp = buf;
79: char *wp = buf;
80:
81: do
82: if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
83: {
84:
85: *wp++ = ' ';
86: rp += 3;
87: }
88: else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1')
89: {
90:
91: *wp++ = '\t';
92: rp += 3;
93: }
94: else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
95: {
96:
97: *wp++ = '\n';
98: rp += 3;
99: }
100: else if (rp[0] == '\\' && rp[1] == '\\')
101: {
102:
103: *wp++ = '\\';
104: rp += 1;
105: }
106: else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4')
107: {
108:
109: *wp++ = '\\';
110: rp += 3;
111: }
112: else
113: *wp++ = *rp;
114: while (*rp++ != '\0');
115:
116: return buf;
117: }
118:
119:
120:
121:
122:
123: struct mntent *
124: __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
125: {
126: char *cp;
127: char *head;
128:
129: flockfile (stream);
130: do
131: {
132: char *end_ptr;
133:
134: if (fgets_unlocked (buffer, bufsiz, stream) == NULL)
135: {
136: funlockfile (stream);
137: return NULL;
138: }
139:
140: end_ptr = strchr (buffer, '\n');
141: if (end_ptr != NULL)
142: *end_ptr = '\0';
143: else
144: {
145:
146: char tmp[1024];
147: while (fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
148: if (strchr (tmp, '\n') != NULL)
149: break;
150: }
151:
152: head = buffer + strspn (buffer, " \t");
153:
154: }
155: while (head[0] == '\0' || head[0] == '#');
156:
157: cp = __strsep (&head, " \t");
158: mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
159: if (head)
160: head += strspn (head, " \t");
161: cp = __strsep (&head, " \t");
162: mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
163: if (head)
164: head += strspn (head, " \t");
165: cp = __strsep (&head, " \t");
166: mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
167: if (head)
168: head += strspn (head, " \t");
169: cp = __strsep (&head, " \t");
170: mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
171: switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
172: {
173: case 0:
174: mp->mnt_freq = 0;
175: case 1:
176: mp->mnt_passno = 0;
177: case 2:
178: break;
179: }
180: funlockfile (stream);
181:
182: return mp;
183: }
184: INTDEF(__getmntent_r)
185: weak_alias (__getmntent_r, getmntent_r)
186:
187:
188:
189:
190:
191:
192: #define encode_name(name) \
193: do { \
194: const char *rp = name; \
195: \
196: while (*rp != '\0') \
197: if (*rp == ' ' || *rp == '\t' || *rp == '\\') \
198: break; \
199: else \
200: ++rp; \
201: \
202: if (*rp != '\0') \
203: { \
204:
205: \
206: char *wp; \
207: \
208: rp = name; \
209: name = wp = (char *) alloca (strlen (name) * 4 + 1); \
210: \
211: do \
212: if (*rp == ' ') \
213: { \
214: *wp++ = '\\'; \
215: *wp++ = '0'; \
216: *wp++ = '4'; \
217: *wp++ = '0'; \
218: } \
219: else if (*rp == '\t') \
220: { \
221: *wp++ = '\\'; \
222: *wp++ = '0'; \
223: *wp++ = '1'; \
224: *wp++ = '1'; \
225: } \
226: else if (*rp == '\n') \
227: { \
228: *wp++ = '\\'; \
229: *wp++ = '0'; \
230: *wp++ = '1'; \
231: *wp++ = '2'; \
232: } \
233: else if (*rp == '\\') \
234: { \
235: *wp++ = '\\'; \
236: *wp++ = '\\'; \
237: } \
238: else \
239: *wp++ = *rp; \
240: while (*rp++ != '\0'); \
241: } \
242: } while (0)
243:
244:
245:
246:
247: int
248: __addmntent (FILE *stream, const struct mntent *mnt)
249: {
250: struct mntent mntcopy = *mnt;
251: if (fseek (stream, 0, SEEK_END))
252: return 1;
253:
254:
255: encode_name (mntcopy.mnt_fsname);
256: encode_name (mntcopy.mnt_dir);
257: encode_name (mntcopy.mnt_type);
258: encode_name (mntcopy.mnt_opts);
259:
260: return (fprintf (stream, "%s %s %s %s %d %d\n",
261: mntcopy.mnt_fsname,
262: mntcopy.mnt_dir,
263: mntcopy.mnt_type,
264: mntcopy.mnt_opts,
265: mntcopy.mnt_freq,
266: mntcopy.mnt_passno)
267: < 0 ? 1 : 0);
268: }
269: weak_alias (__addmntent, addmntent)
270:
271:
272:
273:
274: char *
275: __hasmntopt (const struct mntent *mnt, const char *opt)
276: {
277: const size_t optlen = strlen (opt);
278: char *rest = mnt->mnt_opts, *p;
279:
280: while ((p = strstr (rest, opt)) != NULL)
281: {
282: if ((p == rest || p[-1] == ',')
283: && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
284: return p;
285:
286: rest = strchr (p, ',');
287: if (rest == NULL)
288: break;
289: ++rest;
290: }
291:
292: return NULL;
293: }
294: weak_alias (__hasmntopt, hasmntopt)