diff options
-rw-r--r-- | usr.bin/basename/basename.c | 9 | ||||
-rw-r--r-- | usr.bin/dirname/dirname.c | 6 |
2 files changed, 13 insertions, 2 deletions
diff --git a/usr.bin/basename/basename.c b/usr.bin/basename/basename.c index d807ed9..e22e5a6 100644 --- a/usr.bin/basename/basename.c +++ b/usr.bin/basename/basename.c @@ -43,8 +43,10 @@ static const char copyright[] = static const char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 5/4/95"; #endif /* not lint */ +#include <err.h> #include <libgen.h> #include <stdio.h> +#include <string.h> #include <unistd.h> void usage __P((void)); @@ -54,6 +56,7 @@ main(argc, argv) int argc; char **argv; { + char *p, *q; int ch; while ((ch = getopt(argc, argv, "")) != -1) @@ -68,7 +71,11 @@ main(argc, argv) if (argc != 1 && argc != 2) usage(); - (void)printf("%s\n", basename(*argv)); + if ((p = basename(argv[0])) == NULL) + err(1, "%s", argv[0]); + if (*++argv && (q = strstr(p, *argv)) && strcmp(q, *argv) == 0) + *q = '\0'; + (void)printf("%s\n", p); exit(0); } diff --git a/usr.bin/dirname/dirname.c b/usr.bin/dirname/dirname.c index cb5adbd..f9d3e3d 100644 --- a/usr.bin/dirname/dirname.c +++ b/usr.bin/dirname/dirname.c @@ -43,6 +43,7 @@ static const char copyright[] = static const char sccsid[] = "@(#)dirname.c 8.4 (Berkeley) 5/4/95"; #endif /* not lint */ +#include <err.h> #include <libgen.h> #include <stdio.h> #include <unistd.h> @@ -54,6 +55,7 @@ main(argc, argv) int argc; char **argv; { + char *p; int ch; while ((ch = getopt(argc, argv, "")) != -1) @@ -68,7 +70,9 @@ main(argc, argv) if (argc != 1) usage(); - (void)printf("%s\n", dirname(*argv)); + if ((p = dirname(*argv)) == NULL) + err(1, "%s", *argv); + (void)printf("%s\n", p); exit(0); } |