From 4b66e7e181414f66638e345321c2fbc32c013741 Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Wed, 25 Jun 1997 05:42:54 +0000 Subject: [PATCH] added truncateFile.c --- src/libCom/Makefile.Host | 2 + src/libCom/misc/truncateFile.c | 120 +++++++++++++++++++++++++++++++++ src/libCom/misc/truncateFile.h | 17 +++++ src/libCom/truncateFile.c | 120 +++++++++++++++++++++++++++++++++ src/libCom/truncateFile.h | 17 +++++ 5 files changed, 276 insertions(+) create mode 100644 src/libCom/misc/truncateFile.c create mode 100644 src/libCom/misc/truncateFile.h create mode 100644 src/libCom/truncateFile.c create mode 100644 src/libCom/truncateFile.h diff --git a/src/libCom/Makefile.Host b/src/libCom/Makefile.Host index 369bab916..0533f7e1b 100644 --- a/src/libCom/Makefile.Host +++ b/src/libCom/Makefile.Host @@ -33,6 +33,7 @@ INC += sigPipeIgnore.h INC += dbmf.h INC += ipAddrToA.h INC += epicsString.h +INC += truncateFile.h # For WIN32 we supply getopt as part of libCom: INC_WIN32 := getopt.h @@ -68,6 +69,7 @@ LIBSRCS += sigPipeIgnore.c LIBSRCS += dbmf.c LIBSRCS += ipAddrToA.c LIBSRCS += epicsString.c +LIBSRCS += truncateFile.c # # if CPLUSPLUS isnt empty then include C++ src codes diff --git a/src/libCom/misc/truncateFile.c b/src/libCom/misc/truncateFile.c new file mode 100644 index 000000000..108188bf2 --- /dev/null +++ b/src/libCom/misc/truncateFile.c @@ -0,0 +1,120 @@ + +#include +#include +#include +#include + +#define epicsExportSharedSymbols +#include "truncateFile.h" + +#ifndef SEEK_END +#define SEEK_END 2 +#endif + +/* + * truncate to specified size (we dont use truncate() + * because it is not portable) + */ +epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName, unsigned size) +{ + char tmpName[L_tmpnam]; + long filePos; + char *pTmpFN; + FILE *pFile; + FILE *ptmp; + int status; + int c; + unsigned charNo; + + pFile = fopen(pFileName, "r"); + if (!pFile) { + fprintf (stderr, + "File access problems to `%s' because `%s'\n", + pFileName, + strerror(errno)); + return TF_ERROR; + } + + /* + * This is not required under UNIX but does appear + * to be required under WIN32. + */ + status = fseek (pFile, 0L, SEEK_END); + if (status!=TF_OK) { + fclose (pFile); + return TF_ERROR; + } + + filePos = ftell(pFile); + if (filePos <= size) { + fclose (pFile); + return TF_OK; + } + + pTmpFN = tmpnam (tmpName); + if (!pTmpFN) { + fprintf (stderr,"Unable to create tmp file name?\n"); + fclose (pFile); + return TF_ERROR; + } + + ptmp = fopen (pTmpFN, "w"); + if (!ptmp) { + fprintf (stderr, + "File access problems to `%s' because `%s'\n", + pTmpFN, + strerror(errno)); + fclose (pFile); + return TF_ERROR; + } + rewind (pFile); + charNo = 0u; + while (charNo +#include +#include +#include + +#define epicsExportSharedSymbols +#include "truncateFile.h" + +#ifndef SEEK_END +#define SEEK_END 2 +#endif + +/* + * truncate to specified size (we dont use truncate() + * because it is not portable) + */ +epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName, unsigned size) +{ + char tmpName[L_tmpnam]; + long filePos; + char *pTmpFN; + FILE *pFile; + FILE *ptmp; + int status; + int c; + unsigned charNo; + + pFile = fopen(pFileName, "r"); + if (!pFile) { + fprintf (stderr, + "File access problems to `%s' because `%s'\n", + pFileName, + strerror(errno)); + return TF_ERROR; + } + + /* + * This is not required under UNIX but does appear + * to be required under WIN32. + */ + status = fseek (pFile, 0L, SEEK_END); + if (status!=TF_OK) { + fclose (pFile); + return TF_ERROR; + } + + filePos = ftell(pFile); + if (filePos <= size) { + fclose (pFile); + return TF_OK; + } + + pTmpFN = tmpnam (tmpName); + if (!pTmpFN) { + fprintf (stderr,"Unable to create tmp file name?\n"); + fclose (pFile); + return TF_ERROR; + } + + ptmp = fopen (pTmpFN, "w"); + if (!ptmp) { + fprintf (stderr, + "File access problems to `%s' because `%s'\n", + pTmpFN, + strerror(errno)); + fclose (pFile); + return TF_ERROR; + } + rewind (pFile); + charNo = 0u; + while (charNo