Magellan Linux

Diff of /trunk/mkinitrd-magellan/busybox/coreutils/fold.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 983 by niro, Fri Apr 24 18:33:46 2009 UTC revision 984 by niro, Sun May 30 11:32:42 2010 UTC
# Line 9  Line 9 
9    
10     Licensed under the GPL v2 or later, see the file LICENSE in this tarball.     Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
11  */  */
   
12  #include "libbb.h"  #include "libbb.h"
13    #include "unicode.h"
14    
15  /* Must match getopt32 call */  /* Must match getopt32 call */
16  #define FLAG_COUNT_BYTES        1  #define FLAG_COUNT_BYTES        1
# Line 20  Line 20 
20  /* Assuming the current column is COLUMN, return the column that  /* Assuming the current column is COLUMN, return the column that
21     printing C will move the cursor to.     printing C will move the cursor to.
22     The first column is 0. */     The first column is 0. */
23  static int adjust_column(int column, char c)  static int adjust_column(unsigned column, char c)
24  {  {
25   if (!(option_mask32 & FLAG_COUNT_BYTES)) {   if (option_mask32 & FLAG_COUNT_BYTES)
26   if (c == '\b') {   return ++column;
27   if (column > 0)  
28   column--;   if (c == '\t')
29   } else if (c == '\r')   return column + 8 - column % 8;
30    
31     if (c == '\b') {
32     if ((int)--column < 0)
33   column = 0;   column = 0;
34   else if (c == '\t')   }
35   column = column + 8 - column % 8;   else if (c == '\r')
36   else /* if (isprint (c)) */   column = 0;
37     else { /* just a printable char */
38     if (unicode_status != UNICODE_ON /* every byte is a new char */
39     || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
40     ) {
41   column++;   column++;
42   } else   }
43   column++;   }
44   return column;   return column;
45  }  }
46    
47    /* Note that this function can write NULs, unlike fputs etc. */
48    static void write2stdout(const void *buf, unsigned size)
49    {
50     fwrite(buf, 1, size, stdout);
51    }
52    
53  int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;  int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
54  int fold_main(int argc, char **argv)  int fold_main(int argc UNUSED_PARAM, char **argv)
55  {  {
56   char *line_out = NULL;   char *line_out = NULL;
57   int allocated_out = 0;   const char *w_opt = "80";
58   char *w_opt;   unsigned width;
59   int width = 80;   smallint exitcode = EXIT_SUCCESS;
60   int i;  
61   int errs = 0;   init_unicode();
62    
63   if (ENABLE_INCLUDE_SUSv2) {   if (ENABLE_INCLUDE_SUSv2) {
64   /* Turn any numeric options into -w options.  */   /* Turn any numeric options into -w options.  */
65   for (i = 1; i < argc; i++) {   int i;
66   char const *a = argv[i];   for (i = 1; argv[i]; i++) {
67     const char *a = argv[i];
68   if (*a++ == '-') {   if (*a == '-') {
69     a++;
70   if (*a == '-' && !a[1]) /* "--" */   if (*a == '-' && !a[1]) /* "--" */
71   break;   break;
72   if (isdigit(*a))   if (isdigit(*a))
# Line 62  int fold_main(int argc, char **argv) Line 76  int fold_main(int argc, char **argv)
76   }   }
77    
78   getopt32(argv, "bsw:", &w_opt);   getopt32(argv, "bsw:", &w_opt);
79   if (option_mask32 & FLAG_WIDTH)   width = xatou_range(w_opt, 1, 10000);
  width = xatoul_range(w_opt, 1, 10000);  
80    
81   argv += optind;   argv += optind;
82   if (!*argv)   if (!*argv)
# Line 72  int fold_main(int argc, char **argv) Line 85  int fold_main(int argc, char **argv)
85   do {   do {
86   FILE *istream = fopen_or_warn_stdin(*argv);   FILE *istream = fopen_or_warn_stdin(*argv);
87   int c;   int c;
88   int column = 0; /* Screen column where next char will go. */   unsigned column = 0;     /* Screen column where next char will go */
89   int offset_out = 0; /* Index in 'line_out' for next char. */   unsigned offset_out = 0; /* Index in 'line_out' for next char */
90    
91   if (istream == NULL) {   if (istream == NULL) {
92   errs |= EXIT_FAILURE;   exitcode = EXIT_FAILURE;
93   continue;   continue;
94   }   }
95    
96   while ((c = getc(istream)) != EOF) {   while ((c = getc(istream)) != EOF) {
97   if (offset_out + 1 >= allocated_out) {   /* We grow line_out in chunks of 0x1000 bytes */
98   allocated_out += 1024;   if ((offset_out & 0xfff) == 0) {
99   line_out = xrealloc(line_out, allocated_out);   line_out = xrealloc(line_out, offset_out + 0x1000);
100   }   }
101     rescan:
102     line_out[offset_out] = c;
103   if (c == '\n') {   if (c == '\n') {
104   line_out[offset_out++] = c;   write2stdout(line_out, offset_out + 1);
  fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);  
105   column = offset_out = 0;   column = offset_out = 0;
106   continue;   continue;
107   }   }
  rescan:  
108   column = adjust_column(column, c);   column = adjust_column(column, c);
109     if (column <= width || offset_out == 0) {
110     /* offset_out == 0 case happens
111     * with small width (say, 1) and tabs.
112     * The very first tab already goes to column 8,
113     * but we must not wrap it */
114     offset_out++;
115     continue;
116     }
117    
118   if (column > width) {   /* This character would make the line too long.
119   /* This character would make the line too long.   * Print the line plus a newline, and make this character
120     Print the line plus a newline, and make this character   * start the next line */
121     start the next line. */   if (option_mask32 & FLAG_BREAK_SPACES) {
122   if (option_mask32 & FLAG_BREAK_SPACES) {   unsigned i;
123   /* Look for the last blank. */   unsigned logical_end;
124   int logical_end;  
125     /* Look for the last blank. */
126   for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {   for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) {
127   if (isblank(line_out[logical_end])) {   if (!isblank(line_out[logical_end]))
  break;  
  }  
  }  
  if (logical_end >= 0) {  
  /* Found a blank.  Don't output the part after it. */  
  logical_end++;  
  fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);  
  bb_putchar('\n');  
  /* Move the remainder to the beginning of the next line.  
    The areas being copied here might overlap. */  
  memmove(line_out, line_out + logical_end, offset_out - logical_end);  
  offset_out -= logical_end;  
  for (column = i = 0; i < offset_out; i++) {  
  column = adjust_column(column, line_out[i]);  
  }  
  goto rescan;  
  }  
  } else {  
  if (offset_out == 0) {  
  line_out[offset_out++] = c;  
128   continue;   continue;
129    
130     /* Found a space or tab.
131     * Output up to and including it, and start a new line */
132     logical_end++;
133     /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */
134     write2stdout(line_out, logical_end);
135     putchar('\n');
136     /* Move the remainder to the beginning of the next line.
137     * The areas being copied here might overlap. */
138     memmove(line_out, line_out + logical_end, offset_out - logical_end);
139     offset_out -= logical_end;
140     for (column = i = 0; i < offset_out; i++) {
141     column = adjust_column(column, line_out[i]);
142   }   }
143     goto rescan;
144   }   }
145   line_out[offset_out++] = '\n';   /* No blank found, wrap will split the overlong word */
  fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);  
  column = offset_out = 0;  
  goto rescan;  
146   }   }
147     /* Output what we accumulated up to now, and start a new line */
148   line_out[offset_out++] = c;   line_out[offset_out] = '\n';
149   }   write2stdout(line_out, offset_out + 1);
150     column = offset_out = 0;
151     goto rescan;
152     } /* while (not EOF) */
153    
154   if (offset_out) {   if (offset_out) {
155   fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);   write2stdout(line_out, offset_out);
156   }   }
157    
158   if (fclose_if_not_stdin(istream)) {   if (fclose_if_not_stdin(istream)) {
159   bb_simple_perror_msg(*argv); /* Avoid multibyte problems. */   bb_simple_perror_msg(*argv);
160   errs |= EXIT_FAILURE;   exitcode = EXIT_FAILURE;
161   }   }
162   } while (*++argv);   } while (*++argv);
163    
164   fflush_stdout_and_exit(errs);   fflush_stdout_and_exit(exitcode);
165  }  }

Legend:
Removed from v.983  
changed lines
  Added in v.984