Wed May 22 00:40:50 1996 David Mosberger-Tang <davidm@azstarnet.com>
[glibc/history.git] / misc / mntent.c
blobbdc6aaed7928980494fe24fa9fe408776fd05891
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 + strspn (buf, " \t");
57 } while (head[0] == '#'); /* Skip comment lines. */
59 m.mnt_fsname = strsep (&head, " \t") ?: (char *) "";
60 if (head)
61 head += strspn (head, " \t");
62 m.mnt_dir = strsep (&head, " \t") ?: (char *) "";
63 if (head)
64 head += strspn (head, " \t");
65 m.mnt_type = strsep (&head, " \t") ?: (char *) "";
66 if (head)
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)
71 case 0:
72 m.mnt_freq = 0;
73 case 1:
74 m.mnt_passno = 0;
75 case 2:
78 return &m;
81 /* Write the mount table entry described by MNT to STREAM.
82 Return zero on success, nonzero on failure. */
83 int
84 addmntent (FILE *stream, const struct mntent *mnt)
86 return (fprintf (stream, "%s %s %s %s %d %d\n",
87 mnt->mnt_fsname,
88 mnt->mnt_dir,
89 mnt->mnt_type,
90 mnt->mnt_opts,
91 mnt->mnt_freq,
92 mnt->mnt_passno)
93 < 0 ? -1 : 0);
96 /* Search MNT->mnt_opts for an option matching OPT.
97 Returns the address of the substring, or null if none found. */
98 char *
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' ||
108 p[optlen] == '=' ||
109 p[optlen] == ','))
110 return p;
112 rest = strchr (rest, ',');
115 return NULL;