#include <config.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "bashtypes.h"
#include "posixtime.h"
#include <stdio.h>
#include "builtins.h"
#include "shell.h"
#include "common.h"
int
strftime_builtin (list)
WORD_LIST *list;
{
char *format, *tbuf;
size_t tbsize, tsize;
time_t secs;
struct tm *t;
int n;
intmax_t i;
if (list == 0)
{
builtin_usage ();
return (EX_USAGE);
}
if (no_options (list))
return (EX_USAGE);
format = list->word->word;
if (format == 0 || *format == 0)
{
printf ("\n");
return (EXECUTION_SUCCESS);
}
list = list->next;
if (list && list->word->word)
{
n = legal_number (list->word->word, &i);
if (n == 0 || i < 0 || i != (time_t)i)
{
sh_invalidnum (list->word->word);
return (EXECUTION_FAILURE);
}
else
secs = i;
}
else
secs = NOW;
t = localtime (&secs);
tbsize = strlen (format) * 4;
tbuf = 0;
for (n = 1; n < 4; n++)
{
tbuf = xrealloc (tbuf, tbsize * n);
tsize = strftime (tbuf, tbsize * n, format, t);
if (tsize)
break;
}
printf ("%s\n", tbuf);
free (tbuf);
return (EXECUTION_SUCCESS);
}
char *strftime_doc[] = {
"Converts date and time format to a string and displays it on the",
"standard output. If the optional second argument is supplied, it",
"is used as the number of seconds since the epoch to use in the",
"conversion, otherwise the current time is used.",
(char *)NULL
};
struct builtin strftime_struct = {
"strftime",
strftime_builtin,
BUILTIN_ENABLED,
strftime_doc,
"strftime format [seconds]",
0
};