5 |
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
6 |
* |
* |
7 |
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
|
* |
|
8 |
*/ |
*/ |
9 |
|
|
|
/* BB_AUDIT SUSv3 compliant */ |
|
|
/* http://www.opengroup.org/onlinepubs/007904975/utilities/basename.html */ |
|
|
|
|
|
|
|
10 |
/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
11 |
* |
* |
12 |
* Changes: |
* Changes: |
15 |
* 3) Save some space by using strcmp(). Calling strncmp() here was silly. |
* 3) Save some space by using strcmp(). Calling strncmp() here was silly. |
16 |
*/ |
*/ |
17 |
|
|
18 |
|
/* BB_AUDIT SUSv3 compliant */ |
19 |
|
/* http://www.opengroup.org/onlinepubs/007904975/utilities/basename.html */ |
20 |
|
|
21 |
|
//kbuild:lib-$(CONFIG_BASENAME) += basename.o |
22 |
|
|
23 |
|
//config:config BASENAME |
24 |
|
//config: bool "basename" |
25 |
|
//config: default y |
26 |
|
//config: help |
27 |
|
//config: basename is used to strip the directory and suffix from filenames, |
28 |
|
//config: leaving just the filename itself. Enable this option if you wish |
29 |
|
//config: to enable the 'basename' utility. |
30 |
|
|
31 |
#include "libbb.h" |
#include "libbb.h" |
32 |
|
|
33 |
/* This is a NOFORK applet. Be very careful! */ |
/* This is a NOFORK applet. Be very careful! */ |
38 |
size_t m, n; |
size_t m, n; |
39 |
char *s; |
char *s; |
40 |
|
|
41 |
if (((unsigned int)(argc-2)) >= 2) { |
if ((unsigned)(argc-2) >= 2) { |
42 |
bb_show_usage(); |
bb_show_usage(); |
43 |
} |
} |
44 |
|
|
48 |
m = strlen(s); |
m = strlen(s); |
49 |
if (*++argv) { |
if (*++argv) { |
50 |
n = strlen(*argv); |
n = strlen(*argv); |
51 |
if ((m > n) && ((strcmp)(s+m-n, *argv) == 0)) { |
if ((m > n) && (strcmp(s+m-n, *argv) == 0)) { |
52 |
m -= n; |
m -= n; |
53 |
/*s[m] = '\0'; - redundant */ |
/*s[m] = '\0'; - redundant */ |
54 |
} |
} |