diff --git a/src/libCom/Makefile b/src/libCom/Makefile index 18b13de4a..c00eb0ef6 100644 --- a/src/libCom/Makefile +++ b/src/libCom/Makefile @@ -123,7 +123,6 @@ INC += epicsTypes.h INC += gsd_sync_defs.h INC += shareLib.h INC += epicsExport.h -INC += truncateFile.h INC += unixFileName.h INC += locationException.h INC += ipAddrToAsciiAsynchronous.h diff --git a/src/libCom/misc/truncateFile.c b/src/libCom/misc/truncateFile.c index f3ee98d20..2e65e80e1 100644 --- a/src/libCom/misc/truncateFile.c +++ b/src/libCom/misc/truncateFile.c @@ -15,7 +15,7 @@ #include #define epicsExportSharedSymbols -#include "truncateFile.h" +#include "epicsStdio.h" #ifndef SEEK_END #define SEEK_END 2 @@ -27,9 +27,8 @@ */ epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName, unsigned size) { - char tmpName[L_tmpnam]; + char tmpName[256>L_tmpnam?256:L_tmpnam]; long filePos; - char *pTmpFN; FILE *pFile; FILE *ptmp; int status; @@ -68,18 +67,18 @@ epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName return TF_OK; } - pTmpFN = tmpnam (tmpName); - if (!pTmpFN) { + epicsTempName ( tmpName, sizeof (tmpName) ); + if ( tmpName[0] == '\0' ) { fprintf (stderr,"Unable to create tmp file name?\n"); fclose (pFile); return TF_ERROR; } - ptmp = fopen (pTmpFN, "w"); + ptmp = fopen (tmpName, "w"); if (!ptmp) { fprintf (stderr, "File access problems to `%s' because `%s'\n", - pTmpFN, + tmpName, strerror(errno)); fclose (pFile); return TF_ERROR; @@ -95,18 +94,18 @@ epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName strerror(errno)); fclose (pFile); fclose (ptmp); - remove (pTmpFN); + remove (tmpName); return TF_ERROR; } status = putc (c, ptmp); if (status==EOF) { fprintf(stderr, "File access problems to `%s' because `%s'\n", - pTmpFN, + tmpName, strerror(errno)); fclose (pFile); fclose (ptmp); - remove (pTmpFN); + remove (tmpName); return TF_ERROR; } charNo++; @@ -119,17 +118,17 @@ epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName "Unable to remove `%s' during truncate because `%s'\n", pFileName, strerror(errno)); - remove (pTmpFN); + remove (tmpName); return TF_ERROR; } - status = rename (pTmpFN, pFileName); + status = rename (tmpName, pFileName); if (status!=TF_OK) { fprintf (stderr, "Unable to rename %s to `%s' because `%s'\n", - pTmpFN, + tmpName, pFileName, strerror(errno)); - remove (pTmpFN); + remove (tmpName); return TF_ERROR; } return TF_OK; diff --git a/src/libCom/misc/truncateFile.h b/src/libCom/misc/truncateFile.h deleted file mode 100644 index d58bcb8b1..000000000 --- a/src/libCom/misc/truncateFile.h +++ /dev/null @@ -1,35 +0,0 @@ -/*************************************************************************\ -* Copyright (c) 2002 The University of Chicago, as Operator of Argonne -* National Laboratory. -* Copyright (c) 2002 The Regents of the University of California, as -* Operator of Los Alamos National Laboratory. -* EPICS BASE Versions 3.13.7 -* and higher are distributed subject to a Software License Agreement found -* in file LICENSE that is included with this distribution. -\*************************************************************************/ - -#include "shareLib.h" - -/* - * truncate to specified size (we dont use truncate() - * because it is not portable) - * - * pFileName - name (and optionally path) of file - * size - the new file size (if file is curretly larger) - * - * returns TF_OK if the file is less than size bytes - * or if it was successfully truncated. Returns - * TF_ERROR if the file could not be truncated. - */ - -#ifdef __cplusplus -extern "C" { -#endif - -enum TF_RETURN {TF_OK=0, TF_ERROR=1}; -epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName, unsigned size); - -#ifdef __cplusplus -} -#endif - diff --git a/src/libCom/osi/epicsStdio.h b/src/libCom/osi/epicsStdio.h index b5c4e3fb1..a9c0eb2ce 100644 --- a/src/libCom/osi/epicsStdio.h +++ b/src/libCom/osi/epicsStdio.h @@ -25,7 +25,23 @@ epicsShareFunc int epicsShareAPI epicsSnprintf( char *str, size_t size, const char *format, ...) EPICS_PRINTF_STYLE(3,4); epicsShareFunc int epicsShareAPI epicsVsnprintf( char *str, size_t size, const char *format, va_list ap); -epicsShareFunc FILE * epicsShareAPI epicsTempFile ( void ); +epicsShareFunc void epicsShareAPI epicsTempName ( + char * pNameBuf, size_t nameBufLength ); +epicsShareFunc FILE * epicsShareAPI epicsTempFile (); + +/* + * truncate to specified size (we dont use truncate() + * because it is not portable) + * + * pFileName - name (and optionally path) of file + * size - the new file size (if file is curretly larger) + * + * returns TF_OK if the file is less than size bytes + * or if it was successfully truncated. Returns + * TF_ERROR if the file could not be truncated. + */ +enum TF_RETURN {TF_OK=0, TF_ERROR=1}; +epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile ( const char *pFileName, unsigned size ); #ifdef __cplusplus } diff --git a/src/libCom/osi/os/WIN32/epicsTempFile.cpp b/src/libCom/osi/os/WIN32/epicsTempFile.cpp index 1fdb8cd5f..f12d06b27 100644 --- a/src/libCom/osi/os/WIN32/epicsTempFile.cpp +++ b/src/libCom/osi/os/WIN32/epicsTempFile.cpp @@ -20,7 +20,29 @@ #include #define epicsExportSharedSymbols -#include +#include "epicsStdio.h" + +// +// epicsTempName +// +// allow the teporary file directory to be set with the +// TMP environment varianble +// +extern "C" +epicsShareFunc void epicsShareAPI epicsTempName ( + char * pNameBuf, size_t nameBufLength ) +{ + if ( nameBufLength ) { + pNameBuf[0] = '\0'; + char * pName = _tempnam ( "c:\\tmp", "epics" ); + if ( pName ) { + if ( nameBufLength > strlen ( pName ) ) { + strncpy ( pNameBuf, pName, nameBufLength ); + } + free ( pName ); + } + } +} // // epicsTmpFile @@ -29,10 +51,8 @@ // TMP environment varianble // extern "C" -epicsShareFunc FILE * epicsShareAPI epicsTempFile ( void ) +epicsShareFunc FILE * epicsShareAPI epicsTempFile () { - // Allow the TMP env variable to set the location - // where temporary files live. char * pName = _tempnam ( "c:\\tmp", "epics" ); if( ! pName ) { return 0; @@ -59,13 +79,16 @@ epicsShareFunc FILE * epicsShareAPI epicsTempFile ( void ) const int openFlag = _O_CREAT | _O_EXCL | _O_RDWR | _O_SHORT_LIVED | _O_BINARY | _O_TEMPORARY; int fd = open ( pName, openFlag, _S_IWRITE ); - if ( fd < 0 ) { + FILE * pNewFile = 0; + if ( fd >=0 ) { + pNewFile = _fdopen ( fd, "w+bTD" ); + } + else { printf ( "Temporary file \"%s\" open failed because " "\"%s\"\n", pName, strerror ( errno ) ); - return 0; } free ( pName ); - return _fdopen ( fd, "w+bTD" ); + return pNewFile; } diff --git a/src/libCom/osi/os/posix/epicsStdio.cpp b/src/libCom/osi/os/posix/epicsStdio.cpp index 47c210e89..41a0ea8e9 100644 --- a/src/libCom/osi/os/posix/epicsStdio.cpp +++ b/src/libCom/osi/os/posix/epicsStdio.cpp @@ -8,8 +8,8 @@ * in file LICENSE that is included with this distribution. \*************************************************************************/ -#include -#include +#include "epicsStdio.h" +#include "osiUnistd.h" extern "C" { diff --git a/src/libCom/osi/os/posix/epicsTempFile.cpp b/src/libCom/osi/os/posix/epicsTempFile.cpp index 76b512c17..baa80fdd4 100644 --- a/src/libCom/osi/os/posix/epicsTempFile.cpp +++ b/src/libCom/osi/os/posix/epicsTempFile.cpp @@ -8,7 +8,23 @@ * in file LICENSE that is included with this distribution. \*************************************************************************/ -#include +#include "epicsStdio.h" + +extern "C" +epicsShareFunc void epicsShareAPI epicsTempName ( + char * pNameBuf, size_t nameBufLength ) +{ + if ( nameBufLength ) { + pNameBuf[0] = '\0'; + char nameBuf[L_tmpnam]; + if ( tmpnam ( nameBuf ) ) { + if ( nameBufLength > strlen ( nameBuf ) ) { + strncpy ( pNameBuf, nameBuf, nameBufLength ); + } + } + } +} + extern "C" epicsShareFunc FILE * epicsShareAPI epicsTempFile ( void ) diff --git a/src/toolsComm/flex/Makefile b/src/toolsComm/flex/Makefile index 9bb460b76..2a2517f75 100644 --- a/src/toolsComm/flex/Makefile +++ b/src/toolsComm/flex/Makefile @@ -45,6 +45,7 @@ SRCS += tblcmp.c SRCS += parse.c PROD_HOST = e_flex +PROD_LIBS = Com include $(TOP)/configure/RULES diff --git a/src/toolsComm/flex/flexdef.h b/src/toolsComm/flex/flexdef.h index 4aeb60bc9..3b68ebd4e 100644 --- a/src/toolsComm/flex/flexdef.h +++ b/src/toolsComm/flex/flexdef.h @@ -418,7 +418,7 @@ extern int yymore_really_used, reject_really_used; extern int datapos, dataline, linenum; extern FILE *skelfile, *yyin, *temp_action_file, *backtrack_file; extern char *infilename; -extern char *action_file_name; +extern char action_file_name[]; extern char **input_files; extern int num_input_files; extern char *program_name; diff --git a/src/toolsComm/flex/main.c b/src/toolsComm/flex/main.c index 920dd9bcd..2e188bb93 100644 --- a/src/toolsComm/flex/main.c +++ b/src/toolsComm/flex/main.c @@ -50,6 +50,8 @@ static char rcsid[] = #include "flexdef.h" */ +#include "epicsStdio.h" + static char flex_version[] = "2.3"; @@ -101,7 +103,7 @@ int num_backtracking, bol_needed; FILE *temp_action_file; FILE *backtrack_file; int end_of_buffer_state; -char *action_file_name = NULL; +char action_file_name[256>L_tmpnam?256:L_tmpnam]; char **input_files; int num_input_files; char *program_name; @@ -606,25 +608,13 @@ get_next_arg: /* used by -C and -S flags in lieu of a "continue 2" control */ if ( (skelfile = fopen( skelname, "r" )) == NULL ) lerrsf( "can't open skeleton file %s", skelname ); -#ifdef SYS_V - action_file_name = tmpnam( NULL ); -#endif + epicsTempName ( action_file_name, sizeof ( action_file_name ) ); + if ( action_file_name[0] == '\0' ) + { + lerrsf( "can't create temporary file name", "" ); + } - if ( action_file_name == NULL ) - { - static char temp_action_file_name[32]; - -#ifndef SHORT_FILE_NAMES - (void) strcpy( temp_action_file_name, "/tmp/flexXXXXXX" ); -#else - (void) strcpy( temp_action_file_name, "flexXXXXXX.tmp" ); -#endif - (void) mktemp( temp_action_file_name ); - - action_file_name = temp_action_file_name; - } - - if ( (temp_action_file = fopen( action_file_name, "w" )) == NULL ) + if ( ( temp_action_file = fopen ( action_file_name, "w" ) ) == NULL ) lerrsf( "can't open temporary action file %s", action_file_name ); lastdfa = lastnfa = num_rules = numas = numsnpairs = tmpuses = 0; diff --git a/src/util/iocLogServer.c b/src/util/iocLogServer.c index 94b835317..39bb61856 100644 --- a/src/util/iocLogServer.c +++ b/src/util/iocLogServer.c @@ -34,7 +34,7 @@ #include "fdmgr.h" #include "envDefs.h" #include "osiSock.h" -#include "truncateFile.h" +#include "epicsStdio.h" static unsigned short ioc_log_port; static long ioc_log_file_limit;