fold.c revision 87751
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 1990, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software contributed to Berkeley by 61590Srgrimes * Kevin Ruddy. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 3. All advertising materials mentioning features or use of this software 171590Srgrimes * must display the following acknowledgement: 181590Srgrimes * This product includes software developed by the University of 191590Srgrimes * California, Berkeley and its contributors. 201590Srgrimes * 4. Neither the name of the University nor the names of its contributors 211590Srgrimes * may be used to endorse or promote products derived from this software 221590Srgrimes * without specific prior written permission. 231590Srgrimes * 241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341590Srgrimes * SUCH DAMAGE. 351590Srgrimes */ 361590Srgrimes 371590Srgrimes#ifndef lint 3827270Scharnierstatic const char copyright[] = 391590Srgrimes"@(#) Copyright (c) 1990, 1993\n\ 401590Srgrimes The Regents of the University of California. All rights reserved.\n"; 411590Srgrimes#endif /* not lint */ 421590Srgrimes 431590Srgrimes#ifndef lint 4427270Scharnier#if 0 451590Srgrimesstatic char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93"; 4627270Scharnier#endif 471590Srgrimes#endif /* not lint */ 481590Srgrimes 4987751Scharnier#include <sys/cdefs.h> 5087751Scharnier__FBSDID("$FreeBSD: head/usr.bin/fold/fold.c 87751 2001-12-12 18:25:53Z charnier $"); 5187751Scharnier 5227270Scharnier#include <err.h> 531590Srgrimes#include <stdio.h> 5427270Scharnier#include <stdlib.h> 5527270Scharnier#include <unistd.h> 561590Srgrimes 571590Srgrimes#define DEFLINEWIDTH 80 581590Srgrimes 5927270Scharniervoid fold __P((int)); 6027270Scharnierstatic void usage __P((void)); 6127270Scharnier 6227270Scharnierint 631590Srgrimesmain(argc, argv) 641590Srgrimes int argc; 651590Srgrimes char **argv; 661590Srgrimes{ 671590Srgrimes register int ch; 681590Srgrimes int width; 691590Srgrimes char *p; 701590Srgrimes 711590Srgrimes width = -1; 7224360Simp while ((ch = getopt(argc, argv, "0123456789w:")) != -1) 731590Srgrimes switch (ch) { 741590Srgrimes case 'w': 751590Srgrimes if ((width = atoi(optarg)) <= 0) { 7627270Scharnier errx(1, "illegal width value"); 771590Srgrimes } 781590Srgrimes break; 791590Srgrimes case '0': case '1': case '2': case '3': case '4': 801590Srgrimes case '5': case '6': case '7': case '8': case '9': 811590Srgrimes if (width == -1) { 821590Srgrimes p = argv[optind - 1]; 831590Srgrimes if (p[0] == '-' && p[1] == ch && !p[2]) 841590Srgrimes width = atoi(++p); 851590Srgrimes else 861590Srgrimes width = atoi(argv[optind] + 1); 871590Srgrimes } 881590Srgrimes break; 891590Srgrimes default: 9027270Scharnier usage(); 911590Srgrimes } 921590Srgrimes argv += optind; 931590Srgrimes argc -= optind; 941590Srgrimes 951590Srgrimes if (width == -1) 961590Srgrimes width = DEFLINEWIDTH; 971590Srgrimes if (!*argv) 981590Srgrimes fold(width); 991590Srgrimes else for (; *argv; ++argv) 1001590Srgrimes if (!freopen(*argv, "r", stdin)) { 10127270Scharnier err(1, "%s", *argv); 1021590Srgrimes } else 1031590Srgrimes fold(width); 1041590Srgrimes exit(0); 1051590Srgrimes} 1061590Srgrimes 10727270Scharnierstatic void 10827270Scharnierusage() 10927270Scharnier{ 11027270Scharnier (void)fprintf(stderr, "usage: fold [-w width] [file ...]\n"); 11127270Scharnier exit(1); 11227270Scharnier} 11327270Scharnier 11427270Scharniervoid 1151590Srgrimesfold(width) 1161590Srgrimes register int width; 1171590Srgrimes{ 1181590Srgrimes register int ch, col, new; 1191590Srgrimes 1201590Srgrimes for (col = 0;;) { 1211590Srgrimes switch (ch = getchar()) { 1221590Srgrimes case EOF: 1231590Srgrimes return; 1241590Srgrimes case '\b': 1251590Srgrimes new = col ? col - 1 : 0; 1261590Srgrimes break; 1271590Srgrimes case '\n': 1281590Srgrimes case '\r': 1291590Srgrimes new = 0; 1301590Srgrimes break; 1311590Srgrimes case '\t': 1321590Srgrimes new = (col + 8) & ~7; 1331590Srgrimes break; 1341590Srgrimes default: 1351590Srgrimes new = col + 1; 1361590Srgrimes break; 1371590Srgrimes } 1381590Srgrimes 1391590Srgrimes if (new > width) { 1401590Srgrimes putchar('\n'); 1411590Srgrimes col = 0; 1421590Srgrimes } 1431590Srgrimes putchar(ch); 1441590Srgrimes 1451590Srgrimes switch (ch) { 1461590Srgrimes case '\b': 1471590Srgrimes if (col > 0) 1481590Srgrimes --col; 1491590Srgrimes break; 1501590Srgrimes case '\n': 1511590Srgrimes case '\r': 1521590Srgrimes col = 0; 1531590Srgrimes break; 1541590Srgrimes case '\t': 1551590Srgrimes col += 8; 1561590Srgrimes col &= ~7; 1571590Srgrimes break; 1581590Srgrimes default: 1591590Srgrimes ++col; 1601590Srgrimes break; 1611590Srgrimes } 1621590Srgrimes } 1631590Srgrimes} 164