Wed Jul 17 21:53:45 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / misc / mntent.c
blob0aa1fb8cdeb6c0cb341d98f28155fe889899fbc8
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>
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'. */
27 FILE *
28 setmntent (const char *file, const char *mode)
30 return fopen (file, mode);
33 /* Close a stream opened with `setmntent'. */
34 int
35 endmntent (FILE *stream)
37 if (stream) /* SunOS 4.x allows for NULL stream */
38 fclose (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
45 check). */
46 struct mntent *
47 getmntent (FILE *stream)
49 static char *buf;
50 static size_t bufsiz;
51 static struct mntent m;
52 ssize_t nread;
53 char *head;
57 nread = getline (&buf, &bufsiz, stream);
58 if (nread <= 0)
59 return NULL;
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 *) "";
69 if (head)
70 head += strspn (head, " \t");
71 m.mnt_dir = strsep (&head, " \t") ?: (char *) "";
72 if (head)
73 head += strspn (head, " \t");
74 m.mnt_type = strsep (&head, " \t") ?: (char *) "";
75 if (head)
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)
80 case 0:
81 m.mnt_freq = 0;
82 case 1:
83 m.mnt_passno = 0;
84 case 2:
87 return &m;
90 /* Write the mount table entry described by MNT to STREAM.
91 Return zero on success, nonzero on failure. */
92 int
93 addmntent (FILE *stream, const struct mntent *mnt)
95 if (fseek (stream, 0, SEEK_END))
96 return 1;
98 return (fprintf (stream, "%s %s %s %s %d %d\n",
99 mnt->mnt_fsname,
100 mnt->mnt_dir,
101 mnt->mnt_type,
102 mnt->mnt_opts,
103 mnt->mnt_freq,
104 mnt->mnt_passno)
105 < 0 ? 1 : 0);
108 /* Search MNT->mnt_opts for an option matching OPT.
109 Returns the address of the substring, or null if none found. */
110 char *
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' ||
120 p[optlen] == '=' ||
121 p[optlen] == ','))
122 return p;
124 rest = strchr (rest, ',');
127 return NULL;