Update.
[glibc.git] / misc / mntent_r.c
blobf6ee1ca64e64e74246b2a74ead34b68873d41f08
1 /* Utilities for reading/writing fstab, mtab, etc.
2 Copyright (C) 1995, 1996, 1997, 1998 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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <mntent.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/types.h>
25 #ifdef USE_IN_LIBIO
26 # define flockfile(s) _IO_flockfile (s)
27 # define funlockfile(s) _IO_funlockfile (s)
28 #endif
30 /* Prepare to begin reading and/or writing mount table entries from the
31 beginning of FILE. MODE is as for `fopen'. */
32 FILE *
33 __setmntent (const char *file, const char *mode)
35 return fopen (file, mode);
37 weak_alias (__setmntent, setmntent)
40 /* Close a stream opened with `setmntent'. */
41 int
42 __endmntent (FILE *stream)
44 if (stream) /* SunOS 4.x allows for NULL stream */
45 fclose (stream);
46 return 1; /* SunOS 4.x says to always return 1 */
48 weak_alias (__endmntent, endmntent)
51 /* Read one mount table entry from STREAM. Returns a pointer to storage
52 reused on the next call, or null for EOF or error (use feof/ferror to
53 check). */
54 struct mntent *
55 __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
57 char *head;
59 flockfile (stream);
62 char *end_ptr;
64 if (fgets_unlocked (buffer, bufsiz, stream) == NULL)
66 funlockfile (stream);
67 return NULL;
70 end_ptr = strchr (buffer, '\n');
71 if (end_ptr != NULL) /* chop newline */
72 *end_ptr = '\0';
73 else
75 /* Not the whole line was read. Do it now but forget it. */
76 char tmp[1024];
77 while (fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
78 if (strchr (tmp, '\n') != NULL)
79 break;
82 head = buffer + strspn (buffer, " \t");
83 /* skip empty lines and comment lines: */
84 } while (head[0] == '\0' || head[0] == '#');
86 mp->mnt_fsname = __strsep (&head, " \t") ?: (char *) "";
87 if (head)
88 head += strspn (head, " \t");
89 mp->mnt_dir = __strsep (&head, " \t") ?: (char *) "";
90 if (head)
91 head += strspn (head, " \t");
92 mp->mnt_type = __strsep (&head, " \t") ?: (char *) "";
93 if (head)
94 head += strspn (head, " \t");
95 mp->mnt_opts = __strsep (&head, " \t") ?: (char *) "";
96 switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
98 case 0:
99 mp->mnt_freq = 0;
100 case 1:
101 mp->mnt_passno = 0;
102 case 2:
104 funlockfile (stream);
106 return mp;
108 weak_alias (__getmntent_r, getmntent_r)
110 /* Write the mount table entry described by MNT to STREAM.
111 Return zero on success, nonzero on failure. */
113 __addmntent (FILE *stream, const struct mntent *mnt)
115 if (fseek (stream, 0, SEEK_END))
116 return 1;
118 return (fprintf (stream, "%s %s %s %s %d %d\n",
119 mnt->mnt_fsname,
120 mnt->mnt_dir,
121 mnt->mnt_type,
122 mnt->mnt_opts,
123 mnt->mnt_freq,
124 mnt->mnt_passno)
125 < 0 ? 1 : 0);
127 weak_alias (__addmntent, addmntent)
130 /* Search MNT->mnt_opts for an option matching OPT.
131 Returns the address of the substring, or null if none found. */
132 char *
133 __hasmntopt (const struct mntent *mnt, const char *opt)
135 const size_t optlen = strlen (opt);
136 char *rest = mnt->mnt_opts, *p;
138 while ((p = strstr (rest, opt)) != NULL)
140 if (p == rest
141 || (p[-1] == ','
142 && (p[optlen] == '\0' ||
143 p[optlen] == '=' ||
144 p[optlen] == ',')))
145 return p;
147 rest = strchr (rest, ',');
148 if (rest == NULL)
149 break;
150 ++rest;
153 return NULL;
155 weak_alias (__hasmntopt, hasmntopt)