xref: /titanic_44/usr/src/cmd/vi/misc/fold.c (revision bb25c06cca41ca78e5fb87fbb8e81d55beb18c95)
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 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include <stdio.h>
43 /*
44  * fold - fold long lines for finite output devices
45  *
46  */
47 
48 int	fold =  80;
49 
50 void putch(int c);
51 
52 int
53 main(int argc, char *argv[])
54 {
55 	int c;
56 	FILE *f;
57 	char obuf[BUFSIZ];
58 
59 	argc--, argv++;
60 	setbuf(stdout, obuf);
61 	if (argc > 0 && argv[0][0] == '-') {
62 		fold = 0;
63 		argv[0]++;
64 		while (*argv[0] >= '0' && *argv[0] <= '9')
65 			fold *= 10, fold += *argv[0]++ - '0';
66 		if (*argv[0]) {
67 			printf("Bad number for fold\n");
68 			exit(1);
69 		}
70 		argc--, argv++;
71 	}
72 	do {
73 		if (argc > 0) {
74 			if (freopen(argv[0], "r", stdin) == NULL) {
75 				perror(argv[0]);
76 				exit(1);
77 			}
78 			argc--, argv++;
79 		}
80 		for (;;) {
81 			c = getc(stdin);
82 			if (c == -1)
83 				break;
84 			putch(c);
85 		}
86 	} while (argc > 0);
87 	return (0);
88 }
89 
90 int	col;
91 
92 void
93 putch(int c)
94 {
95 	int ncol;
96 
97 	switch (c) {
98 		case '\n':
99 			ncol = 0;
100 			break;
101 		case '\t':
102 			ncol = (col + 8) &~ 7;
103 			break;
104 		case '\b':
105 			ncol = col ? col - 1 : 0;
106 			break;
107 		case '\r':
108 			ncol = 0;
109 			break;
110 		default:
111 			ncol = col + 1;
112 	}
113 	if (ncol > fold)
114 		putchar('\n'), col = 0;
115 	putchar(c);
116 	switch (c) {
117 		case '\n':
118 			col = 0;
119 			break;
120 		case '\t':
121 			col += 8;
122 			col &= ~7;
123 			break;
124 		case '\b':
125 			if (col)
126 				col--;
127 			break;
128 		case '\r':
129 			col = 0;
130 			break;
131 		default:
132 			col++;
133 			break;
134 	}
135 }
136