GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/zic/zic.c Lines: 948 1254 75.6 %
Date: 2017-11-13 Branches: 702 994 70.6 %

Line Branch Exec Source
1
/*	$OpenBSD: zic.c,v 1.22 2016/03/15 19:50:47 millert Exp $	*/
2
/*
3
** This file is in the public domain, so clarified as of
4
** 2006-07-17 by Arthur David Olson.
5
*/
6
7
#include <sys/types.h>
8
#include <sys/wait.h>
9
#include <sys/stat.h>
10
#include <ctype.h>
11
#include <err.h>
12
#include <errno.h>
13
#include <limits.h>
14
#include <locale.h>
15
#include <stdio.h>
16
#include <stdlib.h>
17
#include <string.h>
18
#include <stdint.h>
19
#include <unistd.h>
20
#include <time.h>
21
22
#include "tzfile.h"
23
24
#define TRUE	1
25
#define FALSE	0
26
27
#define TYPE_SIGNED(type) (((type) -1) < 0)
28
29
#define YEARSPERREPEAT	400	/* years before a Gregorian repeat */
30
31
#define GRANDPARENTED   "Local time zone must be set--see zic manual page"
32
33
#define	ZIC_VERSION	'2'
34
35
typedef int_fast64_t	zic_t;
36
37
#ifndef ZIC_MAX_ABBR_LEN_WO_WARN
38
#define ZIC_MAX_ABBR_LEN_WO_WARN	6
39
#endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
40
41
#define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
42
43
#define OFFSET_STRLEN_MAXIMUM	(7 + INT_STRLEN_MAXIMUM(long))
44
#define RULE_STRLEN_MAXIMUM	8	/* "Mdd.dd.d" */
45
46
#define end(cp, n)	(memchr((cp), '\0', (n)))
47
48
struct rule {
49
	const char 	*r_filename;
50
	int		r_linenum;
51
	const char 	*r_name;
52
53
	int		r_loyear;	/* for example, 1986 */
54
	int		r_hiyear;	/* for example, 1986 */
55
	const char 	*r_yrtype;
56
	int		r_lowasnum;
57
	int		r_hiwasnum;
58
59
	int		r_month;	/* 0..11 */
60
61
	int		r_dycode;	/* see below */
62
	int		r_dayofmonth;
63
	int		r_wday;
64
65
	long		r_tod;		/* time from midnight */
66
	int		r_todisstd;	/* above is standard time if TRUE */
67
					/* or wall clock time if FALSE */
68
	int		r_todisgmt;	/* above is GMT if TRUE */
69
					/* or local time if FALSE */
70
	long		r_stdoff;	/* offset from standard time */
71
	const char 	*r_abbrvar;	/* variable part of abbreviation */
72
73
	int		r_todo;		/* a rule to do (used in outzone) */
74
	zic_t		r_temp;		/* used in outzone */
75
};
76
77
/*
78
**	r_dycode		r_dayofmonth	r_wday
79
*/
80
81
#define DC_DOM		0	/* 1..31 */	/* unused */
82
#define DC_DOWGEQ	1	/* 1..31 */	/* 0..6 (Sun..Sat) */
83
#define DC_DOWLEQ	2	/* 1..31 */	/* 0..6 (Sun..Sat) */
84
85
struct zone {
86
	const char 	*z_filename;
87
	int		z_linenum;
88
89
	const char 	*z_name;
90
	long		z_gmtoff;
91
	const char 	*z_rule;
92
	const char 	*z_format;
93
94
	long		z_stdoff;
95
96
	struct rule 	*z_rules;
97
	int		z_nrules;
98
99
	struct rule	z_untilrule;
100
	zic_t		z_untiltime;
101
};
102
103
static void	addtt(zic_t starttime, int type);
104
static int	addtype(long gmtoff, const char *abbr, int isdst,
105
	    int ttisstd, int ttisgmt);
106
static void	leapadd(zic_t t, int positive, int rolling, int count);
107
static void	adjleap(void);
108
static void	associate(void);
109
static void	convert(long val, char *buf);
110
static void	convert64(zic_t val, char *buf);
111
static void	dolink(const char *fromfield, const char *tofield);
112
static void	doabbr(char *abbr, size_t size, const char *format,
113
	    const char *letters, int isdst, int doquotes);
114
static void	eat(const char *name, int num);
115
static void	eats(const char *name, int num, const char *rname, int rnum);
116
static long	eitol(int i);
117
static void	error(const char *message);
118
static char	**getfields(char *buf);
119
static long	gethms(const char *string, const char *errstrng, int signable);
120
static void	infile(const char *filename);
121
static void	inleap(char **fields, int nfields);
122
static void	inlink(char **fields, int nfields);
123
static void	inrule(char **fields, int nfields);
124
static int	inzcont(char **fields, int nfields);
125
static int	inzone(char **fields, int nfields);
126
static int	inzsub(char **fields, int nfields, int iscont);
127
static int	is32(zic_t x);
128
static int	itsabbr(const char *abbr, const char *word);
129
static int	itsdir(const char *name);
130
static int	mkdirs(char *filename);
131
static void	newabbr(const char *abbr);
132
static long	oadd(long t1, long t2);
133
static void	outzone(const struct zone *zp, int ntzones);
134
static void	puttzcode(long code, FILE *fp);
135
static void	puttzcode64(zic_t code, FILE *fp);
136
static int	rcomp(const void *leftp, const void *rightp);
137
static zic_t	rpytime(const struct rule *rp, int wantedy);
138
static void	rulesub(struct rule *rp, const char *loyearp, const char *hiyearp,
139
	    const char *typep, const char *monthp,
140
	    const char *dayp, const char *timep);
141
static int 	stringoffset(char *result, size_t size, long offset);
142
static int	stringrule(char *result, size_t size, const struct rule *rp,
143
	    long dstoff, long gmtoff);
144
static void 	stringzone(char *result, size_t size,
145
	    const struct zone *zp, int ntzones);
