File: tzmap.c

package info (click to toggle)
dateutils 0.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,924 kB
  • sloc: ansic: 22,097; makefile: 1,666; yacc: 198; sh: 168; lex: 108
file content (784 lines) | stat: -rw-r--r-- 16,963 bytes parent folder | download | duplicates (2)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*** tzmap.c -- zonename maps
 *
 * Copyright (C) 2014-2018 Sebastian Freundt
 *
 * Author:  Sebastian Freundt <freundt@ga-group.nl>
 *
 * This file is part of dateutils.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the author nor the names of any contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ***/
#if defined HAVE_CONFIG_H
# include "config.h"
#endif	/* HAVE_CONFIG_H */
/* for fgetln() */
#define _NETBSD_SOURCE
#define _DARWIN_SOURCE
#define _ALL_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#if defined HAVE_SYS_STDINT_H
# include <sys/stdint.h>
#endif	/* HAVE_SYS_STDINT_H */
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "tzmap.h"
#include "boops.h"

#if !defined LIKELY
# define LIKELY(_x)     __builtin_expect((_x), 1)
#endif  /* !LIKELY */
#if !defined UNLIKELY
# define UNLIKELY(_x)   __builtin_expect((_x), 0)
#endif  /* UNLIKELY */
#if !defined UNUSED
# define UNUSED(_x)     _x __attribute__((unused))
#endif  /* !UNUSED */

#if !defined countof
# define countof(x)     (sizeof(x) / sizeof(*x))
#endif  /* !countof */

#define _paste(x, y)    x ## y
#define paste(x, y)     _paste(x, y)
#if !defined with
# define with(args...)                                                  \
        for (args, *paste(__ep, __LINE__) = (void*)1;                   \
             paste(__ep, __LINE__); paste(__ep, __LINE__) = 0)
#endif  /* !with */

#if !defined HAVE_GETLINE && !defined HAVE_FGETLN
/* as a service to people including this file in their project
 * but who might not necessarily run the corresponding AC_CHECK_FUNS
 * we assume that a getline() is available. */
# define HAVE_GETLINE   1
#endif  /* !HAVE_GETLINE && !HAVE_FGETLN */

#if !defined DEFUN
# define DEFUN
#endif	/* !DEFUN */

#if defined STANDALONE
# if defined TZDIR
static const char tzdir[] = TZDIR;
# else  /* !TZDIR */
static const char tzdir[] = "/usr/share/zoneinfo";
# endif	/* TZDIR */
#endif	/* STANDALONE */


#if defined STANDALONE
static __attribute__((format(printf, 1, 2))) void
error(const char *fmt, ...)
{
        va_list vap;
        va_start(vap, fmt);
        vfprintf(stderr, fmt, vap);
        va_end(vap);
        fputc('\n', stderr);
        return;
}

static __attribute__((format(printf, 1, 2))) void
serror(const char *fmt, ...)
{
        va_list vap;
        va_start(vap, fmt);
        vfprintf(stderr, fmt, vap);
        va_end(vap);
        if (errno) {
                fputc(':', stderr);
                fputc(' ', stderr);
                fputs(strerror(errno), stderr);
        }
        fputc('\n', stderr);
        return;
}

static size_t
xstrlncpy(char *restrict dst, size_t dsz, const char *src, size_t ssz)
{
	if (ssz >= dsz) {
		ssz = dsz - 1U;
	}
	memcpy(dst, src, ssz);
	dst[ssz] = '\0';
	return ssz;
}
#endif	/* STANDALONE */

static void*
deconst(const void *ptr)
{
	return (char*)1 + ((const char*)ptr - (char*)1U);
}

static const void*
align_to(size_t tz, const void *p)
{
	uintptr_t x = (uintptr_t)p;

	if (x % tz) {
		x -= x % tz;
	}
	return (const void*)x;
}
#define ALIGN_TO(tz, p)	align_to(sizeof(tz), p)


/* public API */
static inline size_t
tzm_file_size(tzmap_t m)
{
	return m->flags[1U];
}

static inline int
tzm_fd(tzmap_t m)
{
	return m->flags[0U];
}

static inline const char*
tzm_znames(tzmap_t m)
{
	return m->data;
}

