summaryrefslogtreecommitdiffstats
path: root/usr/klibc/getmntent.c
blob: 8af27f304ce97f37fafda4cd63f875d6e8fa81bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mntent.h>

#define BUFLEN 1024

struct mntent *getmntent_r(FILE *fp, struct mntent *mntbuf, char *buf,
		int buflen)
{
	char *line = NULL, *saveptr = NULL;
	const char *sep = " \t\n";

	if (!fp || !mntbuf || !buf)
		return NULL;

	while ((line = fgets(buf, buflen, fp)) != NULL) {
		if (buf[0] == '#' || buf[0] == '\n')
			continue;
		break;
	}

	if (!line)
		return NULL;

	mntbuf->mnt_fsname = strtok_r(buf, sep, &saveptr);
	if (!mntbuf->mnt_fsname)
		return NULL;

	mntbuf->mnt_dir = strtok_r(NULL, sep, &saveptr);
	if (!mntbuf->mnt_fsname)
		return NULL;

	mntbuf->mnt_type = strtok_r(NULL, sep, &saveptr);
	if (!mntbuf->mnt_type)
		return NULL;

	mntbuf->mnt_opts = strtok_r(NULL, sep, &saveptr);
	if (!mntbuf->mnt_opts)
		mntbuf->mnt_opts = "";

	line = strtok_r(NULL, sep, &saveptr);
	mntbuf->mnt_freq = !line ? 0 : atoi(line);

	line = strtok_r(NULL, sep, &saveptr);
	mntbuf->mnt_passno = !line ? 0 : atoi(line);

	return mntbuf;
}

struct mntent *getmntent(FILE *fp)
{
	static char *buf = NULL;
	static struct mntent mntbuf;

	buf = malloc(BUFLEN);
	if (!buf)
		perror("malloc");

	return getmntent_r(fp, &mntbuf, buf, BUFLEN);
}