]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/nfs_mntent.c
mount.nfs: Fix retry= to handle lack of reserved port situation
[nfs-utils.git] / support / nfs / nfs_mntent.c
1 /* Private version of the libc *mntent() routines. */
2 /* Note slightly different prototypes. */
3
4 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
5  * - added Native Language Support
6  *
7  * 2006-06-08 Amit Gud <agud@redhat.com>
8  * - Moved to nfs-utils/support/nfs from util-linux/mount
9  */
10
11 #include <stdio.h>
12 #include <string.h>             /* for index */
13 #include <ctype.h>              /* for isdigit */
14 #include <sys/stat.h>           /* for umask */
15
16 #include "nfs_mntent.h"
17 #include "nls.h"
18 #include "xcommon.h"
19
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? */
24
25 static unsigned char need_escaping[] = { ' ', '\t', '\n', '\\' };
26
27 static char *
28 mangle(const char *arg) {
29         const unsigned char *s = (const unsigned char *)arg;
30         char *ss, *sp;
31         int n;
32
33         n = strlen(arg);
34         ss = sp = xmalloc(4*n+1);
35         while(1) {
36                 for (n = 0; n < sizeof(need_escaping); n++) {
37                         if (*s == need_escaping[n]) {
38                                 *sp++ = '\\';
39                                 *sp++ = '0' + ((*s & 0300) >> 6);
40                                 *sp++ = '0' + ((*s & 070) >> 3);
41                                 *sp++ = '0' + (*s & 07);
42                                 goto next;
43                         }
44                 }
45                 *sp++ = *s;
46                 if (*s == 0)
47                         break;
48         next:
49                 s++;
50         }
51         return ss;
52 }
53
54 static int
55 is_space_or_tab (char c) {
56         return (c == ' ' || c == '\t');
57 }
58
59 static char *
60 skip_spaces(char *s) {
61         while (is_space_or_tab(*s))
62                 s++;
63         return s;
64 }
65
66 static char *
67 skip_nonspaces(char *s) {
68         while (*s && !is_space_or_tab(*s))
69                 s++;
70         return s;
71 }
72
73 #define isoctal(a) (((a) & ~7) == '0')
74
75 /* returns malloced pointer - no more strdup required */
76 static char *
77 unmangle(char *s) {
78         char *ret, *ss, *sp;
79
80         ss = skip_nonspaces(s);
81         ret = sp = xmalloc(ss-s+1);
82         while(s != ss) {
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);
85                         s += 4;
86                 } else
87                         *sp++ = *s++;
88         }
89         *sp = 0;
90         return ret;
91 }
92
93 /*
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
96  * returns 0.
97  * (On the other hand, mangling and unmangling is meaningless
98  *  for /proc/mounts.)
99  */
100
101 mntFILE *
102 nfs_setmntent (const char *file, char *mode) {
103         mntFILE *mfp = xmalloc(sizeof(*mfp));
104         mode_t old_umask = umask(077);
105
106         mfp->mntent_fp = fopen(file, mode);
107         umask(old_umask);
108         mfp->mntent_file = xstrdup(file);
109         mfp->mntent_errs = (mfp->mntent_fp == NULL);
110         mfp->mntent_softerrs = 0;
111         mfp->mntent_lineno = 0;
112         return mfp;
113 }
114
115 void
116 nfs_endmntent (mntFILE *mfp) {
117         if (mfp) {
118                 if (mfp->mntent_fp)
119                         fclose(mfp->mntent_fp);
120                 if (mfp->mntent_file)
121                         free(mfp->mntent_file);
122                 free(mfp);
123         }
124 }
125
126 int
127 nfs_addmntent (mntFILE *mfp, struct mntent *mnt) {
128         char *m1, *m2, *m3, *m4;
129         int res;
130
131         if (fseek (mfp->mntent_fp, 0, SEEK_END))
132                 return 1;                       /* failure */
133
134         m1 = mangle(mnt->mnt_fsname);
135         m2 = mangle(mnt->mnt_dir);
136         m3 = mangle(mnt->mnt_type);
137         m4 = mangle(mnt->mnt_opts);
138
139         res = fprintf (mfp->mntent_fp, "%s %s %s %s %d %d\n",
140                        m1, m2, m3, m4, mnt->mnt_freq, mnt->mnt_passno);
141
142         free(m1);
143         free(m2);
144         free(m3);
145         free(m4);
146         return (res < 0) ? 1 : 0;
147 }
148
149 /* Read the next entry from the file fp. Stop reading at an incorrect entry. */
150 struct mntent *
151 nfs_getmntent (mntFILE *mfp) {
152         static char buf[4096];
153         static struct mntent me;
154         char *s;
155
156  again:
157         if (mfp->mntent_errs || mfp->mntent_softerrs >= ERR_MAX)
158                 return NULL;
159
160         /* read the next non-blank non-comment line */
161         do {
162                 if (fgets (buf, sizeof(buf), mfp->mntent_fp) == NULL)
163                         return NULL;
164
165                 mfp->mntent_lineno++;
166                 s = index (buf, '\n');
167                 if (s == NULL) {
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"),
173                                         mfp->mntent_file);
174                                 s = index (buf, 0);
175                         } else {
176                                 mfp->mntent_errs = 1;
177                                 goto err;
178                         }
179                 }
180                 *s = 0;
181                 if (--s >= buf && *s == '\r')
182                         *s = 0;
183                 s = skip_spaces(buf);
184         } while (*s == '\0' || *s == '#');
185
186         me.mnt_fsname = unmangle(s);
187         s = skip_nonspaces(s);
188         s = skip_spaces(s);
189         me.mnt_dir = unmangle(s);
190         s = skip_nonspaces(s);
191         s = skip_spaces(s);
192         me.mnt_type = unmangle(s);
193         s = skip_nonspaces(s);
194         s = skip_spaces(s);
195         me.mnt_opts = unmangle(s);
196         s = skip_nonspaces(s);
197         s = skip_spaces(s);
198
199         if (isdigit(*s)) {
200                 me.mnt_freq = atoi(s);
201                 while(isdigit(*s)) s++;
202         } else
203                 me.mnt_freq = 0;
204         if(*s && !is_space_or_tab(*s))
205                 goto err;
206
207         s = skip_spaces(s);
208         if(isdigit(*s)) {
209                 me.mnt_passno = atoi(s);
210                 while(isdigit(*s)) s++;
211         } else
212                 me.mnt_passno = 0;
213         if(*s && !is_space_or_tab(*s))
214                 goto err;
215
216         /* allow more stuff, e.g. comments, on this line */
217
218         return &me;
219
220  err:
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") : "");
226         goto again;
227 }