]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - mount-deprecated/mount_mntent.c
88dab1025ce3b407ca6bdbd680364e777ea3f954
1 /* Private version of the libc *mntent() routines. */
2 /* Note slightly different prototypes. */
4 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
5 * - added Native Language Support
9 #include <string.h> /* for index */
10 #include <ctype.h> /* for isdigit */
11 #include <sys/stat.h> /* for umask */
13 #include "mount_mntent.h"
14 #include "sundries.h" /* for xmalloc */
17 #include "closestream.h"
20 is_space_or_tab (char c
) {
21 return (c
== ' ' || c
== '\t');
25 skip_spaces(char *s
) {
26 while (is_space_or_tab(*s
))
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
35 * (On the other hand, mangling and unmangling is meaningless
40 my_setmntent (const char *file
, char *mode
) {
41 mntFILE
*mfp
= xmalloc(sizeof(*mfp
));
42 mode_t old_umask
= umask(077);
44 mfp
->mntent_fp
= fopen(file
, mode
);
46 mfp
->mntent_file
= xstrdup(file
);
47 mfp
->mntent_errs
= (mfp
->mntent_fp
== NULL
);
48 mfp
->mntent_softerrs
= 0;
49 mfp
->mntent_lineno
= 0;
54 my_endmntent(mntFILE
* mfp
) {
57 if (close_stream(mfp
->mntent_fp
))
58 fprintf(stderr
, _("write error"));
59 free(mfp
->mntent_file
);
65 my_addmntent (mntFILE
*mfp
, struct my_mntent
*mnt
) {
66 char *m1
, *m2
, *m3
, *m4
;
69 if (fseek (mfp
->mntent_fp
, 0, SEEK_END
))
70 return 1; /* failure */
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";
77 res
= fprintf (mfp
->mntent_fp
, "%s %s %s %s %d %d\n",
78 m1
, m2
, m3
, m4
, mnt
->mnt_freq
, mnt
->mnt_passno
);
85 return (res
< 0) ? 1 : 0;
88 /* Read the next entry from the file fp. Stop reading at an incorrect entry. */
90 my_getmntent (mntFILE
*mfp
) {
91 static char buf
[4096];
92 static struct my_mntent me
;
96 if (mfp
->mntent_errs
|| mfp
->mntent_softerrs
>= ERR_MAX
)
99 /* read the next non-blank non-comment line */
101 if (fgets (buf
, sizeof(buf
), mfp
->mntent_fp
) == NULL
)
104 mfp
->mntent_lineno
++;
105 s
= strchr (buf
, '\n');
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"),
115 mfp
->mntent_errs
= 1;
120 if (--s
>= buf
&& *s
== '\r')
122 s
= skip_spaces(buf
);
123 } while (*s
== '\0' || *s
== '#');
125 me
.mnt_fsname
= unmangle(s
, &s
);
127 me
.mnt_dir
= unmangle(s
, &s
);
129 me
.mnt_type
= unmangle(s
, &s
);
131 me
.mnt_opts
= unmangle(s
, &s
);
134 if (!me
.mnt_fsname
|| !me
.mnt_dir
|| !me
.mnt_type
)
138 me
.mnt_freq
= atoi(s
);
139 while(isdigit(*s
)) s
++;
142 if(*s
&& !is_space_or_tab(*s
))
147 me
.mnt_passno
= atoi(s
);
148 while(isdigit(*s
)) s
++;
151 if(*s
&& !is_space_or_tab(*s
))
154 /* allow more stuff, e.g. comments, on this line */
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") : "");