8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / vi / misc / fold.c
blobd1abc6568d6ad6755e976cac5144b521baf5d0e0
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
40 #pragma ident "%Z%%M% %I% %E% SMI"
42 #include <stdio.h>
44 * fold - fold long lines for finite output devices
48 int fold = 80;
50 void putch(int c);
52 int
53 main(int argc, char *argv[])
55 int c;
56 FILE *f;
57 char obuf[BUFSIZ];
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);
70 argc--, argv++;
72 do {
73 if (argc > 0) {
74 if (freopen(argv[0], "r", stdin) == NULL) {
75 perror(argv[0]);
76 exit(1);
78 argc--, argv++;
80 for (;;) {
81 c = getc(stdin);
82 if (c == -1)
83 break;
84 putch(c);
86 } while (argc > 0);
87 return (0);
90 int col;
92 void
93 putch(int c)
95 int ncol;
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;
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;