146
static void	setboundaries(void);
147
static zic_t	tadd(zic_t t1, long t2);
148
static void	usage(void);
149
static void	writezone(const char *name, const char *string);
150
static int	yearistype(int year, const char *type);
151
152
extern char 	*__progname;
153
154
static int		charcnt;
155
static int		errors;
156
static const char 	*filename;
157
static int		leapcnt;
158
static int		leapseen;
159
static int		leapminyear;
160
static int		leapmaxyear;
161
static int		linenum;
162
static int		max_abbrvar_len;
163
static int		max_format_len;
164
static zic_t		max_time;
165
static int		max_year;
166
static zic_t		min_time;
167
static int		min_year;
168
static int		noise;
169
static const char 	*rfilename;
170
static int		rlinenum;
171
static int		timecnt;
172
static int		typecnt;
173
174
/*
175
** Line codes.
176
*/
177
178
#define LC_RULE		0
179
#define LC_ZONE		1
180
#define LC_LINK		2
181
#define LC_LEAP		3
182
183
/*
184
** Which fields are which on a Zone line.
185
*/
186
187
#define ZF_NAME		1
188
#define ZF_GMTOFF	2
189
#define ZF_RULE		3
190
#define ZF_FORMAT	4
191
#define ZF_TILYEAR	5
192
#define ZF_TILMONTH	6
193
#define ZF_TILDAY	7
194
#define ZF_TILTIME	8
195
#define ZONE_MINFIELDS	5
196
#define ZONE_MAXFIELDS	9
197
198
/*
199
** Which fields are which on a Zone continuation line.
200
*/
201
202
#define ZFC_GMTOFF	0
203
#define ZFC_RULE	1
204
#define ZFC_FORMAT	2
205
#define ZFC_TILYEAR	3
206
#define ZFC_TILMONTH	4
207
#define ZFC_TILDAY	5
208
#define ZFC_TILTIME	6
209
#define ZONEC_MINFIELDS	3
210
#define ZONEC_MAXFIELDS	7
211
212
/*
213
** Which files are which on a Rule line.
214
*/
215
216
#define RF_NAME		1
217
#define RF_LOYEAR	2
218
#define RF_HIYEAR	3
219
#define RF_COMMAND	4
220
#define RF_MONTH	5
221
#define RF_DAY		6
222
#define RF_TOD		7
223
#define RF_STDOFF	8
224
#define RF_ABBRVAR	9
225
#define RULE_FIELDS	10
226
227
/*
228
** Which fields are which on a Link line.
229
*/
230
231
#define LF_FROM		1
232
#define LF_TO		2
233
#define LINK_FIELDS	3
234
235
/*
236
** Which fields are which on a Leap line.
237
*/
238
239
#define LP_YEAR		1
240
#define LP_MONTH	2
241
#define LP_DAY		3
242
#define LP_TIME		4
243
#define LP_CORR		5
244
#define LP_ROLL		6
245
#define LEAP_FIELDS	7
246
247
/*
248
** Year synonyms.
249
*/
250
251
#define YR_MINIMUM	0
252
#define YR_MAXIMUM	1
253
#define YR_ONLY		2
254
255
static struct rule 	*rules;
256
static int		nrules;	/* number of rules */
257
258
static struct zone 	*zones;
259
static int		nzones;	/* number of zones */
260
261
struct link {
262
	const char 	*l_filename;
263
	int		l_linenum;
264
	const char 	*l_from;
265
	const char 	*l_to;
266
};
267
268
static struct link 	*links;
269
static int		nlinks;
270
271
struct lookup {
272
	const char 	*l_word;
273
	const int	l_value;
274
};
275
276
static struct lookup const 	*byword(const char *string, const struct lookup *lp);
277
278
static struct lookup const	line_codes[] = {
279
	{ "Rule",	LC_RULE },
280
	{ "Zone",	LC_ZONE },
281
	{ "Link",	LC_LINK },
282
	{ "Leap",	LC_LEAP },
283
	{ NULL,		0}
284
};
285
286
static struct lookup const	mon_names[] = {
287
	{ "January",	TM_JANUARY },
288
	{ "February",	TM_FEBRUARY },
289
	{ "March",	TM_MARCH },
290
	{ "April",	TM_APRIL },
291
	{ "May",	TM_MAY },
292
	{ "June",	TM_JUNE },
293
	{ "July",	TM_JULY },
294
	{ "August",	TM_AUGUST },
295
	{ "September",	TM_SEPTEMBER },
296
	{ "October",	TM_OCTOBER },
297
	{ "November",	TM_NOVEMBER },
298
	{ "December",	TM_DECEMBER },
299
	{ NULL,		0 }
300
};
301
302
static struct lookup const	wday_names[] = {
303
	{ "Sunday",	TM_SUNDAY },
304
	{ "Monday",	TM_MONDAY },
305
	{ "Tuesday",	TM_TUESDAY },
306
	{ "Wednesday",	TM_WEDNESDAY },
307
	{ "Thursday",	TM_THURSDAY },
308
	{ "Friday",	TM_FRIDAY },
309
	{ "Saturday",	TM_SATURDAY },
310
	{ NULL,		0 }
311
};
312
313
static struct lookup const	lasts[] = {
314
	{ "last-Sunday",	TM_SUNDAY },
315
	{ "last-Monday",	TM_MONDAY },
316
	{ "last-Tuesday",	TM_TUESDAY },
317
	{ "last-Wednesday",	TM_WEDNESDAY },
318
	{ "last-Thursday",	TM_THURSDAY },
319
	{ "last-Friday",	TM_FRIDAY },
320
	{ "last-Saturday",	TM_SATURDAY },
321
	{ NULL,			0 }
322
};
323
324
static struct lookup const	begin_years[] = {
325
	{ "minimum",	YR_MINIMUM },
326
	{ "maximum",	YR_MAXIMUM },
327
	{ NULL,		0 }
328
};
329
330
static struct lookup const	end_years[] = {
331
	{ "minimum",	YR_MINIMUM },
332
	{ "maximum",	YR_MAXIMUM },
333
	{ "only",	YR_ONLY },
334
	{ NULL,		0 }
335
};
336
337
static struct lookup const	leap_types[] = {
338
	{ "Rolling",	TRUE },
339
	{ "Stationary",	FALSE },
340
	{ NULL,		0 }
341
};
342
343
static const int	len_months[2][MONSPERYEAR] = {
344
	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
345
	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
346
};
347
348
static const int	len_years[2] = {
349
	DAYSPERNYEAR, DAYSPERLYEAR
350
};
351
352
static struct attype {
353
	zic_t		at;
354
	unsigned char	type;
355
}			attypes[TZ_MAX_TIMES];
356
357
static long		gmtoffs[TZ_MAX_TYPES];
358
static char		isdsts[TZ_MAX_TYPES];
359
static unsigned char	abbrinds[TZ_MAX_TYPES];
360
static char		ttisstds[TZ_MAX_TYPES];
361
static char		ttisgmts[TZ_MAX_TYPES];
362
static char		chars[TZ_MAX_CHARS];
363
static zic_t		trans[TZ_MAX_LEAPS];
364
static long		corr[TZ_MAX_LEAPS];
365
static char		roll[TZ_MAX_LEAPS];
366
367
/*
368
** Memory allocation.
369
*/
370
371
static void *
372
memcheck(void *ptr)
373
{
374
239360
	if (ptr == NULL)
375
		err(1, "Memory exhausted");
376
119680
	return ptr;
377
}
378
379
static char *
380
ecatalloc(char *start, const char *tail)
381
{
382
	size_t len;
383
	char *str;
384
385
8512
	len = strlen(start) + strlen(tail) + 1;
386
4256
	str = memcheck(realloc(start, len));
387
4256
	strlcat(str, tail, len);
388
4256
	return str;
389
}
390
391
#define emalloc(size)		memcheck(malloc(size))
392
#define ereallocarray(ptr, nmemb, size)		memcheck(reallocarray(ptr, nmemb, size))
393
#define erealloc(ptr, size)	memcheck(realloc((ptr), (size)))
394
#define ecpyalloc(ptr)		memcheck(strdup(ptr))
395
396
/*
397
** Error handling.
398
*/
399
400
static void
401
eats(const char *name, int num, const char *rname, int rnum)
402
{
403
10389828
	filename = name;
404
5194914
	linenum = num;
405
5194914
	rfilename = rname;
406
5194914
	rlinenum = rnum;
407
5194914
}
408
409
static void
410
eat(const char *name, int num)
411
{
412
120450
	eats(name, num, NULL, -1);
413
60225
}
414
415
static void
416
error(const char *string)
417
{
418
	/*
419
	** Match the format of "cc" to allow sh users to
420
	**	zic ... 2>&1 | error -t "*" -v
421
	** on BSD systems.
422
	*/
423
	fprintf(stderr, "\"%s\", line %d: %s",
424
		filename, linenum, string);
425
	if (rfilename != NULL)
426
		fprintf(stderr, " (rule from \"%s\", line %d)",
427
			rfilename, rlinenum);
428
	fprintf(stderr, "\n");
429
	++errors;
430
}
431
432
static void
433
warning(const char *string)
434
{
435
	char 	*cp;
436
437
	cp = ecpyalloc("warning: ");
438
	cp = ecatalloc(cp, string);
439
	error(cp);
440
	free(cp);
441
	--errors;
442
}
443
444
445
static const char *
446
scheck(const char *string, const char *format)
447
{
448
	const char 	*fp, *result;
449
126228
	char 		*fbuf, *tp, dummy;
450
	int		c;
451
452
	result = "";
453
63114
	if (string == NULL || format == NULL)
454
		return result;
455
63114
	fbuf = reallocarray(NULL, strlen(format) + 2, 2);
456
63114
	if (fbuf == NULL)
457
		return result;
458
	fp = format;
459
	tp = fbuf;
460
310866
	while ((*tp++ = c = *fp++) != '\0') {
461
102054
		if (c != '%')
462
19470
			continue;
463
82584
		if (*fp == '%') {
464
			*tp++ = *fp++;
465
			continue;
466
		}
467
82584
		*tp++ = '*';
468
82584
		if (*fp == '*')
469
			++fp;
470
165168
		while (isdigit((unsigned char)*fp))
471
			*tp++ = *fp++;
472

124338
		if (*fp == 'l' || *fp == 'h')
473
40830
			*tp++ = *fp++;
474
41754
		else if (*fp == '[')
475
			do {
476
				*tp++ = *fp++;
477
			} while (*fp != '\0' && *fp != ']');
478
82584
		if ((*tp++ = *fp++) == '\0')
479
			break;
480
	}
481
63114
	*(tp - 1) = '%';
482
63114
	*tp++ = 'c';
483
63114
	*tp = '\0';
484
63114
	if (sscanf(string, fbuf, &dummy) != 1)
485
45111
		result = format;
486
63114
	free(fbuf);
487
63114
	return result;
488
63114
}
489
490
static void
491
usage(void)
492
{
493
	fprintf(stderr,
494
	    "usage: %s [-v] [-d directory] [-L leapsecondfilename] [-l timezone]\n"
495
	    "\t\t[-p timezone] [-y command] [filename ...]\n",
496
		__progname);
497
	exit(EXIT_FAILURE);
498
}
499
500
static const char 	*psxrules;
501
static const char 	*lcltime;
502
static const char 	*directory;
503
static const char 	*leapsec;
504
static const char 	*yitcommand;
505
506
int
507
main(int argc, char **argv)
508
{
509
	int	i, j, c;
510
511
8
	if (pledge("stdio rpath wpath cpath proc exec flock", NULL) == -1)
512
		err(1, "pledge");
513
514
4
	umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
515
32
	while ((c = getopt(argc, argv, "d:l:p:L:vy:")) != -1)
516

12
		switch (c) {
517
			default:
518
				usage();
519
			case 'd':
520
4
				if (directory == NULL)
521
4
					directory = optarg;
522
				else
523
					errx(1, "More than one -d option specified");
524
4
				break;
525
			case 'l':
526
				if (lcltime == NULL)
527
					lcltime = optarg;
528
				else
529
					errx(1, "More than one -l option specified");
530
				break;
531
			case 'p':
532
1
				if (psxrules == NULL)
533
1
					psxrules = optarg;
534
				else
535
					errx(1, "More than one -p option specified");
536
1
				break;
537
			case 'y':
538
4
				if (yitcommand == NULL)
539
4
					yitcommand = optarg;
540
				else
541
					errx(1, "More than one -y option specified");
542
4
				break;
543
			case 'L':
544
3
				if (leapsec == NULL)
545
3
					leapsec = optarg;
546
				else
547
					errx(1, "More than one -L option specified");
548
3
				break;
549
			case 'v':
550
				noise = TRUE;
551
				break;
552
		}
553

4
	if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
554
		usage();	/* usage message by request */
555
4
	if (directory == NULL)
556
		directory = TZDIR;
557
4
	if (yitcommand == NULL)
558
		yitcommand = "yearistype";
559
560
4
	setboundaries();
561
562
4
	if (optind < argc && leapsec != NULL) {
563
3
		infile(leapsec);
564
3
		adjleap();
565
3
	}
566
567
80
	for (i = optind; i < argc; ++i)
568
36
		infile(argv[i]);
569
4
	if (errors)
570
		exit(EXIT_FAILURE);
571
4
	associate();
572
2330
	for (i = 0; i < nzones; i = j) {
573
		/*
574
		** Find the next non-continuation zone entry.
575
		*/
576

18321
		for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
577
			continue;
578
1161
		outzone(&zones[i], j - i);
579
	}
580
	/*
581
	** Make links.
582
	*/
583
1244
	for (i = 0; i < nlinks; ++i) {
584
618
		eat(links[i].l_filename, links[i].l_linenum);
585
618
		dolink(links[i].l_from, links[i].l_to);
586
618
		if (noise)
587
			for (j = 0; j < nlinks; ++j)
588
				if (strcmp(links[i].l_to,
589
					links[j].l_from) == 0)
590
						warning("link to link");
591
	}
592
4
	if (lcltime != NULL) {
593
		eat("command line", 1);
594
		dolink(lcltime, TZDEFAULT);
595
	}
596
4
	if (psxrules != NULL) {
597
1
		eat("command line", 1);
598
1
		dolink(psxrules, TZDEFRULES);
599
1
	}
600
4
	return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
601
}
602
603
static void
604
dolink(const char *fromfield, const char *tofield)
605
{
606
	char 	*fromname, *toname;
607
608
1238
	if (fromfield[0] == '/')
609
		fromname = ecpyalloc(fromfield);
610
	else {
611
619
		fromname = ecpyalloc(directory);
612
619
		fromname = ecatalloc(fromname, "/");
613
619
		fromname = ecatalloc(fromname, fromfield);
614
	}
615
619
	if (tofield[0] == '/')
616
		toname = ecpyalloc(tofield);
617
	else {
618
619
		toname = ecpyalloc(directory);
619
619
		toname = ecatalloc(toname, "/");
620
619
		toname = ecatalloc(toname, tofield);
621
	}
622
	/*
623
	** We get to be careful here since
624
	** there's a fair chance of root running us.
625
	*/
626
619
	if (!itsdir(toname))
627
619
		remove(toname);
628
619
	if (link(fromname, toname) != 0) {
629
		int	result;
630
631
		if (mkdirs(toname) != 0)
632
			exit(EXIT_FAILURE);
633
634
		result = link(fromname, toname);
635
		if (result != 0 && errno == EXDEV)
636
			result = symlink(fromname, toname);
637
		if (result != 0)
638
			err(1, "Can't link from %s to %s", fromname, toname);
639
	}
640
619
	free(fromname);
641
619
	free(toname);
642
619
}
643
644
#define TIME_T_BITS_IN_FILE	64
645
646
static void
647
setboundaries(void)
648
{
649
	int	i;
650
651
8
	min_time = -1;
652
512
	for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
653
252
		min_time *= 2;
654
4
	max_time = -(min_time + 1);
655
4
}
656
657
static int
658
itsdir(const char *name)
659
{
660
	char 	*myname;
661
	int	accres;
662
663
3560
	myname = ecpyalloc(name);
664
1780
	myname = ecatalloc(myname, "/.");
665
1780
	accres = access(myname, F_OK);
666
1780
	free(myname);
667
1780
	return accres == 0;
668
}
669
670
/*
671
** Associate sets of rules with zones.
672
*/
673
674
/*
675
** Sort by rule name.
676
*/
677
678
static int
679
rcomp(const void *cp1, const void *cp2)
680
{
681
104976
	return strcmp(((const struct rule *) cp1)->r_name,
682
34992
		((const struct rule *) cp2)->r_name);
683
}
684
685
static void
686
associate(void)
687
{
688
	struct zone 	*zp;
689
	struct rule 	*rp;
690
	int		base, out, i, j;
691
692
8
	if (nrules != 0) {
693
3
		qsort(rules, nrules, sizeof *rules, rcomp);
694
11568
		for (i = 0; i < nrules - 1; ++i) {
695
17343
			if (strcmp(rules[i].r_name,
696
11562
				rules[i + 1].r_name) != 0)
697
					continue;
698
16110
			if (strcmp(rules[i].r_filename,
699
10740
				rules[i + 1].r_filename) == 0)
700
					continue;
701
			eat(rules[i].r_filename, rules[i].r_linenum);
702
			warning("same rule name in multiple files");
703
			eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
704
			warning("same rule name in multiple files");
705
			for (j = i + 2; j < nrules; ++j) {
706
				if (strcmp(rules[i].r_name,
707
					rules[j].r_name) != 0)
708
						break;
709
				if (strcmp(rules[i].r_filename,
710
					rules[j].r_filename) == 0)
711
						continue;
712
				if (strcmp(rules[i + 1].r_filename,
713
					rules[j].r_filename) == 0)
714
						continue;
715
				break;
716
			}
717
			i = j - 1;
718
		}
719
	}
720
12224
	for (i = 0; i < nzones; ++i) {
721
6108
		zp = &zones[i];
722
6108
		zp->z_rules = NULL;
723
6108
		zp->z_nrules = 0;
724
	}
725
836
	for (base = 0; base < nrules; base = out) {
726
414
		rp = &rules[base];
727
11568
		for (out = base + 1; out < nrules; ++out)
728
5781
			if (strcmp(rp->r_name, rules[out].r_name) != 0)
729
				break;
730
1686636
		for (i = 0; i < nzones; ++i) {
731
842904
			zp = &zones[i];
732
842904
			if (strcmp(zp->z_rule, rp->r_name) != 0)
733
				continue;
734
2394
			zp->z_rules = rp;
735
2394
			zp->z_nrules = out - base;
736
2394
		}
737
	}
738
12224
	for (i = 0; i < nzones; ++i) {
739
6108
		zp = &zones[i];
740
6108
		if (zp->z_nrules == 0) {
741
			/*
742
			** Maybe we have a local standard time offset.
743
			*/
744
3714
			eat(zp->z_filename, zp->z_linenum);
745
3714
			zp->z_stdoff = gethms(zp->z_rule, "unruly zone",
746
				TRUE);
747
			/*
748
			** Note, though, that if there's no rule,
749
			** a '%s' in the format is a bad thing.
750
			*/
751
3714
			if (strchr(zp->z_format, '%') != 0)
752
				error("%s in ruleless zone");
753
		}
754
	}
755
4
	if (errors)
756
		exit(EXIT_FAILURE);
757
4
}
758
759
static void
760
infile(const char *name)
761
{
762
	FILE 			*fp;
763
	char			**fields, *cp;
764
	const struct lookup 	*lp;
765
	int			nfields, wantcont, num;
766
78
	char			buf[BUFSIZ];
767
768
39
	if (strcmp(name, "-") == 0) {
769
		name = "standard input";
770
		fp = stdin;
771
39
	} else if ((fp = fopen(name, "r")) == NULL)
772
		err(1, "Can't open %s", name);
773
	wantcont = FALSE;
774
47648
	for (num = 1; ; ++num) {
775
47648
		eat(name, num);
776
47648
		if (fgets(buf, sizeof buf, fp) != buf)
777
			break;
778
47609
		cp = strchr(buf, '\n');
779
47609
		if (cp == NULL) {
780
			error("line too long");
781
			exit(EXIT_FAILURE);
782
		}
783
47609
		*cp = '\0';
784
47609
		fields = getfields(buf);
785
		nfields = 0;
786
283054
		while (fields[nfields] != NULL) {
787
			static char	nada;
788
789
93918
			if (strcmp(fields[nfields], "-") == 0)
790
10863
				fields[nfields] = &nada;
791
93918
			++nfields;
792
		}
793
47609
		if (nfields == 0) {
794
			/* nothing to do */
795
12537
		} else if (wantcont) {
796
4947
			wantcont = inzcont(fields, nfields);
797
4947
		} else {
798
7590
			lp = byword(fields[0], line_codes);
799
7590
			if (lp == NULL)
800
				error("input line of unknown type");
801

7590
			else switch ((int) (lp->l_value)) {
802
				case LC_RULE:
803
5784
					inrule(fields, nfields);
804
					wantcont = FALSE;
805
5784
					break;
806
				case LC_ZONE:
807
1161
					wantcont = inzone(fields, nfields);
808
1161
					break;
809
				case LC_LINK:
810
618
					inlink(fields, nfields);
811
					wantcont = FALSE;
812
618
					break;
813
				case LC_LEAP:
814
27
					if (name != leapsec)
815
						fprintf(stderr,
816
						    "%s: Leap line in non leap seconds file %s\n",
817
							__progname, name);
818
						/* no exit? */
819
					else
820
27
						inleap(fields, nfields);
821
					wantcont = FALSE;
822
27
					break;
823
				default:	/* "cannot happen" */
824
					errx(1, "panic: Invalid l_value %d", lp->l_value);
825
			}
826
		}
827
47609
		free(fields);
828
	}
829

78
	if (ferror(fp))
830
		errx(1, "Error reading %s", filename);
831

78
	if (fp != stdin && fclose(fp))
832
		err(1, "Error closing %s", filename);
833
39
	if (wantcont)
834
		error("expected continuation line not found");
835
39
}
836
837
/*
838
** Convert a string of one of the forms
839
**	h	-h	hh:mm	-hh:mm	hh:mm:ss	-hh:mm:ss
840
** into a number of seconds.
841
** A null string maps to zero.
842
** Call error with errstring and return zero on errors.
843
*/
844
845
static long
846
gethms(const char *string, const char *errstring, int signable)
847
{
848
52728
	long	hh;
849
26364
	int	mm, ss, sign;
850
851

52728
	if (string == NULL || *string == '\0')
852
3537
		return 0;
853
22827
	if (!signable)
854
10758
		sign = 1;
855
12069
	else if (*string == '-') {
856
		sign = -1;
857
2793
		++string;
858
2793
	} else
859
		sign = 1;
860
22827
	if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
861
6291
		mm = ss = 0;
862
16536
	else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
863
15069
		ss = 0;
864
2934
	else if (sscanf(string, scheck(string, "%ld:%d:%d"),
865
1467
	    &hh, &mm, &ss) != 3) {
866
		error(errstring);
867
		return 0;
868
	}
869
114135
	if (hh < 0 ||
870
45654
	    mm < 0 || mm >= MINSPERHOUR ||
871
45654
	    ss < 0 || ss > SECSPERMIN) {
872
		error(errstring);
873
		return 0;
874
	}
875
22827
	if (LONG_MAX / SECSPERHOUR < hh) {
876
		error("time overflow");
877
		return 0;
878
	}
879
45654
	return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
880
22827
	    eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
881
26364
}
882
883
static void
884
inrule(char **fields, int nfields)
885
{
886
	static struct rule	r;
887
888
11568
	if (nfields != RULE_FIELDS) {
889
		error("wrong number of fields on Rule line");
890
		return;
891
	}
892
5784
	if (*fields[RF_NAME] == '\0') {
893
		error("nameless rule");
894
		return;
895
	}
896
5784
	r.r_filename = filename;
897
5784
	r.r_linenum = linenum;
898
5784
	r.r_stdoff = gethms(fields[RF_STDOFF], "invalid saved time", TRUE);
899
11568
	rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
900
5784
		fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
901
5784
	r.r_name = ecpyalloc(fields[RF_NAME]);
902
5784
	r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
903
5784
	if (max_abbrvar_len < strlen(r.r_abbrvar))
904
9
		max_abbrvar_len = strlen(r.r_abbrvar);
905
5784
	rules = ereallocarray(rules, nrules + 1, sizeof *rules);
906
5784
	rules[nrules++] = r;
907
11568
}
908
909
static int
910
inzone(char **fields, int nfields)
911
{
912
	int	i;
913
	static char 	*buf;
914
	size_t		len;
915
916
2322
	if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
917
		error("wrong number of fields on Zone line");
918
		return FALSE;
919
	}
