2000-02-09 03:58:47 +08:00
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-12-10 15:42:50 +08:00
|
|
|
/*
|
2003-03-19 17:13:01 +08:00
|
|
|
* head implementation for busybox
|
1999-12-10 15:42:50 +08:00
|
|
|
*
|
2003-03-19 17:13:01 +08:00
|
|
|
* Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
|
1999-12-10 15:42:50 +08:00
|
|
|
*
|
2006-01-30 09:30:39 +08:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
1999-12-10 15:42:50 +08:00
|
|
|
*/
|
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
/* BB_AUDIT SUSv3 compliant */
|
|
|
|
/* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
|
|
|
|
/* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
|
|
|
|
|
1999-12-10 15:42:50 +08:00
|
|
|
#include <stdio.h>
|
2001-01-27 16:24:39 +08:00
|
|
|
#include <stdlib.h>
|
2003-03-19 17:13:01 +08:00
|
|
|
#include <limits.h>
|
2002-05-28 06:24:53 +08:00
|
|
|
#include <ctype.h>
|
2003-03-19 17:13:01 +08:00
|
|
|
#include <unistd.h>
|
2001-02-20 14:14:08 +08:00
|
|
|
#include "busybox.h"
|
1999-12-10 15:42:50 +08:00
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
static const char head_opts[] =
|
|
|
|
"n:"
|
2005-12-13 18:48:45 +08:00
|
|
|
#if ENABLE_FEATURE_FANCY_HEAD
|
2003-03-19 17:13:01 +08:00
|
|
|
"c:qv"
|
|
|
|
#endif
|
|
|
|
;
|
1999-12-10 15:42:50 +08:00
|
|
|
|
2006-01-30 19:15:11 +08:00
|
|
|
#if ENABLE_FEATURE_FANCY_HEAD
|
|
|
|
static const struct suffix_mult head_suffixes[] = {
|
|
|
|
{ "b", 512 },
|
|
|
|
{ "k", 1024 },
|
|
|
|
{ "m", 1024*1024 },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
static const char header_fmt_str[] = "\n==> %s <==\n";
|
1999-12-10 15:42:50 +08:00
|
|
|
|
2000-02-09 03:58:47 +08:00
|
|
|
int head_main(int argc, char **argv)
|
1999-12-10 15:42:50 +08:00
|
|
|
{
|
2003-03-19 17:13:01 +08:00
|
|
|
unsigned long count = 10;
|
|
|
|
unsigned long i;
|
2005-12-13 18:48:45 +08:00
|
|
|
#if ENABLE_FEATURE_FANCY_HEAD
|
2003-03-19 17:13:01 +08:00
|
|
|
int count_bytes = 0;
|
|
|
|
int header_threshhold = 1;
|
|
|
|
#endif
|
|
|
|
|
2000-09-27 12:09:22 +08:00
|
|
|
FILE *fp;
|
2003-03-19 17:13:01 +08:00
|
|
|
const char *fmt;
|
|
|
|
char *p;
|
|
|
|
int opt;
|
|
|
|
int c;
|
|
|
|
int retval = EXIT_SUCCESS;
|
1999-12-10 15:42:50 +08:00
|
|
|
|
2006-01-30 09:30:39 +08:00
|
|
|
#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
|
2003-03-19 17:13:01 +08:00
|
|
|
/* Allow legacy syntax of an initial numeric option without -n. */
|
|
|
|
if ((argc > 1) && (argv[1][0] == '-')
|
|
|
|
/* && (isdigit)(argv[1][1]) */
|
|
|
|
&& (((unsigned int)(argv[1][1] - '0')) <= 9)
|
|
|
|
) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
p = (*argv) + 1;
|
|
|
|
goto GET_COUNT;
|
2002-05-18 06:18:04 +08:00
|
|
|
}
|
2005-12-11 11:09:05 +08:00
|
|
|
#endif
|
2002-05-18 06:18:04 +08:00
|
|
|
|
2005-12-11 11:09:05 +08:00
|
|
|
/* No size benefit in converting this to bb_getopt_ulflags */
|
2003-03-19 17:13:01 +08:00
|
|
|
while ((opt = getopt(argc, argv, head_opts)) > 0) {
|
|
|
|
switch(opt) {
|
2005-12-13 18:48:45 +08:00
|
|
|
#if ENABLE_FEATURE_FANCY_HEAD
|
2003-03-19 17:13:01 +08:00
|
|
|
case 'q':
|
|
|
|
header_threshhold = INT_MAX;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
header_threshhold = -1;
|
2000-02-09 03:58:47 +08:00
|
|
|
break;
|
2003-03-19 17:13:01 +08:00
|
|
|
case 'c':
|
|
|
|
count_bytes = 1;
|
|
|
|
/* fall through */
|
|
|
|
#endif
|
|
|
|
case 'n':
|
|
|
|
p = optarg;
|
2006-01-30 09:30:39 +08:00
|
|
|
#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
|
2003-03-19 17:13:01 +08:00
|
|
|
GET_COUNT:
|
2005-12-13 18:48:45 +08:00
|
|
|
#endif
|
2006-01-30 19:15:11 +08:00
|
|
|
|
|
|
|
#if !ENABLE_FEATURE_FANCY_HEAD
|
2003-03-19 17:13:01 +08:00
|
|
|
count = bb_xgetularg10(p);
|
2006-01-30 19:15:11 +08:00
|
|
|
#else
|
|
|
|
count = bb_xgetularg_bnd_sfx(p, 10,
|
|
|
|
0, ULONG_MAX,
|
|
|
|
head_suffixes);
|
|
|
|
#endif
|
2003-03-19 17:13:01 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_show_usage();
|
2000-02-09 03:58:47 +08:00
|
|
|
}
|
1999-12-10 15:42:50 +08:00
|
|
|
}
|
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
argv += optind;
|
|
|
|
if (!*argv) {
|
|
|
|
*--argv = "-";
|
|
|
|
}
|
1999-12-10 15:42:50 +08:00
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
fmt = header_fmt_str + 1;
|
2005-12-13 18:48:45 +08:00
|
|
|
#if ENABLE_FEATURE_FANCY_HEAD
|
2003-03-19 17:13:01 +08:00
|
|
|
if (argc - optind <= header_threshhold) {
|
|
|
|
header_threshhold = 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (argc <= optind + 1) {
|
|
|
|
fmt += 11;
|
|
|
|
}
|
|
|
|
/* Now define some things here to avoid #ifdefs in the code below.
|
|
|
|
* These should optimize out of the if conditions below. */
|
|
|
|
#define header_threshhold 1
|
|
|
|
#define count_bytes 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
do {
|
|
|
|
if ((fp = bb_wfopen_input(*argv)) != NULL) {
|
|
|
|
if (fp == stdin) {
|
|
|
|
*argv = (char *) bb_msg_standard_input;
|
2000-09-27 12:09:22 +08:00
|
|
|
}
|
2003-03-19 17:13:01 +08:00
|
|
|
if (header_threshhold) {
|
|
|
|
bb_printf(fmt, *argv);
|
2000-02-09 03:58:47 +08:00
|
|
|
}
|
2003-03-19 17:13:01 +08:00
|
|
|
i = count;
|
|
|
|
while (i && ((c = getc(fp)) != EOF)) {
|
|
|
|
if (count_bytes || (c == '\n')) {
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
putchar(c);
|
|
|
|
}
|
|
|
|
if (bb_fclose_nonstdin(fp)) {
|
|
|
|
bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
|
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
bb_xferror_stdout();
|
1999-12-10 15:42:50 +08:00
|
|
|
}
|
2003-03-19 17:13:01 +08:00
|
|
|
fmt = header_fmt_str;
|
|
|
|
} while (*++argv);
|
1999-12-10 15:42:50 +08:00
|
|
|
|
2003-03-19 17:13:01 +08:00
|
|
|
bb_fflush_stdout_and_exit(retval);
|
2000-09-27 12:09:22 +08:00
|
|
|
}
|