Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini head implementation for busybox |
| 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999 by Lineo, inc. and John Beppu |
| 6 | * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org> |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 7 | * |
| 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 Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 25 | #include <getopt.h> |
| 26 | #include <stdlib.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 27 | #include <string.h> |
Robert Griebl | 53146cc | 2002-05-27 22:24:53 +0000 | [diff] [blame] | 28 | #include <ctype.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 29 | #include "busybox.h" |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 30 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 31 | static int head(int len, FILE *fp) |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 32 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | int i; |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 34 | char *input; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 35 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 36 | for (i = 0; i < len; i++) { |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 37 | if ((input = get_line_from_file(fp)) == NULL) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | break; |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 39 | fputs(input, stdout); |
| 40 | free(input); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | } |
| 42 | return 0; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* BusyBoxed head(1) */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 46 | int head_main(int argc, char **argv) |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 47 | { |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 48 | FILE *fp; |
| 49 | int need_headers, opt, len = 10, status = EXIT_SUCCESS; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 50 | |
Robert Griebl | 53146cc | 2002-05-27 22:24:53 +0000 | [diff] [blame] | 51 | if (( argc >= 2 ) && ( argv [1][0] == '-' ) && isdigit ( argv [1][1] )) { |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 52 | len = atoi ( &argv [1][1] ); |
| 53 | optind = 2; |
| 54 | } |
| 55 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | /* parse argv[] */ |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 57 | while ((opt = getopt(argc, argv, "n:")) > 0) { |
| 58 | switch (opt) { |
| 59 | case 'n': |
| 60 | len = atoi(optarg); |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 61 | if (len >= 0) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | break; |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 63 | /* fallthrough */ |
| 64 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 65 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | /* get rest of argv[] or stdin if nothing's left */ |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 70 | if (argv[optind] == NULL) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | head(len, stdin); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 72 | return status; |
| 73 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 75 | 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 Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 83 | } |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 84 | if (fp) { |
| 85 | if (need_headers) { |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 86 | printf("==> %s <==\n", argv[optind]); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 87 | } |
| 88 | head(len, fp); |
Matt Kraai | 2338d31 | 2001-08-06 16:09:09 +0000 | [diff] [blame] | 89 | if (ferror(fp)) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 90 | perror_msg("%s", argv[optind]); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 91 | status = EXIT_FAILURE; |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 92 | } |
| 93 | if (optind < argc - 1) |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 94 | putchar('\n'); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 95 | if (fp != stdin) |
| 96 | fclose(fp); |
| 97 | } |
| 98 | optind++; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 99 | } |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 100 | |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 101 | return status; |
| 102 | } |