20 |
* 3) Save some space by using strcmp(). Calling strncmp() here was silly. |
* 3) Save some space by using strcmp(). Calling strncmp() here was silly. |
21 |
*/ |
*/ |
22 |
|
|
23 |
#include <stdlib.h> |
#include "libbb.h" |
|
#include <stdio.h> |
|
|
#include <string.h> |
|
|
#include "busybox.h" |
|
24 |
|
|
25 |
|
/* This is a NOFORK applet. Be very careful! */ |
26 |
|
|
27 |
|
int basename_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
28 |
int basename_main(int argc, char **argv) |
int basename_main(int argc, char **argv) |
29 |
{ |
{ |
30 |
size_t m, n; |
size_t m, n; |
34 |
bb_show_usage(); |
bb_show_usage(); |
35 |
} |
} |
36 |
|
|
37 |
s = bb_get_last_path_component(*++argv); |
/* It should strip slash: /abc/def/ -> def */ |
38 |
|
s = bb_get_last_path_component_strip(*++argv); |
39 |
|
|
40 |
|
m = strlen(s); |
41 |
if (*++argv) { |
if (*++argv) { |
42 |
n = strlen(*argv); |
n = strlen(*argv); |
|
m = strlen(s); |
|
43 |
if ((m > n) && ((strcmp)(s+m-n, *argv) == 0)) { |
if ((m > n) && ((strcmp)(s+m-n, *argv) == 0)) { |
44 |
s[m-n] = '\0'; |
m -= n; |
45 |
|
/*s[m] = '\0'; - redundant */ |
46 |
} |
} |
47 |
} |
} |
48 |
|
|
49 |
puts(s); |
/* puts(s) will do, but we can do without stdio this way: */ |
50 |
|
s[m++] = '\n'; |
51 |
fflush_stdout_and_exit(EXIT_SUCCESS); |
/* NB: != is correct here: */ |
52 |
|
return full_write(STDOUT_FILENO, s, m) != (ssize_t)m; |
53 |
} |
} |