]> git.ipfire.org Git - thirdparty/util-linux.git/blob - mount-deprecated/mount_mntent.c
88dab1025ce3b407ca6bdbd680364e777ea3f954
[thirdparty/util-linux.git] / mount-deprecated / mount_mntent.c
1 /* Private version of the libc *mntent() routines. */
2 /* Note slightly different prototypes. */
3
4 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
5 * - added Native Language Support
6 */
7
8 #include <stdio.h>
9 #include <string.h> /* for index */
10 #include <ctype.h> /* for isdigit */
11 #include <sys/stat.h> /* for umask */
12
13 #include "mount_mntent.h"
14 #include "sundries.h" /* for xmalloc */
15 #include "nls.h"
16 #include "mangle.h"
17 #include "closestream.h"
18
19 static int
20 is_space_or_tab (char c) {
21 return (c == ' ' || c == '\t');
22 }
23
24 static char *
25 skip_spaces(char *s) {
26 while (is_space_or_tab(*s))
27 s++;
28 return s;
29 }
30
31 /*
32 * fstat'ing the file and allocating a buffer holding all of it
33 * may be a bad idea: if the file is /proc/mounts, the stat
34 * returns 0.
35 * (On the other hand, mangling and unmangling is meaningless
36 * for /proc/mounts.)
37 */
38
39 mntFILE *
40 my_setmntent (const char *file, char *mode) {
41 mntFILE *mfp = xmalloc(sizeof(*mfp));
42 mode_t old_umask = umask(077);
43
44 mfp->mntent_fp = fopen(file, mode);
45 umask(old_umask);
46 mfp->mntent_file = xstrdup(file);
47 mfp->mntent_errs = (mfp->mntent_fp == NULL);
48 mfp->mntent_softerrs = 0;
49 mfp->mntent_lineno = 0;
50 return mfp;
51 }
52
53 void
54 my_endmntent(mntFILE * mfp) {
55 if (mfp) {
56 if (mfp->mntent_fp)
57 if (close_stream(mfp->mntent_fp))
58 fprintf(stderr, _("write error"));
59 free(mfp->mntent_file);
60 free(mfp);
61 }
62 }
63
64 int
65 my_addmntent (mntFILE *mfp, struct my_mntent *mnt) {
66 char *m1, *m2, *m3, *m4;
67 int res;
68
69 if (fseek (mfp->mntent_fp, 0, SEEK_END))
70 return 1; /* failure */
71
72 m1 = mangle(mnt->mnt_fsname);
73 m2 = mangle(mnt->mnt_dir);
74 m3 = mangle(mnt->mnt_type);
75 m4 = mnt->mnt_opts ? mangle(mnt->mnt_opts) : "rw";
76
77 res = fprintf (mfp->mntent_fp, "%s %s %s %s %d %d\n",
78 m1, m2, m3, m4, mnt->mnt_freq, mnt->mnt_passno);
79
80 free(m1);
81 free(m2);
82 free(m3);
83 if (mnt->mnt_opts)
84 free(m4);
85 return (res < 0) ? 1 : 0;
86 }
87
88 /* Read the next entry from the file fp. Stop reading at an incorrect entry. */
89 struct my_mntent *
90 my_getmntent (mntFILE *mfp) {
91 static char buf[4096];
92 static struct my_mntent me;
93 char *s;
94
95 again:
96 if (mfp->mntent_errs || mfp->mntent_softerrs >= ERR_MAX)
97 return NULL;
98
99 /* read the next non-blank non-comment line */
100 do {
101 if (fgets (buf, sizeof(buf), mfp->mntent_fp) == NULL)
102 return NULL;
103
104 mfp->mntent_lineno++;
105 s = strchr (buf, '\n');
106 if (s == NULL) {
107 /* Missing final newline? Otherwise extremely */
108 /* long line - assume file was corrupted */
109 if (feof(mfp->mntent_fp)) {
110 fprintf(stderr, _("[mntent]: warning: no final "
111 "newline at the end of %s\n"),
112 mfp->mntent_file);
113 s = strchr (buf, 0);
114 } else {
115 mfp->mntent_errs = 1;
116 goto err;
117 }
118 }
119 *s = 0;
120 if (--s >= buf && *s == '\r')
121 *s = 0;
122 s = skip_spaces(buf);
123 } while (*s == '\0' || *s == '#');
124
125 me.mnt_fsname = unmangle(s, &s);
126 s = skip_spaces(s);
127 me.mnt_dir = unmangle(s, &s);
128 s = skip_spaces(s);
129 me.mnt_type = unmangle(s, &s);
130 s = skip_spaces(s);
131 me.mnt_opts = unmangle(s, &s);
132 s = skip_spaces(s);
133
134 if (!me.mnt_fsname || !me.mnt_dir || !me.mnt_type)
135 goto err;
136
137 if (isdigit(*s)) {
138 me.mnt_freq = atoi(s);
139 while(isdigit(*s)) s++;
140 } else
141 me.mnt_freq = 0;
142 if(*s && !is_space_or_tab(*s))
143 goto err;
144
145 s = skip_spaces(s);
146 if(isdigit(*s)) {
147 me.mnt_passno = atoi(s);
148 while(isdigit(*s)) s++;
149 } else
150 me.mnt_passno = 0;
151 if(*s && !is_space_or_tab(*s))
152 goto err;
153
154 /* allow more stuff, e.g. comments, on this line */
155
156 return &me;
157
158 err:
159 mfp->mntent_softerrs++;
160 fprintf(stderr, _("[mntent]: line %d in %s is bad%s\n"),
161 mfp->mntent_lineno, mfp->mntent_file,
162 (mfp->mntent_errs || mfp->mntent_softerrs >= ERR_MAX) ?
163 _("; rest of file ignored") : "");
164 goto again;
165 }