Mercurial > hg > toybox
comparison toys/posix/basename.c @ 781:b300eb824c70
Make basename use basename().
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 04 Jan 2013 21:10:49 -0600 |
parents | 786841fdb1e0 |
children |
comparison
equal
deleted
inserted
replaced
780:6cc69be43c42 | 781:b300eb824c70 |
---|---|
18 | 18 |
19 #include "toys.h" | 19 #include "toys.h" |
20 | 20 |
21 void basename_main(void) | 21 void basename_main(void) |
22 { | 22 { |
23 char *arg = toys.optargs[0], *suffix = toys.optargs[1], *base; | 23 char *base = basename(*toys.optargs), *suffix = toys.optargs[1]; |
24 | |
25 while ((base = strrchr(arg, '/'))) { | |
26 if (base == arg) break; | |
27 if (!base[1]) *base = 0; | |
28 else { | |
29 base++; | |
30 break; | |
31 } | |
32 } | |
33 | |
34 if (!base) base = arg; | |
35 | 24 |
36 // chop off the suffix if provided | 25 // chop off the suffix if provided |
37 if (suffix) { | 26 if (suffix) { |
38 arg = base + strlen(base) - strlen(suffix); | 27 char *s = base + strlen(base) - strlen(suffix); |
39 if (arg > base && !strcmp(arg, suffix)) *arg = 0; | 28 if (s > base && !strcmp(s, suffix)) *s = 0; |
40 } | 29 } |
41 | 30 |
42 puts(base); | 31 puts(base); |
43 } | 32 } |