Tue Sep 12 14:30:07 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / misc / mntent.c
blob42c20817ba0ffe44cd06a89cafdf68324c1c91d2
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. */
20 #include <mntent.h>
21 #include <stdio.h>
22 #include <string.h>
24 /* Prepare to begin reading and/or writing mount table entries from the
25 beginning of FILE. MODE is as for `fopen'. */
26 FILE *
27 setmntent (const char *file, const char *mode)
29 return fopen (file, mode);
32 /* Close a stream opened with `setmntent'. */
33 int
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
42 check). */
43 struct mntent *
44 getmntent (FILE *stream)
46 static char *buf;
47 static size_t bufsiz;
48 static struct mntent m;
49 char *head;
53 if (getline (&buf, &bufsiz, stream) < 0)
54 return NULL;
56 head = buf;
57 } while (head[0] == '#'); /* Skip comment lines. */
59 m.mnt_fsname = strsep (&head, " \t") ?: (char *) "";
60 m.mnt_dir = strsep (&head, " \t") ?: (char *) "";
61 m.mnt_type = strsep (&head, " \t") ?: (char *) "";
62 m.mnt_opts = strsep (&head, " \t") ?: (char *) "";
63 switch (sscanf (head, "%d %d\n", &m.mnt_freq, &m.mnt_passno))
65 case 0:
66 m.mnt_freq = 0;
67 case 1:
68 m.mnt_passno = 0;
69 case 2:
72 return &m;
75 /* Write the mount table entry described by MNT to STREAM.
76 Return zero on success, nonzero on failure. */
77 int
78 addmntent (FILE *stream, const struct mntent *mnt)
80 return (fprintf (stream, "%s %s %s %s %d %d\n",
81 mnt->mnt_fsname,
82 mnt->mnt_dir,
83 mnt->mnt_type,
84 mnt->mnt_opts,
85 mnt->mnt_freq,
86 mnt->mnt_passno)
87 < 0 ? -1 : 0);
90 /* Search MNT->mnt_opts for an option matching OPT.
91 Returns the address of the substring, or null if none found. */
92 char *
93 hasmntopt (const struct mntent *mnt, const char *opt)
95 const size_t optlen = strlen (opt);
96 char *rest = mnt->mnt_opts, *p;
98 while ((p = strstr (rest, opt)) != NULL)
100 if (p == rest || p[-1] == ',' &&
101 (p[optlen] == '\0' ||
102 p[optlen] == '=' ||
103 p[optlen] == ','))
104 return p;
106 rest = strchr (rest, ',');
109 return NULL;