1 /* $OpenBSD: fold.c,v 1.11 2007/04/07 23:20:18 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 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1990, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93"; 45 #endif 46 static char rcsid[] = "$OpenBSD: fold.c,v 1.11 2007/04/07 23:20:18 tedu Exp $"; 47 #endif /* not lint */ 48 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 #include <err.h> 54 #include <limits.h> 55 56 #define DEFLINEWIDTH 80 57 58 static void fold(int); 59 static int new_column_position(int, int); 60 int count_bytes = 0; 61 int split_words = 0; 62 63 int 64 main(int argc, char *argv[]) 65 { 66 int ch; 67 int width; 68 char *p; 69 char *w; 70 const char *errstr; 71 72 width = -1; 73 while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1) 74 switch (ch) { 75 case 'b': 76 count_bytes = 1; 77 break; 78 case 's': 79 split_words = 1; 80 break; 81 case 'w': 82 width = strtonum(optarg, 1, INT_MAX, &errstr); 83 if (errstr != NULL) 84 errx(1, "illegal width value, %s: %s", errstr, 85 optarg); 86 break; 87 case '0': case '1': case '2': case '3': case '4': 88 case '5': case '6': case '7': case '8': case '9': 89 if (width == -1) { 90 p = argv[optind - 1]; 91 if (p[0] == '-' && p[1] == ch && !p[2]) 92 w = ++p; 93 else 94 w = argv[optind] + 1; 95 96 width = strtonum(w, 1, INT_MAX, &errstr); 97 if (errstr != NULL) 98 errx(1, "illegal width value, %s: %s", 99 errstr, optarg); 100 } 101 break; 102 default: 103 (void)fprintf(stderr, 104 "usage: fold [-bs] [-w width] [file ...]\n"); 105 exit(1); 106 } 107 argv += optind; 108 argc -= optind; 109 110 if (width == -1) 111 width = DEFLINEWIDTH; 112 113 if (!*argv) 114 fold(width); 115 else for (; *argv; ++argv) 116 if (!freopen(*argv, "r", stdin)) { 117 err(1, "%s", *argv); 118 /* NOTREACHED */ 119 } else 120 fold(width); 121 exit(0); 122 } 123 124 /* 125 * Fold the contents of standard input to fit within WIDTH columns 126 * (or bytes) and write to standard output. 127 * 128 * If split_words is set, split the line at the last space character 129 * on the line. This flag necessitates storing the line in a buffer 130 * until the current column > width, or a newline or EOF is read. 131 * 132 * The buffer can grow larger than WIDTH due to backspaces and carriage 133 * returns embedded in the input stream. 134 */ 135 static void 136 fold(int width) 137 { 138 static char *buf = NULL; 139 static int buf_max = 0; 140 int ch, col; 141 int indx; 142 143 col = indx = 0; 144 while ((ch = getchar()) != EOF) { 145 if (ch == '\n') { 146 if (indx != 0) 147 fwrite(buf, 1, indx, stdout); 148 putchar('\n'); 149 col = indx = 0; 150 continue; 151 } 152 153 col = new_column_position(col, ch); 154 if (col > width) { 155 int i, last_space; 156 157 if (split_words) { 158 for (i = 0, last_space = -1; i < indx; i++) 159 if(buf[i] == ' ') 160 last_space = i; 161 } 162 163 if (split_words && last_space != -1) { 164 last_space++; 165 166 fwrite(buf, 1, last_space, stdout); 167 memmove(buf, buf+last_space, indx-last_space); 168 169 indx -= last_space; 170 col = 0; 171 for (i = 0; i < indx; i++) { 172 col = new_column_position(col, buf[i]); 173 } 174 } else { 175 fwrite(buf, 1, indx, stdout); 176 col = indx = 0; 177 } 178 putchar('\n'); 179 180 /* calculate the column position for the next line. */ 181 col = new_column_position(col, ch); 182 } 183 184 if (indx + 1 > buf_max) { 185 int newmax = buf_max + 2048; 186 char *newbuf; 187 188 /* Allocate buffer in LINE_MAX increments */ 189 if ((newbuf = realloc(buf, newmax)) == NULL) { 190 err(1, NULL); 191 /* NOTREACHED */ 192 } 193 buf = newbuf; 194 buf_max = newmax; 195 } 196 buf[indx++] = ch; 197 } 198 199 if (indx != 0) 200 fwrite(buf, 1, indx, stdout); 201 } 202 203 /* 204 * calculate the column position 205 */ 206 static int 207 new_column_position(int col, int ch) 208 { 209 if (!count_bytes) { 210 switch (ch) { 211 case '\b': 212 if (col > 0) 213 --col; 214 break; 215 case '\r': 216 col = 0; 217 break; 218 case '\t': 219 col = (col + 8) & ~7; 220 break; 221 default: 222 ++col; 223 break; 224 } 225 } else { 226 ++col; 227 } 228 229 return col; 230 } 231