#include "config.h"
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <stdio.h>
#include "builtins.h"
#include "shell.h"
dirname_builtin (list)
WORD_LIST *list;
{
int slen;
char *string;
if (list == 0 || list->next)
{
builtin_usage ();
return (EX_USAGE);
}
if (no_options (list))
return (EX_USAGE);
string = list->word->word;
slen = strlen (string);
while (slen > 0 && string[slen - 1] == '/')
slen--;
if (slen == 0)
{
fputs ("/\n", stdout);
return (EXECUTION_SUCCESS);
}
string[slen] = '\0';
while (--slen >= 0)
if (string[slen] == '/')
break;
if (slen < 0)
{
fputs (".\n", stdout);
return (EXECUTION_SUCCESS);
}
while (--slen >= 0)
if (string[slen] != '/')
break;
string[++slen] = '\0';
printf ("%s\n", (slen == 0) ? "/" : string);
return (EXECUTION_SUCCESS);
}
char *dirname_doc[] = {
"The STRING is converted to the name of the directory containing",
"the filename corresponding to the last pathname component in STRING.",
(char *)NULL
};
struct builtin dirname_struct = {
"dirname",
dirname_builtin,
BUILTIN_ENABLED,
dirname_doc,
"dirname string",
0
};