summaryrefslogtreecommitdiff
path: root/util/sbase/basename.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/sbase/basename.c')
-rw-r--r--util/sbase/basename.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/util/sbase/basename.c b/util/sbase/basename.c
new file mode 100644
index 00000000..94a2848f
--- /dev/null
+++ b/util/sbase/basename.c
@@ -0,0 +1,37 @@
+/* See LICENSE file for copyright and license details. */
+#include <libgen.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "util.h"
+
+static void
+usage(void)
+{
+ eprintf("usage: %s path [suffix]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ ssize_t off;
+ char *p;
+
+ ARGBEGIN {
+ default:
+ usage();
+ } ARGEND
+
+ if (argc != 1 && argc != 2)
+ usage();
+
+ p = basename(argv[0]);
+ if (argc == 2) {
+ off = strlen(p) - strlen(argv[1]);
+ if (off > 0 && !strcmp(p + off, argv[1]))
+ p[off] = '\0';
+ }
+ puts(p);
+
+ return fshut(stdout, "<stdout>");
+}