]> git.ipfire.org Git - thirdparty/glibc.git/blob - misc/mntent_r.c
Update.
[thirdparty/glibc.git] / misc / mntent_r.c
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.
4
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.
9
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.
14
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. */
19
20 #include <mntent.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/types.h>
24
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)
29 {
30 return fopen (file, mode);
31 }
32 weak_alias (__setmntent, setmntent)
33
34
35 /* Close a stream opened with `setmntent'. */
36 int
37 __endmntent (FILE *stream)
38 {
39 if (stream) /* SunOS 4.x allows for NULL stream */
40 fclose (stream);
41 return 1; /* SunOS 4.x says to always return 1 */
42 }
43 weak_alias (__endmntent, endmntent)
44
45
46 /* Read one mount table entry from STREAM. Returns a pointer to storage
47 reused on the next call, or null for EOF or error (use feof/ferror to
48 check). */
49 struct mntent *
50 __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
51 {
52 char *head;
53
54 flockfile (stream);
55 do
56 {
57 char *end_ptr;
58
59 if (fgets_unlocked (buffer, bufsiz, stream) == NULL)
60 {
61 funlockfile (stream);
62 return NULL;
63 }
64
65 end_ptr = strchr (buffer, '\n');
66 if (end_ptr != NULL) /* chop newline */
67 *end_ptr = '\0';
68 else
69 {
70 /* Not the whole line was read. Do it now but forget it. */
71 char tmp[1024];
72 while (fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
73 if (strchr (tmp, '\n') != NULL)
74 break;
75 }
76
77 head = buffer + strspn (buffer, " \t");
78 /* skip empty lines and comment lines: */
79 } while (head[0] == '\0' || head[0] == '#');
80
81 mp->mnt_fsname = __strsep (&head, " \t") ?: (char *) "";
82 if (head)
83 head += strspn (head, " \t");
84 mp->mnt_dir = __strsep (&head, " \t") ?: (char *) "";
85 if (head)
86 head += strspn (head, " \t");
87 mp->mnt_type = __strsep (&head, " \t") ?: (char *) "";
88 if (head)
89 head += strspn (head, " \t");
90 mp->mnt_opts = __strsep (&head, " \t") ?: (char *) "";
91 switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
92 {
93 case 0:
94 mp->mnt_freq = 0;
95 case 1:
96 mp->mnt_passno = 0;
97 case 2:
98 }
99 funlockfile (stream);
100
101 return mp;
102 }
103 weak_alias (__getmntent_r, getmntent_r)
104
105 /* Write the mount table entry described by MNT to STREAM.
106 Return zero on success, nonzero on failure. */
107 int
108 __addmntent (FILE *stream, const struct mntent *mnt)
109 {
110 if (fseek (stream, 0, SEEK_END))
111 return 1;
112
113 return (fprintf (stream, "%s %s %s %s %d %d\n",
114 mnt->mnt_fsname,
115 mnt->mnt_dir,
116 mnt->mnt_type,
117 mnt->mnt_opts,
118 mnt->mnt_freq,
119 mnt->mnt_passno)
120 < 0 ? 1 : 0);
121 }
122 weak_alias (__addmntent, addmntent)
123
124
125 /* Search MNT->mnt_opts for an option matching OPT.
126 Returns the address of the substring, or null if none found. */
127 char *
128 __hasmntopt (const struct mntent *mnt, const char *opt)
129 {
130 const size_t optlen = strlen (opt);
131 char *rest = mnt->mnt_opts, *p;
132
133 while ((p = strstr (rest, opt)) != NULL)
134 {
135 if (p == rest || p[-1] == ',' &&
136 (p[optlen] == '\0' ||
137 p[optlen] == '=' ||
138 p[optlen] == ','))
139 return p;
140
141 rest = strchr (rest, ',');
142 if (rest == NULL)
143 break;
144 ++rest;
145 }
146
147 return NULL;
148 }
149 weak_alias (__hasmntopt, hasmntopt)