This is Unofficial EPICS BASE Doxygen Site
truncateFile.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include "epicsStdio.h"
+ Include dependency graph for truncateFile.c:

Go to the source code of this file.

Macros

#define SEEK_END   2
 

Functions

LIBCOM_API enum TF_RETURN truncateFile (const char *pFileName, unsigned long size)
 

Macro Definition Documentation

#define SEEK_END   2

Definition at line 20 of file truncateFile.c.

Function Documentation

LIBCOM_API enum TF_RETURN truncateFile ( const char *  pFileName,
unsigned long  size 
)

Definition at line 27 of file truncateFile.c.

28 {
29  long filePos;
30  FILE *pFile;
31  FILE *ptmp;
32  int status;
33  int c;
34  unsigned charNo;
35 
36  /*
37  * see cast of size to long below
38  */
39  if (size>LONG_MAX) {
40  return TF_ERROR;
41  }
42 
43  pFile = fopen(pFileName, "r");
44  if (!pFile) {
45  fprintf (stderr,
46  "File access problems to `%s' because `%s'\n",
47  pFileName,
48  strerror(errno));
49  return TF_ERROR;
50  }
51 
52  /*
53  * This is not required under UNIX but does appear
54  * to be required under WIN32.
55  */
56  status = fseek (pFile, 0L, SEEK_END);
57  if (status!=TF_OK) {
58  fclose (pFile);
59  return TF_ERROR;
60  }
61 
62  filePos = ftell(pFile);
63  if (filePos <= (long) size) {
64  fclose (pFile);
65  return TF_OK;
66  }
67 
68  ptmp = epicsTempFile();
69  if (!ptmp) {
70  fprintf (stderr,
71  "File access problems to temp file because `%s'\n",
72  strerror(errno));
73  fclose (pFile);
74  return TF_ERROR;
75  }
76  rewind (pFile);
77  charNo = 0u;
78  while (charNo<size) {
79  c = getc (pFile);
80  if (c==EOF) {
81  fprintf (stderr,
82  "File access problems to temp file because `%s'\n",
83  strerror(errno));
84  fclose (pFile);
85  fclose (ptmp);
86  return TF_ERROR;
87  }
88  status = putc (c, ptmp);
89  if (status==EOF) {
90  fprintf(stderr,
91  "File access problems to temp file because `%s'\n",
92  strerror(errno));
93  fclose (pFile);
94  fclose (ptmp);
95  return TF_ERROR;
96  }
97  charNo++;
98  }
99  fclose (pFile);
100  pFile = fopen(pFileName, "w");
101  if (!pFile) {
102  fprintf (stderr,
103  "File access problems to `%s' because `%s'\n",
104  pFileName,
105  strerror(errno));
106  fclose (ptmp);
107  return TF_ERROR;
108  }
109  rewind (ptmp);
110  charNo = 0u;
111  while (charNo<size) {
112  c = getc (ptmp);
113  if (c==EOF) {
114  fprintf (stderr,
115  "File access problems to temp file because `%s'\n",
116  strerror(errno));
117  fclose (pFile);
118  fclose (ptmp);
119  return TF_ERROR;
120  }
121  status = putc (c, pFile);
122  if (status==EOF) {
123  fprintf(stderr,
124  "File access problems to `%s' because `%s'\n",
125  pFileName,
126  strerror(errno));
127  fclose (pFile);
128  fclose (ptmp);
129  return TF_ERROR;
130  }
131  charNo++;
132  }
133  fclose(ptmp);
134  fclose(pFile);
135  return TF_OK;
136 }
#define SEEK_END
Definition: truncateFile.c:20
pvd::Status status
LIBCOM_API FILE *epicsStdCall epicsTempFile(void)
Create and open a temporary file.
Definition: epicsTempFile.c:15
#define stderr
Definition: epicsStdio.h:32