fold.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <ctype.h>
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <getopt.h>
  24. #include <sys/types.h>
  25. #include "busybox.h"
  26. /* If nonzero, count bytes, not column positions. */
  27. static int count_bytes;
  28. /* Assuming the current column is COLUMN, return the column that
  29. printing C will move the cursor to.
  30. The first column is 0. */
  31. static int adjust_column(int column, char c)
  32. {
  33. if (!count_bytes) {
  34. if (c == '\b') {
  35. if (column > 0)
  36. column--;
  37. } else if (c == '\r')
  38. column = 0;
  39. else if (c == '\t')
  40. column = column + 8 - column % 8;
  41. else /* if (isprint (c)) */
  42. column++;
  43. } else
  44. column++;
  45. return column;
  46. }
  47. extern int fold_main(int argc, char **argv)
  48. {
  49. /* If nonzero, try to break on whitespace. */
  50. int break_spaces;
  51. /* If nonzero, at least one of the files we read was standard input. */
  52. int have_read_stdin;
  53. int width = 80;
  54. int i;
  55. int optc;
  56. int errs = 0;
  57. break_spaces = count_bytes = have_read_stdin = 0;
  58. /* Turn any numeric options into -w options. */
  59. for (i = 1; i < argc; i++) {
  60. char const *a = argv[i];
  61. if (a[0] == '-') {
  62. if (a[1] == '-' && !a[2])
  63. break;
  64. if (isdigit(a[1])) {
  65. char *s = xmalloc(strlen(a) + 2);
  66. s[0] = '-';
  67. s[1] = 'w';
  68. strcpy(s + 2, a + 1);
  69. argv[i] = s;
  70. }
  71. }
  72. }
  73. while ((optc = getopt(argc, argv, "bsw:")) > 0) {
  74. switch (optc) {
  75. case 'b': /* Count bytes rather than columns. */
  76. count_bytes = 1;
  77. break;
  78. case 's': /* Break at word boundaries. */
  79. break_spaces = 1;
  80. break;
  81. case 'w': { /* Line width. */
  82. width = bb_xgetlarg(optarg, 10, 1, 10000);
  83. break;
  84. }
  85. default:
  86. bb_show_usage();
  87. }
  88. }
  89. argv += optind;
  90. if (!*argv) {
  91. *--argv = "-";
  92. }
  93. do {
  94. FILE *istream = bb_wfopen_input(*argv);
  95. if (istream != NULL) {
  96. int c;
  97. int column = 0; /* Screen column where next char will go. */
  98. int offset_out = 0; /* Index in `line_out' for next char. */
  99. static char *line_out = NULL;
  100. static int allocated_out = 0;
  101. while ((c = getc(istream)) != EOF) {
  102. if (offset_out + 1 >= allocated_out) {
  103. allocated_out += 1024;
  104. line_out = xrealloc(line_out, allocated_out);
  105. }
  106. if (c == '\n') {
  107. line_out[offset_out++] = c;
  108. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  109. column = offset_out = 0;
  110. continue;
  111. }
  112. rescan:
  113. column = adjust_column(column, c);
  114. if (column > width) {
  115. /* This character would make the line too long.
  116. Print the line plus a newline, and make this character
  117. start the next line. */
  118. if (break_spaces) {
  119. /* Look for the last blank. */
  120. int logical_end;
  121. for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
  122. if (isblank(line_out[logical_end])) {
  123. break;
  124. }
  125. }
  126. if (logical_end >= 0) {
  127. /* Found a blank. Don't output the part after it. */
  128. logical_end++;
  129. fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
  130. putchar('\n');
  131. /* Move the remainder to the beginning of the next line.
  132. The areas being copied here might overlap. */
  133. memmove(line_out, line_out + logical_end, offset_out - logical_end);
  134. offset_out -= logical_end;
  135. for (column = i = 0; i < offset_out; i++) {
  136. column = adjust_column(column, line_out[i]);
  137. }
  138. goto rescan;
  139. }
  140. } else {
  141. if (offset_out == 0) {
  142. line_out[offset_out++] = c;
  143. continue;
  144. }
  145. }
  146. line_out[offset_out++] = '\n';
  147. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  148. column = offset_out = 0;
  149. goto rescan;
  150. }
  151. line_out[offset_out++] = c;
  152. }
  153. if (offset_out) {
  154. fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
  155. }
  156. if (ferror(istream) || bb_fclose_nonstdin(istream)) {
  157. bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
  158. errs |= EXIT_FAILURE;
  159. }
  160. } else {
  161. errs |= EXIT_FAILURE;
  162. }
  163. } while (*++argv);
  164. bb_fflush_stdout_and_exit(errs);
  165. }