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 unsigned char *s) {
33 ss = sp = xmalloc(4*n+1);
35 for (n = 0; n < sizeof(need_escaping); n++) {
36 if (*s == need_escaping[n]) {
38 *sp++ = '0' + ((*s & 0300) >> 6);
39 *sp++ = '0' + ((*s & 070) >> 3);
40 *sp++ = '0' + (*s & 07);
54 is_space_or_tab (char c) {
55 return (c == ' ' || c == '\t');
59 skip_spaces(char *s) {
60 while (is_space_or_tab(*s))
66 skip_nonspaces(char *s) {
67 while (*s && !is_space_or_tab(*s))
72 #define isoctal(a) (((a) & ~7) == '0')
74 /* returns malloced pointer - no more strdup required */
79 ss = skip_nonspaces(s);
80 ret = sp = xmalloc(ss-s+1);
82 if (*s == '\\' && isoctal(s[1]) && isoctal(s[2]) && isoctal(s[3])) {
83 *sp++ = 64*(s[1] & 7) + 8*(s[2] & 7) + (s[3] & 7);
93 * fstat'ing the file and allocating a buffer holding all of it
94 * may be a bad idea: if the file is /proc/mounts, the stat
96 * (On the other hand, mangling and unmangling is meaningless
101 nfs_setmntent (const char *file, char *mode) {
102 mntFILE *mfp = xmalloc(sizeof(*mfp));
103 mode_t old_umask = umask(077);
105 mfp->mntent_fp = fopen(file, mode);
107 mfp->mntent_file = xstrdup(file);
108 mfp->mntent_errs = (mfp->mntent_fp == NULL);
109 mfp->mntent_softerrs = 0;
110 mfp->mntent_lineno = 0;
115 nfs_endmntent (mntFILE *mfp) {
118 fclose(mfp->mntent_fp);
119 if (mfp->mntent_file)
120 free(mfp->mntent_file);
126 nfs_addmntent (mntFILE *mfp, nfs_mntent_t *mnt) {
127 char *m1, *m2, *m3, *m4;
130 if (fseek (mfp->mntent_fp, 0, SEEK_END))
131 return 1; /* failure */
133 m1 = mangle(mnt->mnt_fsname);
134 m2 = mangle(mnt->mnt_dir);
135 m3 = mangle(mnt->mnt_type);
136 m4 = mangle(mnt->mnt_opts);
138 res = fprintf (mfp->mntent_fp, "%s %s %s %s %d %d\n",
139 m1, m2, m3, m4, mnt->mnt_freq, mnt->mnt_passno);
145 return (res < 0) ? 1 : 0;
148 /* Read the next entry from the file fp. Stop reading at an incorrect entry. */
150 nfs_getmntent (mntFILE *mfp) {
151 static char buf[4096];
152 static nfs_mntent_t me;
156 if (mfp->mntent_errs || mfp->mntent_softerrs >= ERR_MAX)
159 /* read the next non-blank non-comment line */
161 if (fgets (buf, sizeof(buf), mfp->mntent_fp) == NULL)
164 mfp->mntent_lineno++;
165 s = index (buf, '\n');
167 /* Missing final newline? Otherwise extremely */
168 /* long line - assume file was corrupted */
169 if (feof(mfp->mntent_fp)) {
170 fprintf(stderr, _("[mntent]: warning: no final "
171 "newline at the end of %s\n"),
175 mfp->mntent_errs = 1;
180 if (--s >= buf && *s == '\r')
182 s = skip_spaces(buf);
183 } while (*s == '\0' || *s == '#');
185 me.mnt_fsname = unmangle(s);
186 s = skip_nonspaces(s);
188 me.mnt_dir = unmangle(s);
189 s = skip_nonspaces(s);
191 me.mnt_type = unmangle(s);
192 s = skip_nonspaces(s);
194 me.mnt_opts = unmangle(s);
195 s = skip_nonspaces(s);
199 me.mnt_freq = atoi(s);
200 while(isdigit(*s)) s++;
203 if(*s && !is_space_or_tab(*s))
208 me.mnt_passno = atoi(s);
209 while(isdigit(*s)) s++;
212 if(*s && !is_space_or_tab(*s))
215 /* allow more stuff, e.g. comments, on this line */
220 mfp->mntent_softerrs++;
221 fprintf(stderr, _("[mntent]: line %d in %s is bad%s\n"),
222 mfp->mntent_lineno, mfp->mntent_file,
223 (mfp->mntent_errs || mfp->mntent_softerrs >= ERR_MAX) ?
224 _("; rest of file ignored") : "");