2011-05-24 12:00:30 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 20:02:41 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2015-03-05 20:14:43 +00:00
|
|
|
#include <errno.h>
|
2014-11-23 20:25:39 +00:00
|
|
|
#include <fcntl.h>
|
2014-10-17 15:03:41 +00:00
|
|
|
#include <libgen.h>
|
2015-05-27 16:15:24 +00:00
|
|
|
#include <string.h>
|
2014-10-17 15:03:41 +00:00
|
|
|
#include <unistd.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-05-24 12:00:30 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-01-31 14:23:59 +00:00
|
|
|
eprintf("usage: %s [-f] [-L | -P | -s] target [name]\n"
|
2015-03-05 20:14:43 +00:00
|
|
|
" %s [-f] [-L | -P | -s] target ... dir\n", argv0, argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2011-05-24 12:00:30 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-05-27 16:15:24 +00:00
|
|
|
char *targetdir = ".", *target = NULL;
|
|
|
|
int ret = 0, sflag = 0, fflag = 0, dirfd = AT_FDCWD,
|
|
|
|
hastarget = 0, flags = AT_SYMLINK_FOLLOW;
|
|
|
|
struct stat st, tst;
|
2014-02-17 11:41:37 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'f':
|
2014-11-13 20:24:47 +00:00
|
|
|
fflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
2014-11-23 20:25:39 +00:00
|
|
|
case 'L':
|
|
|
|
flags |= AT_SYMLINK_FOLLOW;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
flags &= ~AT_SYMLINK_FOLLOW;
|
|
|
|
break;
|
2015-03-05 20:14:43 +00:00
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2015-03-05 20:14:43 +00:00
|
|
|
if (!argc)
|
2014-10-17 15:03:41 +00:00
|
|
|
usage();
|
|
|
|
|
2015-05-27 16:15:24 +00:00
|
|
|
if (argc > 1) {
|
2015-03-05 20:14:43 +00:00
|
|
|
if (!stat(argv[argc - 1], &st) && S_ISDIR(st.st_mode)) {
|
2014-11-23 20:25:39 +00:00
|
|
|
if ((dirfd = open(argv[argc - 1], O_RDONLY)) < 0)
|
2015-03-05 20:14:43 +00:00
|
|
|
eprintf("open %s:", argv[argc - 1]);
|
2015-05-27 16:15:24 +00:00
|
|
|
targetdir = argv[argc - 1];
|
|
|
|
if (targetdir[strlen(targetdir) - 1] == '/')
|
|
|
|
targetdir[strlen(targetdir) - 1] = '\0';
|
2014-11-23 20:25:39 +00:00
|
|
|
} else if (argc == 2) {
|
2015-05-27 16:15:24 +00:00
|
|
|
hastarget = 1;
|
|
|
|
target = argv[argc - 1];
|
2014-11-23 20:25:39 +00:00
|
|
|
} else {
|
2015-03-05 20:14:43 +00:00
|
|
|
eprintf("%s: not a directory\n", argv[argc - 1]);
|
2014-11-23 20:25:39 +00:00
|
|
|
}
|
2015-03-05 20:14:43 +00:00
|
|
|
argv[argc - 1] = NULL;
|
2014-11-23 20:25:39 +00:00
|
|
|
argc--;
|
|
|
|
}
|
2014-02-17 11:41:37 +00:00
|
|
|
|
2015-03-05 20:14:43 +00:00
|
|
|
for (; *argv; argc--, argv++) {
|
2015-05-27 16:15:24 +00:00
|
|
|
if (!hastarget)
|
|
|
|
target = basename(*argv);
|
|
|
|
|
2015-06-05 11:11:09 +00:00
|
|
|
if (!sflag) {
|
|
|
|
if (stat(*argv, &st) < 0) {
|
|
|
|
weprintf("stat %s:", *argv);
|
|
|
|
ret = 1;
|
|
|
|
continue;
|
|
|
|
} else if (fstatat(dirfd, target, &tst, AT_SYMLINK_NOFOLLOW) < 0) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
weprintf("fstatat %s %s:", targetdir, target);
|
|
|
|
ret = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (st.st_dev == tst.st_dev && st.st_ino == tst.st_ino) {
|
|
|
|
weprintf("%s and %s/%s are the same file\n",
|
|
|
|
*argv, targetdir, target);
|
2015-05-08 08:16:58 +00:00
|
|
|
ret = 1;
|
2015-05-08 08:16:57 +00:00
|
|
|
continue;
|
2015-03-05 20:14:43 +00:00
|
|
|
}
|
2015-05-27 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fflag && unlinkat(dirfd, target, 0) < 0 && errno != ENOENT) {
|
|
|
|
weprintf("unlinkat %s %s:", targetdir, target);
|
|
|
|
ret = 1;
|
|
|
|
continue;
|
2015-03-05 20:14:43 +00:00
|
|
|
}
|
2015-05-27 16:15:24 +00:00
|
|
|
if ((sflag ? symlinkat(*argv, dirfd, target) :
|
|
|
|
linkat(AT_FDCWD, *argv, dirfd, target, flags)) < 0) {
|
|
|
|
weprintf("%s %s <- %s/%s:", sflag ? "symlinkat" : "linkat",
|
|
|
|
*argv, targetdir, target);
|
2015-05-08 08:16:58 +00:00
|
|
|
ret = 1;
|
2014-11-23 20:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2015-05-08 08:16:58 +00:00
|
|
|
return ret;
|
2011-05-24 12:00:30 +00:00
|
|
|
}
|