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