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
7 * 2006-06-08 Amit Gud <agud@redhat.com>
8 * - Moved to nfs-utils/support/nfs from util-linux/mount
12 #include <string.h> /* for index */
13 #include <ctype.h> /* for isdigit */
14 #include <sys/stat.h> /* for umask */
16 #include "nfs_mntent.h"
20 /* Unfortunately the classical Unix /etc/mtab and /etc/fstab
21 do not handle directory names containing spaces.
22 Here we mangle them, replacing a space by \040.
23 What do other Unices do? */
25 static unsigned char need_escaping[] = { ' ', '\t', '\n', '\\' };
28 mangle(const char *arg) {
29 const unsigned char *s = (const unsigned char *)arg;
34 ss = sp = xmalloc(4*n+1);
36 for (n = 0; n < sizeof(need_escaping); n++) {
37 if (*s == need_escaping[n]) {
39 *sp++ = '0' + ((*s & 0300) >> 6);
40 *sp++ = '0' + ((*s & 070) >> 3);
41 *sp++ = '0' + (*s & 07);
55 is_space_or_tab (char c) {
56 return (c == ' ' || c == '\t');
60 skip_spaces(char *s) {
61 while (is_space_or_tab(*s))
67 skip_nonspaces(char *s) {
68 while (*s && !is_space_or_tab(*s))
73 #define isoctal(a) (((a) & ~7) == '0')
75 /* returns malloced pointer - no more strdup required */
80 ss = skip_nonspaces(s);
81 ret = sp = xmalloc(ss-s+1);
83 if (*s == '\\' && isoctal(s[1]) && isoctal(s[2]) && isoctal(s[3])) {
84 *sp++ = 64*(s[1] & 7) + 8*(s[2] & 7) + (s[3] & 7);
94 * fstat'ing the file and allocating a buffer holding all of it
95 * may be a bad idea: if the file is /proc/mounts, the stat
97 * (On the other hand, mangling and unmangling is meaningless
102 nfs_setmntent (const char *file, char *mode) {
103 mntFILE *mfp = xmalloc(sizeof(*mfp));
104 mode_t old_umask = umask(077);
106 mfp->mntent_fp = fopen(file, mode);
108 mfp->mntent_file = xstrdup(file);
109 mfp->mntent_errs = (mfp->mntent_fp == NULL);
110 mfp->mntent_softerrs = 0;
111 mfp->mntent_lineno = 0;
116 nfs_endmntent (mntFILE *mfp) {
119 fclose(mfp->mntent_fp);
120 if (mfp->mntent_file)
121 free(mfp->mntent_file);
127 nfs_addmntent (mntFILE *mfp, nfs_mntent_t *mnt) {
128 char *m1, *m2, *m3, *m4;
131 if (fseek (mfp->mntent_fp, 0, SEEK_END))
132 return 1; /* failure */
134 m1 = mangle(mnt->mnt_fsname);
135 m2 = mangle(mnt->mnt_dir);
136 m3 = mangle(mnt->mnt_type);
137 m4 = mangle(mnt->mnt_opts);
139 res = fprintf (mfp->mntent_fp, "%s %s %s %s %d %d\n",
140 m1, m2, m3, m4, mnt->mnt_freq, mnt->mnt_passno);
146 return (res < 0) ? 1 : 0;
149 /* Read the next entry from the file fp. Stop reading at an incorrect entry. */
151 nfs_getmntent (mntFILE *mfp) {
152 static char buf[4096];
153 static nfs_mntent_t me;
157 if (mfp->mntent_errs || mfp->mntent_softerrs >= ERR_MAX)
160 /* read the next non-blank non-comment line */
162 if (fgets (buf, sizeof(buf), mfp->mntent_fp) == NULL)
165 mfp->mntent_lineno++;
166 s = index (buf, '\n');
168 /* Missing final newline? Otherwise extremely */
169 /* long line - assume file was corrupted */
170 if (feof(mfp->mntent_fp)) {
171 fprintf(stderr, _("[mntent]: warning: no final "
172 "newline at the end of %s\n"),
176 mfp->mntent_errs = 1;
181 if (--s >= buf && *s == '\r')
183 s = skip_spaces(buf);
184 } while (*s == '\0' || *s == '#');
186 me.mnt_fsname = unmangle(s);
187 s = skip_nonspaces(s);
189 me.mnt_dir = unmangle(s);
190 s = skip_nonspaces(s);
192 me.mnt_type = unmangle(s);
193 s = skip_nonspaces(s);
195 me.mnt_opts = unmangle(s);
196 s = skip_nonspaces(s);
200 me.mnt_freq = atoi(s);
201 while(isdigit(*s)) s++;
204 if(*s && !is_space_or_tab(*s))
209 me.mnt_passno = atoi(s);
210 while(isdigit(*s)) s++;
213 if(*s && !is_space_or_tab(*s))
216 /* allow more stuff, e.g. comments, on this line */
221 mfp->mntent_softerrs++;
222 fprintf(stderr, _("[mntent]: line %d in %s is bad%s\n"),
223 mfp->mntent_lineno, mfp->mntent_file,
224 (mfp->mntent_errs || mfp->mntent_softerrs >= ERR_MAX) ?
225 _("; rest of file ignored") : "");