]>
git.proxmox.com Git - mirror_lxc.git/blob - src/include/lxcmntent.c
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.
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.
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.
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
30 /* Since the values in a line are separated by spaces, a name cannot
31 * contain a space. Therefore some programs encode spaces in names
32 * by the strings "\040". We undo the encoding when reading an entry.
33 * The decoding happens in place.
35 static char *decode_name(char *buf
)
41 if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '4' &&
43 /* \040 is a SPACE. */
46 } else if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '1' &&
51 } else if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '1' &&
53 /* \012 is a NEWLINE. */
56 } else if (rp
[0] == '\\' && rp
[1] == '\\') {
57 /* We have to escape \\ to be able to represent all characters. */
60 } else if (rp
[0] == '\\' && rp
[1] == '1' && rp
[2] == '3' &&
62 /* \134 is also \\. */
68 } while (*rp
++ != '\0');
73 /* Read one mount table entry from STREAM. Returns a pointer to storage
74 * reused on the next call, or null for EOF or error (use feof/ferror to check).
76 struct mntent
*getmntent_r(FILE *stream
, struct mntent
*mp
, char *buffer
, int bufsiz
)
84 if (!fgets(buffer
, bufsiz
, stream
))
87 end_ptr
= strchr(buffer
, '\n');
88 if (end_ptr
!= NULL
) {
92 /* Not the whole line was read. Do it now but forget it. */
95 while (fgets(tmp
, sizeof tmp
, stream
))
96 if (strchr(tmp
, '\n') != NULL
)
100 head
= buffer
+ strspn(buffer
, " \t");
101 /* skip empty lines and comment lines: */
102 } while (head
[0] == '\0' || head
[0] == '#');
104 cp
= strsep(&head
, " \t");
105 mp
->mnt_fsname
= cp
? decode_name(cp
) : (char *)"";
107 head
+= strspn(head
, " \t");
109 cp
= strsep(&head
, " \t");
110 mp
->mnt_dir
= cp
? decode_name(cp
) : (char *)"";
112 head
+= strspn(head
, " \t");
114 cp
= strsep(&head
, " \t");
115 mp
->mnt_type
= cp
? decode_name(cp
) : (char *)"";
117 head
+= strspn(head
, " \t");
119 cp
= strsep(&head
, " \t");
120 mp
->mnt_opts
= cp
? decode_name(cp
) : (char *)"";
122 int ret
= sscanf(head
, " %d %d ", &mp
->mnt_freq
, &mp
->mnt_passno
);
139 struct mntent
*getmntent(FILE *stream
)
141 static struct mntent m
;
142 static char *getmntent_buffer
;
144 if (!getmntent_buffer
) {
145 getmntent_buffer
= (char *)malloc(LXC_MAX_BUFFER
);
146 if (!getmntent_buffer
)
150 return getmntent_r(stream
, &m
, getmntent_buffer
, LXC_MAX_BUFFER
);
153 /* Prepare to begin reading and/or writing mount table entries from the
154 * beginning of FILE. MODE is as for `fopen'.
156 FILE *setmntent(const char *file
, const char *mode
)
158 /* Extend the mode parameter with "c" to disable cancellation in the
159 * I/O functions and "e" to set FD_CLOEXEC.
161 size_t modelen
= strlen(mode
);
164 newmode
= alloca(modelen
+ 3);
166 memcpy(newmode
, mode
, modelen
);
167 memcpy(newmode
+ modelen
, "ce", 3);
169 return fopen (file
, newmode
);
172 /* Close a stream opened with `setmntent'. */
173 int endmntent(FILE *stream
)
175 /* SunOS 4.x allows for NULL stream */
179 /* SunOS 4.x says to always return 1 */
183 /* Search MNT->mnt_opts for an option matching OPT.
184 * Returns the address of the substring, or null if none found.
186 char *hasmntopt(const struct mntent
*mnt
, const char *opt
)
188 const size_t optlen
= strlen(opt
);
189 char *rest
= mnt
->mnt_opts
, *p
;
191 while ((p
= strstr(rest
, opt
))) {
192 if ((p
== rest
|| p
[-1] == ',') &&
193 (p
[optlen
] == '\0' || p
[optlen
] == '=' || p
[optlen
] == ','))
196 rest
= strchr(p
, ',');