commit f34b5200e6a7d6feda435e13ddf244ccdb9dd105
parent 4dd515e1248755ff46115f9bc26d6fa54bf7c3c8
Author: Louis Burda <quent.burda@gmail.com>
Date: Thu, 15 Jun 2023 13:36:53 +0200
Remove err.h dependency
Diffstat:
1 file changed, 19 insertions(+), 11 deletions(-)
diff --git a/unsymlink.c b/unsymlink.c
@@ -1,8 +1,10 @@
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
-#include <err.h>
+#include <errno.h>
#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
static int status = 0;
@@ -11,16 +13,20 @@ unsymlink(const char *path)
{
struct stat attrs;
- if (fstatat(AT_FDCWD, path, &attrs, AT_SYMLINK_NOFOLLOW))
- err(1, "unsymlink: stat");
+ if (fstatat(AT_FDCWD, path, &attrs, AT_SYMLINK_NOFOLLOW)) {
+ fprintf(stderr, "unsymlink: stat: %s\n", strerror(errno));
+ exit(1);
+ }
if ((attrs.st_mode & S_IFMT) != S_IFLNK) {
- warnx("not a symlink: %s", path);
+ fprintf(stderr, "unsymlink: not a symlink '%s'\n", path);
return 1;
}
- if (unlink(path))
- err(1, "unsymlink: unlink");
+ if (unlink(path)) {
+ fprintf(stderr, "unsymlink: unlink: %s\n", strerror(errno));
+ exit(1);
+ }
return 0;
}
@@ -28,14 +34,16 @@ unsymlink(const char *path)
int
main(int argc, const char **argv)
{
- int i;
+ int status, i;
if (argc <= 1) {
- printf("Usage: unsymlink FILE..\n");
- } else {
- for (i = 1; i < argc; i++)
- status |= unsymlink(argv[i]);
+ fprintf(stderr, "Usage: unsymlink FILE..\n");
+ return 1;
}
+ status = 0;
+ for (i = 1; i < argc; i++)
+ status |= unsymlink(argv[i]);
+
return status;
}