1 /* dirname - return directory portion of pathname */
3 /* See Makefile for compilation details. */
6 Copyright (C) 1999-2009 Free Software Foundation, Inc.
8 This file is part of GNU Bash.
9 Bash 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 Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.
25 #if defined (HAVE_UNISTD_H)
34 dirname_builtin (list)
40 if (list == 0 || list->next)
46 if (no_options (list))
49 string = list->word->word;
50 slen = strlen (string);
52 /* Strip trailing slashes */
53 while (slen > 0 && string[slen - 1] == '/')
56 /* (2) If string consists entirely of slash characters, string shall be
57 set to a single slash character. In this case, skip steps (3)
61 fputs ("/\n", stdout);
62 return (EXECUTION_SUCCESS);
65 /* (3) If there are any trailing slash characters in string, they
69 /* (4) If there are no slash characters remaining in string, string
70 shall be set to a single period character. In this case, skip
71 steps (5) through (8).
73 (5) If there are any trailing nonslash characters in string,
74 they shall be removed. */
77 if (string[slen] == '/')
82 fputs (".\n", stdout);
83 return (EXECUTION_SUCCESS);
86 /* (7) If there are any trailing slash characters in string, they
89 if (string[slen] != '/')
91 string[++slen] = '\0';
93 /* (8) If the remaining string is empty, string shall be set to a single
95 printf ("%s\n", (slen == 0) ? "/" : string);
96 return (EXECUTION_SUCCESS);
99 char *dirname_doc[] = {
100 "Display directory portion of pathname.",
102 "The STRING is converted to the name of the directory containing",
103 "the filename corresponding to the last pathname component in STRING.",
107 /* The standard structure describing a builtin command. bash keeps an array
108 of these structures. */
109 struct builtin dirname_struct = {
110 "dirname", /* builtin name */
111 dirname_builtin, /* function implementing the builtin */
112 BUILTIN_ENABLED, /* initial flags for builtin */
113 dirname_doc, /* array of long documentation strings. */
114 "dirname string", /* usage synopsis */
115 0 /* reserved for internal use */