1 /* dirname - return directory portion of pathname */
3 /* See Makefile for compilation details. */
7 #if defined (HAVE_UNISTD_H)
15 dirname_builtin (list
)
21 if (list
== 0 || list
->next
)
27 if (no_options (list
))
30 string
= list
->word
->word
;
31 slen
= strlen (string
);
33 /* Strip trailing slashes */
34 while (slen
> 0 && string
[slen
- 1] == '/')
37 /* (2) If string consists entirely of slash characters, string shall be
38 set to a single slash character. In this case, skip steps (3)
42 fputs ("/\n", stdout
);
43 return (EXECUTION_SUCCESS
);
46 /* (3) If there are any trailing slash characters in string, they
50 /* (4) If there are no slash characters remaining in string, string
51 shall be set to a single period character. In this case, skip
52 steps (5) through (8).
54 (5) If there are any trailing nonslash characters in string,
55 they shall be removed. */
58 if (string
[slen
] == '/')
63 fputs (".\n", stdout
);
64 return (EXECUTION_SUCCESS
);
67 /* (7) If there are any trailing slash characters in string, they
70 if (string
[slen
] != '/')
72 string
[++slen
] = '\0';
74 /* (8) If the remaining string is empty, string shall be set to a single
76 printf ("%s\n", (slen
== 0) ? "/" : string
);
77 return (EXECUTION_SUCCESS
);
80 char *dirname_doc
[] = {
81 "The STRING is converted to the name of the directory containing",
82 "the filename corresponding to the last pathname component in STRING.",
86 /* The standard structure describing a builtin command. bash keeps an array
87 of these structures. */
88 struct builtin dirname_struct
= {
89 "dirname", /* builtin name */
90 dirname_builtin
, /* function implementing the builtin */
91 BUILTIN_ENABLED
, /* initial flags for builtin */
92 dirname_doc
, /* array of long documentation strings. */
93 "dirname string", /* usage synopsis */
94 0 /* reserved for internal use */