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 "libbb.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. 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. }
  55. flags = getopt32(argv, "bsw:", &w_opt);
  56. if (flags & FLAG_WIDTH)
  57. width = xatoul_range(w_opt, 1, 10000);
  58. argv += optind;
  59. if (!*argv) {
  60. *--argv = (char*)"-";
  61. }
  62. do {
  63. FILE *istream = fopen_or_warn_stdin(*argv);
  64. int c;
  65. int column = 0; /* Screen column where next char will go. */
  66. int offset_out = 0; /* Index in `line_out' for next char. */
  67. if (istream == NULL) {
  68. errs |= EXIT_FAILURE;
  69. continue;
  70. }
  71. while ((c = getc(istream)) != EOF) {
  72. if (offset_out + 1 >= allocated_out) {
  73. allocated_out += 1024;
  74. line_out = xrealloc(line_out, allocated_out);
  75. }
  76. if (c == '\n') {
  77. line_out[offset_out++] = c;
  78. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  79. column = offset_out = 0;
  80. continue;
  81. }
  82. rescan:
  83. column = adjust_column(column, c);
  84. if (column > width) {
  85. /* This character would make the line too long.
  86. Print the line plus a newline, and make this character
  87. start the next line. */
  88. if (flags & FLAG_BREAK_SPACES) {
  89. /* Look for the last blank. */
  90. int logical_end;
  91. for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
  92. if (isblank(line_out[logical_end])) {
  93. break;
  94. }
  95. }
  96. if (logical_end >= 0) {
  97. /* Found a blank. Don't output the part after it. */
  98. logical_end++;
  99. fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
  100. putchar('\n');
  101. /* Move the remainder to the beginning of the next line.
  102. The areas being copied here might overlap. */
  103. memmove(line_out, line_out + logical_end, offset_out - logical_end);
  104. offset_out -= logical_end;
  105. for (column = i = 0; i < offset_out; i++) {
  106. column = adjust_column(column, line_out[i]);
  107. }
  108. goto rescan;
  109. }
  110. } else {
  111. if (offset_out == 0) {
  112. line_out[offset_out++] = c;
  113. continue;
  114. }
  115. }
  116. line_out[offset_out++] = '\n';
  117. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  118. column = offset_out = 0;
  119. goto rescan;
  120. }
  121. line_out[offset_out++] = c;
  122. }
  123. if (offset_out) {
  124. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  125. }
  126. if (ferror(istream) || fclose_if_not_stdin(istream)) {
  127. bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
  128. errs |= EXIT_FAILURE;
  129. }
  130. } while (*++argv);
  131. fflush_stdout_and_exit(errs);
  132. }