blob: 4a1677146d906135caf6b2159c7c7aa8982cba2b [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>
Eric Andersencbe31da2001-02-20 06:14:08 +000028#include "busybox.h"
John Beppu3157b1f1999-12-10 07:42:50 +000029
Eric Andersen3e6ff902001-03-09 21:24:12 +000030static int head(int len, FILE *fp)
John Beppu3157b1f1999-12-10 07:42:50 +000031{
Erik Andersene49d5ec2000-02-08 19:58:47 +000032 int i;
Matt Kraaic0321f92000-09-27 04:09:22 +000033 char *input;
John Beppu3157b1f1999-12-10 07:42:50 +000034
Erik Andersene49d5ec2000-02-08 19:58:47 +000035 for (i = 0; i < len; i++) {
Matt Kraaic0321f92000-09-27 04:09:22 +000036 if ((input = get_line_from_file(fp)) == NULL)
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 break;
Matt Kraaic0321f92000-09-27 04:09:22 +000038 fputs(input, stdout);
39 free(input);
Erik Andersene49d5ec2000-02-08 19:58:47 +000040 }
41 return 0;
John Beppu3157b1f1999-12-10 07:42:50 +000042}
43
44/* BusyBoxed head(1) */
Erik Andersene49d5ec2000-02-08 19:58:47 +000045int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +000046{
Matt Kraaic0321f92000-09-27 04:09:22 +000047 FILE *fp;
48 int need_headers, opt, len = 10, status = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +000049
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 /* parse argv[] */
Matt Kraaic0321f92000-09-27 04:09:22 +000051 while ((opt = getopt(argc, argv, "n:")) > 0) {
52 switch (opt) {
53 case 'n':
54 len = atoi(optarg);
55 if (len >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 break;
Matt Kraaic0321f92000-09-27 04:09:22 +000057 /* fallthrough */
58 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000059 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 }
61 }
62
63 /* get rest of argv[] or stdin if nothing's left */
Matt Kraaic0321f92000-09-27 04:09:22 +000064 if (argv[optind] == NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 head(len, stdin);
Matt Kraaic0321f92000-09-27 04:09:22 +000066 return status;
67 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000068
Matt Kraaic0321f92000-09-27 04:09:22 +000069 need_headers = optind != (argc - 1);
70 while (argv[optind]) {
71 if (strcmp(argv[optind], "-") == 0) {
72 fp = stdin;
73 argv[optind] = "standard input";
74 } else {
75 if ((fp = wfopen(argv[optind], "r")) == NULL)
76 status = EXIT_FAILURE;
John Beppu3157b1f1999-12-10 07:42:50 +000077 }
Matt Kraaic0321f92000-09-27 04:09:22 +000078 if (fp) {
79 if (need_headers) {
Matt Kraai12f417e2001-01-18 02:57:08 +000080 printf("==> %s <==\n", argv[optind]);
Matt Kraaic0321f92000-09-27 04:09:22 +000081 }
82 head(len, fp);
Matt Kraai2338d312001-08-06 16:09:09 +000083 if (ferror(fp)) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +000084 perror_msg("%s", argv[optind]);
Matt Kraaic0321f92000-09-27 04:09:22 +000085 status = EXIT_FAILURE;
Matt Kraaic0321f92000-09-27 04:09:22 +000086 }
87 if (optind < argc - 1)
Matt Kraai12f417e2001-01-18 02:57:08 +000088 putchar('\n');
Matt Kraaic0321f92000-09-27 04:09:22 +000089 if (fp != stdin)
90 fclose(fp);
91 }
92 optind++;
John Beppu3157b1f1999-12-10 07:42:50 +000093 }
John Beppu3157b1f1999-12-10 07:42:50 +000094
Matt Kraaic0321f92000-09-27 04:09:22 +000095 return status;
96}