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