121555Sdist /* 221555Sdist * Copyright (c) 1980 Regents of the University of California. 3*34911Sbostic * All rights reserved. 4*34911Sbostic * 5*34911Sbostic * Redistribution and use in source and binary forms are permitted 6*34911Sbostic * provided that the above copyright notice and this paragraph are 7*34911Sbostic * duplicated in all such forms and that any documentation, 8*34911Sbostic * advertising materials, and other materials related to such 9*34911Sbostic * distribution and use acknowledge that the software was developed 10*34911Sbostic * by the University of California, Berkeley. The name of the 11*34911Sbostic * University may not be used to endorse or promote products derived 12*34911Sbostic * from this software without specific prior written permission. 13*34911Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34911Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34911Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1621555Sdist */ 1721555Sdist 1821555Sdist #ifndef lint 1921555Sdist char copyright[] = 2021555Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 2121555Sdist All rights reserved.\n"; 22*34911Sbostic #endif /* not lint */ 2321555Sdist 2421555Sdist #ifndef lint 25*34911Sbostic static char sccsid[] = "@(#)fold.c 5.2 (Berkeley) 06/29/88"; 26*34911Sbostic #endif /* not lint */ 2721555Sdist 281018Sbill #include <stdio.h> 291018Sbill /* 301018Sbill * fold - fold long lines for finite output devices 311018Sbill * 321018Sbill * Bill Joy UCB June 28, 1977 331018Sbill */ 341018Sbill 351018Sbill int fold = 80; 361018Sbill 371018Sbill main(argc, argv) 381018Sbill int argc; 391018Sbill char *argv[]; 401018Sbill { 411018Sbill register c; 421018Sbill FILE *f; 431018Sbill 441018Sbill argc--, argv++; 451018Sbill if (argc > 0 && argv[0][0] == '-') { 461018Sbill fold = 0; 471018Sbill argv[0]++; 481018Sbill while (*argv[0] >= '0' && *argv[0] <= '9') 4910821Ssam fold *= 10, fold += *argv[0]++ - '0'; 501018Sbill if (*argv[0]) { 511018Sbill printf("Bad number for fold\n"); 521018Sbill exit(1); 531018Sbill } 541018Sbill argc--, argv++; 551018Sbill } 561018Sbill do { 571018Sbill if (argc > 0) { 581018Sbill if (freopen(argv[0], "r", stdin) == NULL) { 591018Sbill perror(argv[0]); 601018Sbill exit(1); 611018Sbill } 621018Sbill argc--, argv++; 631018Sbill } 641018Sbill for (;;) { 651018Sbill c = getc(stdin); 661018Sbill if (c == -1) 671018Sbill break; 681018Sbill putch(c); 691018Sbill } 701018Sbill } while (argc > 0); 711018Sbill exit(0); 721018Sbill } 731018Sbill 741018Sbill int col; 751018Sbill 761018Sbill putch(c) 771018Sbill register c; 781018Sbill { 791018Sbill register ncol; 801018Sbill 811018Sbill switch (c) { 821018Sbill case '\n': 831018Sbill ncol = 0; 841018Sbill break; 851018Sbill case '\t': 861018Sbill ncol = (col + 8) &~ 7; 871018Sbill break; 881018Sbill case '\b': 891018Sbill ncol = col ? col - 1 : 0; 901018Sbill break; 911018Sbill case '\r': 921018Sbill ncol = 0; 931018Sbill break; 941018Sbill default: 951018Sbill ncol = col + 1; 961018Sbill } 971018Sbill if (ncol > fold) 981018Sbill putchar('\n'), col = 0; 991018Sbill putchar(c); 1001018Sbill switch (c) { 1011018Sbill case '\n': 1021018Sbill col = 0; 1031018Sbill break; 1041018Sbill case '\t': 10510821Ssam col += 8; 10610821Ssam col &= ~7; 1071018Sbill break; 1081018Sbill case '\b': 1091018Sbill if (col) 1101018Sbill col--; 1111018Sbill break; 1121018Sbill case '\r': 1131018Sbill col = 0; 1141018Sbill break; 1151018Sbill default: 1161018Sbill col++; 1171018Sbill break; 1181018Sbill } 1191018Sbill } 120