John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini head implementation for busybox |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
Eric Andersen | cf53687 | 1999-12-10 08:29:20 +0000 | [diff] [blame] | 6 | * Written by John Beppu <beppu@lineo.com> |
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 | |
| 24 | #include "internal.h" |
| 25 | #include <errno.h> |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | const char head_usage[] = |
Erik Andersen | 5509af7 | 2000-01-23 18:19:02 +0000 | [diff] [blame^] | 29 | "head [OPTION] [FILE]...\n\n" |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 30 | "Print first 10 lines of each FILE to standard output.\n" |
Eric Andersen | cf53687 | 1999-12-10 08:29:20 +0000 | [diff] [blame] | 31 | "With more than one FILE, precede each with a header giving the\n" |
Erik Andersen | 5509af7 | 2000-01-23 18:19:02 +0000 | [diff] [blame^] | 32 | "file name. With no FILE, or when FILE is -, read standard input.\n\n" |
| 33 | "Options:\n" |
| 34 | "\t-n NUM\t\tPrint first NUM lines instead of first 10\n"; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 35 | |
| 36 | int |
| 37 | head(int len, FILE *src) |
| 38 | { |
| 39 | int i; |
| 40 | char buffer[1024]; |
| 41 | |
| 42 | for (i = 0; i < len; i++) { |
| 43 | fgets(buffer, 1024, src); |
| 44 | if (feof(src)) { break; } |
| 45 | fputs(buffer, stdout); |
| 46 | } |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /* BusyBoxed head(1) */ |
| 51 | int |
| 52 | head_main(int argc, char **argv) |
| 53 | { |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 54 | char opt; |
Erik Andersen | 5509af7 | 2000-01-23 18:19:02 +0000 | [diff] [blame^] | 55 | int len = 10, tmplen, i; |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 56 | |
| 57 | /* parse argv[] */ |
Erik Andersen | 5509af7 | 2000-01-23 18:19:02 +0000 | [diff] [blame^] | 58 | for (i = 1; i < argc; i++) { |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 59 | if (argv[i][0] == '-') { |
| 60 | opt = argv[i][1]; |
| 61 | switch (opt) { |
Erik Andersen | 5509af7 | 2000-01-23 18:19:02 +0000 | [diff] [blame^] | 62 | case 'n': |
| 63 | tmplen = 0; |
| 64 | if (i++ < argc) |
| 65 | tmplen = atoi(argv[i]); |
| 66 | if (tmplen < 1) |
| 67 | usage(head_usage); |
| 68 | len = tmplen; |
| 69 | break; |
Eric Andersen | cf53687 | 1999-12-10 08:29:20 +0000 | [diff] [blame] | 70 | case '-': |
John Beppu | 3157b1f | 1999-12-10 07:42:50 +0000 | [diff] [blame] | 71 | case 'h': |
| 72 | usage(head_usage); |
| 73 | default: |
| 74 | fprintf(stderr, "head: invalid option -- %c\n", opt); |
| 75 | usage(head_usage); |
| 76 | } |
| 77 | } else { |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* get rest of argv[] or stdin if nothing's left */ |
| 83 | if (i >= argc) { |
| 84 | head(len, stdin); |
| 85 | |
| 86 | } else { |
| 87 | int need_headers = ((argc - i) > 1); |
| 88 | for ( ; i < argc; i++) { |
| 89 | FILE *src; |
| 90 | src = fopen(argv[i], "r"); |
| 91 | if (!src) { |
| 92 | fprintf(stderr,"head: %s: %s\n", argv[i], strerror(errno)); |
| 93 | } else { |
| 94 | /* emulating GNU behaviour */ |
| 95 | if (need_headers) { |
| 96 | fprintf(stdout, "==> %s <==\n", argv[i]); |
| 97 | } |
| 98 | head(len, src); |
| 99 | if (i < argc - 1) { |
| 100 | fprintf(stdout, "\n"); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | exit(0); |
| 106 | } |
| 107 | |
Erik Andersen | 5509af7 | 2000-01-23 18:19:02 +0000 | [diff] [blame^] | 108 | /* $Id: head.c,v 1.5 2000/01/23 18:19:02 erik Exp $ */ |