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 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 37 | n = xatoul_range(n_arg, 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 { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 48 | file = bb_wfopen(*argv, "r"); |
| 49 | if (file) { |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 50 | PIPE: |
| 51 | count = 0; |
| 52 | do { |
| 53 | c = fgetc(file); |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 54 | if (isprint(c) || c == '\t') { |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 55 | if (i <= n) { |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 56 | string[i] = c; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 57 | } else { |
| 58 | putchar(c); |
| 59 | } |
| 60 | if (i == n) { |
| 61 | if (opt & PRINT_NAME) { |
| 62 | printf(fmt, *argv); |
| 63 | } |
| 64 | if (opt & PRINT_OFFSET) { |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 65 | printf("%7lo ", count - n); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 66 | } |
Eric Andersen | 92b7e7b | 2003-03-13 18:49:45 +0000 | [diff] [blame] | 67 | printf("%s", string); |
| 68 | } |
Eric Andersen | 92b7e7b | 2003-03-13 18:49:45 +0000 | [diff] [blame] | 69 | i++; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 70 | } else { |
| 71 | if (i > n) { |
Glenn L McGrath | bb13624 | 2003-08-30 12:38:13 +0000 | [diff] [blame] | 72 | putchar('\n'); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 73 | } |
| 74 | i = 0; |
Eric Andersen | 92b7e7b | 2003-03-13 18:49:45 +0000 | [diff] [blame] | 75 | } |
| 76 | count++; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 77 | } while (c != EOF); |
Glenn L McGrath | e01c550 | 2003-08-29 15:48:37 +0000 | [diff] [blame] | 78 | bb_fclose_nonstdin(file); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 79 | } else { |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 80 | status = EXIT_FAILURE; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 81 | } |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 82 | } while (--argc > 0); |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 83 | |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 84 | if (ENABLE_FEATURE_CLEAN_UP) |
| 85 | free(string); |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 86 | |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 87 | bb_fflush_stdout_and_exit(status); |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 88 | } |