ln: allow with one argument

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
This commit is contained in:
Hiltjo Posthuma 2014-07-10 21:00:02 +00:00 committed by sin
parent 72b909df9c
commit 81bb8422a5

23
ln.c
View File

@ -4,19 +4,20 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
#include "util.h" #include "util.h"
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-fs] target linkname\n", argv0); eprintf("usage: %s [-fs] target [linkname]\n", argv0);
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int (*flink)(const char *, const char *); int (*flink)(const char *, const char *);
char *fname; char *fname, *to;
bool sflag = false; bool sflag = false;
bool fflag = false; bool fflag = false;
@ -31,12 +32,24 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
flink = sflag ? symlink : link; if(sflag) {
fname = sflag ? "symlink" : "link"; flink = symlink;
fname = "symlink";
} else {
flink = link;
fname = "link";
}
if(argc < 2) {
if((to = strrchr(argv[0], '/')))
to++;
} else {
to = argv[1];
}
if (fflag == true) if (fflag == true)
remove(argv[1]); remove(argv[1]);
if (flink(argv[0], argv[1]) < 0) if (flink(argv[0], to) < 0)
eprintf("%s:", fname); eprintf("%s:", fname);
return EXIT_SUCCESS; return EXIT_SUCCESS;