/* * fmt: format stdin to 80 columns. */ #include #include #define WIDTH 78 #define MAXWORD 160 int curcol=0; /* * format in the flesh */ main() { char word[MAXWORD]; while (fscanf(stdin, "%s", word) > 0) { if (curcol > 0) /* don't do anything special for start of line */ if (curcol + strlen(word) >= WIDTH) { /* * if this word will make the screen wrap, kick out a * newline first. */ curcol = 0; putchar('\n'); } else { /* * otherwise just drop a space in front of the word */ putchar(' '); curcol++; } /* * put the word and update curcol. If the word was longer than * WIDTH, we dump it straight and hope that the tty will wrap. * */ fputs(word, stdout); curcol += strlen(word); } /* * drop out a newline at the end (if we were in the middle of a * line when we ran out of input. */ if (curcol > 0) putchar('\n'); } /* fmt */