mount syscall and simple mount program

This commit is contained in:
Matheus
2023-07-26 00:19:51 -03:00
parent 755fd1a8d1
commit dcaa20ae9c
10 changed files with 199 additions and 12 deletions
+14
View File
@@ -0,0 +1,14 @@
.phony: all install
CC=x86_64-astral-gcc
LD=x86_64-astral-gcc
all: mount
install:
mkdir -p "$(DESTDIR)/$(PREFIX)/bin/"
cp mount "$(DESTDIR)/$(PREFIX)/bin/mount"
mount: main.o
$(LD) $(LDFLAGS) -o $@ $^
main.o: main.c
$(CC) -c $(CFLAGS) -o $@ $<
+44
View File
@@ -0,0 +1,44 @@
#include <sys/mount.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
void usage(char *name) {
fprintf(stderr, "%s: usage: %s [-d device] mountpoint filesystem\n", name, name);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
char *dev = NULL;
char *mountp = NULL;
char *fs = NULL;
bool devnext = false;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-d") == 0) {
devnext = true;
continue;
}
if (devnext) {
devnext = false;
dev = argv[i];
} else if (mountp == NULL)
mountp = argv[i];
else if (fs == NULL)
fs = argv[i];
else
usage(argv[0]);
}
if (mount == NULL || fs == NULL)
usage(argv[0]);
if (mount(dev, mountp, fs, 0, NULL) == -1) {
perror("mount failed");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}