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 | * |
| 5 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000 by Lineo, inc. |
Eric Andersen | cf53687 | 1999-12-10 08:29:20 +0000 | [diff] [blame] | 7 | * Written by John Beppu <beppu@lineo.com> |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 8 | * |
| 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 Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 25 | #include "busybox.h" |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 26 | #include <errno.h> |
| 27 | #include <stdio.h> |
| 28 | |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 29 | int head(int len, FILE *fp) |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 30 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 31 | int i; |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 32 | char *input; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 33 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 34 | for (i = 0; i < len; i++) { |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 35 | if ((input = get_line_from_file(fp)) == NULL) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 36 | break; |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 37 | fputs(input, stdout); |
| 38 | free(input); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 39 | } |
| 40 | return 0; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* BusyBoxed head(1) */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | int head_main(int argc, char **argv) |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 45 | { |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 46 | FILE *fp; |
| 47 | int need_headers, opt, len = 10, status = EXIT_SUCCESS; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 48 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | /* parse argv[] */ |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 50 | while ((opt = getopt(argc, argv, "n:")) > 0) { |
| 51 | switch (opt) { |
| 52 | case 'n': |
| 53 | len = atoi(optarg); |
| 54 | if (len >= 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | break; |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 56 | /* fallthrough */ |
| 57 | default: |
| 58 | usage(head_usage); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | /* get rest of argv[] or stdin if nothing's left */ |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 63 | if (argv[optind] == NULL) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | head(len, stdin); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 65 | return status; |
| 66 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 68 | 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 Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 76 | } |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 77 | if (fp) { |
| 78 | if (need_headers) { |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame^] | 79 | printf("==> %s <==\n", argv[optind]); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 80 | } |
| 81 | head(len, fp); |
| 82 | if (errno) { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 83 | perror_msg("%s", argv[optind]); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 84 | status = EXIT_FAILURE; |
| 85 | errno = 0; |
| 86 | } |
| 87 | if (optind < argc - 1) |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame^] | 88 | putchar('\n'); |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 89 | if (fp != stdin) |
| 90 | fclose(fp); |
| 91 | } |
| 92 | optind++; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 93 | } |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 94 | |
Matt Kraai | c0321f9 | 2000-09-27 04:09:22 +0000 | [diff] [blame] | 95 | return status; |
| 96 | } |