Debian Codegraph @

src/cronotest.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* ====================================================================
2 * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
20 *
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * [email protected].
25 *
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Group.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Group and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Group and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
55 *
56 */
57 /*
58 * cronotest -- test harness for cronoutils
59 *
60 * Copyright (c) 1996-1999 by Ford & Mason Ltd
61 *
62 * This software was submitted by Ford & Mason Ltd to the Apache
63 * Software Foundation in December 1999. Future revisions and
64 * derivatives of this source code must acknowledge Ford & Mason Ltd
65 * as the original contributor of this module. All other licensing
66 * and usage conditions are those of the Apache Software Foundation.
67 *
68 * Originally written by Andrew Ford <[email protected]>
69 *
70 * Usage:
71 *
72 * cronotest [OPTIONS] template count
73 */
74
75 #include "cronoutils.h"
76 #include "getopt.h"
77
78
79 #define VERSION_MSG "%s: test program for " PACKAGE " version " VERSION "\n"
80
81 #define USAGE_MSG "usage: %s [OPTIONS] template count\n" \
82 "\n"\
83 " -a, --american American date formats\n" \
84 " -e, --european European date formats (default)\n" \
85 " -p PERIOD, --period=PERIOD set the rotation period explicitly\n" \
86 " -d DELAY, --delay=DELAY set the rotation period delay\n" \
87 " -s, --start-time=TIME starting time\n" \
88 " -z TZ, --time-zone=TZ use TZ for timezone\n" \
89 " -h, --help print this help, then exit\n" \
90 " -v, --verbose print verbose messages\n" \
91 " -V, --version print version number, then exit\n" \
92 "\n" \
93 "Starting time can be expressed in one of the following formats:\n" \
94 "\n" \
95 " dd month year [HH:MM[:SS]]\n" \
96 " dd mm year [HH:MM[:SS]]\n" \
97 "\n" \
98 "If American date formats are selected then the day and month \n" \
99 "specifiers are transposed.\n" \
100 "\n"
101
102
103 char *short_options = "ad:ep:s:z:hVvD";
104 struct option long_options[] =
105 {
106 { "american", no_argument, NULL, 'a' },
107 { "european", no_argument, NULL, 'e' },
108 { "start-time", required_argument, NULL, 's' },
109 { "time-zone", required_argument, NULL, 'z' },
110 { "period", required_argument, NULL, 'p' },
111 { "delay", required_argument, NULL, 'd' },
112 { "test-subdirs", required_argument, NULL, 'D' },
113 { "help", no_argument, NULL, 'h' },
114 { "version", no_argument, NULL, 'V' },
•115 { "verbose", no_argument, NULL, 'v' },
116 { NULL, 0, NULL, 0 }
117 };
118
119
120 /* Test harness for determine_periodicity and start_of_this/next_period
121 */
122 int
123 main(int argc, char **argv, char **envp)
124 {
125 PERIODICITY periodicity = UNKNOWN;
126 PERIODICITY period_delay_units = UNKNOWN;
127 int period_multiple = 1;
128 int period_delay = 0;
129 int use_american_date_formats = 0;
130 time_t time_now = time(NULL);
131 struct tm *tm;
132 char *start_time = NULL;
133 char *template;
134 int ch;
135 int n;
136 int i;
137 char buf[BUFSIZE];
138 char filename[MAX_PATH];
139 int test_subdir_creation = 0;
140
141 debug_file = stdout;
142
143 while ((ch = getopt_long(argc, argv, short_options, long_options, NULL)) != EOF)
144 {
145 switch (ch)
146 {
147 case 'a':
148 use_american_date_formats = 1;
149 break;
150
151 case 'e':
152 use_american_date_formats = 0;
153 break;
154
155 case 'd':
156 period_delay_units = parse_timespec(optarg, &period_delay);
157 break;
158
159 case 'p':
160 periodicity = parse_timespec(optarg, &period_multiple);
161 if ( (periodicity == INVALID_PERIOD)
162 || (periodicity == PER_SECOND) && (60 % period_multiple)
163 || (periodicity == PER_MINUTE) && (60 % period_multiple)
164 || (periodicity == HOURLY) && (24 % period_multiple)
165 || (periodicity == DAILY) && (period_multiple > 365)
166 || (periodicity == WEEKLY) && (period_multiple > 52)
167 || (periodicity == MONTHLY) && (12 % period_multiple)) {
168 fprintf(stderr, "%s: invalid explicit period specification (%s)\n", argv[0], start_time);
169 exit(1);
170 }
171 break;
172
173 case 's':
174 start_time = optarg;
175 break;
176
177 case 'z':
178 sprintf(buf, "TZ=%s", optarg);
179 putenv(buf);
180 break;
181
182 case 'V':
183 fprintf(stderr, VERSION_MSG, argv[0]);
184 exit(0);
185
186 case 'D':
187 test_subdir_creation++;
188 break;
189
190 case 'v':
191 debug_file = stdout;
192 break;
193
194 case 'h':
195 case '?':
196 fprintf(stderr, USAGE_MSG, argv[0]);
197 exit(1);
198 }
199 }
200
201 if (optind != argc - 2)
202 {
203 fprintf(stderr, USAGE_MSG, argv[0]);
204 exit(1);
205 }
206
207 if (start_time)
208 {
209 time_now = parse_time(start_time, use_american_date_formats);
210 if (time_now == -1)
211 {
212 fprintf(stderr, "%s: invalid start time (%s)\n", argv[0], start_time);
213 exit(1);
214 }
215 }
216
217
218 template = argv[optind++];
219 n = atoi(argv[optind]);
220 if (periodicity == UNKNOWN) {
221 periodicity = determine_periodicity(template);
222 }
223
224
225 if (period_delay) {
226 if ( (period_delay_units > periodicity)
227 || ( period_delay_units == periodicity
228 && abs(period_delay) >= period_multiple)) {
229 fprintf(stderr, "%s: period delay cannot be larger than the rollover period\n", argv[0], start_time);
230 exit(1);
231 }
232 period_delay *= period_seconds[period_delay_units];
233 }
234
235 printf("Rotation period is per %d %s\n", period_multiple, periods[periodicity]);
236
237 tm = localtime(&time_now);
238 strftime(buf, sizeof (buf), "%c %Z", tm);
239 printf("Start time is %s (%ld)\n", buf, time_now);
240
241 for (i = 1; i <= n; i++)
242 {
243 tm = localtime(&time_now);
244 strftime(buf, sizeof (buf), "%c %Z", tm);
245 printf("Period %d starts at %s (%ld): ", i, buf, time_now);
246 time_now = start_of_this_period(time_now, periodicity, period_multiple);
247 tm = localtime(&time_now);
248 strftime(filename, MAX_PATH, template, tm);
249 printf("\"%s\"\n", filename);
250 if (test_subdir_creation)
251 {
252 create_subdirs(filename);
253 }
254 time_now = start_of_next_period(time_now, periodicity, period_multiple) + period_delay;
255 }
256
257 return 0;
258 }
259
260
261