fold.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* vi: set sw=4 ts=4: */
  2. /* fold -- wrap each input line to fit in specified width.
  3. Written by David MacKenzie, djm@gnu.ai.mit.edu.
  4. Copyright (C) 91, 1995-2002 Free Software Foundation, Inc.
  5. Modified for busybox based on coreutils v 5.0
  6. Copyright (C) 2003 Glenn McGrath
  7. Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  8. */
  9. #include "libbb.h"
  10. /* Must match getopt32 call */
  11. #define FLAG_COUNT_BYTES 1
  12. #define FLAG_BREAK_SPACES 2
  13. #define FLAG_WIDTH 4
  14. /* Assuming the current column is COLUMN, return the column that
  15. printing C will move the cursor to.
  16. The first column is 0. */
  17. static int adjust_column(int column, char c)
  18. {
  19. if (!(option_mask32 & FLAG_COUNT_BYTES)) {
  20. if (c == '\b') {
  21. if (column > 0)
  22. column--;
  23. } else if (c == '\r')
  24. column = 0;
  25. else if (c == '\t')
  26. column = column + 8 - column % 8;
  27. else /* if (isprint (c)) */
  28. column++;
  29. } else
  30. column++;
  31. return column;
  32. }
  33. int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  34. int fold_main(int argc, char **argv)
  35. {
  36. char *line_out = NULL;
  37. int allocated_out = 0;
  38. char *w_opt;
  39. int width = 80;
  40. int i;
  41. int errs = 0;
  42. if (ENABLE_INCLUDE_SUSv2) {
  43. /* Turn any numeric options into -w options. */
  44. for (i = 1; i < argc; i++) {
  45. char const *a = argv[i];
  46. if (*a++ == '-') {
  47. if (*a == '-' && !a[1]) /* "--" */
  48. break;
  49. if (isdigit(*a))
  50. argv[i] = xasprintf("-w%s", a);
  51. }
  52. }
  53. }
  54. getopt32(argv, "bsw:", &w_opt);
  55. if (option_mask32 & FLAG_WIDTH)
  56. width = xatoul_range(w_opt, 1, 10000);
  57. argv += optind;
  58. if (!*argv)
  59. *--argv = (char*)"-";
  60. do {
  61. FILE *istream = fopen_or_warn_stdin(*argv);
  62. int c;
  63. int column = 0; /* Screen column where next char will go. */
  64. int offset_out = 0; /* Index in 'line_out' for next char. */
  65. if (istream == NULL) {
  66. errs |= EXIT_FAILURE;
  67. continue;
  68. }
  69. while ((c = getc(istream)) != EOF) {
  70. if (offset_out + 1 >= allocated_out) {
  71. allocated_out += 1024;
  72. line_out = xrealloc(line_out, allocated_out);
  73. }
  74. if (c == '\n') {
  75. line_out[offset_out++] = c;
  76. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  77. column = offset_out = 0;
  78. continue;
  79. }
  80. rescan:
  81. column = adjust_column(column, c);
  82. if (column > width) {
  83. /* This character would make the line too long.
  84. Print the line plus a newline, and make this character
  85. start the next line. */
  86. if (option_mask32 & FLAG_BREAK_SPACES) {
  87. /* Look for the last blank. */
  88. int logical_end;
  89. for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
  90. if (isblank(line_out[logical_end])) {
  91. break;
  92. }
  93. }
  94. if (logical_end >= 0) {
  95. /* Found a blank. Don't output the part after it. */
  96. logical_end++;
  97. fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
  98. bb_putchar('\n');
  99. /* Move the remainder to the beginning of the next line.
  100. The areas being copied here might overlap. */
  101. memmove(line_out, line_out + logical_end, offset_out - logical_end);
  102. offset_out -= logical_end;
  103. for (column = i = 0; i < offset_out; i++) {
  104. column = adjust_column(column, line_out[i]);
  105. }
  106. goto rescan;
  107. }
  108. } else {
  109. if (offset_out == 0) {
  110. line_out[offset_out++] = c;
  111. continue;
  112. }
  113. }
  114. line_out[offset_out++] = '\n';
  115. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  116. column = offset_out = 0;
  117. goto rescan;
  118. }
  119. line_out[offset_out++] = c;
  120. }
  121. if (offset_out) {
  122. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  123. }
  124. if (fclose_if_not_stdin(istream)) {
  125. bb_simple_perror_msg(*argv); /* Avoid multibyte problems. */
  126. errs |= EXIT_FAILURE;
  127. }
  128. } while (*++argv);
  129. fflush_stdout_and_exit(errs);
  130. }