From 49feb50f22e2db90556f79dbc0de36f8ecb8b521 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 26 Oct 2019 17:22:55 -0500 Subject: Add ln -r and relative_path() to lib, plus test. And a few small cleanups while I was there. --- toys/posix/ln.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'toys/posix/ln.c') diff --git a/toys/posix/ln.c b/toys/posix/ln.c index 306f0cac..3cd5c7b8 100644 --- a/toys/posix/ln.c +++ b/toys/posix/ln.c @@ -4,7 +4,7 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/ln.html -USE_LN(NEWTOY(ln, "<1t:Tvnfs", TOYFLAG_BIN)) +USE_LN(NEWTOY(ln, "<1rt:Tvnfs", TOYFLAG_BIN)) config LN bool "ln" @@ -18,6 +18,7 @@ config LN -s Create a symbolic link -f Force the creation of the link, even if TO already exists -n Symlink at TO treated as file + -r Create relative symlink from -> to -t Create links in DIR -T TO always treated as file, max 2 arguments -v Verbose @@ -34,30 +35,37 @@ void ln_main(void) { char *dest = TT.t ? TT.t : toys.optargs[--toys.optc], *new; struct stat buf; - int i; + int i, rc; + + if (FLAG(T) && toys.optc>1) help_exit("Max 2 arguments"); // With one argument, create link in current directory. if (!toys.optc) { toys.optc++; - dest="."; + dest = "."; } - if (FLAG(T) && toys.optc>1) help_exit("Max 2 arguments"); // Is destination a directory? if (!((FLAG(n)||FLAG(T)) ? lstat : stat)(dest, &buf)) { - i = S_ISDIR(buf.st_mode); - - if ((FLAG(T) && i) || (!i && (toys.optc>1 || TT.t))) + if ((i = S_ISDIR(buf.st_mode)) ? FLAG(T) : (toys.optc>1 || TT.t)) error_exit("'%s' %s a directory", dest, i ? "is" : "not"); } else buf.st_mode = 0; for (i=0; i '%s'\n", new, try); + if (try != toys.optargs[i]) free(try); if (new != dest) free(new); } } -- cgit v1.2.3