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