1: #ifndef lint
   2: static char *sccsid = "@(#)fold.c	4.1 (Berkeley) 10/1/80";
   3: #endif
   4: #include <stdio.h>
   5: /*
   6:  * fold - fold long lines for finite output devices
   7:  *
   8:  * Bill Joy UCB June 28, 1977
   9:  */
  10: 
  11: int fold =  80;
  12: 
  13: main(argc, argv)
  14:     int argc;
  15:     char *argv[];
  16: {
  17:     register c;
  18:     char obuf[BUFSIZ];
  19: 
  20:     argc--, argv++;
  21:     setbuf(stdout, obuf);
  22:     if (argc > 0 && argv[0][0] == '-') {
  23:         fold = 0;
  24:         argv[0]++;
  25:         while (*argv[0] >= '0' && *argv[0] <= '9')
  26:             fold *= 10, fold += *argv[0]++ - '0';
  27:         if (*argv[0]) {
  28:             printf("Bad number for fold\n");
  29:             exit(1);
  30:         }
  31:         argc--, argv++;
  32:     }
  33:     do {
  34:         if (argc > 0) {
  35:             if (freopen(argv[0], "r", stdin) == NULL) {
  36:                 perror(argv[0]);
  37:                 exit(1);
  38:             }
  39:             argc--, argv++;
  40:         }
  41:         for (;;) {
  42:             c = getc(stdin);
  43:             if (c == -1)
  44:                 break;
  45:             putch(c);
  46:         }
  47:     } while (argc > 0);
  48:     exit(0);
  49: }
  50: 
  51: int col;
  52: 
  53: putch(c)
  54:     register c;
  55: {
  56:     register ncol;
  57: 
  58:     switch (c) {
  59:         case '\n':
  60:             ncol = 0;
  61:             break;
  62:         case '\t':
  63:             ncol = (col + 8) &~ 7;
  64:             break;
  65:         case '\b':
  66:             ncol = col ? col - 1 : 0;
  67:             break;
  68:         case '\r':
  69:             ncol = 0;
  70:             break;
  71:         default:
  72:             ncol = col + 1;
  73:     }
  74:     if (ncol > fold)
  75:         putchar('\n'), col = 0;
  76:     putchar(c);
  77:     switch (c) {
  78:         case '\n':
  79:             col = 0;
  80:             break;
  81:         case '\t':
  82:             col += 8;
  83:             col &= ~7;
  84:             break;
  85:         case '\b':
  86:             if (col)
  87:                 col--;
  88:             break;
  89:         case '\r':
  90:             col = 0;
  91:             break;
  92:         default:
  93:             col++;
  94:             break;
  95:     }
  96: }

Defined functions

main defined in line 13; never used
putch defined in line 53; used 1 times
  • in line 45

Defined variables

col defined in line 51; used 12 times
fold defined in line 11; used 4 times
sccsid defined in line 2; never used
Last modified: 1982-09-04
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 708
Valid CSS Valid XHTML 1.0 Strict