commit 4dd515e1248755ff46115f9bc26d6fa54bf7c3c8
Author: Louis Burda <quent.burda@gmail.com>
Date: Fri, 11 Nov 2022 17:02:16 +0100
Initial version
Diffstat:
3 files changed, 59 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+unsymlink
diff --git a/Makefile b/Makefile
@@ -0,0 +1,17 @@
+PREFIX ?= /usr/local
+BINDIR ?= /bin
+
+all: unsymlink
+
+clean:
+ rm -f unsymlink
+
+unsymlink: unsymlink.c
+
+install: unsymlink
+ install -m755 unsymlink -t "$(DESTDIR)$(PREFIX)$(BINDIR)"
+
+uninstall:
+ rm -f "$(DESTDIR)$(PREFIX)$(BINDIR)/unsymlink"
+
+.PHONY: all clean install uninstall
diff --git a/unsymlink.c b/unsymlink.c
@@ -0,0 +1,41 @@
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <err.h>
+#include <stdio.h>
+
+static int status = 0;
+
+int
+unsymlink(const char *path)
+{
+ struct stat attrs;
+
+ if (fstatat(AT_FDCWD, path, &attrs, AT_SYMLINK_NOFOLLOW))
+ err(1, "unsymlink: stat");
+
+ if ((attrs.st_mode & S_IFMT) != S_IFLNK) {
+ warnx("not a symlink: %s", path);
+ return 1;
+ }
+
+ if (unlink(path))
+ err(1, "unsymlink: unlink");
+
+ return 0;
+}
+
+int
+main(int argc, const char **argv)
+{
+ int i;
+
+ if (argc <= 1) {
+ printf("Usage: unsymlink FILE..\n");
+ } else {
+ for (i = 1; i < argc; i++)
+ status |= unsymlink(argv[i]);
+ }
+
+ return status;
+}