mount opt support add

This commit is contained in:
Your Name 2023-12-19 22:37:48 +03:00
parent b3a7ade1fa
commit 4b25396408
2 changed files with 62 additions and 5 deletions

View File

@ -1,9 +1,52 @@
#ifndef _PARSE_MOUNT_H
#define _PARSE_MOUNT_H
#include <string.h>
unsigned long mu_parse_opts(const char *str) {
(void)str;
return 0;
typedef struct {
char *opt;
char *notopt;
char *desc;
unsigned int val;
} MU_MOUNT_OPTS;
#if defined(__BSD__)
/* Todo */
#define MS_RDONLY MNT_RDONLY
#define MS_REMOUNT 0
#define MS_SYNCHRONOUS MNT_SYNCHRONOUS
#define MS_NODEV 0
#endif
MU_MOUNT_OPTS mu_options[] = {
{"defaults", NULL, NULL, 0},
{"ro", "rw", "Read only / Read and write", MS_RDONLY},
{"remount", NULL, "Remount a mounted filesystem", MS_REMOUNT},
{"sync", "async", "Writes are [a]synchronous", MS_SYNCHRONOUS},
{"nodev", "dev", "(Dis)allow use of special device files", MS_NODEV}
};
unsigned long mu_parse_opts(char *str) {
unsigned long opt = 1;
char *token = strtok(str, ",");
while (token != NULL) {
for (size_t i = 0; i < sizeof(mu_options) / sizeof(mu_options[0]); i++) {
if (mu_options[i].opt && !strcmp(token, mu_options[i].opt)) {
opt |= mu_options[i].val;
break;
}
if (mu_options[i].notopt && !strcmp(token, mu_options[i].notopt)) {
opt &= ~mu_options[i].val;
break;
}
}
token = strtok(NULL, ",");
}
printf("%lu\n", opt);
return opt;
}
#endif

View File

@ -45,12 +45,25 @@ void parse_fstab(void) {
free(buf);
}
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;
unsigned long mode = 0;
int opt;
while ((opt = getopt(argc, argv, "t:ao:")) != -1) {
while ((opt = getopt(argc, argv, "at:o:")) != -1) {
switch (opt) {
case 'a':
parse_fstab();
@ -65,7 +78,8 @@ int main(int argc, char **argv) {
break;
default:
printf("mount [DEVICE] [NODE]\n\t[-t Fs type (default %s)] [-o Options]\n\t[-a Mount all fs in /etc/fstab]\n\nOptions:\n\tremount - Remount a mounted filesystem\n\tro - Read only\n", MOUNT_DEF_FS);
printf("mount [DEVICE] [NODE]\n\t[-t Fs type, default %s] [-o Options]\n\t[-a Mount all fs in /etc/fstab]\n\nOptions:\n", MOUNT_DEF_FS);
print_opts();
return 0;
}
}