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 | |
Eric Andersen | 65ddf77 | 2003-01-13 23:19:31 +0000 | [diff] [blame] | 10 | #include <getopt.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 11 | |
| 12 | #include "libbb.h" |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 13 | |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 14 | #define WHOLE_FILE 1 |
| 15 | #define PRINT_NAME 2 |
| 16 | #define PRINT_OFFSET 4 |
| 17 | #define SIZE 8 |
| 18 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 19 | int strings_main(int argc, char **argv); |
Eric Andersen | 65ddf77 | 2003-01-13 23:19:31 +0000 | [diff] [blame] | 20 | int strings_main(int argc, char **argv) |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 21 | { |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 22 | int n, c, status = EXIT_SUCCESS; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 23 | unsigned opt; |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 24 | unsigned count; |
| 25 | off_t offset; |
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: "; |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 29 | const char *n_arg = "4"; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 31 | opt = getopt32(argv, "afon:", &n_arg); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 32 | /* -a is our default behaviour */ |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 33 | /*argc -= optind;*/ |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 34 | argv += optind; |
| 35 | |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 36 | n = xatou_range(n_arg, 1, INT_MAX); |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 37 | string = xzalloc(n + 1); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 38 | n--; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 40 | if (!*argv) { |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 41 | fmt = "{%s}: "; |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 42 | *--argv = (char *)bb_msg_standard_input; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 43 | goto PIPE; |
Eric Andersen | 65ddf77 | 2003-01-13 23:19:31 +0000 | [diff] [blame] | 44 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 45 | |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 46 | do { |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 47 | file = fopen_or_warn(*argv, "r"); |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 48 | if (!file) { |
| 49 | status = EXIT_FAILURE; |
| 50 | continue; |
| 51 | } |
| 52 | PIPE: |
| 53 | offset = 0; |
| 54 | count = 0; |
| 55 | do { |
| 56 | c = fgetc(file); |
| 57 | if (isprint(c) || c == '\t') { |
| 58 | if (count > n) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame^] | 59 | bb_putchar(c); |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 60 | } else { |
| 61 | string[count] = c; |
| 62 | if (count == n) { |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 63 | if (opt & PRINT_NAME) { |
| 64 | printf(fmt, *argv); |
| 65 | } |
| 66 | if (opt & PRINT_OFFSET) { |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 67 | printf("%7"OFF_FMT"o ", offset - n); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 68 | } |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 69 | fputs(string, stdout); |
Eric Andersen | 92b7e7b | 2003-03-13 18:49:45 +0000 | [diff] [blame] | 70 | } |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 71 | count++; |
Eric Andersen | 92b7e7b | 2003-03-13 18:49:45 +0000 | [diff] [blame] | 72 | } |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 73 | } else { |
| 74 | if (count > n) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame^] | 75 | bb_putchar('\n'); |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 76 | } |
| 77 | count = 0; |
| 78 | } |
| 79 | offset++; |
| 80 | } while (c != EOF); |
| 81 | fclose_if_not_stdin(file); |
| 82 | } while (*++argv); |
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 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 87 | fflush_stdout_and_exit(status); |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 88 | } |