mntent.c 1.3 KB
Newer Older
R
Rich Felker 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <stdio.h>
#include <string.h>
#include <mntent.h>

FILE *setmntent(const char *name, const char *mode)
{
	return fopen(name, mode);
}

int endmntent(FILE *f)
{
	fclose(f);
	return 1;
}

16
struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
R
Rich Felker 已提交
17 18 19
{
	int cnt, n[8];

20 21
	mnt->mnt_freq = 0;
	mnt->mnt_passno = 0;
R
Rich Felker 已提交
22 23

	do {
24
		fgets(linebuf, buflen, f);
R
Rich Felker 已提交
25 26 27
		if (feof(f)) return NULL;
		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,
28
			&mnt->mnt_freq, &mnt->mnt_passno);
29
	} while (cnt < 2 || linebuf[n[0]] == '#');
R
Rich Felker 已提交
30 31 32 33 34 35

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

36 37 38 39 40 41 42
	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 已提交
43

44 45 46 47 48
struct mntent *getmntent(FILE *f)
{
	static char linebuf[256];
	static struct mntent mnt;
	return getmntent_r(f, &mnt, linebuf, sizeof linebuf);
R
Rich Felker 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
}

int addmntent(FILE *f, const struct mntent *mnt)
{
	fseek(f, 0, SEEK_END);
	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)
{
	return strstr(mnt->mnt_opts, opt);
}