commit: 71af8117be0c5ed8abde37caf42ff2850e411668
parent 26fcf5a25759cc431bbf2d6dd7ede48e61203b09
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 27 Apr 2024 20:12:41 +0200
ln-stub.c: new
Diffstat:
3 files changed, 60 insertions(+), 3 deletions(-)
diff --git a/init.sh b/init.sh
@@ -23,8 +23,10 @@ build_awk() {
}
build_stubs() {
- $CC $CFLAGS -o /bin/ls /ls-stub.c
- $CC $CFLAGS -o /bin/mv /mv-stub.c
+ for i in ln ls mv
+ do
+ $CC $CFLAGS -o "/bin/$i" "/${i}-stub.c" || die "Failed compiling $i stub"
+ done
}
build_bmake() {
diff --git a/ln-stub.c b/ln-stub.c
@@ -0,0 +1,55 @@
+#define _POSIX_C_SOURCE 200809L
+
+#include <stdio.h> // fprintf
+#include <unistd.h> // getopt, symlink, link
+#include <string.h> // strerror
+#include <errno.h>
+#include <stdbool.h>
+
+int
+main(int argc, char *argv[])
+{
+ bool opt_s = false;
+
+ int c = -1;
+ while((c = getopt(argc, argv, ":s")) != -1)
+ {
+ switch(c)
+ {
+ case 's':
+ opt_s = true;
+ break;
+ case '?':
+ fprintf(stderr, "ln: Unknown option '-%c'\n", optopt);
+ break;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if(argc != 2)
+ {
+ fprintf(stderr, "Usage: ln [-s] src dest\n");
+ return 1;
+ }
+
+ if(opt_s)
+ {
+ if(symlink(argv[0], argv[1]) < 0)
+ {
+ fprintf(stderr, "ln: Failed creating symlink: %s\n", strerror(errno));
+ return 1;
+ }
+ }
+ else
+ {
+ if(link(argv[0], argv[1]) < 0)
+ {
+ fprintf(stderr, "ln: Failed creating (hard) link: %s\n", strerror(errno));
+ return 1;
+ }
+ }
+
+ return 0;
+}
diff --git a/make-initrd.sh b/make-initrd.sh
@@ -68,7 +68,7 @@ cp "${WORKDIR}/init.c" ./init || die "copying init"
sed -i '1i#!/bin/tcc -run' ./init || die "failed adding tcc shebang to init"
chmod 755 init || die "init chmod"
-for i in init.sh ls-stub.c mv-stub.c
+for i in init.sh ln-stub.c ls-stub.c mv-stub.c
do
cp -p "${WORKDIR}/$i" ./ || die "failed copying $i"
done