micro-utils/include/libmu/parse_mount.h

73 lines
1.7 KiB
C
Raw Normal View History

2023-12-19 13:13:27 +00:00
#ifndef _PARSE_MOUNT_H
#define _PARSE_MOUNT_H
2023-12-19 19:37:48 +00:00
#include <string.h>
2023-12-19 13:13:27 +00:00
2023-12-19 19:37:48 +00:00
typedef struct {
char *opt;
char *notopt;
char *desc;
unsigned int val;
} MU_MOUNT_OPTS;
MU_MOUNT_OPTS mu_options[] = {
2023-12-24 09:37:42 +00:00
{"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},
{"bind", "rbind", "Bind a file or directory", MS_BIND},
{"noexec", "exec", "(Dis)allow use of executable files", MS_NOEXEC},
{"noatime", "atime", "Disable/enable updates to inode access times", MS_NOATIME}
2023-12-19 19:37:48 +00:00
};
2023-12-24 09:37:42 +00:00
unsigned long mu_parse_opts(char *str, char *data, const size_t data_size) {
memset(data, '\0', data_size);
unsigned long opt = 0;
size_t data_len = 0;
2023-12-19 19:37:48 +00:00
char *token = strtok(str, ",");
while (token != NULL) {
2023-12-24 09:37:42 +00:00
int invalidopt = 1;
2023-12-19 19:37:48 +00:00
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;
2023-12-24 09:37:42 +00:00
invalidopt = 0;
2023-12-19 19:37:48 +00:00
break;
}
if (mu_options[i].notopt && !strcmp(token, mu_options[i].notopt)) {
opt &= ~mu_options[i].val;
2023-12-24 09:37:42 +00:00
invalidopt = 0;
2023-12-19 19:37:48 +00:00
break;
}
}
2023-12-24 09:37:42 +00:00
/* Unknow opt, pass in mount() */
size_t len = strlen(token);
if (invalidopt && len > 0) {
/* Copy token string */
for (size_t i = 0; i < len; i++) {
if (data_len + 1 >= data_size)
break;
else {
data[data_len] = token[i];
data_len++;
}
}
if (data_len && data_len + 1 < data_size)
data[data_len++] = ',';
}
2023-12-19 19:37:48 +00:00
token = strtok(NULL, ",");
}
2024-01-06 12:41:21 +00:00
if (data_len > 0)
data[data_len - 1] = '\0';
2023-12-19 19:37:48 +00:00
return opt;
2023-12-19 13:13:27 +00:00
}
#endif