dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / basename / basename.c
blobd6a02f2e443b4af645208d2024f79ec9dd402b46
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
23 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
29 #include <locale.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 int
35 main(int argc, char **argv)
37 char *p;
38 char *string;
39 char *suffix;
42 * For better performance, defer the setlocale()/textdomain()
43 * calls until they get really required.
45 #if !defined(TEXT_DOMAIN)
46 #define TEXT_DOMAIN "SYS_TEST"
47 #endif
48 if (argc == 1) {
49 (void) puts(".");
50 return (0);
53 if (strcmp(argv[1], "--") == 0) {
54 argv++;
55 argc--;
56 if (argc == 1) {
57 (void) puts(".");
58 return (0);
62 if (argc > 3) {
63 (void) setlocale(LC_ALL, "");
64 (void) textdomain(TEXT_DOMAIN);
65 (void) fputs(gettext("Usage: basename string [ suffix ]\n"),
66 stderr);
67 return (1);
70 string = argv[1];
71 suffix = (argc == 2) ? NULL : argv[2];
73 if (*string == '\0') {
74 (void) puts(".");
75 return (0);
78 /* remove trailing slashes */
79 p = string + strlen(string) - 1;
80 while (p >= string && *p == '/')
81 *p-- = '\0';
83 if (*string == '\0') {
84 (void) puts("/");
85 return (0);
88 /* skip to one past last slash */
89 if ((p = strrchr(string, '/')) != NULL)
90 string = p + 1;
92 if (suffix == NULL) {
93 (void) puts(string);
94 return (0);
98 * if a suffix is present and is not the same as the remaining
99 * string and is identical to the last characters in the remaining
100 * string, remove those characters from the string.
102 if (strcmp(string, suffix) != 0) {
103 p = string + strlen(string) - strlen(suffix);
104 if (strcmp(p, suffix) == 0)
105 *p = '\0';
107 (void) puts(string);
108 return (0);