2011-05-24 12:00:30 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2011-06-04 01:56:18 +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>
|
2011-06-04 01:56:18 +00:00
|
|
|
#include <stdio.h>
|
2011-05-24 12:00:30 +00:00
|
|
|
#include <stdlib.h>
|
2014-07-10 21:00:02 +00:00
|
|
|
#include <string.h>
|
2014-11-23 20:25:39 +00:00
|
|
|
#include <sys/stat.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-26 13:54:22 +00:00
|
|
|
eprintf("usage: %1$s [-f] [-L | -P | -s] target [name]\n"
|
|
|
|
" %1$s [-f] [-L | -P | -s] target ... directory\n", argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2011-05-24 12:00:30 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2014-07-10 21:00:02 +00:00
|
|
|
char *fname, *to;
|
2014-11-13 20:24:47 +00:00
|
|
|
int sflag = 0;
|
|
|
|
int fflag = 0;
|
2014-11-23 20:25:39 +00:00
|
|
|
int hasto = 0;
|
|
|
|
int dirfd = AT_FDCWD;
|
|
|
|
int flags = AT_SYMLINK_FOLLOW;
|
|
|
|
struct stat st;
|
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;
|
|
|
|
case 's':
|
2014-11-13 20:24:47 +00:00
|
|
|
sflag = 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;
|
2013-06-14 18:20:47 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-23 20:25:39 +00:00
|
|
|
if (argc == 0)
|
2014-10-17 15:03:41 +00:00
|
|
|
usage();
|
|
|
|
|
2014-11-23 20:25:39 +00:00
|
|
|
fname = sflag ? "symlink" : "link";
|
2014-07-10 21:00:02 +00:00
|
|
|
|
2014-11-23 20:25:39 +00:00
|
|
|
if (argc >= 2) {
|
|
|
|
if (stat(argv[argc - 1], &st) == 0 && S_ISDIR(st.st_mode)) {
|
|
|
|
if ((dirfd = open(argv[argc - 1], O_RDONLY)) < 0)
|
|
|
|
eprintf("open:");
|
|
|
|
} else if (argc == 2) {
|
|
|
|
to = argv[1];
|
|
|
|
hasto = 1;
|
|
|
|
} else {
|
|
|
|
eprintf("destination is not a directory\n");
|
|
|
|
}
|
|
|
|
argc--;
|
|
|
|
}
|
2014-02-17 11:41:37 +00:00
|
|
|
|
2014-11-23 20:25:39 +00:00
|
|
|
for (; argc > 0; argc--, argv++) {
|
|
|
|
if (!hasto)
|
|
|
|
to = basename(argv[0]);
|
|
|
|
if (fflag)
|
2015-01-22 16:26:30 +00:00
|
|
|
unlinkat(dirfd, to, 0);
|
2014-11-23 20:25:39 +00:00
|
|
|
if ((!sflag ? linkat(AT_FDCWD, argv[0], dirfd, to, flags)
|
|
|
|
: symlinkat(argv[0], dirfd, to)) < 0) {
|
|
|
|
eprintf("%s %s <- %s:", fname, argv[0], to);
|
|
|
|
}
|
|
|
|
}
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2011-05-24 12:00:30 +00:00
|
|
|
}
|