1 /* basename - return nondirectory portion of pathname */
3 /* See Makefile for compilation details. */
7 #if defined (HAVE_UNISTD_H)
16 basename_builtin (list)
19 int slen, sufflen, off;
20 char *string, *suffix, *fn;
28 if (no_options (list))
31 string = list->word->word;
32 suffix = (char *)NULL;
36 suffix = list->word->word;
45 slen = strlen (string);
47 /* Strip trailing slashes */
48 while (slen > 0 && string[slen - 1] == '/')
51 /* (2) If string consists entirely of slash characters, string shall be
52 set to a single slash character. In this case, skip steps (3)
56 fputs ("/\n", stdout);
57 return (EXECUTION_SUCCESS);
60 /* (3) If there are any trailing slash characters in string, they
64 /* (4) If there are any slash characters remaining in string, the prefix
65 of string up to an including the last slash character in string
68 if (string[slen] == '/')
71 fn = string + slen + 1;
73 /* (5) If the suffix operand is present, is not identical to the
74 characters remaining in string, and is identical to a suffix
75 of the characters remaining in string, the suffix suffix
76 shall be removed from string. Otherwise, string shall not be
77 modified by this step. */
80 sufflen = strlen (suffix);
85 if (strcmp (fn + off, suffix) == 0)
90 return (EXECUTION_SUCCESS);
93 char *basename_doc[] = {
94 "Return non-directory portion of pathname.",
96 "The STRING is converted to a filename corresponding to the last",
97 "pathname component in STRING. If the suffix string SUFFIX is",
98 "supplied, it is removed.",
102 /* The standard structure describing a builtin command. bash keeps an array
103 of these structures. */
104 struct builtin basename_struct = {
105 "basename", /* builtin name */
106 basename_builtin, /* function implementing the builtin */
107 BUILTIN_ENABLED, /* initial flags for builtin */
108 basename_doc, /* array of long documentation strings. */
109 "basename string [suffix]", /* usage synopsis */
110 0 /* reserved for internal use */