2000-02-08 19:58:47 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-12-10 07:42:50 +00:00
|
|
|
/*
|
2003-03-19 09:13:01 +00:00
|
|
|
* head implementation for busybox
|
1999-12-10 07:42:50 +00:00
|
|
|
*
|
2003-03-19 09:13:01 +00:00
|
|
|
* Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
|
1999-12-10 07:42:50 +00:00
|
|
|
*
|
2006-01-30 01:30:39 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
1999-12-10 07:42:50 +00:00
|
|
|
*/
|
|
|
|
|
2003-03-19 09:13:01 +00: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 */
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
1999-12-10 07:42:50 +00:00
|
|
|
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char head_opts[] ALIGN1 =
|
2003-03-19 09:13:01 +00:00
|
|
|
"n:"
|
2005-12-13 10:48:45 +00:00
|
|
|
#if ENABLE_FEATURE_FANCY_HEAD
|
2003-03-19 09:13:01 +00:00
|
|
|
"c:qv"
|
|
|
|
#endif
|
|
|
|
;
|
1999-12-10 07:42:50 +00:00
|
|
|
|
2006-01-30 11:15:11 +00:00
|
|
|
#if ENABLE_FEATURE_FANCY_HEAD
|
|
|
|
static const struct suffix_mult head_suffixes[] = {
|
|
|
|
{ "b", 512 },
|
|
|
|
{ "k", 1024 },
|
|
|
|
{ "m", 1024*1024 },
|
2007-07-27 15:06:25 +00:00
|
|
|
{ }
|
2006-01-30 11:15:11 +00:00
|
|
|
};
|
|
|
|
#endif
|
2006-09-17 16:28:10 +00:00
|
|
|
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char header_fmt_str[] ALIGN1 = "\n==> %s <==\n";
|
1999-12-10 07:42:50 +00:00
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2000-02-08 19:58:47 +00:00
|
|
|
int head_main(int argc, char **argv)
|
1999-12-10 07:42:50 +00:00
|
|
|
{
|
2003-03-19 09:13:01 +00:00
|
|
|
unsigned long count = 10;
|
|
|
|
unsigned long i;
|
2005-12-13 10:48:45 +00:00
|
|
|
#if ENABLE_FEATURE_FANCY_HEAD
|
2003-03-19 09:13:01 +00:00
|
|
|
int count_bytes = 0;
|
|
|
|
int header_threshhold = 1;
|
|
|
|
#endif
|
|
|
|
|
2000-09-27 04:09:22 +00:00
|
|
|
FILE *fp;
|
2003-03-19 09:13:01 +00:00
|
|
|
const char *fmt;
|
|
|
|
char *p;
|
|
|
|
int opt;
|
|
|
|
int c;
|
|
|
|
int retval = EXIT_SUCCESS;
|
1999-12-10 07:42:50 +00:00
|
|
|
|
2006-12-22 13:56:36 +00:00
|
|
|
#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
|
2003-03-19 09:13:01 +00:00
|
|
|
/* Allow legacy syntax of an initial numeric option without -n. */
|
2006-11-27 14:44:18 +00:00
|
|
|
if (argc > 1 && argv[1][0] == '-'
|
|
|
|
&& isdigit(argv[1][1])
|
2003-03-19 09:13:01 +00:00
|
|
|
) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
p = (*argv) + 1;
|
|
|
|
goto GET_COUNT;
|
2002-05-17 22:18:04 +00:00
|
|
|
}
|
2005-12-11 03:09:05 +00:00
|
|
|
#endif
|
2002-05-17 22:18:04 +00:00
|
|
|
|
2006-10-03 21:00:06 +00:00
|
|
|
/* No size benefit in converting this to getopt32 */
|
2003-03-19 09:13:01 +00:00
|
|
|
while ((opt = getopt(argc, argv, head_opts)) > 0) {
|
2006-09-27 19:51:06 +00:00
|
|
|
switch (opt) {
|
2005-12-13 10:48:45 +00:00
|
|
|
#if ENABLE_FEATURE_FANCY_HEAD
|
2006-10-08 12:49:22 +00:00
|
|
|
case 'q':
|
|
|
|
header_threshhold = INT_MAX;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
header_threshhold = -1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
count_bytes = 1;
|
|
|
|
/* fall through */
|
2003-03-19 09:13:01 +00:00
|
|
|
#endif
|
2006-10-08 12:49:22 +00:00
|
|
|
case 'n':
|
|
|
|
p = optarg;
|
2006-12-22 13:56:36 +00:00
|
|
|
#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
|
|
|
|
GET_COUNT:
|
2005-12-13 10:48:45 +00:00
|
|
|
#endif
|
2006-01-30 11:15:11 +00:00
|
|
|
|
|
|
|
#if !ENABLE_FEATURE_FANCY_HEAD
|
2006-10-08 12:49:22 +00:00
|
|
|
count = xatoul(p);
|
2006-01-30 11:15:11 +00:00
|
|
|
#else
|
2006-10-08 12:49:22 +00:00
|
|
|
count = xatoul_sfx(p, head_suffixes);
|
2006-01-30 11:15:11 +00:00
|
|
|
#endif
|
2006-10-08 12:49:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_show_usage();
|
2000-02-08 19:58:47 +00:00
|
|
|
}
|
1999-12-10 07:42:50 +00:00
|
|
|
}
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
argv += optind;
|
|
|
|
if (!*argv) {
|
2007-01-29 22:51:00 +00:00
|
|
|
*--argv = (char*)"-";
|
2003-03-19 09:13:01 +00:00
|
|
|
}
|
1999-12-10 07:42:50 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
fmt = header_fmt_str + 1;
|
2005-12-13 10:48:45 +00:00
|
|
|
#if ENABLE_FEATURE_FANCY_HEAD
|
2003-03-19 09:13:01 +00: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 {
|
2006-10-26 23:25:17 +00:00
|
|
|
fp = fopen_or_warn_stdin(*argv);
|
2006-10-26 23:21:47 +00:00
|
|
|
if (fp) {
|
2003-03-19 09:13:01 +00:00
|
|
|
if (fp == stdin) {
|
|
|
|
*argv = (char *) bb_msg_standard_input;
|
2000-09-27 04:09:22 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
if (header_threshhold) {
|
2006-10-26 23:21:47 +00:00
|
|
|
printf(fmt, *argv);
|
2000-02-08 19:58:47 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
i = count;
|
|
|
|
while (i && ((c = getc(fp)) != EOF)) {
|
|
|
|
if (count_bytes || (c == '\n')) {
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
putchar(c);
|
|
|
|
}
|
2006-10-26 23:25:17 +00:00
|
|
|
if (fclose_if_not_stdin(fp)) {
|
2007-10-01 11:58:38 +00:00
|
|
|
bb_simple_perror_msg(*argv); /* Avoid multibyte problems. */
|
2003-03-19 09:13:01 +00:00
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
}
|
2006-10-26 23:21:47 +00:00
|
|
|
die_if_ferror_stdout();
|
1999-12-10 07:42:50 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
fmt = header_fmt_str;
|
|
|
|
} while (*++argv);
|
1999-12-10 07:42:50 +00:00
|
|
|
|
2006-10-26 23:21:47 +00:00
|
|
|
fflush_stdout_and_exit(retval);
|
2000-09-27 04:09:22 +00:00
|
|
|
}
|