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