2 * Copyright (c) 1985, 1987, 1988 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ** The two things date knows about time are. . .
26 #define TM_YEAR_BASE 1900
27 #endif /* !defined TM_YEAR_BASE */
31 #endif /* !defined SECSPERMIN */
33 extern char ** environ;
36 extern char * tzname[2];
38 static int retval = EXIT_SUCCESS;
40 static void display(const char *, time_t);
41 static void dogmt(void);
42 static void errensure(void);
43 static void timeout(FILE *, const char *, const struct tm *);
44 static void usage(void);
47 main(const int argc, char *argv[])
49 register const char * format;
50 register const char * cp;
52 register bool rflag = false;
58 setlocale(LC_ALL, "");
59 #endif /* defined(LC_ALL) */
62 bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
63 #endif /* defined(TEXTDOMAINDIR) */
64 textdomain(TZ_DOMAIN);
65 #endif /* HAVE_GETTEXT */
68 while ((ch = getopt(argc, argv, "ucr:")) != EOF && ch != -1) {
72 case 'u': /* do it in UT */
76 case 'r': /* seconds since 1970 */
79 _("date: error: multiple -r's used"));
84 secs = strtoimax (optarg, &endarg, 0);
85 if (*endarg || optarg == endarg)
87 else if (! (time_t_min <= secs && secs <= time_t_max))
98 while (optind < argc) {
105 _("date: error: multiple formats in command line\n"));
109 fprintf(stderr, _("date: unknown operand: %s\n"), cp);
121 static char ** fakeenv;
123 if (fakeenv == NULL) {
127 static char tzegmt0[] = "TZ=GMT0";
129 for (n = 0; environ[n] != NULL; ++n)
131 fakeenv = malloc((n + 2) * sizeof *fakeenv);
132 if (fakeenv == NULL) {
133 perror(_("Memory exhausted"));
138 fakeenv[to++] = tzegmt0;
139 for (from = 1; environ[from] != NULL; ++from)
140 if (strncmp(environ[from], "TZ=", 3) != 0)
141 fakeenv[to++] = environ[from];
150 if (retval == EXIT_SUCCESS)
151 retval = EXIT_FAILURE;
158 _("date: usage: date [-u] [-c] [-r seconds]"
165 display(char const *format, time_t now)
169 tmp = localtime(&now);
172 _("date: error: time out of range\n"));
176 timeout(stdout, format ? format : "%+", tmp);
180 if (ferror(stdout) || ferror(stderr)) {
182 _("date: error: couldn't write results\n"));
190 timeout(FILE *fp, char const *format, struct tm const *tmp)
200 fprintf(stderr, _("date: error: time out of range\n"));
211 _("date: error: can't get memory\n"));
216 result = strftime(cp, size, format, tmp);
217 if (result != 0 || cp[0] == '\0')
220 cp = realloc(cp, size);
222 fwrite(cp, 1, result, fp);