*** libgr-2.0.13/pbm/pbmtext.c Sat Apr 5 05:23:03 1997 --- libgr-2.0.13/pbm/pbmtext.c.s-max Sat Nov 27 20:13:23 1999 *************** *** 16,21 **** --- 16,22 ---- static void fix_control_chars ARGS(( char* buf )); static void fill_rect ARGS(( bit** bits, int row0, int col0, int height, int width, bit color )); static void copy_rect ARGS(( bit** fbits, int frow0, int fcol0, int height, int width, bit** tbits, int trow0, int tcol0 )); + static char * read_line ARGS(( void )); int main( argc, argv ) *************** *** 104,111 **** } else { /* Read text from stdin. */ lines = 0; ! while ( gets( buf ) != NULL ) { int l; --- 105,113 ---- } else { /* Read text from stdin. */ + char * buf; lines = 0; ! while ((buf = read_line())) { int l; *************** *** 267,270 **** --- 269,315 ---- for ( row = 0; row < height; ++row ) for ( col = 0; col < width; ++col ) tbits[trow0 + row][tcol0 + col] = fbits[frow0 + row][fcol0 + col]; + } + + #define SIZE_MAX (~((size_t) 0)) + static char * + read_line() + { + static char * buf = (char *) 0; + static size_t buf_len = 0; + size_t len_was, to_read; + char * p; + + if (!buf) + { + buf = (char *) malloc( buf_len = 4096 ); + if (!buf) + pm_error( "out of memory" ); + } + + for (p = buf, to_read = len_was = buf_len; fgets(p, to_read, stdin); ) + { + if (strchr(p, '\n') || feof(stdin)) + return buf; /* End of line; return line. */ + + /* Else expand buf and slurp up some more data. */ + if (buf_len == SIZE_MAX) + pm_error( "out of memory" ); /* Already at max; no room to grow. */ + + len_was = buf_len; + buf_len = ((SIZE_MAX / 2) < buf_len) ? SIZE_MAX : buf_len * 2; + buf = (char *) realloc( buf, buf_len ); + + if (!buf) + pm_error( "out of memory" ); + + p = buf + strlen( buf ); + to_read = buf_len - len_was + 1; + } + + /* EOF. */ + free( buf ); + buf_len = 0; + buf = (char *) 0; + return buf; }