4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
40 main(int argc
, char **argv
)
55 * For better performance, defer the setlocale()/textdomain()
56 * calls until they get really required.
58 #if !defined(TEXT_DOMAIN)
59 #define TEXT_DOMAIN "SYS_TEST"
67 if (strcmp(argv
[1], "--") == 0) {
77 (void) setlocale(LC_ALL
, "");
78 (void) textdomain(TEXT_DOMAIN
);
79 (void) fputs(gettext("Usage: basename string [ suffix ]\n"),
85 suffix
= (argc
== 2) ? NULL
: argv
[2];
87 if (*string
== '\0') {
92 /* remove trailing slashes */
93 p
= string
+ strlen(string
) - 1;
94 while (p
>= string
&& *p
== '/')
97 if (*string
== '\0') {
102 /* skip to one past last slash */
103 if ((p
= strrchr(string
, '/')) != NULL
)
106 if (suffix
== NULL
) {
113 * if a suffix is present and is not the same as the remaining
114 * string and is identical to the last characters in the remaining
115 * string, remove those characters from the string.
117 if (strcmp(string
, suffix
) != 0) {
118 p
= string
+ strlen(string
) - strlen(suffix
);
119 if (strcmp(p
, suffix
) == 0)
125 (void) setlocale(LC_ALL
, "");
126 (void) textdomain(TEXT_DOMAIN
);
128 suf_len
= 6 + strlen(suffix
) + 1 + 1; /* \(.*\)suffix$ */
129 if (suf_len
> sizeof (suf_buf
)) {
130 suf_pat
= malloc(suf_len
);
131 if (suf_pat
== NULL
) {
132 (void) fputs("malloc failed\n", stderr
);
138 (void) strcpy(suf_pat
, "\\(.*\\)");
139 (void) strcpy(suf_pat
+ 6, suffix
);
140 *(suf_pat
+ suf_len
- 1 - 1) = '$';
141 *(suf_pat
+ suf_len
- 1) = '\0';
143 r
= regcomp(®
, suf_pat
, 0);
145 (void) fprintf(stderr
,
146 "Internal error: regcomp failed for \"%s\"\n",
150 r
= regexec(®
, string
, 2, pmatch
, 0);
152 if (pmatch
[0].rm_so
== (regoff_t
)-1 ||
153 pmatch
[1].rm_so
== (regoff_t
)-1 ||
154 pmatch
[1].rm_so
!= 0) {
155 (void) fprintf(stderr
, "Internal error: regexec did "
156 "not set sub-expression for:\n");
157 (void) fprintf(stderr
, "path: \"%s\"\n", string
);
158 (void) fprintf(stderr
, "pattern: \"%s\"", suf_pat
);
161 if (pmatch
[1].rm_so
== pmatch
[1].rm_eo
) {
162 /* a null string matched */
163 (void) printf("%s\n", string
);
166 string
[pmatch
[1].rm_eo
] = '\0';