summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorOtto Moerbeek <otto@cvs.openbsd.org>2005-04-07 07:22:48 +0000
committerOtto Moerbeek <otto@cvs.openbsd.org>2005-04-07 07:22:48 +0000
commitf8a49f0f52c5c38b973a36f61304c5768fb4baba (patch)
tree55c12542994f1de738a5c919ae9ff78fac9692a6 /usr.bin
parenta791080d43e6ea72e99990cf0bea5d6dd91e249a (diff)
- Reintroduce getopt(3) for option processing. POSIX requires tools that
take no options to recognize --. - Use basename(3) to do the groundwork, instead of a rolling our own. Visible changes in behaviour: basename "" now returns a dot instead of an empty line; pathnames longer than PATH_MAX will produce an error message. ok deraadt@ jaredy@ millert@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/basename/basename.c71
1 files changed, 23 insertions, 48 deletions
diff --git a/usr.bin/basename/basename.c b/usr.bin/basename/basename.c
index 758b95afc08..bb1544a2550 100644
--- a/usr.bin/basename/basename.c
+++ b/usr.bin/basename/basename.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: basename.c,v 1.6 2003/06/10 22:20:44 deraadt Exp $ */
+/* $OpenBSD: basename.c,v 1.7 2005/04/07 07:22:47 otto Exp $ */
/* $NetBSD: basename.c,v 1.9 1995/09/02 05:29:46 jtc Exp $ */
/*-
@@ -40,9 +40,11 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 5/4/95";
#endif
-static char rcsid[] = "$OpenBSD: basename.c,v 1.6 2003/06/10 22:20:44 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: basename.c,v 1.7 2005/04/07 07:22:47 otto Exp $";
#endif /* not lint */
+#include <err.h>
+#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -54,62 +56,34 @@ void usage(void);
int
main(int argc, char *argv[])
{
+ int ch;
char *p;
setlocale(LC_ALL, "");
- if (argc != 2 && argc != 3)
- usage();
- argc--;
- argv++;
-
- /*
- * (1) If string is // it is implementation defined whether steps (2)
- * through (5) are skipped or processed.
- *
- * (2) If string consists entirely of slash characters, string shall
- * be set to a single slash character. In this case, skip steps
- * (3) through (5).
- */
- for (p = *argv;; ++p) {
- if (!*p) {
- if (p > *argv)
- (void)putchar('/');
- (void)putchar('\n');
- exit(0);
+ while ((ch = getopt(argc, argv, "")) != -1) {
+ switch (ch) {
+ default:
+ usage();
}
- if (*p != '/')
- break;
}
+ argc -= optind;
+ argv += optind;
- /*
- * (3) If there are any trailing slash characters in string, they
- * shall be removed.
- */
- for (; *p; ++p)
- continue;
- while (*--p == '/')
- continue;
- *++p = '\0';
-
- /*
- * (4) If there are any slash characters remaining in string, the
- * prefix of string up to an including the last slash character
- * in string shall be removed.
- */
- while (--p >= *argv)
- if (*p == '/')
- break;
- ++p;
+ if (argc != 1 && argc != 2)
+ usage();
+ p = basename(*argv);
+ if (p == NULL)
+ err(1, "%s", *argv);
/*
- * (5) If the suffix operand is present, is not identical to the
- * characters remaining in string, and is identical to a suffix
- * of the characters remaining in string, the suffix suffix
- * shall be removed from string.
+ * If the suffix operand is present, is not identical to the
+ * characters remaining in string, and is identical to a suffix
+ * of the characters remaining in string, the suffix suffix
+ * shall be removed from string.
*/
if (*++argv) {
- int suffixlen, stringlen, off;
+ size_t suffixlen, stringlen, off;
suffixlen = strlen(*argv);
stringlen = strlen(p);
@@ -124,10 +98,11 @@ main(int argc, char *argv[])
exit(0);
}
+extern char *__progname;
void
usage(void)
{
- (void)fprintf(stderr, "usage: basename string [suffix]\n");
+ (void)fprintf(stderr, "usage: %s string [suffix]\n", __progname);
exit(1);
}