static inline size_t
tzm_zname_size(tzmap_t m)
{
	return m->off;
}

static inline const char*
tzm_mnames(tzmap_t m)
{
	return m->data + tzm_zname_size(m);
}

static inline size_t
tzm_mname_size(tzmap_t m)
{
	size_t fz = tzm_file_size(m);
	return fz - tzm_zname_size(m) - sizeof(*m);
}

DEFUN tzmap_t
tzm_open(const char *fn)
{
#define FAIL	(tzmap_t)MAP_FAILED
#define TZMP	(PROT_READ | PROT_WRITE)
	struct stat st[1U];
	size_t fz;
	struct tzmap_s *m;
	int fd;

	if ((fd = open(fn, O_RDONLY)) < 0) {
		return NULL;
	} else if (fstat(fd, st) < 0) {
		goto clo;
	} else if ((fz = st->st_size) < sizeof(*m)) {
		goto clo;
	} else if ((m = mmap(0, fz, TZMP, MAP_PRIVATE, fd, 0)) == FAIL) {
		goto clo;
	} else if (memcmp(m->magic, TZM_MAGIC, sizeof(m->magic))) {
		goto mun;
	}
	/* turn offset into native endianness */
	m->off = be32toh(m->off);
	/* also put fd and map size into m */
	m->flags[0U] = (znoff_t)fd;
	m->flags[1U] = (znoff_t)st->st_size;
	/* and here we go */
	return m;

#undef FAIL
#undef TZMP
	/* failure cases, clean up and return NULL */
mun:
	munmap(m, st->st_size);
clo:
	close(fd);
	return NULL;
}

DEFUN void
tzm_close(tzmap_t m)
{
	size_t fz = tzm_file_size(m);
	int fd = tzm_fd(m);

	/* hopefully privately mapped */
	munmap(deconst(m), fz);
	close(fd);
	return;
}

DEFUN const char*
tzm_find(tzmap_t m, const char *mname)
{
/* lookup zname for MNAME */
	const znoff_t *sp = (const void*)tzm_mnames(m);
	const znoff_t *ep = sp + tzm_mname_size(m) / sizeof(*sp) - 1U;
	const char *zns = tzm_znames(m);

	/* do a bisection now */
	do {
		const char *mp = mname;
		const char *tp;
		const char *p;

		tp = (const char*)(sp + (ep - sp) / 2U);
		if (!*tp) {
			/* fast forward to the next entry */
			tp += sizeof(*sp);
		} else {
			while (tp[-1] != '\0') {
				/* rewind to beginning */
				tp--;
			}
		}
		/* store tp again */
		p = tp;
		/* now unroll a strcmp */
		for (; *mp && *mp == *tp; mp++, tp++);
		if (*mp - *tp < 0) {
			/* use lower half */
			ep = (const znoff_t*)p - 1U;
		} else {
			/* forward to the next znoff_t alignment */
			const znoff_t *op =
				(const znoff_t*)ALIGN_TO(znoff_t, tp - 1U) + 1U;

			if (*mp - *tp > 0) {
				/* use upper half */
				sp = op + 1U;
			} else {
				/* found it */
				return zns + (be32toh(*op) >> 8U);
			}
		}
	} while (sp < ep);
	return NULL;
}


#if defined STANDALONE
/* array of all zone names */
static char *zns;
static size_t znz;
static ptrdiff_t zni;
/* array for all mappee strings */
static znoff_t *mns;
static size_t mnz;
static ptrdiff_t mni;

static void
init_tzm(void)
{
	zns = calloc(znz = 64U, sizeof(*zns));
	mns = calloc(mnz = 64U, sizeof(*mns));
	return;
}

static void
free_tzm(void)
{
	free(zns);
	free(mns);
	return;
}

static znoff_t
tzm_find_zn(const char *zn, size_t zz)
{
	char *restrict p = zns;
	const char *const ep = zns + znz;

	for (; p < ep && *p && strncmp(p, zn, zz); p += strlen(p), p++);
	if (*p) {
		/* found it, yay */
		return p - zns;
	}
	/* otherwise append, first check if there's room */
	if (p + zz + 4U >= ep) {
		/* compute new p */
		ptrdiff_t d = p - zns;
		/* resize, double the size */
		p = (zns = realloc(zns, znz *= 2U)) + d;
		memset(p, 0, (znz - (p - zns)) * sizeof(*zns));
	}
	/* really append now */
	memcpy(p, zn, zz);
	zni = (p - zns) + zz + 1U;
	return p - zns;
}

