aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/basename.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2003-11-23 18:04:47 +0000
committerrsc <devnull@localhost>2003-11-23 18:04:47 +0000
commitbc7cb1a15a67c859c8c71c4b52bb35fe9425a63d (patch)
tree8ca0fe4e2418e6aa18dc74a236c577a719f6c6ed /src/cmd/basename.c
parentf08fdedcee12c06e3ce9ac9bec363915978e8289 (diff)
downloadplan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.tar.gz
plan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.tar.bz2
plan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.zip
new utilities.
the .C files compile but are renamed to avoid building automatically.
Diffstat (limited to 'src/cmd/basename.c')
-rw-r--r--src/cmd/basename.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/cmd/basename.c b/src/cmd/basename.c
new file mode 100644
index 00000000..7b274b22
--- /dev/null
+++ b/src/cmd/basename.c
@@ -0,0 +1,41 @@
+#include <u.h>
+#include <libc.h>
+
+void
+main(int argc, char *argv[])
+{
+ char *pr;
+ int n, dflag;
+
+ dflag = 0;
+ if(argc>1 && strcmp(argv[1], "-d") == 0){
+ --argc;
+ ++argv;
+ dflag = 1;
+ }
+ if(argc < 2 || argc > 3){
+ fprint(2, "usage: basename [-d] string [suffix]\n");
+ exits("usage");
+ }
+ pr = utfrrune(argv[1], '/');
+ if(dflag){
+ if(pr){
+ *pr = 0;
+ print("%s\n", argv[1]);
+ exits(0);
+ }
+ print(".\n");
+ exits(0);
+ }
+ if(pr)
+ pr++;
+ else
+ pr = argv[1];
+ if(argc==3){
+ n = strlen(pr)-strlen(argv[2]);
+ if(n >= 0 && !strcmp(pr+n, argv[2]))
+ pr[n] = 0;
+ }
+ print("%s\n", pr);
+ exits(0);
+}