loongarch64: add new syscall numbers
[musl.git] / src / misc / mntent.c
blob78bf0cd0f52fa6f0fb8aa2a9bea48c0819604601
1 #include <stdio.h>
2 #include <string.h>
3 #include <mntent.h>
4 #include <errno.h>
5 #include <limits.h>
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)
19 if (f) fclose(f);
20 return 1;
23 static char *unescape_ent(char *beg)
25 char *dest = beg;
26 const char *src = beg;
27 while (*src) {
28 const char *val;
29 unsigned char cval = 0;
30 if (*src != '\\') {
31 *dest++ = *src++;
32 continue;
34 if (src[1] == '\\') {
35 ++src;
36 *dest++ = *src++;
37 continue;
39 val = src + 1;
40 for (int i = 0; i < 3; ++i) {
41 if (*val >= '0' && *val <= '7') {
42 cval <<= 3;
43 cval += *val++ - '0';
44 } else {
45 break;
48 if (cval) {
49 *dest++ = cval;
50 src = val;
51 } else {
52 *dest++ = *src++;
55 *dest = 0;
56 return beg;
59 struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
61 int n[8], use_internal = (linebuf == SENTINEL);
62 size_t len, i;
64 mnt->mnt_freq = 0;
65 mnt->mnt_passno = 0;
67 do {
68 if (use_internal) {
69 getline(&internal_buf, &internal_bufsize, f);
70 linebuf = internal_buf;
71 } else {
72 fgets(linebuf, buflen, f);
74 if (feof(f) || ferror(f)) return 0;
75 if (!strchr(linebuf, '\n')) {
76 fscanf(f, "%*[^\n]%*[\n]");
77 errno = ERANGE;
78 return 0;
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%*[^ \t]%n %n%*[^ \t]%n %n%*[^ \t]%n %n%*[^ \t]%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);
89 linebuf[n[1]] = 0;
90 linebuf[n[3]] = 0;
91 linebuf[n[5]] = 0;
92 linebuf[n[7]] = 0;
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]);
99 return mnt;
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);