sbase/ln.c

54 lines
828 B
C
Raw Normal View History

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>
2011-05-24 12:00:30 +00:00
#include <stdbool.h>
2011-06-04 01:56:18 +00:00
#include <stdio.h>
2011-05-24 12:00:30 +00:00
#include <stdlib.h>
#include <unistd.h>
#include "util.h"
2011-06-04 01:56:18 +00:00
static int ln(const char *, const char *);
static bool sflag = false;
static bool fflag = false;
2013-06-14 18:20:47 +00:00
static void
usage(void)
{
eprintf("usage: %s [-fs] target linkname\n", argv0);
}
2011-05-24 12:00:30 +00:00
int
main(int argc, char *argv[])
{
2013-06-14 18:20:47 +00:00
ARGBEGIN {
case 'f':
fflag = true;
break;
case 's':
sflag = true;
break;
default:
usage();
} ARGEND;
enmasse(argc, &argv[0], ln);
return 0;
2011-05-24 12:00:30 +00:00
}
2011-06-04 01:56:18 +00:00
int
ln(const char *s1, const char *s2)
{
2011-06-22 22:45:03 +00:00
int (*flink)(const char *, const char *) = sflag ? symlink : link;
if(flink(s1, s2) == 0)
return 0;
if(fflag && errno == EEXIST) {
if(remove(s2) == -1)
eprintf("remove %s:", s2);
return flink(s1, s2);
}
return -1;
2011-06-04 01:56:18 +00:00
}
2013-06-14 18:20:47 +00:00