summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorOtto Moerbeek <otto@cvs.openbsd.org>2005-04-07 07:16:22 +0000
committerOtto Moerbeek <otto@cvs.openbsd.org>2005-04-07 07:16:22 +0000
commita791080d43e6ea72e99990cf0bea5d6dd91e249a (patch)
tree06bb9136af08594828163b05948f1a580f5dc247 /usr.bin
parent4827a6d292dfd0a8ebfb887bda98551757974610 (diff)
Reintroduce getopt(3) for option processing. POSIX requires tools that
take no options to recognize --. ok deraadt@ jaredy@ millert@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/dirname/dirname.c35
1 files changed, 27 insertions, 8 deletions
diff --git a/usr.bin/dirname/dirname.c b/usr.bin/dirname/dirname.c
index 2f88c3cf5ab..8202a6b2489 100644
--- a/usr.bin/dirname/dirname.c
+++ b/usr.bin/dirname/dirname.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dirname.c,v 1.10 2003/07/10 00:06:50 david Exp $ */
+/* $OpenBSD: dirname.c,v 1.11 2005/04/07 07:16:21 otto Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -17,7 +17,7 @@
*/
#ifndef lint
-static char rcsid[] = "$OpenBSD: dirname.c,v 1.10 2003/07/10 00:06:50 david Exp $";
+static char rcsid[] = "$OpenBSD: dirname.c,v 1.11 2005/04/07 07:16:21 otto Exp $";
#endif /* not lint */
#include <err.h>
@@ -25,22 +25,41 @@ static char rcsid[] = "$OpenBSD: dirname.c,v 1.10 2003/07/10 00:06:50 david Exp
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
+
+void usage(void);
int
main(int argc, char *argv[])
{
+ int ch;
char *dir;
- extern char *__progname;
setlocale(LC_ALL, "");
- if (argc != 2) {
- (void)fprintf(stderr, "Usage: %s pathname\n", __progname);
- exit(1);
+ while ((ch = getopt(argc, argv, "")) != -1) {
+ switch (ch) {
+ default:
+ usage();
+ }
}
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 1)
+ usage();
- if ((dir = dirname(argv[1])) == NULL)
- err(1, NULL);
+ if ((dir = dirname(argv[0])) == NULL)
+ err(1, "%s", argv[0]);
puts(dir);
exit(0);
}
+
+extern char *__progname;
+
+void
+usage(void)
+{
+ (void)fprintf(stderr, "Usage: %s pathname\n", __progname);
+ exit(1);
+}