]>
git.proxmox.com Git - mirror_lxc.git/blob - src/include/lxcmntent.c
53f5256a1de76e923bd506435342ad33fbcb8cc4
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
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. */
31 decode_name (char *buf
)
37 if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '4' && rp
[3] == '0')
39 /* \040 is a SPACE. */
43 else if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '1' && rp
[3] == '1')
49 else if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '1' && rp
[3] == '2')
51 /* \012 is a NEWLINE. */
55 else if (rp
[0] == '\\' && rp
[1] == '\\')
57 /* We have to escape \\ to be able to represent all characters. */
61 else if (rp
[0] == '\\' && rp
[1] == '1' && rp
[2] == '3' && rp
[3] == '4')
63 /* \134 is also \\. */
69 while (*rp
++ != '\0');
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
77 struct mntent
*getmntent_r (FILE *stream
, struct mntent
*mp
, char *buffer
, int bufsiz
)
86 if (fgets (buffer
, bufsiz
, stream
) == NULL
)
91 end_ptr
= strchr (buffer
, '\n');
92 if (end_ptr
!= NULL
) /* chop newline */
96 /* Not the whole line was read. Do it now but forget it. */
98 while (fgets (tmp
, sizeof tmp
, stream
) != NULL
)
99 if (strchr (tmp
, '\n') != NULL
)
103 head
= buffer
+ strspn (buffer
, " \t");
104 /* skip empty lines and comment lines: */
106 while (head
[0] == '\0' || head
[0] == '#');
108 cp
= strsep (&head
, " \t");
109 mp
->mnt_fsname
= cp
!= NULL
? decode_name (cp
) : (char *) "";
111 head
+= strspn (head
, " \t");
112 cp
= strsep (&head
, " \t");
113 mp
->mnt_dir
= cp
!= NULL
? decode_name (cp
) : (char *) "";
115 head
+= strspn (head
, " \t");
116 cp
= strsep (&head
, " \t");
117 mp
->mnt_type
= cp
!= NULL
? decode_name (cp
) : (char *) "";
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)
135 struct mntent
*getmntent (FILE *stream
)
137 static struct mntent m
;
138 static char *getmntent_buffer
;
140 #define BUFFER_SIZE 4096
141 if (getmntent_buffer
== NULL
) {
142 getmntent_buffer
= (char *) malloc (BUFFER_SIZE
);
145 return getmntent_r (stream
, &m
, getmntent_buffer
, BUFFER_SIZE
);
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
)
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
);
159 newmode
= alloca(modelen
+ 3);
161 memcpy (newmode
, mode
, modelen
);
162 memcpy (newmode
+ modelen
, "ce", 3);
163 FILE *result
= fopen (file
, newmode
);
169 /* Close a stream opened with `setmntent'. */
170 int endmntent (FILE *stream
)
172 if (stream
) /* SunOS 4.x allows for NULL stream */
174 return 1; /* SunOS 4.x says to always return 1 */
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
)
181 const size_t optlen
= strlen (opt
);
182 char *rest
= mnt
->mnt_opts
, *p
;
184 while ((p
= strstr (rest
, opt
)) != NULL
)
186 if ((p
== rest
|| p
[-1] == ',')
187 && (p
[optlen
] == '\0' || p
[optlen
] == '=' || p
[optlen
] == ','))
190 rest
= strchr (p
, ',');