summaryrefslogtreecommitdiff
path: root/bin/ln/ln.c
diff options
context:
space:
mode:
authorNils Nordman <nino@cvs.openbsd.org>2003-05-04 12:44:18 +0000
committerNils Nordman <nino@cvs.openbsd.org>2003-05-04 12:44:18 +0000
commit6cdff94d7ce0ffb3a803242ce7bc621507b06dfe (patch)
treed3bfffb0d5a04f30f09ee2670bc9a2a4219b688d /bin/ln/ln.c
parent34f42003a7cbe252e190eea019992c1456b439ba (diff)
The custom logic used for determining the basename of source file
was flawed in that it did not account for trailing slashes. Use basename(3) instead. Fixes invocations where target is a directory and source has a trailing slash (e.g. ln -s /tmp/ .). Ok millert@.
Diffstat (limited to 'bin/ln/ln.c')
-rw-r--r--bin/ln/ln.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/bin/ln/ln.c b/bin/ln/ln.c
index f036a8ec218..98a384cf1ec 100644
--- a/bin/ln/ln.c
+++ b/bin/ln/ln.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ln.c,v 1.8 2002/07/04 04:26:40 deraadt Exp $ */
+/* $OpenBSD: ln.c,v 1.9 2003/05/04 12:44:17 nino Exp $ */
/* $NetBSD: ln.c,v 1.10 1995/03/21 09:06:10 cgd Exp $ */
/*
@@ -44,7 +44,7 @@ static const char copyright[] =
#if 0
static const char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
#else
-static const char rcsid[] = "$OpenBSD: ln.c,v 1.8 2002/07/04 04:26:40 deraadt Exp $";
+static const char rcsid[] = "$OpenBSD: ln.c,v 1.9 2003/05/04 12:44:17 nino Exp $";
#endif
#endif /* not lint */
@@ -53,6 +53,7 @@ static const char rcsid[] = "$OpenBSD: ln.c,v 1.8 2002/07/04 04:26:40 deraadt Ex
#include <err.h>
#include <errno.h>
+#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -142,10 +143,10 @@ linkit(char *target, char *source, int isdir)
/* If the source is a directory, append the target's name. */
if (isdir || (!statf(source, &sb) && S_ISDIR(sb.st_mode))) {
- if ((p = strrchr(target, '/')) == NULL)
- p = target;
- else
- ++p;
+ if ((p = basename(target)) == NULL) {
+ warn("%s", target);
+ return (1);
+ }
(void)snprintf(path, sizeof(path), "%s/%s", source, p);
source = path;
}