1 /* basename - print last part of a path Authors: B. Garfolo & P. Nelson */
3 /* Basename - print the last part of a path.
5 * For MINIX -- Conforms to POSIX - P1003.2/D10
6 * Exception -- it ignores the LC environment variables.
8 * Original MINIX author: Blaine Garfolo
9 * POSIX rewrite author: Philip A. Nelson
11 * POSIX version - October 20, 1990
12 * Feb 14, 1991: changed rindex to strrchr. (PAN)
23 _PROTOTYPE(int main
, (int argc
, char **argv
));
29 char *result_string
; /* The pointer into argv[1]. */
30 char *temp
; /* Used to move around in argv[1]. */
31 int suffix_len
; /* Length of the suffix. */
32 int suffix_start
; /* Where the suffix should start. */
35 /* Check for the correct number of arguments. */
36 if ((argc
< 2) || (argc
> 3)) {
37 fprintf(stderr
, "Usage: basename string [suffix] \n");
41 /* Check for all /'s */
42 for (temp
= argv
[1]; *temp
== '/'; temp
++) /* Move to next char. */
49 /* Build the basename. */
50 result_string
= argv
[1];
52 /* Find the last /'s */
53 temp
= strrchr(result_string
, '/');
56 /* Remove trailing /'s. */
57 while ((*(temp
+ 1) == EOS
) && (*temp
== '/')) *temp
-- = EOS
;
59 /* Set result_string to last part of path. */
60 if (*temp
!= '/') temp
= strrchr(result_string
, '/');
61 if (temp
!= NULL
&& *temp
== '/') result_string
= temp
+ 1;
64 /* Remove the suffix, if any. */
66 suffix_len
= strlen(argv
[2]);
67 suffix_start
= strlen(result_string
) - suffix_len
;
69 if (strcmp(result_string
+ suffix_start
, argv
[2]) == EOS
)
70 *(result_string
+ suffix_start
) = EOS
;
73 /* Print the resultant string. */
74 printf("%s\n", result_string
);