sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit ab26b5583e607b701b684d05d2eabe71eb4246b0
parent aea256c288d1eca5d0fce0225a89952690eda544
Author: FRIGN <dev@frign.de>
Date:   Thu Mar  5 21:14:43 +0100

Audit ln(1)

1) Clarify behaviour when the f-flag is given and a target is in its
   own way.
2) Fix usage()-style.
3) Group local variable declarations.
4) reorder args
5) argc style, other boolean style changes
6) improve error messages
7) set argv[argc - 1] to NULL to allow argv-centric loop later
8) BUGFIX: POSIX specifies that when with the f-flag there's a
   situation where a file stands in its own way for linking it
   should be ignored.
9) Add weprintf() where possible, so we don't pussy out when there's
   a small issue. This is sbase ffs!

Diffstat:
README | 2+-
ln.1 | 6++++--
ln.c | 56+++++++++++++++++++++++++++++++++-----------------------
3 files changed, 38 insertions(+), 26 deletions(-)
diff --git a/README b/README @@ -39,7 +39,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support, =*| hostname non-posix none =* kill yes none =*| link yes none -=* ln yes none +=*| ln yes none =* logger yes none =* logname yes none = ls no (-C), -S, -f, -m, -s, -x diff --git a/ln.1 b/ln.1 @@ -1,4 +1,4 @@ -.Dd January 26, 2015 +.Dd March 5, 2015 .Dt LN 1 .Os sbase .Sh NAME @@ -37,7 +37,9 @@ hardlinks them in the existing .It Fl f If .Ar name -exists, remove it to allow the link. +exists and is not a +.Ar target , +remove it to allow the link. .It Fl L | Fl P If .Ar target diff --git a/ln.c b/ln.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include <sys/stat.h> +#include <errno.h> #include <fcntl.h> #include <libgen.h> #include <unistd.h> @@ -11,64 +12,73 @@ static void usage(void) { eprintf("usage: %s [-f] [-L | -P | -s] target [name]\n" - " %s [-f] [-L | -P | -s] target ... directory\n", - argv0, argv0); + " %s [-f] [-L | -P | -s] target ... dir\n", argv0, argv0); } int main(int argc, char *argv[]) { char *fname, *to; - int sflag = 0; - int fflag = 0; - int hasto = 0; - int dirfd = AT_FDCWD; - int flags = AT_SYMLINK_FOLLOW; - struct stat st; + int sflag = 0, fflag = 0, hasto = 0, dirfd = AT_FDCWD, flags = AT_SYMLINK_FOLLOW; + struct stat st, tost; ARGBEGIN { case 'f': fflag = 1; break; - case 's': - sflag = 1; - break; case 'L': flags |= AT_SYMLINK_FOLLOW; break; case 'P': flags &= ~AT_SYMLINK_FOLLOW; break; + case 's': + sflag = 1; + break; default: usage(); } ARGEND; - if (argc == 0) + if (!argc) usage(); fname = sflag ? "symlink" : "link"; if (argc >= 2) { - if (stat(argv[argc - 1], &st) == 0 && S_ISDIR(st.st_mode)) { + if (!stat(argv[argc - 1], &st) && S_ISDIR(st.st_mode)) { if ((dirfd = open(argv[argc - 1], O_RDONLY)) < 0) - eprintf("open:"); + eprintf("open %s:", argv[argc - 1]); } else if (argc == 2) { - to = argv[1]; + to = argv[argc - 1]; hasto = 1; } else { - eprintf("destination is not a directory\n"); + eprintf("%s: not a directory\n", argv[argc - 1]); } + argv[argc - 1] = NULL; argc--; } - for (; argc > 0; argc--, argv++) { + for (; *argv; argc--, argv++) { if (!hasto) - to = basename(argv[0]); - if (fflag) - unlinkat(dirfd, to, 0); - if ((!sflag ? linkat(AT_FDCWD, argv[0], dirfd, to, flags) - : symlinkat(argv[0], dirfd, to)) < 0) { - eprintf("%s %s <- %s:", fname, argv[0], to); + to = basename(*argv); + if (fflag) { + if (stat(*argv, &st) < 0) { + weprintf("stat %s:", *argv); + continue; + } else if (fstatat(dirfd, to, &tost, AT_SYMLINK_NOFOLLOW) < 0) { + if (errno != ENOENT) + eprintf("stat %s:", to); + } else { + if (st.st_dev == tost.st_dev && st.st_ino == tost.st_ino) { + weprintf("%s and %s are the same file\n", *argv, *argv); + continue; + } + unlinkat(dirfd, to, 0); + } + } + if ((!sflag ? linkat(AT_FDCWD, *argv, dirfd, to, flags) + : symlinkat(*argv, dirfd, to)) < 0) { + weprintf("%s %s <- %s:", fname, *argv, to); } }