added truncateFile.c

This commit is contained in:
Jeff Hill
1997-06-25 05:42:54 +00:00
parent d7868a20f1
commit 4b66e7e181
5 changed files with 276 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,120 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#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<size) {
c = getc (pFile);
if (c==EOF) {
fprintf (stderr,
"File access problems to `%s' because `%s'\n",
pFileName,
strerror(errno));
fclose (pFile);
fclose (ptmp);
remove (pTmpFN);
return TF_ERROR;
}
status = putc (c, ptmp);
if (status==EOF) {
fprintf(stderr,
"File access problems to `%s' because `%s'\n",
pTmpFN,
strerror(errno));
fclose (pFile);
fclose (ptmp);
remove (pTmpFN);
return TF_ERROR;
}
charNo++;
}
fclose (pFile);
fclose (ptmp);
status = remove (pFileName);
if (status!=TF_OK) {
fprintf (stderr,
"Unable to remove `%s' during truncate because `%s'\n",
pFileName,
strerror(errno));
remove (pTmpFN);
return TF_ERROR;
}
status = rename (pTmpFN, pFileName);
if (status!=TF_OK) {
fprintf (stderr,
"Unable to rename %s to `%s' because `%s'\n",
pTmpFN,
pFileName,
strerror(errno));
remove (pTmpFN);
return TF_ERROR;
}
return TF_OK;
}

View File

@@ -0,0 +1,17 @@
#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.
*/
enum TF_RETURN {TF_OK=0, TF_ERROR=1};
epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName, unsigned size);

120
src/libCom/truncateFile.c Normal file
View File

@@ -0,0 +1,120 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#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<size) {
c = getc (pFile);
if (c==EOF) {
fprintf (stderr,
"File access problems to `%s' because `%s'\n",
pFileName,
strerror(errno));
fclose (pFile);
fclose (ptmp);
remove (pTmpFN);
return TF_ERROR;
}
status = putc (c, ptmp);
if (status==EOF) {
fprintf(stderr,
"File access problems to `%s' because `%s'\n",
pTmpFN,
strerror(errno));
fclose (pFile);
fclose (ptmp);
remove (pTmpFN);
return TF_ERROR;
}
charNo++;
}
fclose (pFile);
fclose (ptmp);
status = remove (pFileName);
if (status!=TF_OK) {
fprintf (stderr,
"Unable to remove `%s' during truncate because `%s'\n",
pFileName,
strerror(errno));
remove (pTmpFN);
return TF_ERROR;
}
status = rename (pTmpFN, pFileName);
if (status!=TF_OK) {
fprintf (stderr,
"Unable to rename %s to `%s' because `%s'\n",
pTmpFN,
pFileName,
strerror(errno));
remove (pTmpFN);
return TF_ERROR;
}
return TF_OK;
}

17
src/libCom/truncateFile.h Normal file
View File

@@ -0,0 +1,17 @@
#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.
*/
enum TF_RETURN {TF_OK=0, TF_ERROR=1};
epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName, unsigned size);