2024-07-03 17:22:50 +03:00

111 lines
2.2 KiB
C

#define _MOUNT_C
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <mntent.h>
#include <sys/mount.h>
#include "parse_mount.h"
#include "config.h"
int do_mount(const char *src, const char *dst, const char *fs_type, unsigned long opt, char *data) {
if (mount(src, dst, fs_type, opt, data) < 0) {
fprintf(stderr, "mount: %s: %s\n", src, strerror(errno));
return 1;
}
return 0;
}
int parse_fstab(void) {
FILE *fp = setmntent(MOUNT_CFG, "r");
if (fp == NULL) {
fprintf(stderr, "mount: %s\n", strerror(errno));
return 1;
}
char data[MOUNT_OPT_SIZE + 1];
struct mntent *me;
while ((me = getmntent(fp))) {
unsigned long mode = mu_parse_opts(me->mnt_opts, data, sizeof(data));
do_mount(me->mnt_fsname, me->mnt_dir, me->mnt_type, mode, data);
}
endmntent(fp);
return 0;
}
int print_mounts(void) {
FILE *fp = fopen(MOUNT_LIST, "r");
if (fp == NULL) {
fprintf(stderr, "mount: %s\n", strerror(errno));
return 1;
}
char *buf = NULL;
size_t len = 0;
while (getline(&buf, &len, fp) != EOF)
printf("%s", buf);
free(buf);
fclose(fp);
return 0;
}
void print_opts(void) {
for (size_t i = 0; i < sizeof(mu_options) / sizeof(mu_options[0]); i++) {
printf("\t%s", mu_options[i].opt);
if (mu_options[i].notopt != NULL)
printf(" / %s", mu_options[i].notopt);
if (mu_options[i].desc != NULL)
printf(" - %s", mu_options[i].desc);
putchar('\n');
}
}
int main(int argc, char **argv) {
char *fs_type = MOUNT_DEF_FS;
char data[MOUNT_OPT_SIZE + 1];
unsigned long mode = 0;
int opt;
while ((opt = getopt(argc, argv, "at:o:")) != -1) {
switch (opt) {
case 'a':
return parse_fstab();
case 't':
fs_type = optarg;
break;
case 'o':
mode = mu_parse_opts(optarg, data, sizeof(data));
break;
default:
printf("mount [ato] [DEVICE] [NODE]\n\t-t Fs type, default %s\n\t-o Options\n\t-a Mount all fs in /etc/fstab\n\nOptions:", MOUNT_DEF_FS);
print_opts();
return 0;
}
}
argc -= optind;
argv += optind;
if (argc == 2)
return do_mount(argv[0], argv[1], fs_type, mode, data);
else if (argc == 1)
return do_mount(NULL, argv[0], fs_type, mode, data);
else
return print_mounts();
}