--- OLD/ppm/ppmtorgb3.c Thu Jan 1 00:00:00 1970 +++ NEW/ppm/ppmtorgb3.c Thu Jan 1 00:00:00 1970 @@ -10,6 +10,14 @@ ** implied warranty. */ +/* + * Hacked on, 2003-06-14, to support output to arbitrary filenames + * (including, of course, pipes, when /dev/fd/ is available), and in + * the process remove a potential buffer overflow. All intellectual + * property rights I have in the resulting derivative works I hereby + * release into the public domain. -Mouse + */ + #include "ppm.h" #include "pgm.h" @@ -22,8 +30,9 @@ FILE* redfile; FILE* grnfile; FILE* blufile; - char* basename; - char filename[100]; + char *r_name; + char *g_name; + char *b_name; char* cp; pixel* pixelrow; register pixel* pP; @@ -36,36 +45,45 @@ ppm_init( &argc, argv ); - if ( argc > 2 ) - pm_usage( "[ppmfile]" ); - - if ( argc == 2 ) - { - ifp = pm_openr( argv[1] ); - basename = argv[1]; - cp = rindex( basename, '.' ); - if ( cp != NULL ) - *cp = '\0'; - } - else - { - ifp = stdin; - basename = "noname"; - } + switch (argc) + { case 1: + ifp = stdin; + r_name = "noname.red"; + g_name = "noname.grn"; + b_name = "noname.blu"; + break; + case 2: + ifp = pm_openr( argv[1] ); + cp = rindex( argv[1], '.' ); + if (cp) *cp = '\0'; + asprintf(&r_name,"%s.red",argv[1]); + asprintf(&g_name,"%s.grn",argv[1]); + asprintf(&b_name,"%s.blu",argv[1]); + break; + case 4: + ifp = stdin; + r_name = argv[1]; + g_name = argv[2]; + b_name = argv[3]; + break; + case 5: + ifp = pm_openr( argv[1] ); + r_name = argv[2]; + g_name = argv[3]; + b_name = argv[4]; + break; + default: + pm_usage( "[ppmfile] [r-out g-out b-out]" ); + break; + } ppm_readppminit( ifp, &cols, &rows, &maxval, &format ); pixelrow = ppm_allocrow( cols ); - (void) strcpy( filename, basename ); - (void) strcat( filename, ".red" ); - redfile = pm_openw( filename ); + redfile = pm_openw( r_name ); pgm_writepgminit( redfile, cols, rows, (gray) maxval, 0 ); - (void) strcpy( filename, basename ); - (void) strcat( filename, ".grn" ); - grnfile = pm_openw( filename ); + grnfile = pm_openw( g_name ); pgm_writepgminit( grnfile, cols, rows, (gray) maxval, 0 ); - (void) strcpy( filename, basename ); - (void) strcat( filename, ".blu" ); - blufile = pm_openw( filename ); + blufile = pm_openw( b_name ); pgm_writepgminit( blufile, cols, rows, (gray) maxval, 0 ); grayrow = pgm_allocrow( cols );