blob: 688c250b16fd54fbc8025f42259736638a439015 [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 <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000026#include <getopt.h>
27#include <stdlib.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000028#include <string.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
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 /* parse argv[] */
Matt Kraaic0321f92000-09-27 04:09:22 +000052 while ((opt = getopt(argc, argv, "n:")) > 0) {
53 switch (opt) {
54 case 'n':
55 len = atoi(optarg);
56 if (len >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 break;
Matt Kraaic0321f92000-09-27 04:09:22 +000058 /* fallthrough */
59 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000060 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 }
62 }
63
64 /* get rest of argv[] or stdin if nothing's left */
Matt Kraaic0321f92000-09-27 04:09:22 +000065 if (argv[optind] == NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 head(len, stdin);
Matt Kraaic0321f92000-09-27 04:09:22 +000067 return status;
68 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000069
Matt Kraaic0321f92000-09-27 04:09:22 +000070 need_headers = optind != (argc - 1);
71 while (argv[optind]) {
72 if (strcmp(argv[optind], "-") == 0) {
73 fp = stdin;
74 argv[optind] = "standard input";
75 } else {
76 if ((fp = wfopen(argv[optind], "r")) == NULL)
77 status = EXIT_FAILURE;
John Beppu3157b1f1999-12-10 07:42:50 +000078 }
Matt Kraaic0321f92000-09-27 04:09:22 +000079 if (fp) {
80 if (need_headers) {
Matt Kraai12f417e2001-01-18 02:57:08 +000081 printf("==> %s <==\n", argv[optind]);
Matt Kraaic0321f92000-09-27 04:09:22 +000082 }
83 head(len, fp);
Matt Kraai2338d312001-08-06 16:09:09 +000084 if (ferror(fp)) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +000085 perror_msg("%s", argv[optind]);
Matt Kraaic0321f92000-09-27 04:09:22 +000086 status = EXIT_FAILURE;
Matt Kraaic0321f92000-09-27 04:09:22 +000087 }
88 if (optind < argc - 1)
Matt Kraai12f417e2001-01-18 02:57:08 +000089 putchar('\n');
Matt Kraaic0321f92000-09-27 04:09:22 +000090 if (fp != stdin)
91 fclose(fp);
92 }
93 optind++;
John Beppu3157b1f1999-12-10 07:42:50 +000094 }
John Beppu3157b1f1999-12-10 07:42:50 +000095
Matt Kraaic0321f92000-09-27 04:09:22 +000096 return status;
97}