3
0

strings.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * strings implementation for busybox
  4. *
  5. * Copyright Tito Ragusa <farmatito@tiscali.it>
  6. *
  7. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  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 opt;
  19. unsigned count;
  20. off_t offset;
  21. FILE *file;
  22. char *string;
  23. const char *fmt = "%s: ";
  24. const char *n_arg = "4";
  25. opt = getopt32(argv, "afon:", &n_arg);
  26. /* -a is our default behaviour */
  27. /*argc -= optind;*/
  28. argv += optind;
  29. n = xatou_range(n_arg, 1, INT_MAX);
  30. string = xzalloc(n + 1);
  31. n--;
  32. if (!*argv) {
  33. fmt = "{%s}: ";
  34. *--argv = (char *)bb_msg_standard_input;
  35. }
  36. do {
  37. file = fopen_or_warn_stdin(*argv);
  38. if (!file) {
  39. status = EXIT_FAILURE;
  40. continue;
  41. }
  42. offset = 0;
  43. count = 0;
  44. do {
  45. c = fgetc(file);
  46. if (isprint(c) || c == '\t') {
  47. if (count > n) {
  48. bb_putchar(c);
  49. } else {
  50. string[count] = c;
  51. if (count == n) {
  52. if (opt & PRINT_NAME) {
  53. printf(fmt, *argv);
  54. }
  55. if (opt & PRINT_OFFSET) {
  56. printf("%7"OFF_FMT"o ", offset - n);
  57. }
  58. fputs(string, stdout);
  59. }
  60. count++;
  61. }
  62. } else {
  63. if (count > n) {
  64. bb_putchar('\n');
  65. }
  66. count = 0;
  67. }
  68. offset++;
  69. } while (c != EOF);
  70. fclose_if_not_stdin(file);
  71. } while (*++argv);
  72. if (ENABLE_FEATURE_CLEAN_UP)
  73. free(string);
  74. fflush_stdout_and_exit(status);
  75. }