strings.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. //usage:#define strings_trivial_usage
  10. //usage: "[-afo] [-n LEN] [FILE]..."
  11. //usage:#define strings_full_usage "\n\n"
  12. //usage: "Display printable strings in a binary file\n"
  13. //usage: "\n -a Scan whole file (default)"
  14. //usage: "\n -f Precede strings with filenames"
  15. //usage: "\n -n LEN At least LEN characters form a string (default 4)"
  16. //usage: "\n -o Precede strings with decimal offsets"
  17. #include "libbb.h"
  18. #define WHOLE_FILE 1
  19. #define PRINT_NAME 2
  20. #define PRINT_OFFSET 4
  21. #define SIZE 8
  22. int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  23. int strings_main(int argc UNUSED_PARAM, char **argv)
  24. {
  25. int n, c, status = EXIT_SUCCESS;
  26. unsigned count;
  27. off_t offset;
  28. FILE *file;
  29. char *string;
  30. const char *fmt = "%s: ";
  31. const char *n_arg = "4";
  32. getopt32(argv, "afon:", &n_arg);
  33. /* -a is our default behaviour */
  34. /*argc -= optind;*/
  35. argv += optind;
  36. n = xatou_range(n_arg, 1, INT_MAX);
  37. string = xzalloc(n + 1);
  38. n--;
  39. if (!*argv) {
  40. fmt = "{%s}: ";
  41. *--argv = (char *)bb_msg_standard_input;
  42. }
  43. do {
  44. file = fopen_or_warn_stdin(*argv);
  45. if (!file) {
  46. status = EXIT_FAILURE;
  47. continue;
  48. }
  49. offset = 0;
  50. count = 0;
  51. do {
  52. c = fgetc(file);
  53. if (isprint_asciionly(c) || c == '\t') {
  54. if (count > n) {
  55. bb_putchar(c);
  56. } else {
  57. string[count] = c;
  58. if (count == n) {
  59. if (option_mask32 & PRINT_NAME) {
  60. printf(fmt, *argv);
  61. }
  62. if (option_mask32 & PRINT_OFFSET) {
  63. printf("%7"OFF_FMT"o ", offset - n);
  64. }
  65. fputs(string, stdout);
  66. }
  67. count++;
  68. }
  69. } else {
  70. if (count > n) {
  71. bb_putchar('\n');
  72. }
  73. count = 0;
  74. }
  75. offset++;
  76. } while (c != EOF);
  77. fclose_if_not_stdin(file);
  78. } while (*++argv);
  79. if (ENABLE_FEATURE_CLEAN_UP)
  80. free(string);
  81. fflush_stdout_and_exit(status);
  82. }