1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
| | #include <assert.h>
#include <stdio.h>
#include <string.h>
#include <mntent.h>
#include <errno.h>
#include <limits.h>
static char *internal_buf;
static size_t internal_bufsize;
#define SENTINEL (char *)&internal_buf
FILE *setmntent(const char *name, const char *mode)
{
return fopen(name, mode);
}
int endmntent(FILE *f)
{
if (f) fclose(f);
return 1;
}
static inline int decode1(char** in_buf, char** out_buf, const char* from, const char to) {
if(strncmp(from, *in_buf, strlen(from)) == 0) {
*(*out_buf)++ = to;
*in_buf += strlen(from);
return 1;
}
return 0;
}
static inline char* decode(char* buf) {
assert(buf != NULL);
char* read_cursor = buf;
char* write_cursor = buf;
while(*read_cursor) {
// space
decode1(&read_cursor, &write_cursor, "\\040", '\040')
// tab
|| decode1(&read_cursor, &write_cursor, "\\011", '\011')
// newline
|| decode1(&read_cursor, &write_cursor, "\\012", '\012')
// backslash
|| decode1(&read_cursor, &write_cursor, "\\134", '\134')
|| decode1(&read_cursor, &write_cursor, "\\\\", '\\')
// default: copy char as is.
|| (*write_cursor++ = *read_cursor++);
}
*write_cursor = *read_cursor;
return buf;
}
struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
{
int n[8], use_internal = (linebuf == SENTINEL);
size_t len, i;
mnt->mnt_freq = 0;
mnt->mnt_passno = 0;
do {
if (use_internal) {
getline(&internal_buf, &internal_bufsize, f);
linebuf = internal_buf;
} else {
fgets(linebuf, buflen, f);
}
if (feof(f) || ferror(f)) return 0;
if (!strchr(linebuf, '\n')) {
fscanf(f, "%*[^\n]%*[\n]");
errno = ERANGE;
return 0;
}
len = strlen(linebuf);
if (len > INT_MAX) continue;
for (i = 0; i < sizeof n / sizeof *n; i++) n[i] = len;
sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
n, n+1, n+2, n+3, n+4, n+5, n+6, n+7,
&mnt->mnt_freq, &mnt->mnt_passno);
} while (linebuf[n[0]] == '#' || n[1]==len);
linebuf[n[1]] = 0;
linebuf[n[3]] = 0;
linebuf[n[5]] = 0;
linebuf[n[7]] = 0;
mnt->mnt_fsname = decode(linebuf+n[0]);
mnt->mnt_dir = decode(linebuf+n[2]);
mnt->mnt_type = decode(linebuf+n[4]);
mnt->mnt_opts = decode(linebuf+n[6]);
return mnt;
}
struct mntent *getmntent(FILE *f)
{
static struct mntent mnt;
return getmntent_r(f, &mnt, SENTINEL, 0);
}
static inline int write_string(FILE *f, const char* str)
{
char c;
int error_occured = 0;
while(str && !error_occured && (c = *str++) != 0) {
if(c == '\040') // space
error_occured = fprintf(f, "%s", "\\040") < 0;
else if (c == '\011') // tab
error_occured = fprintf(f, "%s", "\\011") < 0;
else if (c == '\012') // newline
error_occured = fprintf(f, "%s", "\\012") < 0;
else if (c == '\\')
error_occured = fprintf(f, "%s", "\\\\") < 0;
else
error_occured = fprintf(f, "%c", c) < 0;
}
return error_occured;
}
int addmntent(FILE *f, const struct mntent *mnt)
{
if (fseek(f, 0, SEEK_END)) return 1;
flockfile(f);
int result =
write_string(f, mnt->mnt_fsname)
|| (fprintf(f, "\t") < 0)
|| write_string(f, mnt->mnt_dir)
|| (fprintf(f, "\t") < 0)
|| write_string(f, mnt->mnt_type)
|| (fprintf(f, "\t") < 0)
|| write_string(f, mnt->mnt_opts)
|| (fprintf(f, "\t%d\t%d\n",
mnt->mnt_freq, mnt->mnt_passno) < 0);
funlockfile(f);
return result;
}
char *hasmntopt(const struct mntent *mnt, const char *opt)
{
return strstr(mnt->mnt_opts, opt);
}
|