/************************************************************************ 一行の長さを制限するプログラム lenlimit copyright 2004, TKEN ************************************************************************/ #include static int newline_p( int, int ); int main( int argc, char **argv ) { int i; int limit; int c; int count; int flag; int nc; int ncount; char *tmpbuf; if ( argc > 2 ) { fprintf( stderr, "Usage:%s [-num]\n", argv[0] ); exit(1); } if ( argc == 2 ) { limit = atoi( &argv[1][1] ); if ( limit < 1 ) { fprintf( stderr, "wrong number [%s]\n", argv[1] ); exit(1); } } else { limit = 1024; } tmpbuf = (char *)malloc( limit + 1 ); if ( tmpbuf == NULL ) { fprintf( stderr, "Cannot alloc enough memory\n" ); exit(1); } count = 0; flag = 0; while( ( c = fgetc( stdin ) ) != EOF ) { nc = fgetc( stdin ); ungetc( nc, stdin ); if ( count == limit ) { tmpbuf[limit] = '\0'; printf( "%s\n", tmpbuf ); count = 0; flag = 1; } if ( ( ncount = newline_p( c, nc ) ) != 0 ) { if ( flag == 0 ) { tmpbuf[count] = '\0'; printf( "%s\n", tmpbuf ); } for ( i = 0; i < ncount - 1; i++ ) { fgetc( stdin ); } flag = 0; count = 0; continue; } if ( flag == 0 ) { tmpbuf[count] = c; count++; } } free( tmpbuf ); return 0; } static int newline_p( int c1, int c2 ) { if ( ( c1 == 13) && ( c2 == 10 ) ) { return 2; } if ( c1 == 13 ) { return 1; } if ( c1 == 10 ) { return 1; } return 0; }