1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23 /* All Rights Reserved */ 24 25 26 /* 27 * University Copyright- Copyright (c) 1982, 1986, 1988 28 * The Regents of the University of California 29 * All Rights Reserved 30 * 31 * University Acknowledgment- Portions of this document are derived from 32 * software developed by the University of California, Berkeley, and its 33 * contributors. 34 */ 35 36 #pragma ident "%Z%%M% %I% %E% SMI" 37 38 #include <stdio.h> 39 /* 40 * fold - fold long lines for finite output devices 41 * 42 */ 43 44 int fold = 80; 45 46 main(argc, argv) 47 int argc; 48 char *argv[]; 49 { 50 register c; 51 FILE *f; 52 char obuf[BUFSIZ]; 53 54 argc--, argv++; 55 setbuf(stdout, obuf); 56 if (argc > 0 && argv[0][0] == '-') { 57 fold = 0; 58 argv[0]++; 59 while (*argv[0] >= '0' && *argv[0] <= '9') 60 fold *= 10, fold += *argv[0]++ - '0'; 61 if (*argv[0]) { 62 printf("Bad number for fold\n"); 63 exit(1); 64 } 65 argc--, argv++; 66 } 67 do { 68 if (argc > 0) { 69 if (freopen(argv[0], "r", stdin) == NULL) { 70 perror(argv[0]); 71 exit(1); 72 } 73 argc--, argv++; 74 } 75 for (;;) { 76 c = getc(stdin); 77 if (c == -1) 78 break; 79 putch(c); 80 } 81 } while (argc > 0); 82 exit(0); 83 } 84 85 int col; 86 87 putch(c) 88 register c; 89 { 90 register ncol; 91 92 switch (c) { 93 case '\n': 94 ncol = 0; 95 break; 96 case '\t': 97 ncol = (col + 8) &~ 7; 98 break; 99 case '\b': 100 ncol = col ? col - 1 : 0; 101 break; 102 case '\r': 103 ncol = 0; 104 break; 105 default: 106 ncol = col + 1; 107 } 108 if (ncol > fold) 109 putchar('\n'), col = 0; 110 putchar(c); 111 switch (c) { 112 case '\n': 113 col = 0; 114 break; 115 case '\t': 116 col += 8; 117 col &= ~7; 118 break; 119 case '\b': 120 if (col) 121 col--; 122 break; 123 case '\r': 124 col = 0; 125 break; 126 default: 127 col++; 128 break; 129 } 130 } 131