xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/nca/ncab2clf.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  *
31*0Sstevel@tonic-gate  *	Converts binary log files to CLF (Common Log Format).
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <unistd.h>
37*0Sstevel@tonic-gate #include <strings.h>
38*0Sstevel@tonic-gate #include <sys/types.h>
39*0Sstevel@tonic-gate #include <fcntl.h>
40*0Sstevel@tonic-gate #include <stdio.h>
41*0Sstevel@tonic-gate #include <locale.h>
42*0Sstevel@tonic-gate #include <errno.h>
43*0Sstevel@tonic-gate #include <time.h>
44*0Sstevel@tonic-gate #include <synch.h>
45*0Sstevel@tonic-gate #include <syslog.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #ifndef	TRUE
48*0Sstevel@tonic-gate #define	TRUE	1
49*0Sstevel@tonic-gate #endif	/* TRUE */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #ifndef	FALSE
52*0Sstevel@tonic-gate #define	FALSE	0
53*0Sstevel@tonic-gate #endif	/* FALSE */
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate #include "ncadoorhdr.h"
56*0Sstevel@tonic-gate #include "ncalogd.h"
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate extern char *gettext();
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate typedef	enum	{	/* Boolean type */
61*0Sstevel@tonic-gate 	false = 0,
62*0Sstevel@tonic-gate 	true  = 1
63*0Sstevel@tonic-gate } bool;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static const char *const
66*0Sstevel@tonic-gate g_method_strings[8] = {
67*0Sstevel@tonic-gate 	"UNKNOWN",
68*0Sstevel@tonic-gate 	"OPTIONS",
69*0Sstevel@tonic-gate 	"GET",
70*0Sstevel@tonic-gate 	"HEAD",
71*0Sstevel@tonic-gate 	"POST",
72*0Sstevel@tonic-gate 	"PUT",
73*0Sstevel@tonic-gate 	"DELETE",
74*0Sstevel@tonic-gate 	"TRACE"
75*0Sstevel@tonic-gate };
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate /* Short month strings */
78*0Sstevel@tonic-gate static const char * const sMonthStr [12] = {
79*0Sstevel@tonic-gate 	"Jan",
80*0Sstevel@tonic-gate 	"Feb",
81*0Sstevel@tonic-gate 	"Mar",
82*0Sstevel@tonic-gate 	"Apr",
83*0Sstevel@tonic-gate 	"May",
84*0Sstevel@tonic-gate 	"Jun",
85*0Sstevel@tonic-gate 	"Jul",
86*0Sstevel@tonic-gate 	"Aug",
87*0Sstevel@tonic-gate 	"Sep",
88*0Sstevel@tonic-gate 	"Oct",
89*0Sstevel@tonic-gate 	"Nov",
90*0Sstevel@tonic-gate 	"Dec",
91*0Sstevel@tonic-gate };
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate #define	SEC_PER_MIN		(60)
94*0Sstevel@tonic-gate #define	SEC_PER_HOUR		(60*60)
95*0Sstevel@tonic-gate #define	SEC_PER_DAY		(24*60*60)
96*0Sstevel@tonic-gate #define	SEC_PER_YEAR		(365*24*60*60)
97*0Sstevel@tonic-gate #define	LEAP_TO_70		(70/4)
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate #define	KILO_BYTE		(1024)
100*0Sstevel@tonic-gate #define	MEGA_BYTE		(KILO_BYTE * KILO_BYTE)
101*0Sstevel@tonic-gate #define	GIGA_BYTE		(KILO_BYTE * MEGA_BYTE)
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate #define	CLF_DATE_BUF_LENGTH	(128)
104*0Sstevel@tonic-gate #define	OUTFILE_BUF_SIZE	(256 * KILO_BYTE)
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static bool	g_enable_directio = true;
107*0Sstevel@tonic-gate static ssize_t	g_invalid_count = 0;
108*0Sstevel@tonic-gate static ssize_t	g_skip_count = 0;
109*0Sstevel@tonic-gate static char	*g_start_time_str = NULL;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate /* init value must match logd & NCA kmod */
112*0Sstevel@tonic-gate static ssize_t	g_n_log_upcall = 0;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate /* input binary file was written in 64k chunks by default  */
115*0Sstevel@tonic-gate static ssize_t	g_infile_blk_size = NCA_DEFAULT_LOG_BUF_SIZE;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate /* num of output records, by default infinite */
118*0Sstevel@tonic-gate static ssize_t	g_out_records = -1;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate /* start time for log output, default none (i.e. output all) */
121*0Sstevel@tonic-gate static struct tm g_start_time;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate /*
124*0Sstevel@tonic-gate  * http_version(version)
125*0Sstevel@tonic-gate  *
126*0Sstevel@tonic-gate  * Returns out the string of a given http version
127*0Sstevel@tonic-gate  */
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate static char *
http_version(int http_ver)130*0Sstevel@tonic-gate http_version(int http_ver)
131*0Sstevel@tonic-gate {
132*0Sstevel@tonic-gate 	char	*ver_num;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	switch (http_ver) {
135*0Sstevel@tonic-gate 	case HTTP_0_9:
136*0Sstevel@tonic-gate 	case HTTP_0_0:
137*0Sstevel@tonic-gate 		ver_num = "HTTP/0.9";
138*0Sstevel@tonic-gate 		break;
139*0Sstevel@tonic-gate 	case HTTP_ERR:
140*0Sstevel@tonic-gate 	case HTTP_1_0:
141*0Sstevel@tonic-gate 		ver_num = "HTTP/1.0";
142*0Sstevel@tonic-gate 		break;
143*0Sstevel@tonic-gate 	case HTTP_1_1:
144*0Sstevel@tonic-gate 		ver_num = "HTTP/1.1";
145*0Sstevel@tonic-gate 		break;
146*0Sstevel@tonic-gate 	default:
147*0Sstevel@tonic-gate 		ver_num = "HTTP/unknown";
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	return (ver_num);
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate static bool
valid_version(int http_ver)154*0Sstevel@tonic-gate valid_version(int http_ver)
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	switch (http_ver) {
157*0Sstevel@tonic-gate 	case HTTP_0_9:
158*0Sstevel@tonic-gate 	case HTTP_0_0:
159*0Sstevel@tonic-gate 	case HTTP_1_0:
160*0Sstevel@tonic-gate 	case HTTP_1_1:
161*0Sstevel@tonic-gate 		return (true);
162*0Sstevel@tonic-gate 	default:
163*0Sstevel@tonic-gate 		break;
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	return (false);
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate static bool
valid_method(int method)170*0Sstevel@tonic-gate valid_method(int method)
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate 	switch (method) {
173*0Sstevel@tonic-gate 	case NCA_OPTIONS:
174*0Sstevel@tonic-gate 	case NCA_GET:
175*0Sstevel@tonic-gate 	case NCA_HEAD:
176*0Sstevel@tonic-gate 	case NCA_POST:
177*0Sstevel@tonic-gate 	case NCA_PUT:
178*0Sstevel@tonic-gate 	case NCA_DELETE:
179*0Sstevel@tonic-gate 	case NCA_TRACE:
180*0Sstevel@tonic-gate 		return (true);
181*0Sstevel@tonic-gate 	default:
182*0Sstevel@tonic-gate 		break;
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	return (false);
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate /*
189*0Sstevel@tonic-gate  * http_method
190*0Sstevel@tonic-gate  *
191*0Sstevel@tonic-gate  *   Returns the method string for the given method.
192*0Sstevel@tonic-gate  */
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate static char *
http_method(int method)195*0Sstevel@tonic-gate http_method(int method)
196*0Sstevel@tonic-gate {
197*0Sstevel@tonic-gate 	if (method < sizeof (g_method_strings) / sizeof (g_method_strings[0]))
198*0Sstevel@tonic-gate 		return ((char *)(g_method_strings[method]));
199*0Sstevel@tonic-gate 	else
200*0Sstevel@tonic-gate 		return ((char *)(g_method_strings[0]));
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate /* sMonth: Return short month string */
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate static const char *
sMonth(int index)206*0Sstevel@tonic-gate sMonth(int index)
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate 	return (sMonthStr[index]);
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate /*
212*0Sstevel@tonic-gate  * Debug formatting routine.  Returns a character string representation of the
213*0Sstevel@tonic-gate  * addr in buf, of the form xxx.xxx.xxx.xxx.  This routine takes the address
214*0Sstevel@tonic-gate  * as a pointer.  The "xxx" parts including left zero padding so the final
215*0Sstevel@tonic-gate  * string will fit easily in tables.  It would be nice to take a padding
216*0Sstevel@tonic-gate  * length argument instead.
217*0Sstevel@tonic-gate  */
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate static char *
ip_dot_saddr(uchar_t * addr,char * buf)220*0Sstevel@tonic-gate ip_dot_saddr(uchar_t *addr, char *buf)
221*0Sstevel@tonic-gate {
222*0Sstevel@tonic-gate 	(void) sprintf(buf, "%03d.%03d.%03d.%03d",
223*0Sstevel@tonic-gate 	    addr[0] & 0xFF, addr[1] & 0xFF, addr[2] & 0xFF, addr[3] & 0xFF);
224*0Sstevel@tonic-gate 	return (buf);
225*0Sstevel@tonic-gate }
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate /*
228*0Sstevel@tonic-gate  * Debug formatting routine.  Returns a character string representation of the
229*0Sstevel@tonic-gate  * addr in buf, of the form xxx.xxx.xxx.xxx.  This routine takes the address
230*0Sstevel@tonic-gate  * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
231*0Sstevel@tonic-gate  */
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate static char *
ip_dot_addr(ipaddr_t addr,char * buf)234*0Sstevel@tonic-gate ip_dot_addr(ipaddr_t addr, char *buf)
235*0Sstevel@tonic-gate {
236*0Sstevel@tonic-gate 	return (ip_dot_saddr((uchar_t *)&addr, buf));
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate static int
http_clf_date(char * buf,int bufsize,time_t t)240*0Sstevel@tonic-gate http_clf_date(char *buf, int bufsize, time_t t)
241*0Sstevel@tonic-gate {
242*0Sstevel@tonic-gate 	struct tm	local_time;
243*0Sstevel@tonic-gate 	long		time_zone_info;
244*0Sstevel@tonic-gate 	char		sign;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	if (localtime_r(&t, &local_time) == NULL)
247*0Sstevel@tonic-gate 		return (0);
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	if (g_start_time.tm_year > 0 &&
250*0Sstevel@tonic-gate 	    (local_time.tm_year < g_start_time.tm_year ||
251*0Sstevel@tonic-gate 	    (local_time.tm_year == g_start_time.tm_year &&
252*0Sstevel@tonic-gate 	    local_time.tm_mon < g_start_time.tm_mon ||
253*0Sstevel@tonic-gate 	    (local_time.tm_mon == g_start_time.tm_mon &&
254*0Sstevel@tonic-gate 	    local_time.tm_mday < g_start_time.tm_mday ||
255*0Sstevel@tonic-gate 	    (local_time.tm_mday == g_start_time.tm_mday &&
256*0Sstevel@tonic-gate 	    local_time.tm_hour < g_start_time.tm_hour ||
257*0Sstevel@tonic-gate 	    (local_time.tm_hour == g_start_time.tm_hour &&
258*0Sstevel@tonic-gate 	    local_time.tm_min < g_start_time.tm_min ||
259*0Sstevel@tonic-gate 	    (local_time.tm_min == g_start_time.tm_min &&
260*0Sstevel@tonic-gate 	    local_time.tm_sec < g_start_time.tm_sec))))))) {
261*0Sstevel@tonic-gate 		/* clf record before the specified start time */
262*0Sstevel@tonic-gate 		return (1);
263*0Sstevel@tonic-gate 	}
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	if (local_time.tm_isdst)
266*0Sstevel@tonic-gate 		time_zone_info = -timezone + SEC_PER_HOUR;
267*0Sstevel@tonic-gate 	else
268*0Sstevel@tonic-gate 		time_zone_info = -timezone;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	if (time_zone_info < 0) {
271*0Sstevel@tonic-gate 		sign = '-';
272*0Sstevel@tonic-gate 		time_zone_info = -time_zone_info;
273*0Sstevel@tonic-gate 	} else {
274*0Sstevel@tonic-gate 		sign = '+';
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	(void) snprintf(buf, bufsize,
278*0Sstevel@tonic-gate 		"[%02d/%s/%04d:%02d:%02d:%02d %c%02ld%02ld]",
279*0Sstevel@tonic-gate 		local_time.tm_mday, sMonth(local_time.tm_mon),
280*0Sstevel@tonic-gate 		1900 + local_time.tm_year, local_time.tm_hour,
281*0Sstevel@tonic-gate 		local_time.tm_min, local_time.tm_sec,
282*0Sstevel@tonic-gate 		sign, time_zone_info / SEC_PER_HOUR,
283*0Sstevel@tonic-gate 		time_zone_info % SEC_PER_HOUR);
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	return (0);
286*0Sstevel@tonic-gate }
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate /*
289*0Sstevel@tonic-gate  * xmalloc(size)
290*0Sstevel@tonic-gate  * Abort if malloc fails
291*0Sstevel@tonic-gate  */
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate static void *
xmalloc(size_t size)294*0Sstevel@tonic-gate xmalloc(size_t size)
295*0Sstevel@tonic-gate {
296*0Sstevel@tonic-gate 	void *p;
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	if (! size)
299*0Sstevel@tonic-gate 		size = 1;
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	if ((p = malloc(size)) == NULL) {
302*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("Error: ncab2clf: Out of memory\n"));
303*0Sstevel@tonic-gate 		abort();
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	return (p);
307*0Sstevel@tonic-gate }
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate /*
310*0Sstevel@tonic-gate  * xstrdup(string)
311*0Sstevel@tonic-gate  *   duplicate string
312*0Sstevel@tonic-gate  */
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate static char *
xstrdup(const char * string)315*0Sstevel@tonic-gate xstrdup(const char *string)
316*0Sstevel@tonic-gate {
317*0Sstevel@tonic-gate 	char	*new_string;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	if (string) {
320*0Sstevel@tonic-gate 		new_string = xmalloc(strlen(string) + 1);
321*0Sstevel@tonic-gate 		(void) strcpy(new_string, string);
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 		return (new_string);
324*0Sstevel@tonic-gate 	}
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	return (NULL);
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate static void
usage()330*0Sstevel@tonic-gate usage()
331*0Sstevel@tonic-gate {
332*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
333*0Sstevel@tonic-gate 	    "\nncab2clf [-Dhv] [-b <block-size>] [-i <binary-log-file>] "
334*0Sstevel@tonic-gate 			"[-n <n>]\n"
335*0Sstevel@tonic-gate 		"    [-o <output-file>] [-s <date/time>]\n"
336*0Sstevel@tonic-gate 	    "\tconverts a NCA binary log file to HTTP CLF"
337*0Sstevel@tonic-gate 		" (Common Log Format)\n\n"
338*0Sstevel@tonic-gate 	    "\t-b <block-size>\n"
339*0Sstevel@tonic-gate 		"\t\tinput file blocking size in KB\n"
340*0Sstevel@tonic-gate 		"\t\t- default is 64K bytes\n"
341*0Sstevel@tonic-gate 	    "\t-D\tdisable directio on <output-file-name>\n"
342*0Sstevel@tonic-gate 	    "\t-h\tthis usage message\n"
343*0Sstevel@tonic-gate 	    "\t-i <binary-log-file>\n"
344*0Sstevel@tonic-gate 		"\t\tspecify input file\n"
345*0Sstevel@tonic-gate 	    "\t-n <n>\n"
346*0Sstevel@tonic-gate 		"\t\toutput <n> CLF records\n"
347*0Sstevel@tonic-gate 	    "\t-o <output-file>\n"
348*0Sstevel@tonic-gate 		"\t\tspecify output file\n"
349*0Sstevel@tonic-gate 	    "\t-s <date/time>\n"
350*0Sstevel@tonic-gate 		"\t\tskip any records before <date/time>\n"
351*0Sstevel@tonic-gate 		"\t\t- <date/time> may be in CLF format\n"
352*0Sstevel@tonic-gate 		"\t\t- <date/time> may be in time format as specified "
353*0Sstevel@tonic-gate 			"by touch(1)\n"
354*0Sstevel@tonic-gate 	    "\t-v\tverbose output\n"
355*0Sstevel@tonic-gate 	    "\tNote: if no <output-file> - output goes to standard output\n"
356*0Sstevel@tonic-gate 	    "\tNote: if no <binary-log-file> - input is taken from standard "
357*0Sstevel@tonic-gate 			"input\n"));
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	exit(3);
360*0Sstevel@tonic-gate }
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate /*
363*0Sstevel@tonic-gate  * atoi_for2(p, value)
364*0Sstevel@tonic-gate  *   - stores the numerical value of the two digit string p into value
365*0Sstevel@tonic-gate  *   - return TRUE upon success and FALSE upon failure
366*0Sstevel@tonic-gate  */
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate static int
atoi_for2(char * p,int * value)369*0Sstevel@tonic-gate atoi_for2(char *p, int *value) {
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 	*value = (*p - '0') * 10 + *(p+1) - '0';
372*0Sstevel@tonic-gate 	if ((*value < 0) || (*value > 99))
373*0Sstevel@tonic-gate 		return (FALSE);
374*0Sstevel@tonic-gate 	return (TRUE);
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate /*
378*0Sstevel@tonic-gate  * parse_time(t, tm)
379*0Sstevel@tonic-gate  *   - parses the string t to retrieve the UNIX time format as specified by
380*0Sstevel@tonic-gate  *     touch(1).
381*0Sstevel@tonic-gate  *   - return TRUE upon success and FALSE upon failure
382*0Sstevel@tonic-gate  */
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate static int
parse_time(char * t,struct tm * tm)385*0Sstevel@tonic-gate parse_time(char *t, struct tm *tm)
386*0Sstevel@tonic-gate {
387*0Sstevel@tonic-gate 	int		century = 0;
388*0Sstevel@tonic-gate 	int		seconds = 0;
389*0Sstevel@tonic-gate 	time_t		when;
390*0Sstevel@tonic-gate 	char		*p;
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	/*
393*0Sstevel@tonic-gate 	 * time in the following format (defined by the touch(1) spec):
394*0Sstevel@tonic-gate 	 *	[[CC]YY]MMDDhhmm[.SS]
395*0Sstevel@tonic-gate 	 */
396*0Sstevel@tonic-gate 	if ((p = strchr(t, '.')) != NULL) {
397*0Sstevel@tonic-gate 		if (strchr(p+1, '.') != NULL)
398*0Sstevel@tonic-gate 			return (FALSE);
399*0Sstevel@tonic-gate 		if (!atoi_for2(p+1, &seconds))
400*0Sstevel@tonic-gate 			return (FALSE);
401*0Sstevel@tonic-gate 		*p = '\0';
402*0Sstevel@tonic-gate 	}
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	when = time(0);
405*0Sstevel@tonic-gate 	bzero(tm, sizeof (struct tm));
406*0Sstevel@tonic-gate 	tm->tm_year = localtime(&when)->tm_year;
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	switch (strlen(t)) {
409*0Sstevel@tonic-gate 		case 12:	/* CCYYMMDDhhmm */
410*0Sstevel@tonic-gate 			if (!atoi_for2(t, &century))
411*0Sstevel@tonic-gate 				return (FALSE);
412*0Sstevel@tonic-gate 			t += 2;
413*0Sstevel@tonic-gate 			/* FALLTHROUGH */
414*0Sstevel@tonic-gate 		case 10:	/* YYMMDDhhmm */
415*0Sstevel@tonic-gate 			if (!atoi_for2(t, &tm->tm_year))
416*0Sstevel@tonic-gate 				return (FALSE);
417*0Sstevel@tonic-gate 			t += 2;
418*0Sstevel@tonic-gate 			if (century == 0) {
419*0Sstevel@tonic-gate 				if (tm->tm_year < 69)
420*0Sstevel@tonic-gate 					tm->tm_year += 100;
421*0Sstevel@tonic-gate 			} else
422*0Sstevel@tonic-gate 				tm->tm_year += (century - 19) * 100;
423*0Sstevel@tonic-gate 			/* FALLTHROUGH */
424*0Sstevel@tonic-gate 		case 8:		/* MMDDhhmm */
425*0Sstevel@tonic-gate 			if (!atoi_for2(t, &tm->tm_mon))
426*0Sstevel@tonic-gate 				return (FALSE);
427*0Sstevel@tonic-gate 			tm->tm_mon--;
428*0Sstevel@tonic-gate 			t += 2;
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 			if (!atoi_for2(t, &tm->tm_mday))
431*0Sstevel@tonic-gate 				return (FALSE);
432*0Sstevel@tonic-gate 			t += 2;
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 			if (!atoi_for2(t, &tm->tm_hour))
435*0Sstevel@tonic-gate 				return (FALSE);
436*0Sstevel@tonic-gate 			t += 2;
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 			if (!atoi_for2(t, &tm->tm_min))
439*0Sstevel@tonic-gate 				return (FALSE);
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 			tm->tm_sec = seconds;
442*0Sstevel@tonic-gate 			break;
443*0Sstevel@tonic-gate 		default:
444*0Sstevel@tonic-gate 			return (FALSE);
445*0Sstevel@tonic-gate 	}
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	return (TRUE);
448*0Sstevel@tonic-gate }
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate static void
close_files(int ifd,int ofd)451*0Sstevel@tonic-gate close_files(int ifd, int ofd)
452*0Sstevel@tonic-gate {
453*0Sstevel@tonic-gate 	if (ifd != STDIN_FILENO)
454*0Sstevel@tonic-gate 		(void) close(ifd);
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	if (ofd != STDOUT_FILENO)
457*0Sstevel@tonic-gate 		(void) close(ofd);
458*0Sstevel@tonic-gate }
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate /*
461*0Sstevel@tonic-gate  * Read the requested number of bytes from the given file descriptor
462*0Sstevel@tonic-gate  */
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate static ssize_t
read_n_bytes(int fd,char * buf,ssize_t bufsize)465*0Sstevel@tonic-gate read_n_bytes(int fd, char *buf, ssize_t bufsize)
466*0Sstevel@tonic-gate {
467*0Sstevel@tonic-gate 	ssize_t	num_to_read = bufsize;
468*0Sstevel@tonic-gate 	ssize_t	num_already_read = 0;
469*0Sstevel@tonic-gate 	ssize_t	i;
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	while (num_to_read > 0) {
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 		i = read(fd, &(buf[num_already_read]), num_to_read);
474*0Sstevel@tonic-gate 		if (i < 0) {
475*0Sstevel@tonic-gate 			if (errno == EINTR)
476*0Sstevel@tonic-gate 				continue;
477*0Sstevel@tonic-gate 			else
478*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
479*0Sstevel@tonic-gate 				    "Error: ncab2clf: "
480*0Sstevel@tonic-gate 				    "reading input file: %s\n"),
481*0Sstevel@tonic-gate 				    strerror(errno));
482*0Sstevel@tonic-gate 				return (-1);	/* some wierd interrupt */
483*0Sstevel@tonic-gate 		}
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 		if (i == 0)
486*0Sstevel@tonic-gate 			break;
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 		num_already_read += i;
489*0Sstevel@tonic-gate 		num_to_read -= i;
490*0Sstevel@tonic-gate 	}
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 	return (num_already_read);
493*0Sstevel@tonic-gate }
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate /*
496*0Sstevel@tonic-gate  * Write the requested number of bytes to the given file descriptor
497*0Sstevel@tonic-gate  */
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate static ssize_t
write_n_bytes(int fd,char * buf,ssize_t bufsize)500*0Sstevel@tonic-gate write_n_bytes(int fd, char *buf, ssize_t bufsize)
501*0Sstevel@tonic-gate {
502*0Sstevel@tonic-gate 	ssize_t	num_to_write = bufsize;
503*0Sstevel@tonic-gate 	ssize_t	num_written = 0;
504*0Sstevel@tonic-gate 	ssize_t	i;
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate 	while (num_to_write > 0) {
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate 		i = write(fd, &(buf[num_written]), num_to_write);
509*0Sstevel@tonic-gate 		if (i < 0) {
510*0Sstevel@tonic-gate 			if (errno == EINTR)
511*0Sstevel@tonic-gate 				continue;
512*0Sstevel@tonic-gate 			else
513*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
514*0Sstevel@tonic-gate 				    "Error: ncab2clf: "
515*0Sstevel@tonic-gate 				    "writing output file: %s\n"),
516*0Sstevel@tonic-gate 				    strerror(errno));
517*0Sstevel@tonic-gate 				return (-1);	/* some wierd interrupt */
518*0Sstevel@tonic-gate 		}
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 		num_written += i;
521*0Sstevel@tonic-gate 		num_to_write -= i;
522*0Sstevel@tonic-gate 	}
523*0Sstevel@tonic-gate 
524*0Sstevel@tonic-gate 	return (num_written);
525*0Sstevel@tonic-gate }
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate /* do constraint checks and determine if it's a valid header */
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate static bool
is_valid_header(void * ibuf)530*0Sstevel@tonic-gate is_valid_header(void *ibuf)
531*0Sstevel@tonic-gate {
532*0Sstevel@tonic-gate 	nca_log_buf_hdr_t	*h;
533*0Sstevel@tonic-gate 	nca_log_stat_t		*s;
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate 	h = (nca_log_buf_hdr_t *)ibuf;
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	/* Do some validity checks on ibuf */
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate 	if (((h->nca_loghdr).nca_version != NCA_LOG_VERSION1) ||
540*0Sstevel@tonic-gate 	    ((h->nca_loghdr).nca_op != log_op)) {
541*0Sstevel@tonic-gate 		return (false);
542*0Sstevel@tonic-gate 	}
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	s = &(h->nca_logstats);
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 	if (g_n_log_upcall == 0) {
547*0Sstevel@tonic-gate 		g_n_log_upcall = s->n_log_upcall;
548*0Sstevel@tonic-gate 	} else {
549*0Sstevel@tonic-gate 		if ((++g_n_log_upcall) != (ssize_t)s->n_log_upcall) {
550*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
551*0Sstevel@tonic-gate 				"Warning: ncab2clf:"
552*0Sstevel@tonic-gate 				" expected record number (%d) is"
553*0Sstevel@tonic-gate 				" different from the one seen (%d)\n."
554*0Sstevel@tonic-gate 				" Resetting the expected record"
555*0Sstevel@tonic-gate 				" number.\n"), g_n_log_upcall, s->n_log_upcall);
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 			g_n_log_upcall = s->n_log_upcall;
558*0Sstevel@tonic-gate 		}
559*0Sstevel@tonic-gate 	}
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	return (true);
562*0Sstevel@tonic-gate }
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate /* convert input binary buffer into CLF */
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate static int
b2clf_buf(void * ibuf,char * obuf,ssize_t isize,ssize_t osize,ssize_t * out_size)567*0Sstevel@tonic-gate b2clf_buf(
568*0Sstevel@tonic-gate 	void	*ibuf,
569*0Sstevel@tonic-gate 	char	*obuf,
570*0Sstevel@tonic-gate 	ssize_t	isize,
571*0Sstevel@tonic-gate 	ssize_t	osize,
572*0Sstevel@tonic-gate 	ssize_t	*out_size)
573*0Sstevel@tonic-gate {
574*0Sstevel@tonic-gate 	nca_log_buf_hdr_t	*h;
575*0Sstevel@tonic-gate 	nca_log_stat_t		*s;
576*0Sstevel@tonic-gate 	nca_request_log_t	*r;
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 	char	*br;
579*0Sstevel@tonic-gate 	void	*er;
580*0Sstevel@tonic-gate 	char	ip_buf[64];
581*0Sstevel@tonic-gate 	ssize_t	max_input_size, num_bytes_read;
582*0Sstevel@tonic-gate 	int	n_recs;
583*0Sstevel@tonic-gate 	bool	error_seen;
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	ssize_t	count;
586*0Sstevel@tonic-gate 	char	clf_timebuf[CLF_DATE_BUF_LENGTH];
587*0Sstevel@tonic-gate 	char	*method;
588*0Sstevel@tonic-gate 	char	*http_version_string;
589*0Sstevel@tonic-gate 	char	*ruser;
590*0Sstevel@tonic-gate 	char	*req_url;
591*0Sstevel@tonic-gate 	char	*remote_ip;
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 	h = (nca_log_buf_hdr_t *)ibuf;
594*0Sstevel@tonic-gate 	s = &(h->nca_logstats);
595*0Sstevel@tonic-gate 	r = (nca_request_log_t *)(&(h[1]));
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 	/* OK, it's a valid buffer which we can use, go ahead and convert it */
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	max_input_size = (ssize_t)isize - sizeof (nca_log_buf_hdr_t);
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	*out_size = 0;
602*0Sstevel@tonic-gate 	error_seen = false;
603*0Sstevel@tonic-gate 	num_bytes_read = 0;
604*0Sstevel@tonic-gate 	for (n_recs = 0; n_recs < s->n_log_recs; n_recs++) {
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 		/* Make sure there is enough space in the output buffer */
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 		if ((*out_size >= osize) ||
609*0Sstevel@tonic-gate 				(num_bytes_read >= max_input_size)) {
610*0Sstevel@tonic-gate 			error_seen = true;
611*0Sstevel@tonic-gate 			break;
612*0Sstevel@tonic-gate 		}
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 		if (http_clf_date(clf_timebuf, sizeof (clf_timebuf),
615*0Sstevel@tonic-gate 		    ((time_t)r->start_process_time))) {
616*0Sstevel@tonic-gate 			/* A start time was speced and we're not there yet */
617*0Sstevel@tonic-gate 			++g_skip_count;
618*0Sstevel@tonic-gate 			goto skip;
619*0Sstevel@tonic-gate 		}
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 		/* Only logs valid HTTP ops */
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 		if ((! valid_method((int)r->method)) ||
624*0Sstevel@tonic-gate 				(! valid_version((int)r->version))) {
625*0Sstevel@tonic-gate 			++g_invalid_count;
626*0Sstevel@tonic-gate 			goto skip;
627*0Sstevel@tonic-gate 		}
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate 		method = http_method((int)r->method);
630*0Sstevel@tonic-gate 		http_version_string = http_version((int)r->version);
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 		remote_ip = ip_dot_addr(r->remote_host, (char *)&ip_buf);
633*0Sstevel@tonic-gate 		if (r->remote_user_len) {
634*0Sstevel@tonic-gate 			ruser = NCA_REQLOG_RDATA(r, remote_user);
635*0Sstevel@tonic-gate 		} else {
636*0Sstevel@tonic-gate 			ruser = "-";
637*0Sstevel@tonic-gate 		}
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 		if (r->request_url_len) {
640*0Sstevel@tonic-gate 			req_url = NCA_REQLOG_RDATA(r, request_url);
641*0Sstevel@tonic-gate 		} else {
642*0Sstevel@tonic-gate 			req_url = "UNKNOWN";
643*0Sstevel@tonic-gate 		}
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate 		count = (ssize_t)snprintf(&(obuf[*out_size]), osize - *out_size,
646*0Sstevel@tonic-gate 				"%s %s %s %s \"%s %s %s\" %d %d\n",
647*0Sstevel@tonic-gate 				((remote_ip) ? remote_ip : "-"),
648*0Sstevel@tonic-gate 				/* should be remote_log_name */
649*0Sstevel@tonic-gate 				"-",
650*0Sstevel@tonic-gate 				ruser,
651*0Sstevel@tonic-gate 				clf_timebuf,
652*0Sstevel@tonic-gate 				method,
653*0Sstevel@tonic-gate 				req_url,
654*0Sstevel@tonic-gate 				http_version_string,
655*0Sstevel@tonic-gate 				r->response_status,
656*0Sstevel@tonic-gate 				r->response_len);
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 		*out_size += count;
659*0Sstevel@tonic-gate 	skip:
660*0Sstevel@tonic-gate 		br = (char *)r;
661*0Sstevel@tonic-gate 		er = ((char *)r) + NCA_LOG_REC_SIZE(r);
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 		/*LINTED*/
664*0Sstevel@tonic-gate 		r = (nca_request_log_t *)NCA_LOG_ALIGN(er);
665*0Sstevel@tonic-gate 		num_bytes_read += (ssize_t)(((char *)r) - br);
666*0Sstevel@tonic-gate 		if (g_out_records > 0 && --g_out_records == 0)
667*0Sstevel@tonic-gate 			break;
668*0Sstevel@tonic-gate 	}
669*0Sstevel@tonic-gate 
670*0Sstevel@tonic-gate 	if (error_seen) {
671*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
672*0Sstevel@tonic-gate 			"Error: ncab2clf: "
673*0Sstevel@tonic-gate 			"Input buffer not fully converted.\n"));
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 		if (n_recs != s->n_log_recs)
676*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
677*0Sstevel@tonic-gate 				"Warning: ncab2clf: "
678*0Sstevel@tonic-gate 				"Converted only %d of %d records\n"),
679*0Sstevel@tonic-gate 				n_recs, s->n_log_recs);
680*0Sstevel@tonic-gate 	}
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 	return (0);
683*0Sstevel@tonic-gate }
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate static int
b2clf(int ifd,int ofd)686*0Sstevel@tonic-gate b2clf(int ifd, int ofd)
687*0Sstevel@tonic-gate {
688*0Sstevel@tonic-gate 	char	*ibuf;
689*0Sstevel@tonic-gate 	char	*obuf;
690*0Sstevel@tonic-gate 	bool	error_seen;
691*0Sstevel@tonic-gate 	bool	eof_seen;
692*0Sstevel@tonic-gate 	ssize_t	num_iterations, ni, nh, no, olen;
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate 	nca_log_buf_hdr_t	*h;
695*0Sstevel@tonic-gate 	nca_log_stat_t		*s;
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	ibuf = xmalloc(g_infile_blk_size);
698*0Sstevel@tonic-gate 	obuf = xmalloc(OUTFILE_BUF_SIZE);
699*0Sstevel@tonic-gate 	error_seen = false;
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 	eof_seen = false;
702*0Sstevel@tonic-gate 	num_iterations = 0;
703*0Sstevel@tonic-gate 	while (! eof_seen && g_out_records != 0) {
704*0Sstevel@tonic-gate 		++num_iterations;
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 		nh = ni = no = 0;
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 		/* read the binary header first */
709*0Sstevel@tonic-gate 		nh = read_n_bytes(ifd, ibuf, sizeof (nca_log_buf_hdr_t));
710*0Sstevel@tonic-gate 		if (nh != sizeof (nca_log_buf_hdr_t)) {
711*0Sstevel@tonic-gate 			eof_seen = true;
712*0Sstevel@tonic-gate 			break;
713*0Sstevel@tonic-gate 		}
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate 		if (! is_valid_header(ibuf)) {
716*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
717*0Sstevel@tonic-gate 			    "Error: ncab2clf: "
718*0Sstevel@tonic-gate 			    "Can't convert the input data to CLF\n"));
719*0Sstevel@tonic-gate 			continue;
720*0Sstevel@tonic-gate 		}
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 		/* read the data to be converted */
723*0Sstevel@tonic-gate 		/* LINTED */
724*0Sstevel@tonic-gate 		h = (nca_log_buf_hdr_t *)ibuf;
725*0Sstevel@tonic-gate 		s = &(h->nca_logstats);
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 		if (s->n_log_size == 0)
728*0Sstevel@tonic-gate 			continue;
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 		ni = read_n_bytes(ifd, &(ibuf[nh]), (ssize_t)s->n_log_size);
731*0Sstevel@tonic-gate 		if (ni < 0) {
732*0Sstevel@tonic-gate 			error_seen = true;
733*0Sstevel@tonic-gate 			break;
734*0Sstevel@tonic-gate 		} else if (ni < (ssize_t)s->n_log_size) {
735*0Sstevel@tonic-gate 			eof_seen = true;
736*0Sstevel@tonic-gate 		}
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 		if (ni == 0)
739*0Sstevel@tonic-gate 			break;
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 		/* convert binary input into text output */
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 		if (b2clf_buf(ibuf, obuf, ni + nh, OUTFILE_BUF_SIZE, &olen)) {
744*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
745*0Sstevel@tonic-gate 			    "Error: ncab2clf: "
746*0Sstevel@tonic-gate 			    "Can't convert the input data to CLF\n"));
747*0Sstevel@tonic-gate 			error_seen = true;
748*0Sstevel@tonic-gate 			break;
749*0Sstevel@tonic-gate 		}
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 		/* write out the text data */
752*0Sstevel@tonic-gate 		no = write_n_bytes(ofd, obuf, olen);
753*0Sstevel@tonic-gate 		if (no != olen) {
754*0Sstevel@tonic-gate 			error_seen = true;
755*0Sstevel@tonic-gate 			break;
756*0Sstevel@tonic-gate 		}
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 		bzero(ibuf, nh + ni);
759*0Sstevel@tonic-gate 		bzero(obuf, no);
760*0Sstevel@tonic-gate 	}
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 	free(ibuf);
763*0Sstevel@tonic-gate 	free(obuf);
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate 	if (error_seen)
766*0Sstevel@tonic-gate 		return (-1);
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	return (0);
769*0Sstevel@tonic-gate }
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate int
main(int argc,char ** argv)773*0Sstevel@tonic-gate main(int argc, char **argv)
774*0Sstevel@tonic-gate {
775*0Sstevel@tonic-gate 	int	c;
776*0Sstevel@tonic-gate 	int	ifd;		/* input fd - binary log file */
777*0Sstevel@tonic-gate 	int	ofd;
778*0Sstevel@tonic-gate 	struct tm t;
779*0Sstevel@tonic-gate 
780*0Sstevel@tonic-gate 	char	*infile = NULL;  /* input file name */
781*0Sstevel@tonic-gate 	char	*outfile = NULL; /* output file name */
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate 	char	monstr[64];
784*0Sstevel@tonic-gate 
785*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
786*0Sstevel@tonic-gate 
787*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
788*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
789*0Sstevel@tonic-gate #endif
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 	/* parse any arguments */
794*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hvDi:o:b:n:s:")) != EOF) {
795*0Sstevel@tonic-gate 		switch (c) {
796*0Sstevel@tonic-gate 		case 'h':
797*0Sstevel@tonic-gate 			usage();
798*0Sstevel@tonic-gate 			break;
799*0Sstevel@tonic-gate 		case 'i':
800*0Sstevel@tonic-gate 			infile = xstrdup(optarg);
801*0Sstevel@tonic-gate 			break;
802*0Sstevel@tonic-gate 		case 'D':
803*0Sstevel@tonic-gate 			g_enable_directio = false;
804*0Sstevel@tonic-gate 			break;
805*0Sstevel@tonic-gate 		case 'o':
806*0Sstevel@tonic-gate 			outfile = xstrdup(optarg);
807*0Sstevel@tonic-gate 			break;
808*0Sstevel@tonic-gate 		case 'b':
809*0Sstevel@tonic-gate 			g_infile_blk_size = (KILO_BYTE * atoi(optarg));
810*0Sstevel@tonic-gate 			break;
811*0Sstevel@tonic-gate 		case 'n':
812*0Sstevel@tonic-gate 			g_out_records = atoi(optarg);
813*0Sstevel@tonic-gate 			break;
814*0Sstevel@tonic-gate 		case 's':
815*0Sstevel@tonic-gate 			g_start_time_str = strdup(optarg);
816*0Sstevel@tonic-gate 			bzero(&t, sizeof (t));
817*0Sstevel@tonic-gate 			if (sscanf(optarg, "%d/%3s/%d:%d:%d:%d", &t.tm_mday,
818*0Sstevel@tonic-gate 			    &monstr[0], &t.tm_year, &t.tm_hour, &t.tm_min,
819*0Sstevel@tonic-gate 			    &t.tm_sec) == 6) {
820*0Sstevel@tonic-gate 				/* Valid CLF time (e.g. 06/Apr/2001:09:14:14) */
821*0Sstevel@tonic-gate 				t.tm_mon = 0;
822*0Sstevel@tonic-gate 				do {
823*0Sstevel@tonic-gate 					if (strcasecmp(monstr,
824*0Sstevel@tonic-gate 					    sMonthStr[t.tm_mon]) == 0)
825*0Sstevel@tonic-gate 						break;
826*0Sstevel@tonic-gate 				} while (t.tm_mon++ < 12);
827*0Sstevel@tonic-gate 				t.tm_year -= 1900;
828*0Sstevel@tonic-gate 				g_start_time = t;
829*0Sstevel@tonic-gate 			} else if (parse_time(optarg, &t)) {
830*0Sstevel@tonic-gate 				g_start_time = t;
831*0Sstevel@tonic-gate 			} else {
832*0Sstevel@tonic-gate 				(void) fprintf(stderr,
833*0Sstevel@tonic-gate 				    gettext("Error: ncab2clf:"
834*0Sstevel@tonic-gate 				    " %s: unrecognized date/time.\n"),
835*0Sstevel@tonic-gate 				    optarg);
836*0Sstevel@tonic-gate 			}
837*0Sstevel@tonic-gate 			break;
838*0Sstevel@tonic-gate 		case 'v':
839*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Error: ncab2clf: "
840*0Sstevel@tonic-gate 			    "verbose functionality not yet supported\n"));
841*0Sstevel@tonic-gate 			exit(3);
842*0Sstevel@tonic-gate 			break;
843*0Sstevel@tonic-gate 		case '?':
844*0Sstevel@tonic-gate 			usage();
845*0Sstevel@tonic-gate 			break;
846*0Sstevel@tonic-gate 		}
847*0Sstevel@tonic-gate 	}
848*0Sstevel@tonic-gate 
849*0Sstevel@tonic-gate 	/* set up the input stream */
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	if (infile) {
852*0Sstevel@tonic-gate 
853*0Sstevel@tonic-gate 		if ((ifd = open(infile, O_RDONLY)) < 0) {
854*0Sstevel@tonic-gate 			(void) fprintf(stderr,
855*0Sstevel@tonic-gate 				gettext("Error: ncab2clf: "
856*0Sstevel@tonic-gate 				"Failure to open binary log file %s: %s\n"),
857*0Sstevel@tonic-gate 				infile, strerror(errno));
858*0Sstevel@tonic-gate 			exit(1);
859*0Sstevel@tonic-gate 		}
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 	} else {
862*0Sstevel@tonic-gate 		ifd = STDIN_FILENO;
863*0Sstevel@tonic-gate 	}
864*0Sstevel@tonic-gate 
865*0Sstevel@tonic-gate 	/* set up the output stream */
866*0Sstevel@tonic-gate 
867*0Sstevel@tonic-gate 	if (outfile) {
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate 		if ((ofd = open(outfile, O_WRONLY|O_CREAT, 0644)) < 0) {
870*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
871*0Sstevel@tonic-gate 			    "Error: ncab2clf: "
872*0Sstevel@tonic-gate 			    "Failure to open output file %s: %s\n"),
873*0Sstevel@tonic-gate 			    outfile, strerror(errno));
874*0Sstevel@tonic-gate 			exit(1);
875*0Sstevel@tonic-gate 		}
876*0Sstevel@tonic-gate 
877*0Sstevel@tonic-gate 		/* Enable directio on output stream if specified */
878*0Sstevel@tonic-gate 
879*0Sstevel@tonic-gate 		if (g_enable_directio)
880*0Sstevel@tonic-gate 			(void) directio(ofd, DIRECTIO_ON);
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 	} else {
883*0Sstevel@tonic-gate 		ofd = STDOUT_FILENO;
884*0Sstevel@tonic-gate 	}
885*0Sstevel@tonic-gate 
886*0Sstevel@tonic-gate 	if ((b2clf(ifd, ofd) != 0)) {
887*0Sstevel@tonic-gate 		close_files(ifd, ofd);
888*0Sstevel@tonic-gate 		exit(2);
889*0Sstevel@tonic-gate 	}
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 	close_files(ifd, ofd);
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 	if (g_invalid_count) {
894*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Warning: ncab2clf: %d"
895*0Sstevel@tonic-gate 		" number of invalid log records encountered in binary input"
896*0Sstevel@tonic-gate 		" file were skipped\n"), g_invalid_count);
897*0Sstevel@tonic-gate 	}
898*0Sstevel@tonic-gate 	if (g_skip_count) {
899*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Warning: ncab2clf:"
900*0Sstevel@tonic-gate 		    " %d log records in binary input file before %s"
901*0Sstevel@tonic-gate 		    " were skipped\n"),
902*0Sstevel@tonic-gate 		    g_skip_count, g_start_time_str);
903*0Sstevel@tonic-gate 	}
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate 	return (0);
906*0Sstevel@tonic-gate }
907