2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2005-2008 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include "libfifolog.h"
45 static const char *opt_T;
46 static const char *opt_o;
47 static const char *opt_R;
53 Render(void *priv __unused, time_t now, unsigned flag __unused, const unsigned char *p, unsigned l __unused)
59 if (now < opt_B || now > opt_E)
62 if (opt_R != NULL && regexec(&R, (const char *)p, 0, NULL, 0))
65 if (opt_T != NULL && *opt_T == '\0') {
66 fprintf(fo, "%s\n", p);
67 } else if (opt_T != NULL) {
68 (void)gmtime_r(&now, &utc);
69 i = strftime(buf, sizeof buf, opt_T, &utc);
71 fprintf(fo, "%s %s\n", buf, p);
73 fprintf(fo, "%12ld %s\n", (long)now, p);
77 /*--------------------------------------------------------------------*/
83 "Usage: fiforead [options] fifofile\n"
84 "\t-b <start time integer>\n"
86 "\t-e <end time integer>\n"
88 "\t-o <output file>\n"
89 "\t-R <regexp> # match regexp\n"
90 "\t-t # format timestamps as %%Y%%m%%d%%H%%M%%S\n"
91 "\t-T <timestamp format>\n"
97 main(int argc, char * const *argv)
101 struct fifolog_reader *fl;
105 while ((ch = getopt(argc, argv, "b:B:e:E:o:R:tT:")) != -1) {
108 opt_B = strtoul(optarg, NULL, 0);
111 opt_B = get_date(optarg);
113 errx(1, "Didn't understand \"%s\"", optarg);
116 opt_E = strtoul(optarg, NULL, 0);
119 opt_E = get_date(optarg);
121 errx(1, "Didn't understand \"%s\"", optarg);
130 opt_T = "%Y%m%d%H%M%S";
143 i = regcomp(&R, opt_R, REG_NOSUB);
146 (void)regerror(i, &R, buf, sizeof buf);
147 fprintf(stderr, "-R argument: %s\n", buf);
155 fprintf(stderr, "From\t%jd %s", (intmax_t)opt_B, ctime(&opt_B));
156 fprintf(stderr, "To\t%jd %s", (intmax_t)opt_E, ctime(&opt_E));
158 errx(1, "Begin time not before End time");
160 fl = fifolog_reader_open(argv[0]);
162 if (!strcmp(opt_o, "-"))
165 fo = fopen(opt_o, "w");
167 err(1, "Cannot open: %s", argv[1]);
170 o = fifolog_reader_seek(fl, opt_B);
171 fifolog_reader_process(fl, o, Render, NULL, opt_E);