]>
Commit | Line | Data |
---|---|---|
250b1eec SG |
1 | /* Utilities for reading/writing fstab, mtab, etc. |
2 | Copyright (C) 1995-2000, 2001, 2002, 2003, 2006 | |
3 | Free Software Foundation, Inc. | |
4 | This file is part of the GNU C Library. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
17 | License along with this library; if not, write to the Free Software | |
18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
dd94de5a | 21 | #include <alloca.h> |
edaf8b1b SG |
22 | #include <stdio.h> |
23 | #include <string.h> | |
24 | #include <mntent.h> | |
25 | ||
26 | /* Since the values in a line are separated by spaces, a name cannot | |
27 | contain a space. Therefore some programs encode spaces in names | |
28 | by the strings "\040". We undo the encoding when reading an entry. | |
29 | The decoding happens in place. */ | |
30 | static char * | |
31 | decode_name (char *buf) | |
32 | { | |
33 | char *rp = buf; | |
34 | char *wp = buf; | |
35 | ||
36 | do | |
37 | if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0') | |
38 | { | |
39 | /* \040 is a SPACE. */ | |
40 | *wp++ = ' '; | |
41 | rp += 3; | |
42 | } | |
43 | else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1') | |
44 | { | |
45 | /* \011 is a TAB. */ | |
46 | *wp++ = '\t'; | |
47 | rp += 3; | |
48 | } | |
49 | else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2') | |
50 | { | |
51 | /* \012 is a NEWLINE. */ | |
52 | *wp++ = '\n'; | |
53 | rp += 3; | |
54 | } | |
55 | else if (rp[0] == '\\' && rp[1] == '\\') | |
56 | { | |
57 | /* We have to escape \\ to be able to represent all characters. */ | |
58 | *wp++ = '\\'; | |
59 | rp += 1; | |
60 | } | |
61 | else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4') | |
62 | { | |
63 | /* \134 is also \\. */ | |
64 | *wp++ = '\\'; | |
65 | rp += 3; | |
66 | } | |
67 | else | |
68 | *wp++ = *rp; | |
69 | while (*rp++ != '\0'); | |
70 | ||
71 | return buf; | |
72 | } | |
73 | ||
74 | /* Read one mount table entry from STREAM. Returns a pointer to storage | |
75 | reused on the next call, or null for EOF or error (use feof/ferror to | |
76 | check). */ | |
77 | struct mntent *getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz) | |
78 | { | |
79 | char *cp; | |
80 | char *head; | |
81 | ||
82 | do | |
83 | { | |
84 | char *end_ptr; | |
85 | ||
86 | if (fgets (buffer, bufsiz, stream) == NULL) | |
87 | { | |
88 | return NULL; | |
89 | } | |
90 | ||
91 | end_ptr = strchr (buffer, '\n'); | |
92 | if (end_ptr != NULL) /* chop newline */ | |
93 | *end_ptr = '\0'; | |
94 | else | |
95 | { | |
96 | /* Not the whole line was read. Do it now but forget it. */ | |
97 | char tmp[1024]; | |
98 | while (fgets (tmp, sizeof tmp, stream) != NULL) | |
99 | if (strchr (tmp, '\n') != NULL) | |
100 | break; | |
101 | } | |
102 | ||
103 | head = buffer + strspn (buffer, " \t"); | |
104 | /* skip empty lines and comment lines: */ | |
105 | } | |
106 | while (head[0] == '\0' || head[0] == '#'); | |
107 | ||
108 | cp = strsep (&head, " \t"); | |
109 | mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) ""; | |
110 | if (head) | |
111 | head += strspn (head, " \t"); | |
112 | cp = strsep (&head, " \t"); | |
113 | mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) ""; | |
114 | if (head) | |
115 | head += strspn (head, " \t"); | |
116 | cp = strsep (&head, " \t"); | |
117 | mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) ""; | |
118 | if (head) | |
119 | head += strspn (head, " \t"); | |
120 | cp = strsep (&head, " \t"); | |
121 | mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) ""; | |
122 | switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0) | |
123 | { | |
124 | case 0: | |
125 | mp->mnt_freq = 0; | |
126 | case 1: | |
127 | mp->mnt_passno = 0; | |
128 | case 2: | |
129 | break; | |
130 | } | |
131 | ||
132 | return mp; | |
133 | } | |
134 | ||
135 | struct mntent *getmntent (FILE *stream) | |
136 | { | |
137 | static struct mntent m; | |
138 | static char *getmntent_buffer; | |
139 | ||
140 | #define BUFFER_SIZE 4096 | |
141 | if (getmntent_buffer == NULL) { | |
142 | getmntent_buffer = (char *) malloc (BUFFER_SIZE); | |
143 | } | |
144 | ||
145 | return getmntent_r (stream, &m, getmntent_buffer, BUFFER_SIZE); | |
146 | #undef BUFFER_SIZE | |
147 | } | |
148 | ||
149 | ||
150 | /* Prepare to begin reading and/or writing mount table entries from the | |
151 | beginning of FILE. MODE is as for `fopen'. */ | |
152 | FILE *setmntent (const char *file, const char *mode) | |
153 | { | |
154 | /* Extend the mode parameter with "c" to disable cancellation in the | |
155 | I/O functions and "e" to set FD_CLOEXEC. */ | |
156 | size_t modelen = strlen (mode); | |
dd94de5a CB |
157 | char *newmode; |
158 | ||
159 | newmode = alloca(modelen + 3); | |
160 | ||
edaf8b1b SG |
161 | memcpy (newmode, mode, modelen); |
162 | memcpy (newmode + modelen, "ce", 3); | |
163 | FILE *result = fopen (file, newmode); | |
164 | ||
165 | return result; | |
166 | } | |
167 | ||
168 | ||
169 | /* Close a stream opened with `setmntent'. */ | |
170 | int endmntent (FILE *stream) | |
171 | { | |
172 | if (stream) /* SunOS 4.x allows for NULL stream */ | |
173 | fclose (stream); | |
174 | return 1; /* SunOS 4.x says to always return 1 */ | |
175 | } | |
176 | ||
177 | /* Search MNT->mnt_opts for an option matching OPT. | |
178 | Returns the address of the substring, or null if none found. */ | |
179 | char *hasmntopt (const struct mntent *mnt, const char *opt) | |
180 | { | |
181 | const size_t optlen = strlen (opt); | |
182 | char *rest = mnt->mnt_opts, *p; | |
183 | ||
184 | while ((p = strstr (rest, opt)) != NULL) | |
185 | { | |
186 | if ((p == rest || p[-1] == ',') | |
187 | && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ',')) | |
188 | return p; | |
189 | ||
190 | rest = strchr (p, ','); | |
191 | if (rest == NULL) | |
192 | break; | |
193 | ++rest; | |
194 | } | |
195 | ||
196 | return NULL; | |
197 | } |