blob: ad21e1b959d5fd95bded797a7ed8799371923977 [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 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
John Beppu3157b1f1999-12-10 07:42:50 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
John Beppu3157b1f1999-12-10 07:42:50 +000024#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000025#include <getopt.h>
26#include <stdlib.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000027#include <string.h>
Robert Griebl53146cc2002-05-27 22:24:53 +000028#include <ctype.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000029#include "busybox.h"
John Beppu3157b1f1999-12-10 07:42:50 +000030
Eric Andersen3e6ff902001-03-09 21:24:12 +000031static int head(int len, FILE *fp)
John Beppu3157b1f1999-12-10 07:42:50 +000032{
Erik Andersene49d5ec2000-02-08 19:58:47 +000033 int i;
Matt Kraaic0321f92000-09-27 04:09:22 +000034 char *input;
John Beppu3157b1f1999-12-10 07:42:50 +000035
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 for (i = 0; i < len; i++) {
Matt Kraaic0321f92000-09-27 04:09:22 +000037 if ((input = get_line_from_file(fp)) == NULL)
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 break;
Matt Kraaic0321f92000-09-27 04:09:22 +000039 fputs(input, stdout);
40 free(input);
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 }
42 return 0;
John Beppu3157b1f1999-12-10 07:42:50 +000043}
44
45/* BusyBoxed head(1) */
Erik Andersene49d5ec2000-02-08 19:58:47 +000046int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +000047{
Matt Kraaic0321f92000-09-27 04:09:22 +000048 FILE *fp;
49 int need_headers, opt, len = 10, status = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +000050
Robert Griebl53146cc2002-05-27 22:24:53 +000051 if (( argc >= 2 ) && ( argv [1][0] == '-' ) && isdigit ( argv [1][1] )) {
Robert Griebl13c26fc2002-05-17 22:18:04 +000052 len = atoi ( &argv [1][1] );
53 optind = 2;
54 }
55
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 /* parse argv[] */
Matt Kraaic0321f92000-09-27 04:09:22 +000057 while ((opt = getopt(argc, argv, "n:")) > 0) {
58 switch (opt) {
59 case 'n':
60 len = atoi(optarg);
Robert Griebl13c26fc2002-05-17 22:18:04 +000061 if (len >= 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 break;
Matt Kraaic0321f92000-09-27 04:09:22 +000063 /* fallthrough */
64 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000065 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 }
67 }
68
69 /* get rest of argv[] or stdin if nothing's left */
Matt Kraaic0321f92000-09-27 04:09:22 +000070 if (argv[optind] == NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 head(len, stdin);
Matt Kraaic0321f92000-09-27 04:09:22 +000072 return status;
73 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000074
Matt Kraaic0321f92000-09-27 04:09:22 +000075 need_headers = optind != (argc - 1);
76 while (argv[optind]) {
77 if (strcmp(argv[optind], "-") == 0) {
78 fp = stdin;
79 argv[optind] = "standard input";
80 } else {
81 if ((fp = wfopen(argv[optind], "r")) == NULL)
82 status = EXIT_FAILURE;
John Beppu3157b1f1999-12-10 07:42:50 +000083 }
Matt Kraaic0321f92000-09-27 04:09:22 +000084 if (fp) {
85 if (need_headers) {
Matt Kraai12f417e2001-01-18 02:57:08 +000086 printf("==> %s <==\n", argv[optind]);
Matt Kraaic0321f92000-09-27 04:09:22 +000087 }
88 head(len, fp);
Matt Kraai2338d312001-08-06 16:09:09 +000089 if (ferror(fp)) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +000090 perror_msg("%s", argv[optind]);
Matt Kraaic0321f92000-09-27 04:09:22 +000091 status = EXIT_FAILURE;
Matt Kraaic0321f92000-09-27 04:09:22 +000092 }
93 if (optind < argc - 1)
Matt Kraai12f417e2001-01-18 02:57:08 +000094 putchar('\n');
Matt Kraaic0321f92000-09-27 04:09:22 +000095 if (fp != stdin)
96 fclose(fp);
97 }
98 optind++;
John Beppu3157b1f1999-12-10 07:42:50 +000099 }
John Beppu3157b1f1999-12-10 07:42:50 +0000100
Matt Kraaic0321f92000-09-27 04:09:22 +0000101 return status;
102}