xref: /openbsd/regress/lib/libc/time/strptime/main.c (revision 09467b48)
1 /*	$OpenBSD: main.c,v 1.2 2008/06/26 05:42:05 ray Exp $	*/
2 /*	$NetBSD: main.c,v 1.4 2002/02/21 07:38:18 itojun Exp $	*/
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include <err.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34 
35 int	main(int, char *[]);
36 void	die(void);
37 
38 void
39 die(void)
40 {
41 
42 	if (ferror(stdin))
43 		err(1, "fgetln");
44 	else
45 		errx(1, "input is truncated");
46 }
47 
48 int
49 main(int argc, char *argv[])
50 {
51 	char *p, *title, *buf, *format;
52 	size_t len;
53 	struct tm tm;
54 
55 	for (;;) {
56 		p = fgetln(stdin, &len);
57 		if (p == 0)
58 			die();
59 		title = malloc(len + 1);
60 		memcpy(title, p, len);
61 		title[len] = '\0';
62 
63 		if (!strcmp(title, "EOF\n"))
64 			return(0);
65 		if (title[0] == '#' || title[0] == '\n') {
66 			free(title);
67 			continue;
68 		}
69 
70 		p = fgetln(stdin, &len);
71 		if (p == 0)
72 			die();
73 		buf = malloc(len + 1);
74 		memcpy(buf, p, len);
75 		buf[len] = '\0';
76 
77 		p = fgetln(stdin, &len);
78 		if (p == 0)
79 			die();
80 		format = malloc(len + 1);
81 		memcpy(format, p, len);
82 		format[len] = '\0';
83 
84 		tm.tm_sec = -1;
85 		tm.tm_min = -1;
86 		tm.tm_hour = -1;
87 		tm.tm_mday = -1;
88 		tm.tm_mon = -1;
89 		tm.tm_year = -1;
90 		tm.tm_wday = -1;
91 		tm.tm_yday = -1;
92 
93 		p = strptime(buf, format, &tm);
94 
95 		printf("%s", title);
96 		if (p) {
97 			printf("succeeded\n");
98 			printf("%d %d %d %d %d %d %d %d\n",
99 			    tm.tm_sec, tm.tm_min, tm.tm_hour, tm.tm_mday,
100 			    tm.tm_mon, tm.tm_year, tm.tm_wday, tm.tm_yday);
101 			printf("%s\n", p);
102 		} else {
103 			printf("failed\n");
104 		}
105 
106 		free(title);
107 		free(buf);
108 		free(format);
109 	}
110 }
111