blob: 7d87f32cbad815e20fde56cf1080dbb4ffd2adf1 [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
Eric Andersen3570a342000-09-25 21:45:58 +000025#include "busybox.h"
John Beppu3157b1f1999-12-10 07:42:50 +000026#include <errno.h>
27#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000028#include <getopt.h>
29#include <stdlib.h>
John Beppu3157b1f1999-12-10 07:42:50 +000030
Matt Kraaic0321f92000-09-27 04:09:22 +000031int 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:
60 usage(head_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);
84 if (errno) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +000085 perror_msg("%s", argv[optind]);
Matt Kraaic0321f92000-09-27 04:09:22 +000086 status = EXIT_FAILURE;
87 errno = 0;
88 }
89 if (optind < argc - 1)
Matt Kraai12f417e2001-01-18 02:57:08 +000090 putchar('\n');
Matt Kraaic0321f92000-09-27 04:09:22 +000091 if (fp != stdin)
92 fclose(fp);
93 }
94 optind++;
John Beppu3157b1f1999-12-10 07:42:50 +000095 }
John Beppu3157b1f1999-12-10 07:42:50 +000096
Matt Kraaic0321f92000-09-27 04:09:22 +000097 return status;
98}