]> sourceware.org Git - glibc.git/blame - misc/mntent_r.c
update from main archive 960921
[glibc.git] / misc / mntent_r.c
CommitLineData
845dcb57
UD
1/* Utilities for reading/writing fstab, mtab, etc.
2Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3This file is part of the GNU C Library.
4
5The GNU C Library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10The GNU C Library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with the GNU C Library; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18Cambridge, MA 02139, 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'. */
27FILE *
28__setmntent (const char *file, const char *mode)
29{
30 return fopen (file, mode);
31}
32weak_alias (__setmntent, setmntent)
33
34
35/* Close a stream opened with `setmntent'. */
36int
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}
43weak_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). */
49struct mntent *
50__getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
51{
52 char *head;
53
54 do
55 {
56 char *end_ptr;
57
58 if (fgets (buffer, bufsiz, stream) == NULL)
59 return NULL;
60
61 end_ptr = strchr (buffer, '\n');
62 if (end_ptr != NULL) /* chop newline */
63 *end_ptr = '\0';
64 else
65 {
66 /* Not the whole line was read. Do it now but forget it. */
67 char tmp[1024];
68 while (fgets (tmp, sizeof tmp, stream) != NULL)
69 if (strchr (tmp, '\n') != NULL)
70 break;
71 }
72
73 head = buffer + strspn (buffer, " \t");
74 /* skip empty lines and comment lines: */
75 } while (head[0] == '\0' || head[0] == '#');
76
77 mp->mnt_fsname = strsep (&head, " \t") ?: (char *) "";
78 if (head)
79 head += strspn (head, " \t");
80 mp->mnt_dir = strsep (&head, " \t") ?: (char *) "";
81 if (head)
82 head += strspn (head, " \t");
83 mp->mnt_type = strsep (&head, " \t") ?: (char *) "";
84 if (head)
85 head += strspn (head, " \t");
86 mp->mnt_opts = strsep (&head, " \t") ?: (char *) "";
87 switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
88 {
89 case 0:
90 mp->mnt_freq = 0;
91 case 1:
92 mp->mnt_passno = 0;
93 case 2:
94 }
95
96 return mp;
97}
98weak_alias (__getmntent_r, getmntent_r)
99
100/* Write the mount table entry described by MNT to STREAM.
101 Return zero on success, nonzero on failure. */
102int
103__addmntent (FILE *stream, const struct mntent *mnt)
104{
105 if (fseek (stream, 0, SEEK_END))
106 return 1;
107
108 return (fprintf (stream, "%s %s %s %s %d %d\n",
109 mnt->mnt_fsname,
110 mnt->mnt_dir,
111 mnt->mnt_type,
112 mnt->mnt_opts,
113 mnt->mnt_freq,
114 mnt->mnt_passno)
115 < 0 ? 1 : 0);
116}
117weak_alias (__addmntent, addmntent)
118
119
120/* Search MNT->mnt_opts for an option matching OPT.
121 Returns the address of the substring, or null if none found. */
122char *
123__hasmntopt (const struct mntent *mnt, const char *opt)
124{
125 const size_t optlen = strlen (opt);
126 char *rest = mnt->mnt_opts, *p;
127
128 while ((p = strstr (rest, opt)) != NULL)
129 {
130 if (p == rest || p[-1] == ',' &&
131 (p[optlen] == '\0' ||
132 p[optlen] == '=' ||
133 p[optlen] == ','))
134 return p;
135
136 rest = strchr (rest, ',');
137 }
138
139 return NULL;
140}
141weak_alias (__hasmntopt, hasmntopt)
This page took 0.04059 seconds and 5 git commands to generate.