froese@eric.mpr.ca (Edwin Froese) (02/27/89)
EFTH MINIX report #70 - February 1989 - fold(1)
Yet another tool: folds long lines.....
Terrence W. Holm
holm@bdc.ubc.ca
----------------------------------------------------------
echo x - fold.1
sed '/^X/s///' > fold.1 << '/'
XNAME
X fold(1) - fold long lines
X
XSYNOPSIS
X fold [ -width ] [ file ... ]
X
XDESCRIPTION
X Fold(1) copies the named files to standard output, inserting
X a newline character if a line would be longer than <width>
X characters. If no files are specified then standard input is
X used. The default width is 80.
/
echo x - fold.c
sed '/^X/s///' > fold.c << '/'
X/* fold(1) Terrence W. Holm Feb. 1989 */
X
X/* Usage: fold [ -width ] [ file ... ] */
X
X#include <stdio.h>
X
X#define TAB 8
X#define DEFAULT_WIDTH 80
X
X
Xmain( argc, argv )
X int argc;
X char *argv[];
X
X {
X int width = DEFAULT_WIDTH;
X int i;
X FILE *f;
X
X if ( argc > 1 && argv[1][0] == '-' )
X {
X if ( (width = atoi( &argv[1][1] )) <= 0 )
X {
X fprintf( stderr, "Bad number for fold\n" );
X exit( 1 );
X }
X
X --argc;
X ++argv;
X }
X
X if ( argc == 1 )
X Fold( stdin, width );
X else
X for ( i = 1; i < argc; ++i )
X {
X if ( (f = fopen( argv[i], "r" )) == NULL )
X {
X perror( argv[i] );
X exit( 1 );
X }
X
X Fold( f, width );
X fclose( f );
X }
X
X exit( 0 );
X }
X
X
X
X
Xint column = 0; /* Current column, retained between files */
X
X
XFold( f, width )
X FILE *f;
X int width;
X
X {
X int c;
X
X while ( (c = getc( f )) != EOF )
X {
X if ( c == '\t' )
X column = ( column / TAB + 1 ) * TAB;
X else if ( c == '\b' )
X column = column > 0 ? column - 1 : 0;
X else if ( c == '\n' || c == '\r' )
X column = 0;
X else
X ++column;
X
X if ( column > width )
X {
X putchar( '\n' );
X column = c == '\t' ? TAB : 1;
X }
X
X putchar( c );
X }
X }
/
----------------------------------------------------------