From bd509425fa6d6caa5af70604b752fb46139c1c38 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Tue, 28 Nov 2023 18:07:05 +0000 Subject: Drop infeasible pm_feed_from_filestream, add pm_feed_from_file, pm_accept_to_file git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4791 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- doc/HISTORY | 5 +++-- lib/libsystem.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- lib/pm_system.h | 8 +++++-- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/doc/HISTORY b/doc/HISTORY index 55e290e5..fc97a63c 100644 --- a/doc/HISTORY +++ b/doc/HISTORY @@ -32,8 +32,9 @@ not yet BJH Release 11.05.00 pnmpad: no longer accept old-style options (e.g. -t50). - libnetpbm: Add pm_feed_from_filestream, pm_accept_to_filestream - Standard Input feeder, Output accepter for pm_system. + libnetpbm: Add pm_feed_from_file, pm_accept_to_files, + pm_accept_to_filestream Standard Input feeder, Output accepter + for pm_system. libnetpbm, programs that use color maps: fix buffer overrun with insanely deep images. diff --git a/lib/libsystem.c b/lib/libsystem.c index f5c73f1b..3c23e0c0 100644 --- a/lib/libsystem.c +++ b/lib/libsystem.c @@ -767,18 +767,21 @@ pm_accept_to_memory(int const pipeToSuckFd, void -pm_feed_from_filestream(int const pipeToFeedFd, - void * const feederParm) { +pm_feed_from_file(int const pipeToFeedFd, + void * const feederParm) { - FILE * const inFileP = feederParm; + const char * const inFileNm = feederParm; size_t const bufferSz = 64*1024; FILE * const outFileP = fdopen(pipeToFeedFd, "w"); + FILE * inFileP; unsigned char * buffer; bool eof; + inFileP = pm_openr(inFileNm); + MALLOCARRAY(buffer, bufferSz); if (!buffer) @@ -803,6 +806,7 @@ pm_feed_from_filestream(int const pipeToFeedFd, eof = true; } + pm_close(inFileP); fclose(outFileP); free(buffer); @@ -810,6 +814,63 @@ pm_feed_from_filestream(int const pipeToFeedFd, +void +pm_accept_to_file(int const pipeToSuckFd, + void * const accepterParm ) { + + const char * const outFileNm = accepterParm; + + size_t const bufferSz = 64*1024; + + FILE * const inFileP = fdopen(pipeToSuckFd, "r"); + + FILE * outFileP; + unsigned char * buffer; + bool eof; + + outFileP = pm_openw(outFileNm); + + MALLOCARRAY(buffer, bufferSz); + + if (!buffer) + pm_error("Failed to allocate %u bytes for I/O buffer", + (unsigned) bufferSz); + + for (eof = false; !eof; ) { + size_t byteCtRead; + + byteCtRead = fread(buffer, 1, bufferSz, inFileP); + + if (ferror(inFileP)) + pm_error("Error reading Standard Output accepter pipe. " + "errno=%d (%s)", + errno, strerror(errno)); + + if (byteCtRead > 0) { + fwrite(buffer, 1, byteCtRead, outFileP); + + if (ferror(outFileP)) + pm_error("Error writing to file. errno=%d (%s)", + errno, strerror(errno)); + } else + eof = true; + } + + pm_close(outFileP); + fclose(inFileP); + + free(buffer); +} + + + +/* Note that pm_feed_from_filestream is not possible because Standard Input is + feed by a child process and we can't properly pass a FILE * to a child + process. +*/ + + + void pm_accept_to_filestream(int const pipeToSuckFd, void * const accepterParm ) { diff --git a/lib/pm_system.h b/lib/pm_system.h index f6f87451..5cfc50cf 100644 --- a/lib/pm_system.h +++ b/lib/pm_system.h @@ -102,8 +102,12 @@ pm_accept_to_memory(int const pipetosuckFd, void * const accepterParm); void -pm_feed_from_filestream(int const pipeToFeedFd, - void * const feederParm); +pm_feed_from_file(int const pipeToFeedFd, + void * const feederParm); + +void +pm_accept_to_file(int const pipetosuckFd, + void * const accepterParm); void pm_accept_to_filestream(int const pipetosuckFd, -- cgit 1.4.1