]> git.ipfire.org Git - thirdparty/bash.git/blame - examples/loadables/strftime.c
Imported from ../bash-4.0-rc1.tar.gz.
[thirdparty/bash.git] / examples / loadables / strftime.c
CommitLineData
b80f6443
JA
1/* strftime - loadable builtin interface to strftime(3) */
2
3/* See Makefile for compilation details. */
4
3185942a
JA
5/*
6 Copyright (C) 1999-2009 Free Software Foundation, Inc.
7
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.
13
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.
18
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/>.
21*/
22
b80f6443
JA
23#include <config.h>
24
25#if defined (HAVE_UNISTD_H)
26# include <unistd.h>
27#endif
28
29#include "bashtypes.h"
30#include "posixtime.h"
31
32#include <stdio.h>
33
34#include "builtins.h"
35#include "shell.h"
36#include "common.h"
37
38int
39strftime_builtin (list)
40 WORD_LIST *list;
41{
42 char *format, *tbuf;
43 size_t tbsize, tsize;
44 time_t secs;
45 struct tm *t;
46 int n;
47 intmax_t i;
48
49 if (list == 0)
50 {
51 builtin_usage ();
52 return (EX_USAGE);
53 }
54
55 if (no_options (list))
56 return (EX_USAGE);
57
58 format = list->word->word;
59 if (format == 0 || *format == 0)
60 {
61 printf ("\n");
62 return (EXECUTION_SUCCESS);
63 }
64
65 list = list->next;
66
67 if (list && list->word->word)
68 {
69 n = legal_number (list->word->word, &i);
70 if (n == 0 || i < 0 || i != (time_t)i)
71 {
72 sh_invalidnum (list->word->word);
73 return (EXECUTION_FAILURE);
74 }
75 else
76 secs = i;
77 }
78 else
79 secs = NOW;
80
81 t = localtime (&secs);
82
83 tbsize = strlen (format) * 4;
84 tbuf = 0;
85
86 /* Now try to figure out how big the buffer should really be. strftime(3)
87 will return the number of bytes placed in the buffer unless it's greater
88 than MAXSIZE, in which case it returns 0. */
89 for (n = 1; n < 4; n++)
90 {
91 tbuf = xrealloc (tbuf, tbsize * n);
92 tsize = strftime (tbuf, tbsize * n, format, t);
93 if (tsize)
94 break;
95 }
96
97 printf ("%s\n", tbuf);
98 free (tbuf);
99
100 return (EXECUTION_SUCCESS);
101}
102
103/* An array of strings forming the `long' documentation for a builtin xxx,
104 which is printed by `help xxx'. It must end with a NULL. */
105char *strftime_doc[] = {
3185942a
JA
106 "Display formatted time.",
107 "",
b80f6443
JA
108 "Converts date and time format to a string and displays it on the",
109 "standard output. If the optional second argument is supplied, it",
110 "is used as the number of seconds since the epoch to use in the",
111 "conversion, otherwise the current time is used.",
112 (char *)NULL
113};
114
115/* The standard structure describing a builtin command. bash keeps an array
116 of these structures. The flags must include BUILTIN_ENABLED so the
117 builtin can be used. */
118struct builtin strftime_struct = {
119 "strftime", /* builtin name */
120 strftime_builtin, /* function implementing the builtin */
121 BUILTIN_ENABLED, /* initial flags for builtin */
122 strftime_doc, /* array of long documentation strings. */
123 "strftime format [seconds]", /* usage synopsis; becomes short_doc */
124 0 /* reserved for internal use */
125};