blob: 92b43bae2f75bfaf47e0bc2a500b80389db4ce86 [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 *
Erik Andersen61677fe2000-04-13 01:18:56 +00006 * Copyright (C) 1999,2000 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>
28
Matt Kraaic0321f92000-09-27 04:09:22 +000029int head(int len, FILE *fp)
John Beppu3157b1f1999-12-10 07:42:50 +000030{
Erik Andersene49d5ec2000-02-08 19:58:47 +000031 int i;
Matt Kraaic0321f92000-09-27 04:09:22 +000032 char *input;
John Beppu3157b1f1999-12-10 07:42:50 +000033
Erik Andersene49d5ec2000-02-08 19:58:47 +000034 for (i = 0; i < len; i++) {
Matt Kraaic0321f92000-09-27 04:09:22 +000035 if ((input = get_line_from_file(fp)) == NULL)
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 break;
Matt Kraaic0321f92000-09-27 04:09:22 +000037 fputs(input, stdout);
38 free(input);
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 }
40 return 0;
John Beppu3157b1f1999-12-10 07:42:50 +000041}
42
43/* BusyBoxed head(1) */
Erik Andersene49d5ec2000-02-08 19:58:47 +000044int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +000045{
Matt Kraaic0321f92000-09-27 04:09:22 +000046 FILE *fp;
47 int need_headers, opt, len = 10, status = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +000048
Erik Andersene49d5ec2000-02-08 19:58:47 +000049 /* parse argv[] */
Matt Kraaic0321f92000-09-27 04:09:22 +000050 while ((opt = getopt(argc, argv, "n:")) > 0) {
51 switch (opt) {
52 case 'n':
53 len = atoi(optarg);
54 if (len >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 break;
Matt Kraaic0321f92000-09-27 04:09:22 +000056 /* fallthrough */
57 default:
58 usage(head_usage);
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 }
60 }
61
62 /* get rest of argv[] or stdin if nothing's left */
Matt Kraaic0321f92000-09-27 04:09:22 +000063 if (argv[optind] == NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 head(len, stdin);
Matt Kraaic0321f92000-09-27 04:09:22 +000065 return status;
66 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000067
Matt Kraaic0321f92000-09-27 04:09:22 +000068 need_headers = optind != (argc - 1);
69 while (argv[optind]) {
70 if (strcmp(argv[optind], "-") == 0) {
71 fp = stdin;
72 argv[optind] = "standard input";
73 } else {
74 if ((fp = wfopen(argv[optind], "r")) == NULL)
75 status = EXIT_FAILURE;
John Beppu3157b1f1999-12-10 07:42:50 +000076 }
Matt Kraaic0321f92000-09-27 04:09:22 +000077 if (fp) {
78 if (need_headers) {
79 fprintf(stdout, "==> %s <==\n", argv[optind]);
80 }
81 head(len, fp);
82 if (errno) {
83 errorMsg("%s: %s\n", argv[optind], strerror(errno));
84 status = EXIT_FAILURE;
85 errno = 0;
86 }
87 if (optind < argc - 1)
88 fprintf(stdout, "\n");
89 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}