| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 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 |
|
| 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 |
|