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