diff --git a/cmFile.c b/cmFile.c index a69154a..01c7276 100644 --- a/cmFile.c +++ b/cmFile.c @@ -60,6 +60,27 @@ cmFileRC_t cmFileOpen( cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlag if( cmIsFlag(flags,kUpdateFileFl) ) mode[1]='+'; + // handle requests to use stdin,stdout,stderr + FILE* sfp = NULL; + if( cmIsFlag(flags,kStdoutFileFl) ) + { + sfp = stdout; + fn = "stdout"; + } + else + if( cmIsFlag(flags,kStderrFileFl) ) + { + sfp = stderr; + fn = "stderr"; + } + else + if( cmIsFlag(flags,kStdinFileFl) ) + { + sfp = stdin; + fn = "stdin"; + } + + if( fn == NULL ) return cmErrMsg(&err,kObjAllocFailFileRC,"File object allocation failed due to empty file name."); @@ -73,13 +94,17 @@ cmFileRC_t cmFileOpen( cmFileH_t* hp, const cmChar_t* fn, enum cmFileOpenFlag p->fnStr = (cmChar_t*)(p+1); strcpy(p->fnStr,fn); - - errno = 0; - if((p->fp = fopen(fn,mode)) == NULL ) + if( sfp != NULL ) + p->fp = sfp; + else { - cmFileRC_t rc = _cmFileError(p,kOpenFailFileRC,errno,"File open failed"); - cmMemFree(p); - return rc; + errno = 0; + if((p->fp = fopen(fn,mode)) == NULL ) + { + cmFileRC_t rc = _cmFileError(p,kOpenFailFileRC,errno,"File open failed"); + cmMemFree(p); + return rc; + } } hp->h = p; diff --git a/cmFile.h b/cmFile.h index 66ea4a3..af2341b 100644 --- a/cmFile.h +++ b/cmFile.h @@ -42,7 +42,10 @@ extern "C" { kWriteFileFl = 0x02, //< Create an empty file for writing kAppendFileFl = 0x04, //< Open a file for writing at the end of the file. kUpdateFileFl = 0x08, //< Open a file for reading and writing. - kBinaryFileFl = 0x10 //< Open a file for binary (not text) input/output. + kBinaryFileFl = 0x10, //< Open a file for binary (not text) input/output. + kStdoutFileFl = 0x20, //< Ignore fn use 'stdout' + kStderrFileFl = 0x40, //< Ignore fn use 'stderr' + kStdinFileFl = 0x80, //< Ignore fn use 'stdin' }; // Open or create a file. @@ -51,6 +54,9 @@ extern "C" { // be set to cmFileNullHandle prior to calling this function. If *hp is a valid handle // then it is automatically finalized by an internal call to cmFileClose() prior to // being re-iniitalized. + // + // If kStdoutFileFl, kStderrFileFl or kStdinFileFl are set then + // file name argument 'fn' is ignored. cmFileRC_t cmFileOpen( cmFileH_t* hp, // Pointer to a client supplied cmFileHandle_t to recieve the handle for the new object. const cmChar_t* fn, // The name of the file to open or create.