1 /* basename - return nondirectory portion of pathname */
3 /* See Makefile for compilation details. */
6 Copyright (C) 1999-2020 Free Software Foundation, Inc.
8 This file is part of GNU Bush.
9 Bush is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Bush is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Bush. If not, see <http://www.gnu.org/licenses/>.
25 #if defined (HAVE_UNISTD_H)
33 #include "bushgetopt.h"
36 basename_builtin (list
)
39 int slen
, sufflen
, off
;
40 char *string
, *suffix
, *fn
;
42 if (no_options (list
))
51 string
= list
->word
->word
;
52 suffix
= (char *)NULL
;
56 suffix
= list
->word
->word
;
65 slen
= strlen (string
);
67 /* Strip trailing slashes */
68 while (slen
> 0 && string
[slen
- 1] == '/')
71 /* (2) If string consists entirely of slash characters, string shall be
72 set to a single slash character. In this case, skip steps (3)
76 fputs ("/\n", stdout
);
77 return (EXECUTION_SUCCESS
);
80 /* (3) If there are any trailing slash characters in string, they
84 /* (4) If there are any slash characters remaining in string, the prefix
85 of string up to an including the last slash character in string
88 if (string
[slen
] == '/')
91 fn
= string
+ slen
+ 1;
93 /* (5) If the suffix operand is present, is not identical to the
94 characters remaining in string, and is identical to a suffix
95 of the characters remaining in string, the suffix suffix
96 shall be removed from string. Otherwise, string shall not be
97 modified by this step. */
100 sufflen
= strlen (suffix
);
104 off
= slen
- sufflen
;
105 if (strcmp (fn
+ off
, suffix
) == 0)
110 return (EXECUTION_SUCCESS
);
113 char *basename_doc
[] = {
114 "Return non-directory portion of pathname.",
116 "The STRING is converted to a filename corresponding to the last",
117 "pathname component in STRING. If the suffix string SUFFIX is",
118 "supplied, it is removed.",
122 /* The standard structure describing a builtin command. bush keeps an array
123 of these structures. */
124 struct builtin basename_struct
= {
125 "basename", /* builtin name */
126 basename_builtin
, /* function implementing the builtin */
127 BUILTIN_ENABLED
, /* initial flags for builtin */
128 basename_doc
, /* array of long documentation strings. */
129 "basename string [suffix]", /* usage synopsis */
130 0 /* reserved for internal use */