920
1161
	if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
921
		len = 132 + strlen(TZDEFAULT);
922
		buf = erealloc(buf, len);
923
		snprintf(buf, len,
924
		    "\"Zone %s\" line and -l option are mutually exclusive",
925
		    TZDEFAULT);
926
		error(buf);
927
		return FALSE;
928
	}
929
1161
	if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
930
		len = 132 + strlen(TZDEFRULES);
931
		buf = erealloc(buf, len);
932
		snprintf(buf, len,
933
		    "\"Zone %s\" line and -p option are mutually exclusive",
934
		    TZDEFRULES);
935
		error(buf);
936
		return FALSE;
937
	}
938
2350068
	for (i = 0; i < nzones; ++i)
939

1397946
		if (zones[i].z_name != NULL &&
940
224073
		    strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
941
			len = 132 + strlen(fields[ZF_NAME]) +
942
			    strlen(zones[i].z_filename);
943
			buf = erealloc(buf, len);
944
			snprintf(buf, len,
945
			    "duplicate zone name %s (file \"%s\", line %d)",
946
			    fields[ZF_NAME],
947
			    zones[i].z_filename,
948
			    zones[i].z_linenum);
949
			error(buf);
950
			return FALSE;
951
		}
952
1161
	return inzsub(fields, nfields, FALSE);
953
1161
}
954
955
static int
956
inzcont(char **fields, int nfields)
957
{
958
9894
	if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
959
		error("wrong number of fields on Zone continuation line");
960
		return FALSE;
961
	}
962
4947
	return inzsub(fields, nfields, TRUE);
963
4947
}
964
965
static int
966
inzsub(char **fields, int nfields, int iscont)
967
{
968
	char 		*cp;
969
	static struct zone	z;
970
	int		i_gmtoff, i_rule, i_format;
971
	int		i_untilyear, i_untilmonth;
972
	int		i_untilday, i_untiltime;
973
	int		hasuntil;
974
975
12216
	if (iscont) {
976
		i_gmtoff = ZFC_GMTOFF;
977
		i_rule = ZFC_RULE;
978
		i_format = ZFC_FORMAT;
979
		i_untilyear = ZFC_TILYEAR;
980
		i_untilmonth = ZFC_TILMONTH;
981
		i_untilday = ZFC_TILDAY;
982
		i_untiltime = ZFC_TILTIME;
983
		z.z_name = NULL;
984
4947
	} else {
985
		i_gmtoff = ZF_GMTOFF;
986
		i_rule = ZF_RULE;
987
		i_format = ZF_FORMAT;
988
		i_untilyear = ZF_TILYEAR;
989
		i_untilmonth = ZF_TILMONTH;
990
		i_untilday = ZF_TILDAY;
991
		i_untiltime = ZF_TILTIME;
992
1161
		z.z_name = ecpyalloc(fields[ZF_NAME]);
993
	}
994
6108
	z.z_filename = filename;
995
6108
	z.z_linenum = linenum;
996
6108
	z.z_gmtoff = gethms(fields[i_gmtoff], "invalid UTC offset", TRUE);
997
6108
	if ((cp = strchr(fields[i_format], '%')) != 0) {
998

2868
		if (*++cp != 's' || strchr(cp, '%') != 0) {
999
			error("invalid abbreviation format");
1000
			return FALSE;
1001
		}
1002
	}
1003
6108
	z.z_rule = ecpyalloc(fields[i_rule]);
1004
6108
	z.z_format = ecpyalloc(fields[i_format]);
1005
6108
	if (max_format_len < strlen(z.z_format))
1006
12
		max_format_len = strlen(z.z_format);
1007
6108
	hasuntil = nfields > i_untilyear;
1008
6108
	if (hasuntil) {
1009
4947
		z.z_untilrule.r_filename = filename;
1010
4947
		z.z_untilrule.r_linenum = linenum;
1011
4947
		rulesub(&z.z_untilrule,
1012
4947
			fields[i_untilyear],
1013
			"only",
1014
			"",
1015
13602
			(nfields > i_untilmonth) ?
1016
3708
			fields[i_untilmonth] : "Jan",
1017
13092
			(nfields > i_untilday) ? fields[i_untilday] : "1",
1018
11430
			(nfields > i_untiltime) ? fields[i_untiltime] : "0");
1019
4947
		z.z_untiltime = rpytime(&z.z_untilrule,
1020
4947
			z.z_untilrule.r_loyear);
1021

8856
		if (iscont && nzones > 0 &&
1022
3909
		    z.z_untiltime > min_time &&
1023
3909
		    z.z_untiltime < max_time &&
1024
3909
		    zones[nzones - 1].z_untiltime > min_time &&
1025
3909
		    zones[nzones - 1].z_untiltime < max_time &&
1026
3909
		    zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1027
			error("Zone continuation line end time is not after end time of previous line");
1028
			return FALSE;
1029
		}
1030
	}
1031
6108
	zones = ereallocarray(zones, nzones + 1, sizeof *zones);
1032
6108
	zones[nzones++] = z;
1033
	/*
1034
	** If there was an UNTIL field on this line,
1035
	** there's more information about the zone on the next line.
1036
	*/
1037
6108
	return hasuntil;
1038
6108
}
1039
1040
static void
1041
inleap(char **fields, int nfields)
1042
{
1043
	const char 		*cp;
1044
	const struct lookup 	*lp;
1045
	int			i, j;
1046
54
	int			year, month, day;
1047
	long			dayoff, tod;
1048
	zic_t			t;
1049
1050
27
	if (nfields != LEAP_FIELDS) {
1051
		error("wrong number of fields on Leap line");
1052
		return;
1053
	}
1054
	dayoff = 0;
1055
27
	cp = fields[LP_YEAR];
1056
27
	if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1057
		/*
1058
		** Leapin' Lizards!
1059
		*/
1060
		error("invalid leaping year");
1061
		return;
1062
	}
