fold.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <bug1@iinet.net.au>
  7. Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  8. */
  9. #include "busybox.h"
  10. static unsigned long flags;
  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 (!(flags & 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)
  34. {
  35. char *w_opt;
  36. int width = 80;
  37. int i;
  38. int errs = 0;
  39. if(!ENABLE_DEBUG_YANK_SUSv2) {
  40. /* Turn any numeric options into -w options. */
  41. for (i = 1; i < argc; i++) {
  42. char const *a = argv[i];
  43. if (*a++ == '-') {
  44. if (*a == '-' && !a[1])
  45. break;
  46. if (isdigit(*a)) {
  47. argv[i] = xasprintf("-w%s", a);
  48. }
  49. }
  50. }
  51. }
  52. flags = getopt32(argc, argv, "bsw:", &w_opt);
  53. if (flags & FLAG_WIDTH)
  54. width = xatoul_range(w_opt, 1, 10000);
  55. argv += optind;
  56. if (!*argv) {
  57. *--argv = "-";
  58. }
  59. do {
  60. FILE *istream = fopen_or_warn_stdin(*argv);
  61. int c;
  62. int column = 0; /* Screen column where next char will go. */
  63. int offset_out = 0; /* Index in `line_out' for next char. */
  64. static char *line_out = NULL;
  65. static int allocated_out = 0;
  66. if (istream == NULL) {
  67. errs |= EXIT_FAILURE;
  68. continue;
  69. }
  70. while ((c = getc(istream)) != EOF) {
  71. if (offset_out + 1 >= allocated_out) {
  72. allocated_out += 1024;
  73. line_out = xrealloc(line_out, allocated_out);
  74. }
  75. if (c == '\n') {
  76. line_out[offset_out++] = c;
  77. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  78. column = offset_out = 0;
  79. continue;
  80. }
  81. rescan:
  82. column = adjust_column(column, c);
  83. if (column > width) {
  84. /* This character would make the line too long.
  85. Print the line plus a newline, and make this character
  86. start the next line. */
  87. if (flags & FLAG_BREAK_SPACES) {
  88. /* Look for the last blank. */
  89. int logical_end;
  90. for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
  91. if (isblank(line_out[logical_end])) {
  92. break;
  93. }
  94. }
  95. if (logical_end >= 0) {
  96. /* Found a blank. Don't output the part after it. */
  97. logical_end++;
  98. fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
  99. putchar('\n');
  100. /* Move the remainder to the beginning of the next line.
  101. The areas being copied here might overlap. */
  102. memmove(line_out, line_out + logical_end, offset_out - logical_end);
  103. offset_out -= logical_end;
  104. for (column = i = 0; i < offset_out; i++) {
  105. column = adjust_column(column, line_out[i]);
  106. }
  107. goto rescan;
  108. }
  109. } else {
  110. if (offset_out == 0) {
  111. line_out[offset_out++] = c;
  112. continue;
  113. }
  114. }
  115. line_out[offset_out++] = '\n';
  116. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  117. column = offset_out = 0;
  118. goto rescan;
  119. }
  120. line_out[offset_out++] = c;
  121. }
  122. if (offset_out) {
  123. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  124. }
  125. if (ferror(istream) || fclose_if_not_stdin(istream)) {
  126. bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
  127. errs |= EXIT_FAILURE;
  128. }
  129. } while (*++argv);
  130. fflush_stdout_and_exit(errs);
  131. }