mntent: fix parsing lines with optional fields
[musl.git] / src / misc / mntent.c
blob962b87670c3b5ac8b0cab15de3fafd2bf2dbe480
1 #include <stdio.h>
2 #include <string.h>
3 #include <mntent.h>
4 #include <errno.h>
6 static char *internal_buf;
7 static size_t internal_bufsize;
9 #define SENTINEL (char *)&internal_buf
11 FILE *setmntent(const char *name, const char *mode)
13 return fopen(name, mode);
16 int endmntent(FILE *f)
18 if (f) fclose(f);
19 return 1;
22 struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
24 int n[8], use_internal = (linebuf == SENTINEL);
25 size_t len, i;
27 mnt->mnt_freq = 0;
28 mnt->mnt_passno = 0;
30 do {
31 if (use_internal) {
32 getline(&internal_buf, &internal_bufsize, f);
33 linebuf = internal_buf;
34 } else {
35 fgets(linebuf, buflen, f);
37 if (feof(f) || ferror(f)) return 0;
38 if (!strchr(linebuf, '\n')) {
39 fscanf(f, "%*[^\n]%*[\n]");
40 errno = ERANGE;
41 return 0;
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);
51 linebuf[n[1]] = 0;
52 linebuf[n[3]] = 0;
53 linebuf[n[5]] = 0;
54 linebuf[n[7]] = 0;
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];
61 return mnt;
64 struct mntent *getmntent(FILE *f)
66 static struct mntent mnt;
67 return getmntent_r(f, &mnt, SENTINEL, 0);
70 int addmntent(FILE *f, const struct mntent *mnt)
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;
78 char *hasmntopt(const struct mntent *mnt, const char *opt)
80 return strstr(mnt->mnt_opts, opt);