3
0

fold.c 3.8 KB

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