fixed temp file creation issues on win32

This commit is contained in:
Jeff Hill
2004-04-27 17:39:25 +00:00
parent d844c2dd61
commit b76257a0d6
11 changed files with 91 additions and 82 deletions

View File

@@ -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

View File

@@ -15,7 +15,7 @@
#include <limits.h>
#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;

View File

@@ -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

View File

@@ -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
}

View File

@@ -20,7 +20,29 @@
#include <sys\stat.h>
#define epicsExportSharedSymbols
#include <epicsStdio.h>
#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;
}

View File

@@ -8,8 +8,8 @@
* in file LICENSE that is included with this distribution.
\*************************************************************************/
#include <epicsStdio.h>
#include <osiUnistd.h>
#include "epicsStdio.h"
#include "osiUnistd.h"
extern "C" {

View File

@@ -8,7 +8,23 @@
* in file LICENSE that is included with this distribution.
\*************************************************************************/
#include <epicsStdio.h>
#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 )

View File

@@ -45,6 +45,7 @@ SRCS += tblcmp.c
SRCS += parse.c
PROD_HOST = e_flex
PROD_LIBS = Com
include $(TOP)/configure/RULES

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;