mv machine dependent
[unix-history] / usr / src / usr.bin / fold / fold.c
CommitLineData
22e155fc
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1980 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
13#ifndef lint
14static char sccsid[] = "@(#)fold.c 5.1 (Berkeley) %G%";
15#endif not lint
16
3f1c192a
BJ
17#include <stdio.h>
18/*
19 * fold - fold long lines for finite output devices
20 *
21 * Bill Joy UCB June 28, 1977
22 */
23
24int fold = 80;
25
26main(argc, argv)
27 int argc;
28 char *argv[];
29{
30 register c;
31 FILE *f;
3f1c192a
BJ
32
33 argc--, argv++;
3f1c192a
BJ
34 if (argc > 0 && argv[0][0] == '-') {
35 fold = 0;
36 argv[0]++;
37 while (*argv[0] >= '0' && *argv[0] <= '9')
474113c9 38 fold *= 10, fold += *argv[0]++ - '0';
3f1c192a
BJ
39 if (*argv[0]) {
40 printf("Bad number for fold\n");
41 exit(1);
42 }
43 argc--, argv++;
44 }
45 do {
46 if (argc > 0) {
47 if (freopen(argv[0], "r", stdin) == NULL) {
48 perror(argv[0]);
49 exit(1);
50 }
51 argc--, argv++;
52 }
53 for (;;) {
54 c = getc(stdin);
55 if (c == -1)
56 break;
57 putch(c);
58 }
59 } while (argc > 0);
60 exit(0);
61}
62
63int col;
64
65putch(c)
66 register c;
67{
68 register ncol;
69
70 switch (c) {
71 case '\n':
72 ncol = 0;
73 break;
74 case '\t':
75 ncol = (col + 8) &~ 7;
76 break;
77 case '\b':
78 ncol = col ? col - 1 : 0;
79 break;
80 case '\r':
81 ncol = 0;
82 break;
83 default:
84 ncol = col + 1;
85 }
86 if (ncol > fold)
87 putchar('\n'), col = 0;
88 putchar(c);
89 switch (c) {
90 case '\n':
91 col = 0;
92 break;
93 case '\t':
474113c9
SL
94 col += 8;
95 col &= ~7;
3f1c192a
BJ
96 break;
97 case '\b':
98 if (col)
99 col--;
100 break;
101 case '\r':
102 col = 0;
103 break;
104 default:
105 col++;
106 break;
107 }
108}