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!
This commit is contained in:
FRIGN 2015-03-05 21:14:43 +01:00
parent aea256c288
commit ab26b5583e
3 changed files with 38 additions and 26 deletions

2
README
View File

@ -39,7 +39,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=*| hostname non-posix none =*| hostname non-posix none
=* kill yes none =* kill yes none
=*| link yes none =*| link yes none
=* ln yes none =*| ln yes none
=* logger yes none =* logger yes none
=* logname yes none =* logname yes none
= ls no (-C), -S, -f, -m, -s, -x = ls no (-C), -S, -f, -m, -s, -x

6
ln.1
View File

@ -1,4 +1,4 @@
.Dd January 26, 2015 .Dd March 5, 2015
.Dt LN 1 .Dt LN 1
.Os sbase .Os sbase
.Sh NAME .Sh NAME
@ -37,7 +37,9 @@ hardlinks them in the existing
.It Fl f .It Fl f
If If
.Ar name .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 .It Fl L | Fl P
If If
.Ar target .Ar target

54
ln.c
View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h> #include <libgen.h>
#include <unistd.h> #include <unistd.h>
@ -11,64 +12,73 @@ static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-f] [-L | -P | -s] target [name]\n" eprintf("usage: %s [-f] [-L | -P | -s] target [name]\n"
" %s [-f] [-L | -P | -s] target ... directory\n", " %s [-f] [-L | -P | -s] target ... dir\n", argv0, argv0);
argv0, argv0);
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *fname, *to; char *fname, *to;
int sflag = 0; int sflag = 0, fflag = 0, hasto = 0, dirfd = AT_FDCWD, flags = AT_SYMLINK_FOLLOW;
int fflag = 0; struct stat st, tost;
int hasto = 0;
int dirfd = AT_FDCWD;
int flags = AT_SYMLINK_FOLLOW;
struct stat st;
ARGBEGIN { ARGBEGIN {
case 'f': case 'f':
fflag = 1; fflag = 1;
break; break;
case 's':
sflag = 1;
break;
case 'L': case 'L':
flags |= AT_SYMLINK_FOLLOW; flags |= AT_SYMLINK_FOLLOW;
break; break;
case 'P': case 'P':
flags &= ~AT_SYMLINK_FOLLOW; flags &= ~AT_SYMLINK_FOLLOW;
break; break;
case 's':
sflag = 1;
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
if (argc == 0) if (!argc)
usage(); usage();
fname = sflag ? "symlink" : "link"; fname = sflag ? "symlink" : "link";
if (argc >= 2) { 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) if ((dirfd = open(argv[argc - 1], O_RDONLY)) < 0)
eprintf("open:"); eprintf("open %s:", argv[argc - 1]);
} else if (argc == 2) { } else if (argc == 2) {
to = argv[1]; to = argv[argc - 1];
hasto = 1; hasto = 1;
} else { } else {
eprintf("destination is not a directory\n"); eprintf("%s: not a directory\n", argv[argc - 1]);
} }
argv[argc - 1] = NULL;
argc--; argc--;
} }
for (; argc > 0; argc--, argv++) { for (; *argv; argc--, argv++) {
if (!hasto) if (!hasto)
to = basename(argv[0]); to = basename(*argv);
if (fflag) 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); 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); if ((!sflag ? linkat(AT_FDCWD, *argv, dirfd, to, flags)
: symlinkat(*argv, dirfd, to)) < 0) {
weprintf("%s %s <- %s:", fname, *argv, to);
} }
} }