1063

53
	if (!leapseen || leapmaxyear < year)
1064
26
		leapmaxyear = year;
1065

53
	if (!leapseen || leapminyear > year)
1066
1
		leapminyear = year;
1067
27
	leapseen = TRUE;
1068
	j = EPOCH_YEAR;
1069
1070
	while (j != year) {
1070
508
		if (year > j) {
1071

1145
			i = len_years[isleap(j)];
1072
508
			++j;
1073
508
		} else {
1074
			--j;
1075
			i = -len_years[isleap(j)];
1076
		}
1077
508
		dayoff = oadd(dayoff, eitol(i));
1078
	}
1079
27
	if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1080
		error("invalid month name");
1081
		return;
1082
	}
1083
27
	month = lp->l_value;
1084
	j = TM_JANUARY;
1085
516
	while (j != month) {
1086

521
		i = len_months[isleap(year)][j];
1087
231
		dayoff = oadd(dayoff, eitol(i));
1088
231
		++j;
1089
	}
1090
27
	cp = fields[LP_DAY];
1091

81
	if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1092

88
	    day <= 0 || day > len_months[isleap(year)][month]) {
1093
		error("invalid day of month");
1094
		return;
1095
	}
1096
27
	dayoff = oadd(dayoff, eitol(day - 1));
1097
	if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1098
		error("time before zero");
1099
		return;
1100
	}
1101
27
	if (dayoff < min_time / SECSPERDAY) {
1102
		error("time too small");
1103
		return;
1104
	}
1105
27
	if (dayoff > max_time / SECSPERDAY) {
1106
		error("time too large");
1107
		return;
1108
	}
1109
27
	t = (zic_t) dayoff * SECSPERDAY;
1110
27
	tod = gethms(fields[LP_TIME], "invalid time of day", FALSE);
1111
27
	cp = fields[LP_CORR];
1112
	{
1113
		int	positive;
1114
		int		count;
1115
1116
27
		if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1117
			positive = FALSE;
1118
			count = 1;
1119
27
		} else if (strcmp(cp, "--") == 0) {
1120
			positive = FALSE;
1121
			count = 2;
1122
27
		} else if (strcmp(cp, "+") == 0) {
1123
			positive = TRUE;
1124
			count = 1;
1125
27
		} else if (strcmp(cp, "++") == 0) {
1126
			positive = TRUE;
1127
			count = 2;
1128
		} else {
1129
			error("illegal CORRECTION field on Leap line");
1130
			return;
1131
		}
1132
27
		if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1133
			error("illegal Rolling/Stationary field on Leap line");
1134
			return;
1135
		}
1136
27
		leapadd(tadd(t, tod), positive, lp->l_value, count);
1137
27
	}
1138
54
}
1139
1140
static void
1141
inlink(char **fields, int nfields)
1142
{
1143
	struct link	l;
1144
1145
1236
	if (nfields != LINK_FIELDS) {
1146
		error("wrong number of fields on Link line");
1147
		return;
1148
	}
1149
618
	if (*fields[LF_FROM] == '\0') {
1150
		error("blank FROM field on Link line");
1151
		return;
1152
	}
1153
618
	if (*fields[LF_TO] == '\0') {
1154
		error("blank TO field on Link line");
1155
		return;
1156
	}
1157
618
	l.l_filename = filename;
1158
618
	l.l_linenum = linenum;
1159
618
	l.l_from = ecpyalloc(fields[LF_FROM]);
1160
618
	l.l_to = ecpyalloc(fields[LF_TO]);
1161
618
	links = ereallocarray(links, nlinks + 1, sizeof *links);
1162
618
	links[nlinks++] = l;
1163
1236
}
1164
1165
static void
1166
rulesub(struct rule * const rp, const char * const loyearp,
1167
    const char * const hiyearp, const char * const typep,
1168
    const char * const monthp, const char * const dayp,
1169
    const char * const timep)
1170
{
1171
	const struct lookup 	*lp;
1172
	const char 		*cp;
1173
	char 			*dp, *ep;
1174
1175
21462
	if ((lp = byword(monthp, mon_names)) == NULL) {
1176
		error("invalid month name");
1177
		return;
1178
	}
1179
10731
	rp->r_month = lp->l_value;
1180
10731
	rp->r_todisstd = FALSE;
1181
10731
	rp->r_todisgmt = FALSE;
1182
10731
	dp = ecpyalloc(timep);
1183
10731
	if (*dp != '\0') {
1184
13128
		ep = dp + strlen(dp) - 1;
1185

13128
		switch (tolower((unsigned char)*ep)) {
1186
		case 's':	/* Standard */
1187
2160
			rp->r_todisstd = TRUE;
1188
2160
			rp->r_todisgmt = FALSE;
1189
			*ep = '\0';
1190
2160
			break;
1191
		case 'w':	/* Wall */
1192
			rp->r_todisstd = FALSE;
1193
			rp->r_todisgmt = FALSE;
1194
			*ep = '\0';
1195
			break;
1196
		case 'g':	/* Greenwich */
1197
		case 'u':	/* Universal */
1198
		case 'z':	/* Zulu */
1199
237
			rp->r_todisstd = TRUE;
1200
237
			rp->r_todisgmt = TRUE;
1201
			*ep = '\0';
1202
237
			break;
1203
		}
1204
2397
	}
1205
10731
	rp->r_tod = gethms(dp, "invalid time of day", FALSE);
1206
10731
	free(dp);
1207
	/*
1208
	** Year work.
1209
	*/
1210
	cp = loyearp;
1211
10731
	lp = byword(cp, begin_years);
1212
10731
	rp->r_lowasnum = lp == NULL;
1213

10737
	if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1214
		case YR_MINIMUM:
1215
			rp->r_loyear = INT_MIN;
1216
6
			break;
1217
		case YR_MAXIMUM:
1218
			rp->r_loyear = INT_MAX;
1219
			break;
1220
		default:	/* "cannot happen" */
1221
			errx(1, "panic: Invalid l_value %d", lp->l_value);
1222
10731
	} else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1223
		error("invalid starting year");
1224
		return;
1225
	}
1226
	cp = hiyearp;
1227
10731
	lp = byword(cp, end_years);
1228
10731
	rp->r_hiwasnum = lp == NULL;
1229

19581
	if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1230
		case YR_MINIMUM:
1231
			rp->r_hiyear = INT_MIN;
1232
			break;
1233
		case YR_MAXIMUM:
1234
			rp->r_hiyear = INT_MAX;
1235
204
			break;
1236
		case YR_ONLY:
1237
8646
			rp->r_hiyear = rp->r_loyear;
1238
8646
			break;
1239
		default:	/* "cannot happen" */
1240
			errx(1, "panic: Invalid l_value %d", lp->l_value);
1241
10731
	} else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1242
		error("invalid ending year");
1243
		return;
1244
	}
1245
10731
	if (rp->r_loyear > rp->r_hiyear) {
1246
		error("starting year greater than ending year");
1247
		return;
1248
	}
1249
10731
	if (*typep == '\0')
1250
		rp->r_yrtype = NULL;
1251
	else {
1252
		if (rp->r_loyear == rp->r_hiyear) {
1253
			error("typed single year");
1254
			return;
1255
		}
1256
		rp->r_yrtype = ecpyalloc(typep);
1257
	}
1258
	/*
1259
	** Day work.
1260
	** Accept things such as:
1261
	**	1
1262
	**	last-Sunday
1263
	**	Sun<=20
1264
	**	Sun>=7
1265
	*/
1266
10731
	dp = ecpyalloc(dayp);
1267
10731
	if ((lp = byword(dp, lasts)) != NULL) {
1268
1107
		rp->r_dycode = DC_DOWLEQ;
1269
1107
		rp->r_wday = lp->l_value;
1270
1107
		rp->r_dayofmonth = len_months[1][rp->r_month];
1271
1107
	} else {
1272
9624
		if ((ep = strchr(dp, '<')) != 0)
1273
			rp->r_dycode = DC_DOWLEQ;
1274
9624
		else if ((ep = strchr(dp, '>')) != 0)
1275
1230
			rp->r_dycode = DC_DOWGEQ;
1276
		else {
1277
			ep = dp;
1278
			rp->r_dycode = DC_DOM;
1279
		}
1280
9624
		if (rp->r_dycode != DC_DOM) {
1281
1230
			*ep++ = 0;
1282
1230
			if (*ep++ != '=') {
1283
				error("invalid day of month");
1284
				free(dp);
1285
				return;
1286
			}
1287
1230
			if ((lp = byword(dp, wday_names)) == NULL) {
1288
				error("invalid weekday name");
1289
				free(dp);
1290
				return;
1291
			}
1292
1230
			rp->r_wday = lp->l_value;
1293
1230
		}
1294

19248
		if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1295
9624
		    rp->r_dayofmonth <= 0 ||
1296
9624
		    (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1297
			error("invalid day of month");
1298
			free(dp);
1299
			return;
1300
		}
1301
	}
1302
10731
	free(dp);
1303
21462
}
1304
1305
static void
1306
convert(long val, char *buf)
1307
{
1308
	int	i;
1309
	int	shift;
1310
1311
1493250
	for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1312
543000
		buf[i] = val >> shift;
1313
135750
}
1314
1315
static void
1316
convert64(zic_t val, char *buf)
1317
{
1318
	int	i;
1319
	int	shift;
1320
1321
1947310
	for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1322
819920
		buf[i] = val >> shift;
1323
102490
}
1324
1325
static void
1326
puttzcode(long val, FILE *fp)
1327
{
1328
243636
	char	buf[4];
1329
1330
121818
	convert(val, buf);
1331
121818
	fwrite(buf, sizeof buf, 1, fp);
1332
121818
}
1333
1334
static void
1335
puttzcode64(zic_t val, FILE *fp)
1336
{
1337
204980
	char	buf[8];
1338
1339
102490
	convert64(val, buf);
1340
102490
	fwrite(buf, sizeof buf, 1, fp);
1341
102490
}
1342
1343
static int
1344
atcomp(const void *avp, const void *bvp)
1345
{
1346
1749250
	const zic_t	a = ((const struct attype *) avp)->at;
1347
874625
	const zic_t	b = ((const struct attype *) bvp)->at;
1348
1349
2371659
	return (a < b) ? -1 : (a > b);
1350
}
1351
1352
static int
1353
is32(zic_t x)
1354
{
1355
34046
	return INT32_MIN <= x && x <= INT32_MAX;
1356
}
1357
1358
static void
1359
writezone(const char *name, const char *string)
1360
{
1361
	FILE 			*fp;
1362
	int			i, j;
1363
	int			leapcnt32, leapi32;
1364
	int			timecnt32, timei32;
1365
	int			pass;
1366
	static char 		*fullname;
1367
	static const struct tzhead	tzh0;
1368
	static struct tzhead	tzh;
1369
2322
	zic_t			ats[TZ_MAX_TIMES];
1370
1161
	unsigned char		types[TZ_MAX_TIMES];
1371
	size_t			len;
1372
1373
	/*
1374
	** Sort.
1375
	*/
1376
1161
	if (timecnt > 1)
1377
981
		qsort(attypes, timecnt, sizeof *attypes, atcomp);
1378
	/*
1379
	** Optimize.
1380
	*/
1381
	{
1382
		int	fromi;
1383
		int	toi;
1384
1385
		toi = 0;
1386
		fromi = 0;
1387

3384
		while (fromi < timecnt && attypes[fromi].at < min_time)
1388
			++fromi;
1389
1161
		if (isdsts[0] == 0)
1390

3321
			while (fromi < timecnt && attypes[fromi].type == 0)
1391
3
				++fromi;	/* handled by default rule */
1392
188498
		for ( ; fromi < timecnt; ++fromi) {
1393

369166
			if (toi != 0 && ((attypes[fromi].at +
1394
184052
			    gmtoffs[attypes[toi - 1].type]) <=
1395
275082
			    (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0 :
1396
91030
			    attypes[toi - 2].type]))) {
1397
276
				attypes[toi - 1].type = attypes[fromi].type;
1398
276
				continue;
1399
			}
1400

184562
			if (toi == 0 ||
1401
91750
			    attypes[toi - 1].type != attypes[fromi].type)
1402
92041
				attypes[toi++] = attypes[fromi];
1403
		}
1404
1161
		timecnt = toi;
1405
	}
