mntent.c 1.8 KB
Newer Older
R
Rich Felker 已提交
1 2 3
#include <stdio.h>
#include <string.h>
#include <mntent.h>
4
#include <errno.h>
W
w00349915 已提交
5
#include <unsupported_api.h>
R
Rich Felker 已提交
6

N
Natanael Copa 已提交
7 8 9 10 11
static char *internal_buf;
static size_t internal_bufsize;

#define SENTINEL (char *)&internal_buf

R
Rich Felker 已提交
12 13
FILE *setmntent(const char *name, const char *mode)
{
W
w00349915 已提交
14
	unsupported_api(__FUNCTION__);
R
Rich Felker 已提交
15 16 17 18 19
	return fopen(name, mode);
}

int endmntent(FILE *f)
{
20
	if (f) fclose(f);
R
Rich Felker 已提交
21 22 23
	return 1;
}

24
struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
R
Rich Felker 已提交
25
{
N
Natanael Copa 已提交
26
	int cnt, n[8], use_internal = (linebuf == SENTINEL);
R
Rich Felker 已提交
27

28 29
	mnt->mnt_freq = 0;
	mnt->mnt_passno = 0;
R
Rich Felker 已提交
30

W
w00349915 已提交
31
	unsupported_api(__FUNCTION__);
R
Rich Felker 已提交
32
	do {
N
Natanael Copa 已提交
33 34 35 36 37 38
		if (use_internal) {
			getline(&internal_buf, &internal_bufsize, f);
			linebuf = internal_buf;
		} else {
			fgets(linebuf, buflen, f);
		}
39 40
		if (feof(f) || ferror(f)) return 0;
		if (!strchr(linebuf, '\n')) {
41
			fscanf(f, "%*[^\n]%*[\n]");
42 43 44
			errno = ERANGE;
			return 0;
		}
R
Rich Felker 已提交
45 46
		cnt = sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
			n, n+1, n+2, n+3, n+4, n+5, n+6, n+7,
47
			&mnt->mnt_freq, &mnt->mnt_passno);
48
	} while (cnt < 2 || linebuf[n[0]] == '#');
R
Rich Felker 已提交
49 50 51 52 53 54

	linebuf[n[1]] = 0;
	linebuf[n[3]] = 0;
	linebuf[n[5]] = 0;
	linebuf[n[7]] = 0;

55 56 57 58 59 60 61
	mnt->mnt_fsname = linebuf+n[0];
	mnt->mnt_dir = linebuf+n[2];
	mnt->mnt_type = linebuf+n[4];
	mnt->mnt_opts = linebuf+n[6];

	return mnt;
}
R
Rich Felker 已提交
62

63 64 65
struct mntent *getmntent(FILE *f)
{
	static struct mntent mnt;
W
w00349915 已提交
66
	unsupported_api(__FUNCTION__);
N
Natanael Copa 已提交
67
	return getmntent_r(f, &mnt, SENTINEL, 0);
R
Rich Felker 已提交
68 69 70 71
}

int addmntent(FILE *f, const struct mntent *mnt)
{
W
w00349915 已提交
72
	unsupported_api(__FUNCTION__);
73
	if (fseek(f, 0, SEEK_END)) return 1;
R
Rich Felker 已提交
74 75 76 77 78 79 80
	return fprintf(f, "%s\t%s\t%s\t%s\t%d\t%d\n",
		mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts,
		mnt->mnt_freq, mnt->mnt_passno) < 0;
}

char *hasmntopt(const struct mntent *mnt, const char *opt)
{
W
w00349915 已提交
81
	unsupported_api(__FUNCTION__);
R
Rich Felker 已提交
82 83
	return strstr(mnt->mnt_opts, opt);
}