/* folds long input lines into two or more shorter lines after the * last non-blank character that occurs before the n-th column of * input, handling very long lines and blank-less lines intelligently */ #include #define MAXLINE 20 #define TABSPC 8 #define putbuf buf[bufi] = '\0'; \ printf("%s", buf); \ buf[0] = '\0'; \ bufi = 0; int main(void) { char buf[MAXLINE+1]; int i, c, bufi, t1, t2; i = bufi = 0; buf[0] = '\0'; while ((c=getchar()) != EOF) { if (i >= MAXLINE) { if (bufi < MAXLINE) { putchar('\n'); i = bufi; } else if (bufi > MAXLINE) { t2 = buf[--bufi]; t1 = buf[--bufi]; putbuf; printf("-\n"); buf[bufi++] = t1; buf[bufi++] = t2; i = bufi; } } if (c == ' ') { putbuf; putchar(c); i++; } else if (c == '\t') { putbuf; putchar(c); i += TABSPC; } else if (c == '\n') { putbuf; putchar(c); i = 0; } else { buf[bufi++] = c; i++; } } if (bufi > 0) putbuf; return 0; }