1406
	/*
1407
	** Transfer.
1408
	*/
1409
186404
	for (i = 0; i < timecnt; ++i) {
1410
92041
		ats[i] = attypes[i].at;
1411
92041
		types[i] = attypes[i].type;
1412
	}
1413
	/*
1414
	** Correct for leap seconds.
1415
	*/
1416
186404
	for (i = 0; i < timecnt; ++i) {
1417
92041
		j = leapcnt;
1418
753398
		while (--j >= 0)
1419
309802
			if (ats[i] > trans[j] - corr[j]) {
1420
25144
				ats[i] = tadd(ats[i], corr[j]);
1421
25144
				break;
1422
			}
1423
	}
1424
	/*
1425
	** Figure out 32-bit-limited starts and counts.
1426
	*/
1427
	timecnt32 = timecnt;
1428
	timei32 = 0;
1429
1161
	leapcnt32 = leapcnt;
1430
	leapi32 = 0;
1431

44160
	while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1432
13603
		--timecnt32;
1433

5115
	while (timecnt32 > 0 && !is32(ats[timei32])) {
1434
588
		--timecnt32;
1435
588
		++timei32;
1436
	}
1437

2709
	while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1438
		--leapcnt32;
1439

2709
	while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1440
		--leapcnt32;
1441
		++leapi32;
1442
	}
1443
1161
	len = strlen(directory) + 1 + strlen(name) + 1;
1444
1161
	fullname = erealloc(fullname, len);
1445
1161
	snprintf(fullname, len, "%s/%s", directory, name);
1446
	/*
1447
	** Remove old file, if any, to snap links.
1448
	*/
1449

2322
	if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT)
1450
		err(1, "Can't remove %s", fullname);
1451
1161
	if ((fp = fopen(fullname, "wb")) == NULL) {
1452
		if (mkdirs(fullname) != 0)
1453
			exit(EXIT_FAILURE);
1454
		if ((fp = fopen(fullname, "wb")) == NULL)
1455
			err(1, "Can't create %s", fullname);
1456
	}
1457
6966
	for (pass = 1; pass <= 2; ++pass) {
1458
		int	thistimei, thistimecnt;
1459
		int	thisleapi, thisleapcnt;
1460
		int	thistimelim, thisleaplim;
1461
2322
		int	writetype[TZ_MAX_TIMES];
1462
2322
		int	typemap[TZ_MAX_TYPES];
1463
		int	thistypecnt;
1464
2322
		char	thischars[TZ_MAX_CHARS];
1465
		char	thischarcnt;
1466
2322
		int 	indmap[TZ_MAX_CHARS];
1467
1468
2322
		if (pass == 1) {
1469
			thistimei = timei32;
1470
			thistimecnt = timecnt32;
1471
			thisleapi = leapi32;
1472
			thisleapcnt = leapcnt32;
1473
1161
		} else {
1474
			thistimei = 0;
1475
1161
			thistimecnt = timecnt;
1476
			thisleapi = 0;
1477
1161
			thisleapcnt = leapcnt;
1478
		}
1479
2322
		thistimelim = thistimei + thistimecnt;
1480
2322
		thisleaplim = thisleapi + thisleapcnt;
1481
30522
		for (i = 0; i < typecnt; ++i)
1482
12939
			writetype[i] = thistimecnt == timecnt;
1483
2322
		if (thistimecnt == 0) {
1484
			/*
1485
			** No transition times fall in the current
1486
			** (32- or 64-bit) window.
1487
			*/
1488
231
			if (typecnt != 0)
1489
231
				writetype[typecnt - 1] = TRUE;
1490
		} else {
1491
348146
			for (i = thistimei - 1; i < thistimelim; ++i)
1492
171982
				if (i >= 0)
1493
170413
					writetype[types[i]] = TRUE;
1494
			/*
1495
			** For America/Godthab and Antarctica/Palmer
1496
			*/
1497
2091
			if (thistimei == 0)
1498
1569
				writetype[0] = TRUE;
1499
		}
1500
#ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
1501
		/*
1502
		** For some pre-2011 systems: if the last-to-be-written
1503
		** standard (or daylight) type has an offset different from the
1504
		** most recently used offset,
1505
		** append an (unused) copy of the most recently used type
1506
		** (to help get global "altzone" and "timezone" variables
1507
		** set correctly).
1508
		*/
1509
		{
1510
			int	mrudst, mrustd, hidst, histd, type;
1511
1512
			hidst = histd = mrudst = mrustd = -1;
1513
344426
			for (i = thistimei; i < thistimelim; ++i)
1514
169891
				if (isdsts[types[i]])
1515
82847
					mrudst = types[i];
1516
				else
1517
					mrustd = types[i];
1518
30522
			for (i = 0; i < typecnt; ++i)
1519
12939
				if (writetype[i]) {
1520
12312
					if (isdsts[i])
1521
4527
						hidst = i;
1522
					else
1523
						histd = i;
1524
				}
1525

4440
			if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
1526
414
			    gmtoffs[hidst] != gmtoffs[mrudst]) {
1527
84
				isdsts[mrudst] = -1;
1528
168
				type = addtype(gmtoffs[mrudst],
1529
84
				    &chars[abbrinds[mrudst]],
1530
84
				    TRUE, ttisstds[mrudst],
1531
84
				    ttisgmts[mrudst]);
1532
84
				isdsts[mrudst] = TRUE;
1533
84
				writetype[type] = TRUE;
1534
84
			}
1535

4953
			if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
1536
540
			    gmtoffs[histd] != gmtoffs[mrustd]) {
1537
225
				isdsts[mrustd] = -1;
1538
450
				type = addtype(gmtoffs[mrustd],
1539
225
				    &chars[abbrinds[mrustd]],
1540
225
				    FALSE, ttisstds[mrustd],
1541
225
				    ttisgmts[mrustd]);
1542
225
				isdsts[mrustd] = FALSE;
1543
225
				writetype[type] = TRUE;
1544
225
			}
1545
		}
1546
#endif /* !defined LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
1547
		thistypecnt = 0;
1548
31140
		for (i = 0; i < typecnt; ++i)
1549
39117
			typemap[i] = writetype[i] ?  thistypecnt++ : -1;
1550
236844
		for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1551
116100
			indmap[i] = -1;
1552
		thischarcnt = 0;
1553
31140
		for (i = 0; i < typecnt; ++i) {
1554
			char 	*thisabbr;
1555
1556
13248
			if (!writetype[i])
1557
627
				continue;
1558
12621
			if (indmap[abbrinds[i]] >= 0)
1559
3642
				continue;
1560
8979
			thisabbr = &chars[abbrinds[i]];
1561
157170
			for (j = 0; j < thischarcnt; ++j)
1562
69612
				if (strcmp(&thischars[j], thisabbr) == 0)
1563
					break;
1564
8979
			if (j == thischarcnt) {
1565
17946
				strlcpy(&thischars[(int) thischarcnt],
1566
8973
				    thisabbr, sizeof(thischars) - thischarcnt);
1567
8973
				thischarcnt += strlen(thisabbr) + 1;
1568
8973
			}
1569
8979
			indmap[abbrinds[i]] = j;
1570
8979
		}
1571
#define DO(field)	fwrite(tzh.field, sizeof tzh.field, 1, fp)
1572
2322
		tzh = tzh0;
1573
2322
		strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1574
2322
		tzh.tzh_version[0] = ZIC_VERSION;
1575
2322
		convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
1576
2322
		convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
1577
2322
		convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
1578
2322
		convert(eitol(thistimecnt), tzh.tzh_timecnt);
1579
2322
		convert(eitol(thistypecnt), tzh.tzh_typecnt);
1580
2322
		convert(eitol(thischarcnt), tzh.tzh_charcnt);
1581
2322
		DO(tzh_magic);
1582
2322
		DO(tzh_version);
1583
2322
		DO(tzh_reserved);
1584
2322
		DO(tzh_ttisgmtcnt);
1585
2322
		DO(tzh_ttisstdcnt);
1586
2322
		DO(tzh_leapcnt);
1587
2322
		DO(tzh_timecnt);
1588
2322
		DO(tzh_typecnt);
1589
2322
		DO(tzh_charcnt);
1590
#undef DO
1591
344426
		for (i = thistimei; i < thistimelim; ++i)
1592
169891
			if (pass == 1)
1593
77850
				puttzcode((long) ats[i], fp);
1594
			else
1595
92041
				puttzcode64(ats[i], fp);
1596
344426
		for (i = thistimei; i < thistimelim; ++i) {
1597
169891
			unsigned char	uc;
1598
1599
169891
			uc = typemap[types[i]];
1600
169891
			fwrite(&uc, sizeof uc, 1, fp);
1601
169891
		}
1602
31140
		for (i = 0; i < typecnt; ++i)
1603
13248
			if (writetype[i]) {
1604
12621
				puttzcode(gmtoffs[i], fp);
1605
25242
				putc(isdsts[i], fp);
1606
25242
				putc((unsigned char)indmap[abbrinds[i]], fp);
1607
			}
1608
2322
		if (thischarcnt != 0)
1609
2322
			fwrite(thischars, sizeof thischars[0], thischarcnt, fp);
1610
46440
		for (i = thisleapi; i < thisleaplim; ++i) {
1611
			zic_t	todo;
1612
1613
20898
			if (roll[i]) {
1614
				if (timecnt == 0 || trans[i] < ats[0]) {
1615
					j = 0;
1616
					while (isdsts[j])
1617
						if (++j >= typecnt) {
1618
							j = 0;
1619
							break;
1620
						}
1621
				} else {
1622
					j = 1;
1623
					while (j < timecnt &&
1624
					    trans[i] >= ats[j])
1625
						++j;
1626
					j = types[j - 1];
1627
				}
1628
				todo = tadd(trans[i], -gmtoffs[j]);
1629
			} else
1630
20898
				todo = trans[i];
1631
20898
			if (pass == 1)
1632
10449
				puttzcode((long) todo, fp);
1633
			else
1634
10449
				puttzcode64(todo, fp);
1635
20898
			puttzcode(corr[i], fp);
1636
		}
1637
31140
		for (i = 0; i < typecnt; ++i)
1638
13248
			if (writetype[i])
1639
25242
				putc(ttisstds[i], fp);
1640
31140
		for (i = 0; i < typecnt; ++i)
1641
13248
			if (writetype[i])
1642
25242
				putc(ttisgmts[i], fp);
1643
2322
	}
