Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * strings implementation for busybox |
| 4 | * |
Rob Landley | c052576 | 2006-07-31 16:37:57 +0000 | [diff] [blame] | 5 | * Copyright Tito Ragusa <farmatito@tiscali.it> |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
Eric Andersen | 65ddf77 | 2003-01-13 23:19:31 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Eric Andersen | 65ddf77 | 2003-01-13 23:19:31 +0000 | [diff] [blame] | 13 | #include <getopt.h> |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 14 | #include <ctype.h> |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 15 | |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 16 | #define WHOLE_FILE 1 |
| 17 | #define PRINT_NAME 2 |
| 18 | #define PRINT_OFFSET 4 |
| 19 | #define SIZE 8 |
| 20 | |
Eric Andersen | 65ddf77 | 2003-01-13 23:19:31 +0000 | [diff] [blame] | 21 | int strings_main(int argc, char **argv) |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 22 | { |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 23 | int n, c, i = 0, status = EXIT_SUCCESS; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame^] | 24 | unsigned opt; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 25 | unsigned long count; |
Glenn L McGrath | e16ab47 | 2003-09-15 14:22:37 +0000 | [diff] [blame] | 26 | FILE *file = stdin; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 27 | char *string; |
| 28 | const char *fmt = "%s: "; |
| 29 | char *n_arg = "4"; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame^] | 31 | opt = getopt32(argc, argv, "afon:", &n_arg); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 32 | /* -a is our default behaviour */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 33 | |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 34 | argc -= optind; |
| 35 | argv += optind; |
| 36 | |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 37 | n = bb_xgetlarg(n_arg, 10, 1, INT_MAX); |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 38 | string = xzalloc(n + 1); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 39 | n--; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 40 | |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 41 | if (argc == 0) { |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 42 | fmt = "{%s}: "; |
| 43 | *argv = (char *)bb_msg_standard_input; |
| 44 | goto PIPE; |
Eric Andersen | 65ddf77 | 2003-01-13 23:19:31 +0000 | [diff] [blame] | 45 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 46 | |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 47 | do { |
| 48 | if ((file = bb_wfopen(*argv, "r"))) { |
| 49 | PIPE: |
| 50 | count = 0; |
| 51 | do { |
| 52 | c = fgetc(file); |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 53 | if (isprint(c) || c == '\t') { |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 54 | if (i <= n) { |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 55 | string[i] = c; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 56 | } else { |
| 57 | putchar(c); |
| 58 | } |
| 59 | if (i == n) { |
| 60 | if (opt & PRINT_NAME) { |
| 61 | printf(fmt, *argv); |
| 62 | } |
| 63 | if (opt & PRINT_OFFSET) { |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 64 | printf("%7lo ", count - n); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 65 | } |
Eric Andersen | 92b7e7b | 2003-03-13 18:49:45 +0000 | [diff] [blame] | 66 | printf("%s", string); |
| 67 | } |
Eric Andersen | 92b7e7b | 2003-03-13 18:49:45 +0000 | [diff] [blame] | 68 | i++; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 69 | } else { |
| 70 | if (i > n) { |
Glenn L McGrath | bb13624 | 2003-08-30 12:38:13 +0000 | [diff] [blame] | 71 | putchar('\n'); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 72 | } |
| 73 | i = 0; |
Eric Andersen | 92b7e7b | 2003-03-13 18:49:45 +0000 | [diff] [blame] | 74 | } |
| 75 | count++; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 76 | } while (c != EOF); |
Glenn L McGrath | e01c550 | 2003-08-29 15:48:37 +0000 | [diff] [blame] | 77 | bb_fclose_nonstdin(file); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 78 | } else { |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 79 | status = EXIT_FAILURE; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 80 | } |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 81 | } while (--argc > 0); |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 82 | |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 83 | if (ENABLE_FEATURE_CLEAN_UP) |
| 84 | free(string); |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 85 | |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 86 | bb_fflush_stdout_and_exit(status); |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 87 | } |