1 /* Private version of the libc *mntent() routines. */
2 /* Note slightly different prototypes. */
4 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
5 * - added Native Language Support
9 #include <string.h> /* for index */
10 #include <ctype.h> /* for isdigit */
11 #include <sys/stat.h> /* for umask */
12 #include "mount_mntent.h"
13 #include "sundries.h" /* for xmalloc */
16 /* Unfortunately the classical Unix /etc/mtab and /etc/fstab
17 do not handle directory names containing spaces.
18 Here we mangle them, replacing a space by \040.
19 What do other Unices do? */
21 static unsigned char need_escaping[] = { ' ', '\t', '\n', '\\' };
24 mangle(const char *s) {
29 ss = sp = xmalloc(4*n+1);
31 for (n = 0; n < sizeof(need_escaping); n++) {
32 if (*s == need_escaping[n]) {
34 *sp++ = '0' + ((*s & 0300) >> 6);
35 *sp++ = '0' + ((*s & 070) >> 3);
36 *sp++ = '0' + (*s & 07);
50 is_space_or_tab (char c) {
51 return (c == ' ' || c == '\t');
55 skip_spaces(char *s) {
56 while (is_space_or_tab(*s))
62 skip_nonspaces(char *s) {
63 while (*s && !is_space_or_tab(*s))
68 #define isoctal(a) (((a) & ~7) == '0')
70 /* returns malloced pointer - no more strdup required */
75 ss = skip_nonspaces(s);
76 ret = sp = xmalloc(ss-s+1);
78 if (*s == '\\' && isoctal(s[1]) && isoctal(s[2]) && isoctal(s[3])) {
79 *sp++ = 64*(s[1] & 7) + 8*(s[2] & 7) + (s[3] & 7);
89 * fstat'ing the file and allocating a buffer holding all of it
90 * may be a bad idea: if the file is /proc/mounts, the stat
92 * (On the other hand, mangling and unmangling is meaningless
97 my_setmntent (const char *file, char *mode) {
98 mntFILE *mfp = xmalloc(sizeof(*mfp));
99 mode_t old_umask = umask(077);
101 mfp->mntent_fp = fopen(file, mode);
103 mfp->mntent_file = xstrdup(file);
104 mfp->mntent_errs = (mfp->mntent_fp == NULL);
105 mfp->mntent_softerrs = 0;
106 mfp->mntent_lineno = 0;
111 my_endmntent (mntFILE *mfp) {
114 fclose(mfp->mntent_fp);
115 free(mfp->mntent_file);
121 my_addmntent (mntFILE *mfp, struct my_mntent *mnt) {
122 char *m1, *m2, *m3, *m4;
125 if (fseek (mfp->mntent_fp, 0, SEEK_END))
126 return 1; /* failure */
128 m1 = mangle(mnt->mnt_fsname);
129 m2 = mangle(mnt->mnt_dir);
130 m3 = mangle(mnt->mnt_type);
131 m4 = mangle(mnt->mnt_opts);
133 res = fprintf (mfp->mntent_fp, "%s %s %s %s %d %d\n",
134 m1, m2, m3, m4, mnt->mnt_freq, mnt->mnt_passno);
140 return (res < 0) ? 1 : 0;
143 /* Read the next entry from the file fp. Stop reading at an incorrect entry. */
145 my_getmntent (mntFILE *mfp) {
146 static char buf[4096];
147 static struct my_mntent me;
151 if (mfp->mntent_errs || mfp->mntent_softerrs >= ERR_MAX)
154 /* read the next non-blank non-comment line */
156 if (fgets (buf, sizeof(buf), mfp->mntent_fp) == NULL)
159 mfp->mntent_lineno++;
160 s = strchr (buf, '\n');
162 /* Missing final newline? Otherwise extremely */
163 /* long line - assume file was corrupted */
164 if (feof(mfp->mntent_fp)) {
165 fprintf(stderr, _("[mntent]: warning: no final "
166 "newline at the end of %s\n"),
170 mfp->mntent_errs = 1;
175 if (--s >= buf && *s == '\r')
177 s = skip_spaces(buf);
178 } while (*s == '\0' || *s == '#');
180 me.mnt_fsname = unmangle(s);
181 s = skip_nonspaces(s);
183 me.mnt_dir = unmangle(s);
184 s = skip_nonspaces(s);
186 me.mnt_type = unmangle(s);
187 s = skip_nonspaces(s);
189 me.mnt_opts = unmangle(s);
190 s = skip_nonspaces(s);
194 me.mnt_freq = atoi(s);
195 while(isdigit(*s)) s++;
198 if(*s && !is_space_or_tab(*s))
203 me.mnt_passno = atoi(s);
204 while(isdigit(*s)) s++;
207 if(*s && !is_space_or_tab(*s))
210 /* allow more stuff, e.g. comments, on this line */
215 mfp->mntent_softerrs++;
216 fprintf(stderr, _("[mntent]: line %d in %s is bad%s\n"),
217 mfp->mntent_lineno, mfp->mntent_file,
218 (mfp->mntent_errs || mfp->mntent_softerrs >= ERR_MAX) ?
219 _("; rest of file ignored") : "");