static void
tzm_add_mn(const char *mn, size_t mz, znoff_t off)
{
	znoff_t *restrict p = mns + mni;

	if (UNLIKELY(mz == 0U)) {
		/* useless */
		return;
	}
	/* first check if there's room */
	if (p + 1U + (mz + 4U/*alignment*/) / sizeof(off) >= mns + mnz) {
		/* resize, double the size */
		p = (mns = realloc(mns, (mnz *= 2U) * sizeof(*mns))) + mni;
		memset((char*)p, 0, (mnz - (p - mns)) * sizeof(*mns));
	}
	/* really append now */
	memcpy(p, mn, mz);
	p += (mz - 1U) / sizeof(off) + 1U;
	*p++ = htobe32((off & 0xffffU)<< 8U);
	mni = p - mns;
	return;
}


static unsigned int exst_only_p;
static const char *check_fn;

static bool
tzdir_zone_p(const char *zn, size_t zz)
{
	struct stat st[1U];
	char fullzn[256U] = {0};

	if (*zn == '/') {
		/* absolute zonename? */
		;
	} else if (*zn) {
		/* relative zonename */
		char *zp = fullzn;
		const char *const ep = fullzn + sizeof(fullzn);

		zp += xstrlncpy(zp, ep - zp, tzdir, sizeof(tzdir) - 1U);
		*zp++ = '/';
		zp += xstrlncpy(zp, ep - zp, zn, zz);
		*zp = '\0';
	}
	/* finally the actual check */
	if (stat(fullzn, st) < 0) {
		return false;
	}
	return true;
}

static znoff_t
parse_line(char *ln, size_t lz)
{
	/* find the separator */
	char *lp;
	znoff_t znp;

	if (UNLIKELY(ln == NULL || lz == 0U)) {
		/* finalise */
		return NUL_ZNOFF;
	}
	if ((lp = memchr(ln, '\t', lz)) == NULL) {
		/* buggered line */
		return NUL_ZNOFF;
	} else if (lp == ln) {
		return NUL_ZNOFF;
	} else if (*lp++ = '\0', *lp == '\0') {
		/* huh? no zone name, cunt off */
		return NUL_ZNOFF;
	} else if (lp - ln > 256) {
		/* too long */
		return NUL_ZNOFF;
	} else if (exst_only_p && !tzdir_zone_p(lp, ln + lz - lp)) {
		error("\
Warning: zone `%.*s' skipped: not present in global zone database",
		      (int)(ln + lz - lp), lp);
		return NUL_ZNOFF;
	} else if ((znp = tzm_find_zn(lp, ln + lz - lp)) == -1U) {
		/* brilliant, can't add anything */
		return NUL_ZNOFF;
	}
	tzm_add_mn(ln, lp - ln - 1U, znp);
	return znp;
}

