summaryrefslogtreecommitdiffstats
path: root/main/alpine-baselayout/mkmntdirs.c
blob: 6e3a3ef21dae0437729d318f65c9e79d7586315e (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
62
63
64
65
66
/* 
 * Create mount directories in fstab
 *
 * Copyright(c) 2008 Natanael Copa <natanael.copa@gmail.com>
 * May be distributed under the terms of GPL-2
 *
 * usage: mkmntdirs [fstab] 
 *
 */

#include <sys/stat.h>
#include <sys/types.h>

#include <err.h>
#include <mntent.h>
#include <stdio.h>


#ifdef DEBUG
#define mkdir_recursive(p)	puts((p))
#else
static void mkdir_recursive(char *path)
{
	char *s = path;
	while (1) {
		int c = '\0';
		while (*s) {
			if (*s == '/') {
				do {
					++s;
				} while (*s == '/');
				c = *s; /* Save the current char */
				*s = '\0'; /* and replace it with nul. */
				break;
			}
			++s;
		}
		mkdir(path, 0755);
		if (c == '\0')
			return;
		*s = c;
	}
}
#endif

int main(int argc, const char *argv[])
{
	const char *filename = "/etc/fstab";
	FILE *f;
	struct mntent *ent;
	if (argc == 2)
		filename = argv[1];

	f = setmntent(filename, "r");
	if (f == NULL)
		err(1, "%s", filename);

	while ((ent = getmntent(f)) != NULL) {
		if (strcmp(ent->mnt_dir, "none") != 0)
			mkdir_recursive(ent->mnt_dir);
	}
	
	endmntent(f);
	return 0;
}