diff options
Diffstat (limited to 'coreutils/basename.c')
-rw-r--r-- | coreutils/basename.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/coreutils/basename.c b/coreutils/basename.c new file mode 100644 index 000000000..30f76dc12 --- /dev/null +++ b/coreutils/basename.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini basename implementation for busybox | ||
4 | * | ||
5 | * 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. | ||
8 | * | ||
9 | */ | ||
10 | |||
11 | /* BB_AUDIT SUSv3 compliant */ | ||
12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/basename.html */ | ||
13 | |||
14 | |||
15 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) | ||
16 | * | ||
17 | * Changes: | ||
18 | * 1) Now checks for too many args. Need at least one and at most two. | ||
19 | * 2) Don't check for options, as per SUSv3. | ||
20 | * 3) Save some space by using strcmp(). Calling strncmp() here was silly. | ||
21 | */ | ||
22 | |||
23 | #include <stdlib.h> | ||
24 | #include <stdio.h> | ||
25 | #include <string.h> | ||
26 | #include "busybox.h" | ||
27 | |||
28 | int basename_main(int argc, char **argv) | ||
29 | { | ||
30 | size_t m, n; | ||
31 | char *s; | ||
32 | |||
33 | if (((unsigned int)(argc-2)) >= 2) { | ||
34 | bb_show_usage(); | ||
35 | } | ||
36 | |||
37 | s = bb_get_last_path_component(*++argv); | ||
38 | |||
39 | if (*++argv) { | ||
40 | n = strlen(*argv); | ||
41 | m = strlen(s); | ||
42 | if ((m > n) && ((strcmp)(s+m-n, *argv) == 0)) { | ||
43 | s[m-n] = '\0'; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | puts(s); | ||
48 | |||
49 | fflush_stdout_and_exit(EXIT_SUCCESS); | ||
50 | } | ||