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