strings.c 1.6 KB

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