| 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 |
}; |
| 117 |
|
| 118 |
|
| 119 |
/* Test harness for determine_periodicity and start_of_this/next_period |
| 120 |
*/ |
| 121 |
int |
| 122 |
main(int argc, char **argv, char **envp) |
| 123 |
{ |
| 124 |
PERIODICITY periodicity = UNKNOWN; |
| 125 |
PERIODICITY period_delay_units = UNKNOWN; |
| 126 |
int period_multiple = 1; |
| 127 |
int period_delay = 0; |
| 128 |
int use_american_date_formats = 0; |
| 129 |
time_t time_now = time(NULL); |
| 130 |
struct tm *tm; |
| 131 |
char *start_time = NULL; |
| 132 |
char *template; |
| 133 |
int ch; |
| 134 |
int n; |
| 135 |
int i; |
| 136 |
char buf[BUFSIZE]; |
| 137 |
char filename[MAX_PATH]; |
| 138 |
int test_subdir_creation = 0; |
| 139 |
|
| 140 |
debug_file = stdout; |
| 141 |
|
| 142 |
while ((ch = getopt_long(argc, argv, short_options, long_options, NULL)) != EOF) |
| 143 |
{ |
| 144 |
switch (ch) |
| 145 |
{ |
| 146 |
case 'a': |
| 147 |
use_american_date_formats = 1; |
| 148 |
break; |
| 149 |
|
| 150 |
case 'e': |
| 151 |
use_american_date_formats = 0; |
| 152 |
break; |
| 153 |
|
| 154 |
case 'd': |
| 155 |
period_delay_units = parse_timespec(optarg, &period_delay); |
| 156 |
break; |
| 157 |
|
| 158 |
case 'p': |
| 159 |
periodicity = parse_timespec(optarg, &period_multiple); |
| 160 |
if ( (periodicity == INVALID_PERIOD) |
| 161 |
|| (periodicity == PER_SECOND) && (60 % period_multiple) |
| 162 |
|| (periodicity == PER_MINUTE) && (60 % period_multiple) |
| 163 |
|| (periodicity == HOURLY) && (24 % period_multiple) |
| 164 |
|| (periodicity == DAILY) && (period_multiple > 365) |
| 165 |
|| (periodicity == WEEKLY) && (period_multiple > 52) |
| 166 |
|| (periodicity == MONTHLY) && (12 % period_multiple)) { |
| 167 |
fprintf(stderr, "%s: invalid explicit period specification (%s)\n", argv[0], start_time); |
| 168 |
exit(1); |
| 169 |
} |
| 170 |
break; |
| 171 |
|
| 172 |
case 's': |
| 173 |
start_time = optarg; |
| 174 |
break; |
| 175 |
|
| 176 |
case 'z': |
| 177 |
sprintf(buf, "TZ=%s", optarg); |
| 178 |
putenv(buf); |
| 179 |
break; |
| 180 |
|
| 181 |
case 'V': |
| 182 |
fprintf(stderr, VERSION_MSG, argv[0]); |
| 183 |
exit(0); |
| 184 |
|
| 185 |
case 'D': |
| 186 |
test_subdir_creation++; |
| 187 |
break; |
| 188 |
|
| 189 |
case 'v': |
| 190 |
debug_file = stdout; |
| 191 |
break; |
| 192 |
|
| 193 |
case 'h': |
| 194 |
case '?': |
| 195 |
fprintf(stderr, USAGE_MSG, argv[0]); |
| 196 |
exit(1); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if (optind != argc - 2) |
| 201 |
{ |
| 202 |
fprintf(stderr, USAGE_MSG, argv[0]); |
| 203 |
exit(1); |
| 204 |
} |
| 205 |
|
| 206 |
if (start_time) |
| 207 |
{ |
| 208 |
time_now = parse_time(start_time, use_american_date_formats); |
| 209 |
if (time_now == -1) |
| 210 |
{ |
| 211 |
fprintf(stderr, "%s: invalid start time (%s)\n", argv[0], start_time); |
| 212 |
exit(1); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
template = argv[optind++]; |
| 218 |
n = atoi(argv[optind]); |
| 219 |
if (periodicity == UNKNOWN) { |
| 220 |
periodicity = determine_periodicity(template); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
if (period_delay) { |
| 225 |
if ( (period_delay_units > periodicity) |
| 226 |
|| ( period_delay_units == periodicity |
| 227 |
&& abs(period_delay) >= period_multiple)) { |
| 228 |
fprintf(stderr, "%s: period delay cannot be larger than the rollover period\n", argv[0], start_time); |
| 229 |
exit(1); |
| 230 |
} |
| 231 |
period_delay *= period_seconds[period_delay_units]; |
| 232 |
} |
| 233 |
|
| 234 |
printf("Rotation period is per %d %s\n", period_multiple, periods[periodicity]); |
| 235 |
|
| 236 |
tm = localtime(&time_now); |
| 237 |
strftime(buf, sizeof (buf), "%c %Z", tm); |
| 238 |
printf("Start time is %s (%ld)\n", buf, time_now); |
| 239 |
|
| 240 |
for (i = 1; i <= n; i++) |
| 241 |
{ |
| 242 |
tm = localtime(&time_now); |
| 243 |
strftime(buf, sizeof (buf), "%c %Z", tm); |
| 244 |
printf("Period %d starts at %s (%ld): ", i, buf, time_now); |
| 245 |
time_now = start_of_this_period(time_now, periodicity, period_multiple); |
| 246 |
tm = localtime(&time_now); |
| 247 |
strftime(filename, MAX_PATH, template, tm); |
| 248 |
printf("\"%s\"\n", filename); |
| 249 |
if (test_subdir_creation) |
| 250 |
{ |
| 251 |
create_subdirs(filename); |
| 252 |
} |
| 253 |
time_now = start_of_next_period(time_now, periodicity, period_multiple) + period_delay; |
| 254 |
} |
| 255 |
|
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|