--- ray/src/util/getinfo.c 2014/09/08 18:21:39 2.12 +++ ray/src/util/getinfo.c 2019/07/06 14:08:07 2.18 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: getinfo.c,v 2.12 2014/09/08 18:21:39 greg Exp $"; +static const char RCSid[] = "$Id: getinfo.c,v 2.18 2019/07/06 14:08:07 greg Exp $"; #endif /* * getinfo.c - program to read info. header from file. @@ -11,20 +11,18 @@ static const char RCSid[] = "$Id: getinfo.c,v 2.12 201 #include #include "platform.h" +#include "rtprocess.h" #include "resolu.h" #ifdef getc_unlocked /* avoid nasty file-locking overhead */ +#undef getc #undef getchar #undef putchar +#define getc getc_unlocked #define getchar getchar_unlocked #define putchar putchar_unlocked #endif -#ifdef _WIN32 -#include -#define execvp _execvp -#endif - static gethfunc tabstr; static void getdim(FILE *fp); static void copycat(void); @@ -59,22 +57,55 @@ main( if (argc > 1 && !strcmp(argv[1], "-d")) { argc--; argv++; dim = 1; - SET_FILE_BINARY(stdin); - } else if (argc > 2 && !strcmp(argv[1], "-c")) { - SET_FILE_BINARY(stdin); + } +#ifdef getc_unlocked /* avoid lock/unlock overhead */ + flockfile(stdin); +#endif + SET_FILE_BINARY(stdin); + if (argc > 2 && !strcmp(argv[1], "-c")) { SET_FILE_BINARY(stdout); setvbuf(stdin, NULL, _IONBF, 2); - getheader(stdin, (gethfunc *)fputs, stdout); + if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) + return 1; printargs(argc-2, argv+2, stdout); fputc('\n', stdout); + if (dim) { /* copy resolution string? */ + RESOLU rs; + if (!fgetsresolu(&rs, stdin)) { + fputs("No resolution string\n", stderr); + return 1; + } + fputsresolu(&rs, stdout); + } fflush(stdout); execvp(argv[2], argv+2); perror(argv[2]); return 1; + } else if (argc > 2 && !strcmp(argv[1], "-a")) { + SET_FILE_BINARY(stdout); + if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) + return 1; + for (i = 2; i < argc; i++) { + int len = strlen(argv[i]); + if (!len) continue; + fputs(argv[i], stdout); + if (argv[i][len-1] != '\n') + fputc('\n', stdout); + } + fputc('\n', stdout); + copycat(); + return 0; } else if (argc == 2 && !strcmp(argv[1], "-")) { - SET_FILE_BINARY(stdin); SET_FILE_BINARY(stdout); - getheader(stdin, NULL, NULL); + if (getheader(stdin, NULL, NULL) < 0) + return 1; + if (dim) { /* skip resolution string? */ + RESOLU rs; + if (!fgetsresolu(&rs, stdin)) { + fputs("No resolution string\n", stderr); + return 1; + } + } copycat(); return 0; } @@ -98,7 +129,8 @@ main( if (dim) { getdim(stdin); } else { - getheader(stdin, (gethfunc *)fputs, stdout); + if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) + return 1; fputc('\n', stdout); } } @@ -113,9 +145,11 @@ getdim( /* get dimensions from file */ { int j; int c; - - getheader(fp, NULL, NULL); /* skip header */ - + /* skip header */ + if (getheader(fp, NULL, NULL) < 0) { + fputs("bad header\n", stdout); + return; + } switch (c = getc(fp)) { case '+': /* picture */ case '-': @@ -151,6 +185,6 @@ copycat(void) /* copy input to output */ fflush(stdout); while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) - if (write(fileno(stdout), buf, n) != n) + if (writebuf(fileno(stdout), buf, n) != n) break; }