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