7 static char *internal_buf
;
8 static size_t internal_bufsize
;
10 #define SENTINEL (char *)&internal_buf
12 FILE *setmntent(const char *name
, const char *mode
)
14 return fopen(name
, mode
);
17 int endmntent(FILE *f
)
23 static char *unescape_ent(char *beg
)
26 const char *src
= beg
;
29 unsigned char cval
= 0;
40 for (int i
= 0; i
< 3; ++i
) {
41 if (*val
>= '0' && *val
<= '7') {
59 struct mntent
*getmntent_r(FILE *f
, struct mntent
*mnt
, char *linebuf
, int buflen
)
61 int n
[8], use_internal
= (linebuf
== SENTINEL
);
69 getline(&internal_buf
, &internal_bufsize
, f
);
70 linebuf
= internal_buf
;
72 fgets(linebuf
, buflen
, f
);
74 if (feof(f
) || ferror(f
)) return 0;
75 if (!strchr(linebuf
, '\n')) {
76 fscanf(f
, "%*[^\n]%*[\n]");
81 len
= strlen(linebuf
);
82 if (len
> INT_MAX
) continue;
83 for (i
= 0; i
< sizeof n
/ sizeof *n
; i
++) n
[i
] = len
;
84 sscanf(linebuf
, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
85 n
, n
+1, n
+2, n
+3, n
+4, n
+5, n
+6, n
+7,
86 &mnt
->mnt_freq
, &mnt
->mnt_passno
);
87 } while (linebuf
[n
[0]] == '#' || n
[1]==len
);
94 mnt
->mnt_fsname
= unescape_ent(linebuf
+n
[0]);
95 mnt
->mnt_dir
= unescape_ent(linebuf
+n
[2]);
96 mnt
->mnt_type
= unescape_ent(linebuf
+n
[4]);
97 mnt
->mnt_opts
= unescape_ent(linebuf
+n
[6]);
102 struct mntent
*getmntent(FILE *f
)
104 static struct mntent mnt
;
105 return getmntent_r(f
, &mnt
, SENTINEL
, 0);
108 int addmntent(FILE *f
, const struct mntent
*mnt
)
110 if (fseek(f
, 0, SEEK_END
)) return 1;
111 return fprintf(f
, "%s\t%s\t%s\t%s\t%d\t%d\n",
112 mnt
->mnt_fsname
, mnt
->mnt_dir
, mnt
->mnt_type
, mnt
->mnt_opts
,
113 mnt
->mnt_freq
, mnt
->mnt_passno
) < 0;
116 char *hasmntopt(const struct mntent
*mnt
, const char *opt
)
118 return strstr(mnt
->mnt_opts
, opt
);