1 /* $OpenBSD: fold.c,v 1.15 2015/02/06 09:10:55 tedu Exp $ */ 2 /* $NetBSD: fold.c,v 1.6 1995/09/01 01:42:44 jtc Exp $ */ 3 4 /*- 5 * Copyright (c) 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Kevin Ruddy. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 #include <ctype.h> 41 #include <err.h> 42 #include <limits.h> 43 44 #define DEFLINEWIDTH 80 45 46 static void fold(unsigned int); 47 static unsigned int new_column_position(unsigned int, int); 48 static __dead void usage(void); 49 int count_bytes = 0; 50 int split_words = 0; 51 52 int 53 main(int argc, char *argv[]) 54 { 55 int ch, lastch, newarg, prevoptind; 56 unsigned int width; 57 const char *errstr; 58 59 width = 0; 60 lastch = '\0'; 61 prevoptind = 1; 62 newarg = 1; 63 while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1) { 64 switch (ch) { 65 case 'b': 66 count_bytes = 1; 67 break; 68 case 's': 69 split_words = 1; 70 break; 71 case 'w': 72 width = strtonum(optarg, 1, UINT_MAX, &errstr); 73 if (errstr != NULL) 74 errx(1, "illegal width value, %s: %s", errstr, 75 optarg); 76 break; 77 case '0': case '1': case '2': case '3': case '4': 78 case '5': case '6': case '7': case '8': case '9': 79 if (newarg) 80 width = 0; 81 else if (!isdigit(lastch)) 82 usage(); 83 if (width > UINT_MAX / 10 - 1) 84 errx(1, "illegal width value, too large"); 85 width = (width * 10) + (ch - '0'); 86 if (width < 1) 87 errx(1, "illegal width value, too small"); 88 break; 89 default: 90 usage(); 91 } 92 lastch = ch; 93 newarg = optind != prevoptind; 94 prevoptind = optind; 95 } 96 argv += optind; 97 argc -= optind; 98 99 if (width == 0) 100 width = DEFLINEWIDTH; 101 102 if (!*argv) 103 fold(width); 104 else for (; *argv; ++argv) 105 if (!freopen(*argv, "r", stdin)) { 106 err(1, "%s", *argv); 107 /* NOTREACHED */ 108 } else 109 fold(width); 110 exit(0); 111 } 112 113 /* 114 * Fold the contents of standard input to fit within WIDTH columns 115 * (or bytes) and write to standard output. 116 * 117 * If split_words is set, split the line at the last space character 118 * on the line. This flag necessitates storing the line in a buffer 119 * until the current column > width, or a newline or EOF is read. 120 * 121 * The buffer can grow larger than WIDTH due to backspaces and carriage 122 * returns embedded in the input stream. 123 */ 124 static void 125 fold(unsigned int width) 126 { 127 static char *buf = NULL; 128 static int buf_max = 0; 129 int ch; 130 unsigned int col, indx; 131 132 col = indx = 0; 133 while ((ch = getchar()) != EOF) { 134 if (ch == '\n') { 135 if (indx != 0) 136 fwrite(buf, 1, indx, stdout); 137 putchar('\n'); 138 col = indx = 0; 139 continue; 140 } 141 142 col = new_column_position(col, ch); 143 if (col > width) { 144 unsigned int i, last_space; 145 146 if (split_words) { 147 for (i = 0, last_space = -1; i < indx; i++) 148 if(buf[i] == ' ') 149 last_space = i; 150 } 151 152 if (split_words && last_space != -1) { 153 last_space++; 154 155 fwrite(buf, 1, last_space, stdout); 156 memmove(buf, buf+last_space, indx-last_space); 157 158 indx -= last_space; 159 col = 0; 160 for (i = 0; i < indx; i++) { 161 col = new_column_position(col, buf[i]); 162 } 163 } else { 164 fwrite(buf, 1, indx, stdout); 165 col = indx = 0; 166 } 167 putchar('\n'); 168 169 /* calculate the column position for the next line. */ 170 col = new_column_position(col, ch); 171 } 172 173 if (indx + 1 > buf_max) { 174 int newmax = buf_max + 2048; 175 char *newbuf; 176 177 /* Allocate buffer in LINE_MAX increments */ 178 if ((newbuf = realloc(buf, newmax)) == NULL) { 179 err(1, NULL); 180 /* NOTREACHED */ 181 } 182 buf = newbuf; 183 buf_max = newmax; 184 } 185 buf[indx++] = ch; 186 } 187 188 if (indx != 0) 189 fwrite(buf, 1, indx, stdout); 190 } 191 192 /* 193 * calculate the column position 194 */ 195 static unsigned int 196 new_column_position(unsigned int col, int ch) 197 { 198 if (!count_bytes) { 199 switch (ch) { 200 case '\b': 201 if (col > 0) 202 --col; 203 break; 204 case '\r': 205 col = 0; 206 break; 207 case '\t': 208 col = (col + 8) & ~7; 209 break; 210 default: 211 ++col; 212 break; 213 } 214 } else { 215 ++col; 216 } 217 218 return col; 219 } 220 221 static __dead void 222 usage(void) 223 { 224 (void)fprintf(stderr, "usage: fold [-bs] [-w width] [file ...]\n"); 225 exit(1); 226 } 227