source: trunk/minix/commands/simple/fold.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.1 KB
RevLine 
[9]1/* fold - folds long lines Author: Terrence W. Holm */
2
3/* Usage: fold [ -width ] [ file ... ] */
4
5#include <stdlib.h>
6#include <stdio.h>
7
8#define TAB 8
9#define DEFAULT_WIDTH 80
10
11int column = 0; /* Current column, retained between files */
12
13_PROTOTYPE(int main, (int argc, char **argv));
14_PROTOTYPE(void Fold, (FILE *f, int width));
15
16int main(argc, argv)
17int argc;
18char *argv[];
19{
20 int width = DEFAULT_WIDTH;
21 int i;
22 FILE *f;
23
24 if (argc > 1 && argv[1][0] == '-') {
25 if ((width = atoi(&argv[1][1])) <= 0) {
26 fprintf(stderr, "Bad number for fold\n");
27 exit(1);
28 }
29 --argc;
30 ++argv;
31 }
32 if (argc == 1)
33 Fold(stdin, width);
34 else {
35 for (i = 1; i < argc; ++i) {
36 if ((f = fopen(argv[i], "r")) == NULL) {
37 perror(argv[i]);
38 exit(1);
39 }
40 Fold(f, width);
41 fclose(f);
42 }
43 }
44 return(0);
45}
46
47
48void Fold(f, width)
49FILE *f;
50int width;
51{
52 int c;
53
54 while ((c = getc(f)) != EOF) {
55 if (c == '\t')
56 column = (column / TAB + 1) * TAB;
57 else if (c == '\b')
58 column = column > 0 ? column - 1 : 0;
59 else if (c == '\n' || c == '\r')
60 column = 0;
61 else
62 ++column;
63
64 if (column > width) {
65 putchar('\n');
66 column = c == '\t' ? TAB : 1;
67 }
68 putchar(c);
69 }
70}
Note: See TracBrowser for help on using the repository browser.