source: MondoRescue/branches/2.2.9/mindi-busybox/coreutils/fold.c@ 2725

Last change on this file since 2725 was 2725, checked in by Bruno Cornec, 13 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File size: 4.3 KB
RevLine 
[1765]1/* vi: set sw=4 ts=4: */
[821]2/* fold -- wrap each input line to fit in specified width.
3
4 Written by David MacKenzie, djm@gnu.ai.mit.edu.
5 Copyright (C) 91, 1995-2002 Free Software Foundation, Inc.
6
7 Modified for busybox based on coreutils v 5.0
[2725]8 Copyright (C) 2003 Glenn McGrath
[821]9
[2725]10 Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]11*/
[1765]12#include "libbb.h"
[2725]13#include "unicode.h"
[821]14
[2725]15/* This is a NOEXEC applet. Be very careful! */
[821]16
[2725]17/* Must match getopt32 call */
18#define FLAG_COUNT_BYTES 1
19#define FLAG_BREAK_SPACES 2
20#define FLAG_WIDTH 4
21
[821]22/* Assuming the current column is COLUMN, return the column that
23 printing C will move the cursor to.
24 The first column is 0. */
[2725]25static int adjust_column(unsigned column, char c)
26{
27 if (option_mask32 & FLAG_COUNT_BYTES)
28 return ++column;
[821]29
[2725]30 if (c == '\t')
31 return column + 8 - column % 8;
32
33 if (c == '\b') {
34 if ((int)--column < 0)
[821]35 column = 0;
[2725]36 }
37 else if (c == '\r')
38 column = 0;
39 else { /* just a printable char */
40 if (unicode_status != UNICODE_ON /* every byte is a new char */
41 || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
42 ) {
[821]43 column++;
[2725]44 }
45 }
[821]46 return column;
47}
48
[2725]49/* Note that this function can write NULs, unlike fputs etc. */
50static void write2stdout(const void *buf, unsigned size)
[821]51{
[2725]52 fwrite(buf, 1, size, stdout);
53}
54
55int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
56int fold_main(int argc UNUSED_PARAM, char **argv)
57{
[1765]58 char *line_out = NULL;
[2725]59 const char *w_opt = "80";
60 unsigned width;
61 smallint exitcode = EXIT_SUCCESS;
[821]62
[2725]63 init_unicode();
64
[1765]65 if (ENABLE_INCLUDE_SUSv2) {
[821]66 /* Turn any numeric options into -w options. */
[2725]67 int i;
68 for (i = 1; argv[i]; i++) {
69 const char *a = argv[i];
70 if (*a == '-') {
71 a++;
72 if (*a == '-' && !a[1]) /* "--" */
[821]73 break;
[2725]74 if (isdigit(*a))
[1765]75 argv[i] = xasprintf("-w%s", a);
[821]76 }
77 }
78 }
79
[2725]80 getopt32(argv, "bsw:", &w_opt);
81 width = xatou_range(w_opt, 1, 10000);
[821]82
83 argv += optind;
[2725]84 if (!*argv)
[1765]85 *--argv = (char*)"-";
[821]86
87 do {
[1765]88 FILE *istream = fopen_or_warn_stdin(*argv);
[821]89 int c;
[2725]90 unsigned column = 0; /* Screen column where next char will go */
91 unsigned offset_out = 0; /* Index in 'line_out' for next char */
[821]92
93 if (istream == NULL) {
[2725]94 exitcode = EXIT_FAILURE;
[821]95 continue;
96 }
97
98 while ((c = getc(istream)) != EOF) {
[2725]99 /* We grow line_out in chunks of 0x1000 bytes */
100 if ((offset_out & 0xfff) == 0) {
101 line_out = xrealloc(line_out, offset_out + 0x1000);
[821]102 }
[2725]103 rescan:
104 line_out[offset_out] = c;
[821]105 if (c == '\n') {
[2725]106 write2stdout(line_out, offset_out + 1);
[821]107 column = offset_out = 0;
108 continue;
109 }
110 column = adjust_column(column, c);
[2725]111 if (column <= width || offset_out == 0) {
112 /* offset_out == 0 case happens
113 * with small width (say, 1) and tabs.
114 * The very first tab already goes to column 8,
115 * but we must not wrap it */
116 offset_out++;
117 continue;
118 }
[821]119
[2725]120 /* This character would make the line too long.
121 * Print the line plus a newline, and make this character
122 * start the next line */
123 if (option_mask32 & FLAG_BREAK_SPACES) {
124 unsigned i;
125 unsigned logical_end;
[821]126
[2725]127 /* Look for the last blank. */
128 for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) {
129 if (!isblank(line_out[logical_end]))
[821]130 continue;
[2725]131
132 /* Found a space or tab.
133 * Output up to and including it, and start a new line */
134 logical_end++;
135 /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */
136 write2stdout(line_out, logical_end);
137 putchar('\n');
138 /* Move the remainder to the beginning of the next line.
139 * The areas being copied here might overlap. */
140 memmove(line_out, line_out + logical_end, offset_out - logical_end);
141 offset_out -= logical_end;
142 for (column = i = 0; i < offset_out; i++) {
143 column = adjust_column(column, line_out[i]);
[821]144 }
[2725]145 goto rescan;
[821]146 }
[2725]147 /* No blank found, wrap will split the overlong word */
[821]148 }
[2725]149 /* Output what we accumulated up to now, and start a new line */
150 line_out[offset_out] = '\n';
151 write2stdout(line_out, offset_out + 1);
152 column = offset_out = 0;
153 goto rescan;
154 } /* while (not EOF) */
[821]155
156 if (offset_out) {
[2725]157 write2stdout(line_out, offset_out);
[821]158 }
159
[2725]160 if (fclose_if_not_stdin(istream)) {
161 bb_simple_perror_msg(*argv);
162 exitcode = EXIT_FAILURE;
[821]163 }
164 } while (*++argv);
165
[2725]166 fflush_stdout_and_exit(exitcode);
[821]167}
Note: See TracBrowser for help on using the repository browser.