blob: 0c8ef3d5907b8f020b90a7780c964fe281c4e905 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppu3157b1f1999-12-10 07:42:50 +00002/*
3 * Mini head implementation for busybox
4 *
5 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00006 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Eric Andersencf536871999-12-10 08:29:20 +00007 * Written by John Beppu <beppu@lineo.com>
John Beppu3157b1f1999-12-10 07:42:50 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
John Beppu3157b1f1999-12-10 07:42:50 +000025#include <errno.h>
26#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000027#include <getopt.h>
28#include <stdlib.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000029#include <string.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000030#include "busybox.h"
John Beppu3157b1f1999-12-10 07:42:50 +000031
Eric Andersen3e6ff902001-03-09 21:24:12 +000032static int head(int len, FILE *fp)
John Beppu3157b1f1999-12-10 07:42:50 +000033{
Erik Andersene49d5ec2000-02-08 19:58:47 +000034 int i;
Matt Kraaic0321f92000-09-27 04:09:22 +000035 char *input;
John Beppu3157b1f1999-12-10 07:42:50 +000036
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 for (i = 0; i < len; i++) {
Matt Kraaic0321f92000-09-27 04:09:22 +000038 if ((input = get_line_from_file(fp)) == NULL)
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 break;
Matt Kraaic0321f92000-09-27 04:09:22 +000040 fputs(input, stdout);
41 free(input);
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 }
43 return 0;
John Beppu3157b1f1999-12-10 07:42:50 +000044}
45
46/* BusyBoxed head(1) */
Erik Andersene49d5ec2000-02-08 19:58:47 +000047int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +000048{
Matt Kraaic0321f92000-09-27 04:09:22 +000049 FILE *fp;
50 int need_headers, opt, len = 10, status = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +000051
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 /* parse argv[] */
Matt Kraaic0321f92000-09-27 04:09:22 +000053 while ((opt = getopt(argc, argv, "n:")) > 0) {
54 switch (opt) {
55 case 'n':
56 len = atoi(optarg);
57 if (len >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 break;
Matt Kraaic0321f92000-09-27 04:09:22 +000059 /* fallthrough */
60 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000061 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 }
63 }
64
65 /* get rest of argv[] or stdin if nothing's left */
Matt Kraaic0321f92000-09-27 04:09:22 +000066 if (argv[optind] == NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 head(len, stdin);
Matt Kraaic0321f92000-09-27 04:09:22 +000068 return status;
69 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000070
Matt Kraaic0321f92000-09-27 04:09:22 +000071 need_headers = optind != (argc - 1);
72 while (argv[optind]) {
73 if (strcmp(argv[optind], "-") == 0) {
74 fp = stdin;
75 argv[optind] = "standard input";
76 } else {
77 if ((fp = wfopen(argv[optind], "r")) == NULL)
78 status = EXIT_FAILURE;
John Beppu3157b1f1999-12-10 07:42:50 +000079 }
Matt Kraaic0321f92000-09-27 04:09:22 +000080 if (fp) {
81 if (need_headers) {
Matt Kraai12f417e2001-01-18 02:57:08 +000082 printf("==> %s <==\n", argv[optind]);
Matt Kraaic0321f92000-09-27 04:09:22 +000083 }
84 head(len, fp);
85 if (errno) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +000086 perror_msg("%s", argv[optind]);
Matt Kraaic0321f92000-09-27 04:09:22 +000087 status = EXIT_FAILURE;
88 errno = 0;
89 }
90 if (optind < argc - 1)
Matt Kraai12f417e2001-01-18 02:57:08 +000091 putchar('\n');
Matt Kraaic0321f92000-09-27 04:09:22 +000092 if (fp != stdin)
93 fclose(fp);
94 }
95 optind++;
John Beppu3157b1f1999-12-10 07:42:50 +000096 }
John Beppu3157b1f1999-12-10 07:42:50 +000097
Matt Kraaic0321f92000-09-27 04:09:22 +000098 return status;
99}