1644
1161
	fprintf(fp, "\n%s\n", string);
1645


3483
	if (ferror(fp) || fclose(fp))
1646
		errx(1, "Error writing %s", fullname);
1647
1161
}
1648
1649
static void
1650
doabbr(char *abbr, size_t size, const char *format, const char *letters,
1651
    int isdst, int doquotes)
1652
{
1653
	char 	*cp, *slashp;
1654
	int	len;
1655
1656
288008
	slashp = strchr(format, '/');
1657
144004
	if (slashp == NULL) {
1658
92531
		if (letters == NULL)
1659
3711
			strlcpy(abbr, format, size);
1660
		else
1661
88820
			snprintf(abbr, size, format, letters);
1662
51473
	} else if (isdst) {
1663
26569
		strlcpy(abbr, slashp + 1, size);
1664
26569
	} else {
1665
24904
		if (slashp - format + 1 < size)
1666
24904
			size = slashp - format + 1;
1667
24904
		strlcpy(abbr, format, size);
1668
	}
1669
144004
	if (!doquotes)
1670
142414
		return;
1671
9528
	for (cp = abbr; *cp != '\0'; ++cp)
1672

4407
		if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1673
618
			strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1674
				break;
1675
1590
	len = strlen(abbr);
1676

3180
	if (len > 0 && *cp == '\0')
1677
975
		return;
1678
615
	abbr[len + 2] = '\0';
1679
615
	abbr[len + 1] = '>';
1680
5064
	for ( ; len > 0; --len)
1681
1917
		abbr[len] = abbr[len - 1];
1682
615
	abbr[0] = '<';
1683
144619
}
1684
1685
static void
1686
updateminmax(int x)
1687
{
1688
128586
	if (min_year > x)
1689
1266
		min_year = x;
1690
64293
	if (max_year < x)
1691
2704
		max_year = x;
1692
64293
}
1693
1694
static int
1695
stringoffset(char *result, size_t size, long offset)
1696
{
1697
	int	hours, minutes, seconds;
1698
	char 	*ep;
1699
1700
2922
	result[0] = '\0';
1701
1461
	if (offset < 0) {
1702
612
		strlcpy(result, "-", size);
1703
612
		offset = -offset;
1704
612
	}
1705
1461
	seconds = offset % SECSPERMIN;
1706
1461
	offset /= SECSPERMIN;
1707
1461
	minutes = offset % MINSPERHOUR;
1708
1461
	offset /= MINSPERHOUR;
1709
1461
	hours = offset;
1710
1461
	if (hours >= HOURSPERDAY) {
1711
3
		result[0] = '\0';
1712
3
		return -1;
1713
	}
1714
1458
	ep = end(result, size);
1715
1458
	snprintf(ep, size - (ep - result), "%d", hours);
1716
1458
	if (minutes != 0 || seconds != 0) {
1717
54
		ep = end(result, size);
1718
54
		snprintf(ep, size - (ep - result), ":%02d", minutes);
1719
54
		if (seconds != 0) {
1720
			ep = end(result, size);
1721
			snprintf(ep, size - (ep - result), ":%02d", seconds);
1722
		}
1723
	}
1724
1458
	return 0;
1725
1461
}
1726
1727
static int
1728
stringrule(char *result, size_t size, const struct rule *rp, long dstoff, long gmtoff)
1729
{
1730
	long	tod;
1731
	char 	*ep;
1732
1733
1686
	ep = end(result, size);
1734
843
	size -= ep - result;
1735
	result = ep;
1736
843
	if (rp->r_dycode == DC_DOM) {
1737
		int	month, total;
1738
1739

6
		if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1740
			return -1;
1741
		total = 0;
1742
72
		for (month = 0; month < rp->r_month; ++month)
1743
30
			total += len_months[0][month];
1744
6
		snprintf(result, size, "J%d", total + rp->r_dayofmonth);
1745
6
	} else {
1746
		int	week;
1747
1748
837
		if (rp->r_dycode == DC_DOWGEQ) {
1749
480
			if ((rp->r_dayofmonth % DAYSPERWEEK) != 1)
1750
12
				return -1;
1751
468
			week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1752
825
		} else if (rp->r_dycode == DC_DOWLEQ) {
1753
357
			if (rp->r_dayofmonth == len_months[1][rp->r_month])
1754
357
				week = 5;
1755
			else {
1756
				if ((rp->r_dayofmonth % DAYSPERWEEK) != 0)
1757
					return -1;
1758
				week = rp->r_dayofmonth / DAYSPERWEEK;
1759
			}
1760
		} else
1761
			return -1;	/* "cannot happen" */
1762
825
		snprintf(result, size, "M%d.%d.%d",
1763
825
			rp->r_month + 1, week, rp->r_wday);
1764
825
	}
1765
831
	tod = rp->r_tod;
1766
831
	if (rp->r_todisgmt)
1767
273
		tod += gmtoff;
1768

1170
	if (rp->r_todisstd && rp->r_stdoff == 0)
1769
168
		tod += dstoff;
1770
831
	if (tod < 0) {
1771
3
		result[0] = '\0';
1772
3
		return -1;
1773
	}
1774
828
	if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1775
294
		strlcat(result, "/", size);
1776
294
		ep = end(result, size);
1777
294
		if (stringoffset(ep, size - (ep - result), tod) != 0)
1778
3
			return -1;
1779
	}
1780
825
	return 0;
1781
843
}
1782
1783
static void
1784
stringzone(char *result, size_t size, const struct zone *zpfirst, int zonecount)
1785
{
1786
	const struct zone 	*zp;
1787
	struct rule 		*rp, *stdrp, *dstrp;
1788
	int			i;
1789
	const char 		*abbrvar;
1790
	char 			*ep;
1791
1792
2322
	result[0] = '\0';
1793
1161
	zp = zpfirst + zonecount - 1;
1794
	stdrp = dstrp = NULL;
1795
16590
	for (i = 0; i < zp->z_nrules; ++i) {
1796
7134
		rp = &zp->z_rules[i];
1797

11817
		if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
1798
			continue;
1799
858
		if (rp->r_yrtype != NULL)
1800
			continue;
1801
858
		if (rp->r_stdoff == 0) {
1802
429
			if (stdrp == NULL)
1803
				stdrp = rp;
1804
			else
1805
				return;
1806
429
		} else {
1807
429
			if (dstrp == NULL)
1808
				dstrp = rp;
1809
			else
1810
				return;
1811
		}
1812
	}
1813
1161
	if (stdrp == NULL && dstrp == NULL) {
1814
		/*
1815
		** There are no rules running through "max".
1816
		** Let's find the latest rule.
1817
		*/
1818
3984
		for (i = 0; i < zp->z_nrules; ++i) {
1819
1260
			rp = &zp->z_rules[i];
1820

2508
			if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
1821
951
				(rp->r_hiyear == stdrp->r_hiyear &&
1822
120
				rp->r_month > stdrp->r_month))
1823
387
					stdrp = rp;
1824
		}
1825

864
		if (stdrp != NULL && stdrp->r_stdoff != 0)
1826
			return;	/* We end up in DST (a POSIX no-no). */
1827
		/*
1828
		** Horrid special case: if year is 2037,
1829
		** presume this is a zone handled on a year-by-year basis;
1830
		** do not try to apply a rule to the zone.
1831
		*/
1832

864
		if (stdrp != NULL && stdrp->r_hiyear == 2037)
1833
			return;
1834
	}
1835

2361
	if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
1836
		return;
1837
2883
	abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1838
1161
	doabbr(result, size, zp->z_format, abbrvar, FALSE, TRUE);
1839
1161
	ep = end(result, size);
1840
1161
	if (stringoffset(ep, size - (ep - result), -zp->z_gmtoff) != 0) {
1841
		result[0] = '\0';
1842
		return;
1843
	}
1844
1161
	if (dstrp == NULL)
1845
732
		return;
1846
429
	ep = end(result, size);
