48c85bd627db62c5307220da97911c03730c2107
[musl] / src / linux / mntent.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <mntent.h>
4
5 FILE *setmntent(const char *name, const char *mode)
6 {
7         return fopen(name, mode);
8 }
9
10 int endmntent(FILE *f)
11 {
12         fclose(f);
13         return 1;
14 }
15
16 struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
17 {
18         int cnt, n[8];
19
20         mnt->mnt_freq = 0;
21         mnt->mnt_passno = 0;
22
23         do {
24                 fgets(linebuf, buflen, f);
25                 if (feof(f)) return NULL;
26                 cnt = sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
27                         n, n+1, n+2, n+3, n+4, n+5, n+6, n+7,
28                         &mnt->mnt_freq, &mnt->mnt_passno);
29         } while (cnt < 2 || linebuf[n[0]] == '#');
30
31         linebuf[n[1]] = 0;
32         linebuf[n[3]] = 0;
33         linebuf[n[5]] = 0;
34         linebuf[n[7]] = 0;
35
36         mnt->mnt_fsname = linebuf+n[0];
37         mnt->mnt_dir = linebuf+n[2];
38         mnt->mnt_type = linebuf+n[4];
39         mnt->mnt_opts = linebuf+n[6];
40
41         return mnt;
42 }
43
44 struct mntent *getmntent(FILE *f)
45 {
46         static char linebuf[256];
47         static struct mntent mnt;
48         return getmntent_r(f, &mnt, linebuf, sizeof linebuf);
49 }
50
51 int addmntent(FILE *f, const struct mntent *mnt)
52 {
53         fseek(f, 0, SEEK_END);
54         return fprintf(f, "%s\t%s\t%s\t%s\t%d\t%d\n",
55                 mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts,
56                 mnt->mnt_freq, mnt->mnt_passno) < 0;
57 }
58
59 char *hasmntopt(const struct mntent *mnt, const char *opt)
60 {
61         return strstr(mnt->mnt_opts, opt);
62 }