1 /* Utilities for reading/writing fstab, mtab, etc.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
23 #include <sys/types.h>
25 /* Prepare to begin reading and/or writing mount table entries from the
26 beginning of FILE. MODE is as for `fopen'. */
28 setmntent (const char *file
, const char *mode
)
30 return fopen (file
, mode
);
33 /* Close a stream opened with `setmntent'. */
35 endmntent (FILE *stream
)
37 if (stream
) /* SunOS 4.x allows for NULL stream */
39 return 1; /* SunOS 4.x says to always return 1 */
43 /* Read one mount table entry from STREAM. Returns a pointer to storage
44 reused on the next call, or null for EOF or error (use feof/ferror to
47 getmntent (FILE *stream
)
51 static struct mntent m
;
57 nread
= getline (&buf
, &bufsiz
, stream
);
61 if (buf
[nread
- 1] == '\n') /* chop newline */
62 buf
[nread
- 1] = '\0';
64 head
= buf
+ strspn (buf
, " \t");
65 /* skip empty lines and comment lines: */
66 } while (head
[0] == '\0' || head
[0] == '#');
68 m
.mnt_fsname
= strsep (&head
, " \t") ?: (char *) "";
70 head
+= strspn (head
, " \t");
71 m
.mnt_dir
= strsep (&head
, " \t") ?: (char *) "";
73 head
+= strspn (head
, " \t");
74 m
.mnt_type
= strsep (&head
, " \t") ?: (char *) "";
76 head
+= strspn (head
, " \t");
77 m
.mnt_opts
= strsep (&head
, " \t") ?: (char *) "";
78 switch (head
? sscanf (head
, " %d %d ", &m
.mnt_freq
, &m
.mnt_passno
) : 0)
90 /* Write the mount table entry described by MNT to STREAM.
91 Return zero on success, nonzero on failure. */
93 addmntent (FILE *stream
, const struct mntent
*mnt
)
95 if (fseek (stream
, 0, SEEK_END
))
98 return (fprintf (stream
, "%s %s %s %s %d %d\n",
108 /* Search MNT->mnt_opts for an option matching OPT.
109 Returns the address of the substring, or null if none found. */
111 hasmntopt (const struct mntent
*mnt
, const char *opt
)
113 const size_t optlen
= strlen (opt
);
114 char *rest
= mnt
->mnt_opts
, *p
;
116 while ((p
= strstr (rest
, opt
)) != NULL
)
118 if (p
== rest
|| p
[-1] == ',' &&
119 (p
[optlen
] == '\0' ||
124 rest
= strchr (rest
, ',');