unsymlink

Symlink (only) unlinker
git clone https://git.sinitax.com/sinitax/unsymlink
Log | Files | Refs | LICENSE | sfeed.txt

unsymlink.c (820B)


      1#include <sys/stat.h>
      2#include <unistd.h>
      3#include <fcntl.h>
      4#include <errno.h>
      5#include <stdio.h>
      6#include <string.h>
      7#include <stdlib.h>
      8
      9static int status = 0;
     10
     11int
     12unsymlink(const char *path)
     13{
     14	struct stat attrs;
     15
     16	if (fstatat(AT_FDCWD, path, &attrs, AT_SYMLINK_NOFOLLOW)) {
     17		fprintf(stderr, "unsymlink: stat: %s\n", strerror(errno));
     18		exit(1);
     19	}
     20
     21	if ((attrs.st_mode & S_IFMT) != S_IFLNK) {
     22		fprintf(stderr, "unsymlink: not a symlink '%s'\n", path);
     23		return 1;
     24	}
     25
     26	if (unlink(path)) {
     27		fprintf(stderr, "unsymlink: unlink: %s\n", strerror(errno));
     28		exit(1);
     29	}
     30
     31	return 0;
     32}
     33
     34int
     35main(int argc, const char **argv)
     36{
     37	int status, i;
     38
     39	if (argc <= 1) {
     40		fprintf(stderr, "Usage: unsymlink FILE..\n");
     41		return 1;
     42	}
     43
     44	status = 0;
     45	for (i = 1; i < argc; i++)
     46		status |= unsymlink(argv[i]);
     47
     48	return status;
     49}