1847
429
	doabbr(ep, size - (ep - result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
1848
429
	if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR) {
1849
6
		ep = end(result, size);
1850
18
		if (stringoffset(ep, size - (ep - result),
1851
12
			-(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
1852
				result[0] = '\0';
1853
				return;
1854
		}
1855
	}
1856
429
	strlcat(result, ",", size);
1857
429
	if (stringrule(result, size, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1858
15
		result[0] = '\0';
1859
15
		return;
1860
	}
1861
414
	strlcat(result, ",", size);
1862
414
	if (stringrule(result, size, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1863
3
		result[0] = '\0';
1864
3
		return;
1865
	}
1866
1572
}
1867
1868
static void
1869
outzone(const struct zone *zpfirst, int zonecount)
1870
{
1871
	const struct zone 	*zp;
1872
	struct rule 		*rp;
1873
	int			i, j, usestart, useuntil, type;
1874
	zic_t			starttime = 0, untiltime = 0;
1875
	long			gmtoff, stdoff, startoff;
1876
	int			year, startttisstd = FALSE, startttisgmt = FALSE;
1877
	char 			*startbuf, *ab, *envvar;
1878
	int			max_abbr_len, max_envvar_len;
1879
	int			prodstic; /* all rules are min to max */
1880
1881
2322
	max_abbr_len = 2 + max_format_len + max_abbrvar_len;
1882
1161
	max_envvar_len = 2 * max_abbr_len + 5 * 9;
1883
1161
	startbuf = emalloc(max_abbr_len + 1);
1884
1161
	ab = emalloc(max_abbr_len + 1);
1885
1161
	envvar = emalloc(max_envvar_len + 1);
1886
	/*
1887
	** Now. . .finally. . .generate some useful data!
1888
	*/
1889
1161
	timecnt = 0;
1890
1161
	typecnt = 0;
1891
1161
	charcnt = 0;
1892
1161
	prodstic = zonecount == 1;
1893
	/*
1894
	** Thanks to Earl Chew
1895
	** for noting the need to unconditionally initialize startttisstd.
1896
	*/
1897
1161
	min_year = max_year = EPOCH_YEAR;
1898
1161
	if (leapseen) {
1899
387
		updateminmax(leapminyear);
1900
387
		updateminmax(leapmaxyear + (leapmaxyear < INT_MAX));
1901
387
	}
1902
14538
	for (i = 0; i < zonecount; ++i) {
1903
6108
		zp = &zpfirst[i];
1904
6108
		if (i < zonecount - 1)
1905
4947
			updateminmax(zp->z_untilrule.r_loyear);
1906
101268
		for (j = 0; j < zp->z_nrules; ++j) {
1907
44526
			rp = &zp->z_rules[j];
1908
44526
			if (rp->r_lowasnum)
1909
44526
				updateminmax(rp->r_loyear);
1910
44526
			if (rp->r_hiwasnum)
1911
14046
				updateminmax(rp->r_hiyear);
1912

44526
			if (rp->r_lowasnum || rp->r_hiwasnum)
1913
44526
				prodstic = FALSE;
1914
		}
1915
	}
1916
	/*
1917
	** Generate lots of data if a rule can't cover all future times.
1918
	*/
1919
1161
	stringzone(envvar, max_envvar_len + 1, zpfirst, zonecount);
1920

1161
	if (noise && envvar[0] == '\0') {
1921
		char *	wp;
1922
1923
		wp = ecpyalloc("no POSIX environment variable for zone");
1924
		wp = ecatalloc(wp, " ");
1925
		wp = ecatalloc(wp, zpfirst->z_name);
1926
		warning(wp);
1927
		free(wp);
1928
	}
1929
1161
	if (envvar[0] == '\0') {
1930
36
		if (min_year >= INT_MIN + YEARSPERREPEAT)
1931
18
			min_year -= YEARSPERREPEAT;
1932
		else
1933
			min_year = INT_MIN;
1934
36
		if (max_year <= INT_MAX - YEARSPERREPEAT)
1935
18
			max_year += YEARSPERREPEAT;
1936
		else
1937
			max_year = INT_MAX;
1938
		/*
1939
		** Regardless of any of the above,
1940
		** for a "proDSTic" zone which specifies that its rules
1941
		** always have and always will be in effect,
1942
		** we only need one cycle to define the zone.
1943
		*/
1944
18
		if (prodstic) {
1945
			min_year = 1900;
1946
			max_year = min_year + YEARSPERREPEAT;
1947
		}
1948
	}
1949
	/*
1950
	** For the benefit of older systems,
1951
	** generate data from 1900 through 2037.
1952
	*/
1953
1161
	if (min_year > 1900)
1954
660
		min_year = 1900;
1955
1161
	if (max_year < 2037)
1956
1089
		max_year = 2037;
1957
14538
	for (i = 0; i < zonecount; ++i) {
1958
		/*
1959
		** A guess that may well be corrected later.
1960
		*/
1961
		stdoff = 0;
1962
6108
		zp = &zpfirst[i];
1963
17163
		usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1964
6108
		useuntil = i < (zonecount - 1);
1965

11055
		if (useuntil && zp->z_untiltime <= min_time)
1966
			continue;
1967
6108
		gmtoff = zp->z_gmtoff;
1968
6108
		eat(zp->z_filename, zp->z_linenum);
1969
6108
		*startbuf = '\0';
1970
6108
		startoff = zp->z_gmtoff;
1971
6108
		if (zp->z_nrules == 0) {
1972
3714
			stdoff = zp->z_stdoff;
1973
7428
			doabbr(startbuf, max_abbr_len + 1, zp->z_format,
1974
3714
				NULL, stdoff != 0, FALSE);
1975
3714
			type = addtype(oadd(zp->z_gmtoff, stdoff),
1976
				startbuf, stdoff != 0, startttisstd,
1977
				startttisgmt);
1978
3714
			if (usestart) {
1979
2577
				addtt(starttime, type);
1980
				usestart = FALSE;
1981
3714
			} else if (stdoff != 0)
1982
				addtt(min_time, type);
1983
526726
		} else for (year = min_year; year <= max_year; ++year) {
1984

429161
			if (useuntil && year > zp->z_untilrule.r_hiyear)
1985
				break;
1986
			/*
1987
			** Mark which rules to do in the current year.
1988
			** For those to do, calculate rpytime(rp, year);
1989
			*/
1990
10193384
			for (j = 0; j < zp->z_nrules; ++j) {
1991
4835723
				rp = &zp->z_rules[j];
1992
9671446
				eats(zp->z_filename, zp->z_linenum,
1993
4835723
					rp->r_filename, rp->r_linenum);
1994
9671446
				rp->r_todo = year >= rp->r_loyear &&
1995
6547010
				    year <= rp->r_hiyear &&
1996
139864
				    yearistype(year, rp->r_yrtype);
1997
4835723
				if (rp->r_todo)
1998
139864
					rp->r_temp = rpytime(rp, year);
1999
			}
2000
349347
			for ( ; ; ) {
2001
				int	k;
2002
				zic_t	jtime, ktime = 0;
2003
				long	offset;
2004
2005
399033
				if (useuntil) {
2006
					/*
2007
					** Turn untiltime into UTC
2008
					** assuming the current gmtoff and
2009
					** stdoff values.
2010
					*/
2011
230589
					untiltime = zp->z_untiltime;
2012
230589
					if (!zp->z_untilrule.r_todisgmt)
2013
225489
						untiltime = tadd(untiltime,
2014
225489
							-gmtoff);
2015
230589
					if (!zp->z_untilrule.r_todisstd)
2016
163194
						untiltime = tadd(untiltime,
2017
163194
							-stdoff);
2018
				}
2019
				/*
2020
				** Find the rule (of those to do, if any)
2021
				** that takes effect earliest in the year.
2022
				*/
2023
				k = -1;
2024
16235610
				for (j = 0; j < zp->z_nrules; ++j) {
2025
7718772
					rp = &zp->z_rules[j];
2026
7718772
					if (!rp->r_todo)
2027
						continue;
2028
421176
					eats(zp->z_filename, zp->z_linenum,
2029
210588
						rp->r_filename, rp->r_linenum);
2030
210588
					offset = rp->r_todisgmt ? 0 : gmtoff;
2031
210588
					if (!rp->r_todisstd)
2032
124830
						offset = oadd(offset, stdoff);
2033
210588
					jtime = rp->r_temp;
2034

421176
					if (jtime == min_time ||
2035
210588
						jtime == max_time)
2036
							continue;
2037
210588
					jtime = tadd(jtime, -offset);
2038

281999
					if (k < 0 || jtime < ktime) {
2039
						k = j;
2040
						ktime = jtime;
2041
177092
					}
2042
				}
2043
399033
				if (k < 0)
2044
259856
					break;	/* go on to next year */
2045
139177
				rp = &zp->z_rules[k];
2046
139177
				rp->r_todo = FALSE;
2047

206353
				if (useuntil && ktime >= untiltime)
2048
1113
					break;
2049
138064
				stdoff = rp->r_stdoff;
2050

263490
				if (usestart && ktime == starttime)
2051
234
					usestart = FALSE;
2052
138064
				if (usestart) {
2053
125192
					if (ktime < starttime) {
2054
49686
						startoff = oadd(zp->z_gmtoff,
2055
						    stdoff);
2056
49686
						doabbr(startbuf,
2057
						    max_abbr_len + 1,
2058
49686
						    zp->z_format,
2059
49686
						    rp->r_abbrvar,
2060
49686
						    rp->r_stdoff != 0,
2061
						    FALSE);
2062
49686
						continue;
2063
					}
2064

76775
					if (*startbuf == '\0' &&
2065
1269
					    startoff == oadd(zp->z_gmtoff,
2066
					    stdoff)) {
2067
636
						doabbr(startbuf,
2068
						    max_abbr_len + 1,
2069
636
						    zp->z_format,
2070
636
						    rp->r_abbrvar,
2071
636
						    rp->r_stdoff != 0,
2072
						    FALSE);
2073
636
					}
2074
				}
2075
176756
				eats(zp->z_filename, zp->z_linenum,
2076
88378
				    rp->r_filename, rp->r_linenum);
2077
176756
				doabbr(ab, max_abbr_len + 1, zp->z_format,
2078
88378
				    rp->r_abbrvar, rp->r_stdoff != 0, FALSE);
2079
88378
				offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2080
176756
				type = addtype(offset, ab, rp->r_stdoff != 0,
2081
88378
				    rp->r_todisstd, rp->r_todisgmt);
2082
88378
				addtt(ktime, type);
2083

88378
			}
2084
		}
2085
6108
		if (usestart) {
2086

2136
			if (*startbuf == '\0' &&
2087
			    zp->z_format != NULL &&
2088
			    strchr(zp->z_format, '%') == NULL &&
2089
			    strchr(zp->z_format, '/') == NULL)
2090
				strlcpy(startbuf, zp->z_format, max_abbr_len + 1);
2091
2136
			eat(zp->z_filename, zp->z_linenum);
2092
2136
			if (*startbuf == '\0')
2093
				error("can't determine time zone abbreviation to use just after until time");
2094
			else
2095
2136
				addtt(starttime,
2096
2136
				    addtype(startoff, startbuf,
2097
2136
				    startoff != zp->z_gmtoff,
2098
				    startttisstd, startttisgmt));
2099
		}
2100
		/*
2101
		** Now we may get to set starttime for the next zone line.
2102
		*/
2103
6108
		if (useuntil) {
2104
4947
			startttisstd = zp->z_untilrule.r_todisstd;
2105
4947
			startttisgmt = zp->z_untilrule.r_todisgmt;
2106
4947
			starttime = zp->z_untiltime;
2107
4947
			if (!startttisstd)
2108
4227
				starttime = tadd(starttime, -stdoff);
2109
4947
			if (!startttisgmt)
2110
4866
				starttime = tadd(starttime, -gmtoff);
2111
		}
2112
	}
2113
1161
	writezone(zpfirst->z_name, envvar);
2114
1161
	free(startbuf);
2115
1161
	free(ab);
2116
1161
	free(envvar);
2117
1161
}
2118
2119
static void
2120
addtt(const zic_t starttime, int type)
2121
{
2122
	size_t len;
2123
2124

187163
	if (starttime <= min_time ||
2125
94072
	    (timecnt == 1 && attypes[0].at < min_time)) {
2126
		gmtoffs[0] = gmtoffs[type];
2127
		isdsts[0] = isdsts[type];
2128
		ttisstds[0] = ttisstds[type];
2129
		ttisgmts[0] = ttisgmts[type];
2130
		if (abbrinds[type] != 0) {
2131
			len = strlen(&chars[abbrinds[type]]) + 1;
2132
			memmove(chars, &chars[abbrinds[type]], len);
2133
		}
2134
		abbrinds[0] = 0;
2135
		charcnt = strlen(chars) + 1;
2136
		typecnt = 1;
2137
		timecnt = 0;
2138
		type = 0;
2139
	}
2140
93091
	if (timecnt >= TZ_MAX_TIMES) {
2141
		error("too many transitions?!");
2142
		exit(EXIT_FAILURE);
2143
	}
2144
93091
	attypes[timecnt].at = starttime;
2145
93091
	attypes[timecnt].type = type;
2146
93091
	++timecnt;
2147
93091
}
2148
2149
static int
2150
addtype(long gmtoff, const char *abbr, int isdst, int ttisstd, int ttisgmt)
2151
{
2152
	int	i, j;
2153
2154
189074
	if (isdst != TRUE && isdst != FALSE) {
2155
		error("internal error - addtype called with bad isdst");
2156
		exit(EXIT_FAILURE);
2157
	}
2158
94537
	if (ttisstd != TRUE && ttisstd != FALSE) {
2159
		error("internal error - addtype called with bad ttisstd");
2160
		exit(EXIT_FAILURE);
2161
	}
2162
94537
	if (ttisgmt != TRUE && ttisgmt != FALSE) {
2163
		error("internal error - addtype called with bad ttisgmt");
2164
		exit(EXIT_FAILURE);
2165
	}
2166
	/*
2167
	** See if there's already an entry for this zone type.
2168
	** If so, just return its index.
2169
	*/
2170
932736
	for (i = 0; i < typecnt; ++i) {
2171

714162
		if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2172
134190
		    strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2173
125634
		    ttisstd == ttisstds[i] &&
2174
96754
		    ttisgmt == ttisgmts[i])
2175
87913
			return i;
2176
	}
2177
	/*
2178
	** There isn't one; add a new one, unless there are already too
2179
	** many.
2180
	*/
2181
6624
	if (typecnt >= TZ_MAX_TYPES) {
2182
		error("too many local time types");
2183
		exit(EXIT_FAILURE);
2184
	}
2185
6624
	if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2186
		error("UTC offset out of range");
2187
		exit(EXIT_FAILURE);
2188
	}
2189
6624
	gmtoffs[i] = gmtoff;
2190
6624
	isdsts[i] = isdst;
2191
6624
	ttisstds[i] = ttisstd;
2192
6624
	ttisgmts[i] = ttisgmt;
2193
2194
130908
	for (j = 0; j < charcnt; ++j)
2195
60678
		if (strcmp(&chars[j], abbr) == 0)
2196
			break;
2197
6624
	if (j == charcnt)
2198
4776
		newabbr(abbr);
2199
6624
	abbrinds[i] = j;
2200
6624
	++typecnt;
2201
6624
	return i;
2202
94537
}
2203
2204
static void
2205
leapadd(zic_t t, int positive, int rolling, int count)
2206
{
2207
	int	i, j;
2208
2209
54
	if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2210
		error("too many leap seconds");
2211
		exit(EXIT_FAILURE);
2212
	}
2213
756
	for (i = 0; i < leapcnt; ++i)
2214
351
		if (t <= trans[i]) {
2215
			if (t == trans[i]) {
2216
				error("repeated leap second moment");
2217
				exit(EXIT_FAILURE);
2218
			}
2219
			break;
2220
		}
2221
27
	do {
2222
54
		for (j = leapcnt; j > i; --j) {
2223
			trans[j] = trans[j - 1];
2224
			corr[j] = corr[j - 1];
2225
			roll[j] = roll[j - 1];
2226
		}
2227
27
		trans[i] = t;
2228
54
		corr[i] = positive ? 1L : eitol(-count);
2229
27
		roll[i] = rolling;
2230
27
		++leapcnt;
2231

54
	} while (positive && --count != 0);
2232
27
}
2233
2234
static void
2235
adjleap(void)
2236
{
2237
	int	i;
2238
	long	last = 0;
2239
2240
	/*
2241
	** propagate leap seconds forward
2242
	*/
2243
63
	for (i = 0; i < leapcnt; ++i) {
2244
27
		trans[i] = tadd(trans[i], last);
2245
27
		last = corr[i] += last;
2246
	}
2247
3
}
2248
2249
static int
2250
yearistype(int year, const char *type)
2251
{
2252
	static char 	*buf;
2253
	int		result;
2254
	size_t		len;
2255
2256

279728
	if (type == NULL || *type == '\0')
2257
139864
		return TRUE;
2258
	len = 132 + strlen(yitcommand) + strlen(type);
2259
	buf = erealloc(buf, len);
2260
	snprintf(buf, len, "%s %d %s", yitcommand, year, type);
2261
	result = system(buf);
2262
	if (WIFEXITED(result))
2263
		switch (WEXITSTATUS(result)) {
2264
		case 0:
2265
			return TRUE;
2266
		case 1:
2267
			return FALSE;
2268
		}
2269
	error("Wild result from command execution");
2270
	errx(1, "command was '%s', result was %d", buf, result);
2271
139864
}
2272
2273
/* this function is not strncasecmp */
2274
static int
2275
itsabbr(const char *sabbr, const char *sword)
2276
{
2277
	const unsigned char *abbr = sabbr;
2278
	const unsigned char *word = sword;
2279
2280
462324
	if (tolower(*abbr) != tolower(*word))
2281
202727
		return FALSE;
2282
174238
	while (*++abbr != '\0')
2283
74573
		do {
2284
158380
			++word;
2285
158380
			if (*word == '\0')
2286
15889
				return FALSE;
2287
142491
		} while (tolower(*word) != tolower(*abbr));
2288
12546
	return TRUE;
2289
231162
}
2290
2291
static const struct lookup *
2292
byword(const char *word, const struct lookup *table)
2293
{
2294
	const struct lookup 	*foundlp;
2295
	const struct lookup 	*lp;
2296
2297
103596
	if (word == NULL || table == NULL)
2298
		return NULL;
2299
	/*
2300
	** Look for exact match.
2301
	*/
2302
611748
	for (lp = table; lp->l_word != NULL; ++lp)
2303
271098
		if (strcasecmp(word, lp->l_word) == 0)
2304
17022
			return lp;
2305
	/*
2306
	** Look for inexact match.
2307
	*/
2308
	foundlp = NULL;
2309
531876
	for (lp = table; lp->l_word != NULL; ++lp)
2310
231162
		if (itsabbr(word, lp->l_word)) {
2311
12546
			if (foundlp == NULL)
2312
				foundlp = lp;
2313
			else
2314
				return NULL;	/* multiple inexact matches */
2315
12546
		}
2316
34776
	return foundlp;
2317
51798
}
2318
2319
static char **
2320
getfields(char *cp)
2321
{
2322
	char		*dp;
2323
	char		**array;
2324
	int		nsubs;
2325
2326
95218
	if (cp == NULL)
2327
		return NULL;
2328
47609
	array = ereallocarray(NULL, strlen(cp) + 1, sizeof *array);
2329
	nsubs = 0;
2330
141527
	for ( ; ; ) {
2331

651716
		while (isascii((unsigned char)*cp) &&
2332
162929
		    isspace((unsigned char)*cp))
2333
21402
			++cp;
2334

267246
		if (*cp == '\0' || *cp == '#')
2335
			break;
2336
93918
		array[nsubs++] = dp = cp;
2337
93918
		do {
2338
348996
			if ((*dp = *cp++) != '"') {
2339
348996
				++dp;
2340
348996
			} else {
2341
				while ((*dp = *cp++) != '"') {
2342
					if (*dp != '\0')
2343
						++dp;
2344
					else {
2345
						error("Odd number of quotation marks");
2346
						exit(EXIT_FAILURE);
2347
					}
2348
				}
2349
			}
2350

1023288
		} while (*cp != '\0' && *cp != '#' &&
2351
674292
		    (!isascii((unsigned char)*cp) || !isspace((unsigned char)*cp)));
2352

187836
		if (isascii((unsigned char)*cp) && isspace((unsigned char)*cp))
2353
82068
			++cp;
2354
93918
		*dp = '\0';
2355
	}
2356
47609
	array[nsubs] = NULL;
2357
47609
	return array;
2358
47609
}
2359
2360
static long
2361
oadd(long t1, long t2)
2362
{
2363
17915906
	long	t = t1 + t2;
2364
2365


26670862
	if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2366
		error("time overflow");
2367
		exit(EXIT_FAILURE);
2368
	}
2369
8957953
	return t;
2370
}
2371
2372
static zic_t
2373
tadd(zic_t t1, long t2)
2374
{
2375
	zic_t	t;
2376
2377
1556746
	if (t1 == max_time && t2 > 0)
2378
		return max_time;
2379
778373
	if (t1 == min_time && t2 < 0)
2380
		return min_time;
2381
778373
	t = t1 + t2;
2382


2110796
	if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2383
		error("time overflow");
2384
		exit(EXIT_FAILURE);
2385
	}
