retire nonsymbolic rootdev, dev2name
[minix.git] / commands / fold / fold.c
blob752b9b2d41266e8a118ce5a4646b94a3437ffdf6
1 /* fold - folds long lines Author: Terrence W. Holm */
3 /* Usage: fold [ -width ] [ file ... ] */
5 #include <stdlib.h>
6 #include <stdio.h>
8 #define TAB 8
9 #define DEFAULT_WIDTH 80
11 int column = 0; /* Current column, retained between files */
13 int main(int argc, char **argv);
14 void Fold(FILE *f, int width);
16 int main(argc, argv)
17 int argc;
18 char *argv[];
20 int width = DEFAULT_WIDTH;
21 int i;
22 FILE *f;
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);
29 --argc;
30 ++argv;
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);
40 Fold(f, width);
41 fclose(f);
44 return(0);
48 void Fold(f, width)
49 FILE *f;
50 int width;
52 int c;
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;
64 if (column > width) {
65 putchar('\n');
66 column = c == '\t' ? TAB : 1;
68 putchar(c);