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 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 dirname_builtin (list
)
42 if (no_options (list
))
46 if (list
== 0 || list
->next
)
52 string
= list
->word
->word
;
53 slen
= strlen (string
);
55 /* Strip trailing slashes */
56 while (slen
> 0 && string
[slen
- 1] == '/')
59 /* (2) If string consists entirely of slash characters, string shall be
60 set to a single slash character. In this case, skip steps (3)
64 fputs ("/\n", stdout
);
65 return (EXECUTION_SUCCESS
);
68 /* (3) If there are any trailing slash characters in string, they
72 /* (4) If there are no slash characters remaining in string, string
73 shall be set to a single period character. In this case, skip
74 steps (5) through (8).
76 (5) If there are any trailing nonslash characters in string,
77 they shall be removed. */
80 if (string
[slen
] == '/')
85 fputs (".\n", stdout
);
86 return (EXECUTION_SUCCESS
);
89 /* (7) If there are any trailing slash characters in string, they
92 if (string
[slen
] != '/')
94 string
[++slen
] = '\0';
96 /* (8) If the remaining string is empty, string shall be set to a single
98 printf ("%s\n", (slen
== 0) ? "/" : string
);
99 return (EXECUTION_SUCCESS
);
102 char *dirname_doc
[] = {
103 "Display directory portion of pathname.",
105 "The STRING is converted to the name of the directory containing",
106 "the filename corresponding to the last pathname component in STRING.",
110 /* The standard structure describing a builtin command. bush keeps an array
111 of these structures. */
112 struct builtin dirname_struct
= {
113 "dirname", /* builtin name */
114 dirname_builtin
, /* function implementing the builtin */
115 BUILTIN_ENABLED
, /* initial flags for builtin */
116 dirname_doc
, /* array of long documentation strings. */
117 "dirname string", /* usage synopsis */
118 0 /* reserved for internal use */