3
0

strings.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * strings implementation for busybox
  4. *
  5. * Copyright 2003 Tito Ragusa <farmatito@tiscali.it>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config STRINGS
  10. //config: bool "strings (4.6 kb)"
  11. //config: default y
  12. //config: help
  13. //config: strings prints the printable character sequences for each file
  14. //config: specified.
  15. //applet:IF_STRINGS(APPLET(strings, BB_DIR_USR_BIN, BB_SUID_DROP))
  16. //kbuild:lib-$(CONFIG_STRINGS) += strings.o
  17. //usage:#define strings_trivial_usage
  18. //usage: "[-fo] [-t o/d/x] [-n LEN] [FILE]..."
  19. //usage:#define strings_full_usage "\n\n"
  20. //usage: "Display printable strings in a binary file\n"
  21. //We usually don't bother user with "nop" options. They work, but are not shown:
  22. ////usage: "\n -a Scan whole file (default)"
  23. //unimplemented alternative is -d: Only strings from initialized, loaded data sections
  24. //usage: "\n -f Precede strings with filenames"
  25. //usage: "\n -o Precede strings with octal offsets"
  26. //usage: "\n -t o/d/x Precede strings with offsets in base 8/10/16"
  27. //usage: "\n -n LEN At least LEN characters form a string (default 4)"
  28. #include "libbb.h"
  29. #define WHOLE_FILE 1
  30. #define PRINT_NAME 2
  31. #define PRINT_OFFSET 4
  32. #define SIZE 8
  33. #define PRINT_RADIX 16
  34. int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  35. int strings_main(int argc UNUSED_PARAM, char **argv)
  36. {
  37. int n, c, status = EXIT_SUCCESS;
  38. unsigned count;
  39. off_t offset;
  40. FILE *file;
  41. char *string;
  42. const char *fmt = "%s: ";
  43. const char *n_arg = "4";
  44. /* default for -o */
  45. const char *radix = "o";
  46. char *radix_fmt;
  47. getopt32(argv, "afon:t:", &n_arg, &radix);
  48. /* -a is our default behaviour */
  49. /*argc -= optind;*/
  50. argv += optind;
  51. n = xatou_range(n_arg, 1, INT_MAX);
  52. string = xzalloc(n + 1);
  53. n--;
  54. if ((radix[0] != 'd' && radix[0] != 'o' && radix[0] != 'x') || radix[1] != 0)
  55. bb_show_usage();
  56. radix_fmt = xasprintf("%%7"OFF_FMT"%s ", radix);
  57. if (!*argv) {
  58. fmt = "{%s}: ";
  59. *--argv = (char *)bb_msg_standard_input;
  60. }
  61. do {
  62. file = fopen_or_warn_stdin(*argv);
  63. if (!file) {
  64. status = EXIT_FAILURE;
  65. continue;
  66. }
  67. offset = 0;
  68. count = 0;
  69. do {
  70. c = fgetc(file);
  71. if (isprint_asciionly(c) || c == '\t') {
  72. if (count > n) {
  73. bb_putchar(c);
  74. } else {
  75. string[count] = c;
  76. if (count == n) {
  77. if (option_mask32 & PRINT_NAME) {
  78. printf(fmt, *argv);
  79. }
  80. if (option_mask32 & (PRINT_OFFSET | PRINT_RADIX)) {
  81. printf(radix_fmt, offset - n);
  82. }
  83. fputs(string, stdout);
  84. }
  85. count++;
  86. }
  87. } else {
  88. if (count > n) {
  89. bb_putchar('\n');
  90. }
  91. count = 0;
  92. }
  93. offset++;
  94. } while (c != EOF);
  95. fclose_if_not_stdin(file);
  96. } while (*++argv);
  97. if (ENABLE_FEATURE_CLEAN_UP) {
  98. free(string);
  99. free(radix_fmt);
  100. }
  101. fflush_stdout_and_exit(status);
  102. }