mntent: fix parsing lines with optional fields
[musl] / src / misc / mntent.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <mntent.h>
4 #include <errno.h>
5
6 static char *internal_buf;
7 static size_t internal_bufsize;
8
9 #define SENTINEL (char *)&internal_buf
10
11 FILE *setmntent(const char *name, const char *mode)
12 {
13         return fopen(name, mode);
14 }
15
16 int endmntent(FILE *f)
17 {
18         if (f) fclose(f);
19         return 1;
20 }
21
22 struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
23 {
24         int n[8], use_internal = (linebuf == SENTINEL);
25         size_t len, i;
26
27         mnt->mnt_freq = 0;
28         mnt->mnt_passno = 0;
29
30         do {
31                 if (use_internal) {
32                         getline(&internal_buf, &internal_bufsize, f);
33                         linebuf = internal_buf;
34                 } else {
35                         fgets(linebuf, buflen, f);
36                 }
37                 if (feof(f) || ferror(f)) return 0;
38                 if (!strchr(linebuf, '\n')) {
39                         fscanf(f, "%*[^\n]%*[\n]");
40                         errno = ERANGE;
41                         return 0;
42                 }
43
44                 len = strlen(linebuf);
45                 for (i = 0; i < sizeof n / sizeof *n; i++) n[i] = len;
46                 sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
47                         n, n+1, n+2, n+3, n+4, n+5, n+6, n+7,
48                         &mnt->mnt_freq, &mnt->mnt_passno);
49         } while (linebuf[n[0]] == '#' || n[1]==len);
50
51         linebuf[n[1]] = 0;
52         linebuf[n[3]] = 0;
53         linebuf[n[5]] = 0;
54         linebuf[n[7]] = 0;
55
56         mnt->mnt_fsname = linebuf+n[0];
57         mnt->mnt_dir = linebuf+n[2];
58         mnt->mnt_type = linebuf+n[4];
59         mnt->mnt_opts = linebuf+n[6];
60
61         return mnt;
62 }
63
64 struct mntent *getmntent(FILE *f)
65 {
66         static struct mntent mnt;
67         return getmntent_r(f, &mnt, SENTINEL, 0);
68 }
69
70 int addmntent(FILE *f, const struct mntent *mnt)
71 {
72         if (fseek(f, 0, SEEK_END)) return 1;
73         return fprintf(f, "%s\t%s\t%s\t%s\t%d\t%d\n",
74                 mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts,
75                 mnt->mnt_freq, mnt->mnt_passno) < 0;
76 }
77
78 char *hasmntopt(const struct mntent *mnt, const char *opt)
79 {
80         return strstr(mnt->mnt_opts, opt);
81 }