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. */
24 /* Prepare to begin reading and/or writing mount table entries from the
25 beginning of FILE. MODE is as for `fopen'. */
27 setmntent (const char *file
, const char *mode
)
29 return fopen (file
, mode
);
32 /* Close a stream opened with `setmntent'. */
34 endmntent (FILE *stream
)
36 return fclose (stream
);
40 /* Read one mount table entry from STREAM. Returns a pointer to storage
41 reused on the next call, or null for EOF or error (use feof/ferror to
44 getmntent (FILE *stream
)
48 static struct mntent m
;
53 if (getline (&buf
, &bufsiz
, stream
) < 0)
56 head
= buf
+ strspn (buf
, " \t");
57 } while (head
[0] == '#'); /* Skip comment lines. */
59 m
.mnt_fsname
= strsep (&head
, " \t") ?: (char *) "";
61 head
+= strspn (head
, " \t");
62 m
.mnt_dir
= strsep (&head
, " \t") ?: (char *) "";
64 head
+= strspn (head
, " \t");
65 m
.mnt_type
= strsep (&head
, " \t") ?: (char *) "";
67 head
+= strspn (head
, " \t");
68 m
.mnt_opts
= strsep (&head
, " \t") ?: (char *) "";
69 switch (head
? sscanf (head
, " %d %d\n", &m
.mnt_freq
, &m
.mnt_passno
) : 0)
81 /* Write the mount table entry described by MNT to STREAM.
82 Return zero on success, nonzero on failure. */
84 addmntent (FILE *stream
, const struct mntent
*mnt
)
86 return (fprintf (stream
, "%s %s %s %s %d %d\n",
96 /* Search MNT->mnt_opts for an option matching OPT.
97 Returns the address of the substring, or null if none found. */
99 hasmntopt (const struct mntent
*mnt
, const char *opt
)
101 const size_t optlen
= strlen (opt
);
102 char *rest
= mnt
->mnt_opts
, *p
;
104 while ((p
= strstr (rest
, opt
)) != NULL
)
106 if (p
== rest
|| p
[-1] == ',' &&
107 (p
[optlen
] == '\0' ||
112 rest
= strchr (rest
, ',');