2386
778373
	return t;
2387
778373
}
2388
2389
/*
2390
** Given a rule, and a year, compute the date - in seconds since January 1,
2391
** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2392
*/
2393
static zic_t
2394
rpytime(const struct rule *rp, int wantedy)
2395
{
2396
	int	y, m, i;
2397
	long	dayoff;			/* with a nod to Margaret O. */
2398
	zic_t	t;
2399
2400
289622
	if (wantedy == INT_MIN)
2401
		return min_time;
2402
144811
	if (wantedy == INT_MAX)
2403
		return max_time;
2404
	dayoff = 0;
2405
	m = TM_JANUARY;
2406
	y = EPOCH_YEAR;
2407
15074488
	while (wantedy != y) {
2408
7392433
		if (wantedy > y) {
2409

13822413
			i = len_years[isleap(y)];
2410
6115828
			++y;
2411
6115828
		} else {
2412
1276605
			--y;
2413

2877330
			i = -len_years[isleap(y)];
2414
		}
2415
7392433
		dayoff = oadd(dayoff, eitol(i));
2416
	}
2417
1916904
	while (m != rp->r_month) {
2418

1831224
		i = len_months[isleap(y)][m];
2419
813641
		dayoff = oadd(dayoff, eitol(i));
2420
813641
		++m;
2421
	}
2422
144811
	i = rp->r_dayofmonth;
2423


145036
	if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2424
21
		if (rp->r_dycode == DC_DOWLEQ)
2425
21
			--i;
2426
		else {
2427
			error("use of 2/29 in non leap-year");
2428
			exit(EXIT_FAILURE);
2429
		}
2430
21
	}
2431
144811
	--i;
2432
144811
	dayoff = oadd(dayoff, eitol(i));
2433

246773
	if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2434
		long	wday;
2435
2436
#define LDAYSPERWEEK	((long) DAYSPERWEEK)
2437
108409
		wday = eitol(EPOCH_WDAY);
2438
		/*
2439
		** Don't trust mod of negative numbers.
2440
		*/
2441
108409
		if (dayoff >= 0)
2442
92692
			wday = (wday + dayoff) % LDAYSPERWEEK;
2443
		else {
2444
15717
			wday -= ((-dayoff) % LDAYSPERWEEK);
2445
15717
			if (wday < 0)
2446
4170
				wday += LDAYSPERWEEK;
2447
		}
2448
848014
		while (wday != eitol(rp->r_wday))
2449
315598
			if (rp->r_dycode == DC_DOWGEQ) {
2450
124390
				dayoff = oadd(dayoff, 1);
2451
124390
				if (++wday >= LDAYSPERWEEK)
2452
					wday = 0;
2453
124390
				++i;
2454
124390
			} else {
2455
191208
				dayoff = oadd(dayoff, -1);
2456
191208
				if (--wday < 0)
2457
					wday = LDAYSPERWEEK - 1;
2458
191208
				--i;
2459
			}
2460
108409
	}
2461
144811
	if (dayoff < min_time / SECSPERDAY)
2462
		return min_time;
2463
144811
	if (dayoff > max_time / SECSPERDAY)
2464
		return max_time;
2465
144811
	t = (zic_t) dayoff * SECSPERDAY;
2466
144811
	return tadd(t, rp->r_tod);
2467
144811
}
2468
2469
static void
2470
newabbr(const char *string)
2471
{
2472
	int	i;
2473
2474
9552
	if (strcmp(string, GRANDPARENTED) != 0) {
2475
		const char *	cp;
2476
		char *		wp;
2477
2478
		cp = string;
2479
		wp = NULL;
2480

45585
		while (isascii((unsigned char)*cp) &&
2481

31842
		    (isalnum((unsigned char)*cp) || *cp == '-' || *cp == '+'))
2482
15180
			++cp;
2483

4776
		if (noise && cp - string > 3)
2484
			wp = "time zone abbreviation has more than 3 characters";
2485
4776
		if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2486
			wp = "time zone abbreviation has too many characters";
2487
4776
		if (*cp != '\0')
2488
			wp = "time zone abbreviation differs from POSIX standard";
2489
4776
		if (wp != NULL) {
2490
			wp = ecpyalloc(wp);
2491
			wp = ecatalloc(wp, " (");
2492
			wp = ecatalloc(wp, string);
2493
			wp = ecatalloc(wp, ")");
2494
			warning(wp);
2495
			free(wp);
2496
		}
2497
4776
	}
2498
4776
	i = strlen(string) + 1;
2499
4776
	if (charcnt + i > TZ_MAX_CHARS) {
2500
		error("too many, or too long, time zone abbreviations");
2501
		exit(EXIT_FAILURE);
2502
	}
2503
4776
	strlcpy(&chars[charcnt], string, sizeof(chars) - charcnt);
2504
4776
	charcnt += eitol(i);
2505
4776
}
2506
2507
static int
2508
mkdirs(char *argname)
2509
{
2510
	char *	name;
2511
	char *	cp;
2512
2513
	if (argname == NULL || *argname == '\0')
2514
		return 0;
2515
	cp = name = ecpyalloc(argname);
2516
	while ((cp = strchr(cp + 1, '/')) != 0) {
2517
		*cp = '\0';
2518
		if (!itsdir(name)) {
2519
			/*
2520
			** It doesn't seem to exist, so we try to create it.
2521
			** Creation may fail because of the directory being
2522
			** created by some other multiprocessor, so we get
2523
			** to do extra checking.
2524
			*/
2525
			if (mkdir(name, MKDIR_UMASK) != 0) {
2526
				const char *e = strerror(errno);
2527
2528
				if (errno != EEXIST || !itsdir(name)) {
2529
					fprintf(stderr,
2530
					    "%s: Can't create directory %s: %s\n",
2531
					    __progname, name, e);
2532
					free(name);
2533
					return -1;
2534
				}
2535
			}
2536
		}
2537
		*cp = '/';
2538
	}
2539
	free(name);
2540
	return 0;
2541
}
2542
2543
static long
2544
eitol(int i)
2545
{
2546
18079474
	long	l = i;
2547
2548

27119211
	if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0))
2549
		errx(1, "%d did not sign extend correctly", i);
2550
9039737
	return l;
2551
}