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