diff options
Diffstat (limited to 'main/alpine-baselayout/mkmntdirs.c')
-rw-r--r-- | main/alpine-baselayout/mkmntdirs.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/main/alpine-baselayout/mkmntdirs.c b/main/alpine-baselayout/mkmntdirs.c new file mode 100644 index 0000000000..6e3a3ef21d --- /dev/null +++ b/main/alpine-baselayout/mkmntdirs.c @@ -0,0 +1,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; +} + |