1 /* basename - return nondirectory portion of pathname */
3 /* See Makefile for compilation details. */
7 #if defined (HAVE_UNISTD_H)
15 basename_builtin (list)
18 int slen, sufflen, off;
19 char *string, *suffix, *fn;
27 if (no_options (list))
30 string = list->word->word;
31 suffix = (char *)NULL;
35 suffix = list->word->word;
44 slen = strlen (string);
46 /* Strip trailing slashes */
47 while (slen > 0 && string[slen - 1] == '/')
50 /* (2) If string consists entirely of slash characters, string shall be
51 set to a single slash character. In this case, skip steps (3)
55 fputs ("/\n", stdout);
56 return (EXECUTION_SUCCESS);
59 /* (3) If there are any trailing slash characters in string, they
63 /* (4) If there are any slash characters remaining in string, the prefix
64 of string up to an including the last slash character in string
67 if (string[slen] == '/')
70 fn = string + slen + 1;
72 /* (5) If the suffix operand is present, is not identical to the
73 characters remaining in string, and is identical to a suffix
74 of the characters remaining in string, the suffix suffix
75 shall be removed from string. Otherwise, string shall not be
76 modified by this step. */
79 sufflen = strlen (suffix);
84 if (strcmp (fn + off, suffix) == 0)
89 return (EXECUTION_SUCCESS);
92 char *basename_doc[] = {
93 "The STRING is converted to a filename corresponding to the last",
94 "pathname component in STRING. If the suffix string SUFFIX is",
95 "supplied, it is removed.",
99 /* The standard structure describing a builtin command. bash keeps an array
100 of these structures. */
101 struct builtin basename_struct = {
102 "basename", /* builtin name */
103 basename_builtin, /* function implementing the builtin */
104 BUILTIN_ENABLED, /* initial flags for builtin */
105 basename_doc, /* array of long documentation strings. */
106 "basename string [suffix]", /* usage synopsis */
107 0 /* reserved for internal use */