static int
check_line(char *ln, size_t lz)
{
	static char last[256U];
	static unsigned int lno;
	size_t cz;
	char *lp;
	int rc = 0;

	if (UNLIKELY(ln == NULL || lz == 0U)) {
		/* finalise */
		lno = 0U;
		*last = '\0';
		return 0;
	}
	/* advance line number */
	lno++;

#define CHECK_ERROR(fmt, args...)		\
	error("Error in %s:%u: " fmt, check_fn, lno, ## args)

	/* the actual checks here */
	if ((lp = memchr(ln, '\t', lz)) == NULL) {
		/* buggered line */
		CHECK_ERROR("no separator");
		return -1;
	} else if (lp == ln) {
		CHECK_ERROR("no code");
		return -1;
	} else if (*lp++ = '\0', *lp == '\0') {
		/* huh? no zone name, cunt off */
		CHECK_ERROR("no zone name");
		rc = -1;
	}
	if ((cz = lp - ln - 1U) >= sizeof(last)) {
		CHECK_ERROR("code too long (%zu chars, max is 255)", cz);
		rc = -1;
	} else if (strncmp(last, ln, cz) >= 0) {
		CHECK_ERROR("non-ascending order `%s' (after `%s')", ln, last);
		rc = -1;
	}
	/* make sure to memorise ln for the next run */
	xstrlncpy(last, sizeof(last), ln, cz);

	/* it's none of our business really, but go through the zonenames
	 * and check for their existence now */
	if (!*lp) {
		/* already warned about this */
		;
	} else if (!tzdir_zone_p(lp, lz - (lp - ln))) {
		lp[lz - (lp - ln)] = '\0';
		CHECK_ERROR("cannot find zone `%s' in TZDIR", lp);
		rc = -1;
	}
#undef CHECK_ERROR
	return rc;
}

static int
parse_file(const char *file)
{
	char *line = NULL;
	size_t llen = 0U;
	FILE *fp;

	if (file == NULL) {
		fp = stdin;
	} else if ((fp = fopen(file, "r")) == NULL) {
		return -1;
	}

#if defined HAVE_GETLINE
	for (ssize_t nrd; (nrd = getline(&line, &llen, fp)) > 0;) {
		parse_line(line, nrd - 1);
	}
#elif defined HAVE_FGETLN
	while ((line = fgetln(fp, &llen)) != NULL && llen > 0U) {
		parse_line(line, llen - 1);
	}
#else
# error neither getline() nor fgetln() available, cannot read file line by line
#endif	/* GETLINE/FGETLN */

#if defined HAVE_GETLINE
	/* free line buffer resources */
	free(line);
#endif	/* HAVE_GETLINE */

	fclose(fp);
	return 0;
}

static bool
tzmccp(FILE *fp)
{
	static char buf[4U];

	if (fread(buf, sizeof(*buf), countof(buf), fp) < sizeof(buf)) {
		/* definitely buggered */
		;
	} else if (!memcmp(buf, TZM_MAGIC, sizeof(buf))) {
		return true;
	}
	/* otherwise, good try, seek back to the beginning */
	fseek(fp, 0, SEEK_SET);
	return false;
}

static int
tzm_check(const char *fn)
{
	tzmap_t m;
	int rc = 0;

	if ((m = tzm_open(fn)) == NULL) {
		serror("cannot open input file `%s'", fn);
		return -1;
	}

	{
		/* traverse them all */
		const znoff_t *p = (const void*)tzm_mnames(m);
		const znoff_t *const ep =
			(const void*)((const char*)p + tzm_mname_size(m));

		while (p < ep) {
			const char *mn = (const void*)p;
			size_t mz = strlen(mn);
			znoff_t off;
			const char *zn;
			size_t zz;

			p += (mz - 1U) / sizeof(*p) + 1U;
			off = be32toh(*p++) >> 8U;

			zn = m->data + off;
			zz = strlen(zn);
			if (!tzdir_zone_p(zn, zz)) {
				error("cannot find zone `%s' in TZDIR", zn);
				rc = -1;
			}
		}
	}

	tzm_close(m);
	return rc;
}

static int
check_file(const char *file)
{
	char *line = NULL;
	size_t llen = 0U;
	FILE *fp;
	int rc = 0;

	if (file == NULL) {
		fp = stdin;
		check_fn = "-";
	} else if ((fp = fopen(check_fn = file, "r")) == NULL) {
		serror("Cannot open file `%s'", file);
		return -1;
	} else if (tzmccp(fp)) {
		/* oh yikes, can't use the line reader can we */
		fclose(fp);
		return tzm_check(file);
	}

#if defined HAVE_GETLINE
	for (ssize_t nrd; (nrd = getline(&line, &llen, fp)) > 0;) {
		rc |= check_line(line, nrd - 1);
	}
#elif defined HAVE_FGETLN
	while ((line = fgetln(fp, &llen)) != NULL && llen > 0U) {
		rc |= check_line(line, llen - 1);
	}
#else
# error neither getline() nor fgetln() available, cannot read file line by line
#endif	/* GETLINE/FGETLN */

#if defined HAVE_GETLINE
	/* free line buffer resources */
	free(line);
#endif	/* HAVE_GETLINE */

	/* reset line checker */
	check_line(NULL, 0U);
	/* and clear resources */
	fclose(fp);
	return rc;
}
#endif	/* STANDALONE */


#if defined STANDALONE
#include "tzmap.yucc"

static int
cmd_cc(const struct yuck_cmd_cc_s argi[static 1U])
{
	const char *outf;
	int rc = 0;
	int ofd;

	/* reserve some space */
	init_tzm();
	/* establish environment */
	exst_only_p = argi->existing_only_flag;

	if (parse_file(argi->args[0U]) < 0) {
		error("cannot read file `%s'", *argi->args ?: "stdin");
		rc = 1;
		goto out;
	} else if ((outf = argi->output_arg ?: "tzcc.tzm", false)) {
		/* we used to make -o|--output mandatory */
		;
	} else if ((ofd = open(outf, O_RDWR | O_CREAT | O_TRUNC, 0666)) < 0) {
		serror("cannot open output file `%s'", outf);
		rc = 1;
		goto out;
	}

	/* generate a disk version now */
	with (znoff_t off = zni) {
		static struct tzmap_s r = {TZM_MAGIC};
		ssize_t sz;

		off = (off + sizeof(off) - 1U) / sizeof(off) * sizeof(off);
		r.off = htobe32(off);
		if (sz = sizeof(r), write(ofd, &r, sz) < sz) {
			goto trunc;
		} else if (sz = off, write(ofd, zns, sz) < sz) {
			goto trunc;
		} else if (sz = mni * sizeof(*mns), write(ofd, mns, sz) < sz) {
			goto trunc;
		}
		close(ofd);
		break;

	trunc:
		/* some write failed, leave a 0 byte file around */
		close(ofd);
		unlink(outf);
		rc = 1;
	}

out:
	free_tzm();
	return rc;
}

static int
cmd_show(const struct yuck_cmd_show_s argi[static 1U])
{
	const char *fn;
	tzmap_t m;
	int rc = 0;

	if ((fn = argi->tzmap_arg ?: "tzcc.tzm", false)) {
		/* we used to make -f|--tzmap mandatory */
		;
	} else if ((m = tzm_open(fn)) == NULL) {
		serror("cannot open input file `%s'", fn);
		return 1;
	}

	if (!argi->nargs) {
		/* dump mode */
		const znoff_t *p = (const void*)tzm_mnames(m);
		const znoff_t *const ep =
			(const void*)((const char*)p + tzm_mname_size(m));

		while (p < ep) {
			const char *mn = (const void*)p;
			size_t mz = strlen(mn);
			znoff_t off;

			p += (mz - 1U) / sizeof(*p) + 1U;
			off = be32toh(*p++) >> 8U;

			/* actually print the strings */
			fputs(mn, stdout);
			fputc('\t', stdout);
			fputs(m->data + off, stdout);
			fputc('\n', stdout);
		}
	}
	/* otherwise */
	for (size_t i = 0U; i < argi->nargs; i++) {
		const char *zn;

		if ((zn = tzm_find(m, argi->args[i])) != NULL) {
			puts(zn);
		}
	}

	/* and off we go */
	tzm_close(m);
	return rc;
}

static int
cmd_check(const struct yuck_cmd_check_s argi[static 1U])
{
	int rc = 0;

	for (size_t i = 0U; i < argi->nargs || i == 0U; i++) {
		rc |= check_file(argi->args[i]);
	}
	return -rc;
}

int
main(int argc, char *argv[])
{
	yuck_t argi[1U];
	int rc = 0;

	if (yuck_parse(argi, argc, argv) < 0) {
		rc = 1;
		goto out;
	}

	switch (argi->cmd) {
	case TZMAP_CMD_CC:
		rc = cmd_cc((void*)argi);
		break;
	case TZMAP_CMD_SHOW:
		rc = cmd_show((void*)argi);
		break;
	case TZMAP_CMD_CHECK:
		rc = cmd_check((void*)argi);
		break;
	default:
		rc = 1;
		break;
	}

out:
	yuck_free(argi);
	return rc;
}
#endif	/* STANDALONE */

/* tzmap.c ends here */