This is Unofficial EPICS BASE Doxygen Site
truncateFile.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE Versions 3.13.7
7 * and higher are distributed subject to a Software License Agreement found
8 * in file LICENSE that is included with this distribution.
9 \*************************************************************************/
10 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <limits.h>
16 
17 #include "epicsStdio.h"
18 
19 #ifndef SEEK_END
20 #define SEEK_END 2
21 #endif
22 
23 /*
24  * truncate to specified size (we dont use truncate()
25  * because it is not portable)
26  */
27 LIBCOM_API enum TF_RETURN truncateFile (const char *pFileName, unsigned long size)
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 }
137 
#define SEEK_END
Definition: truncateFile.c:20
pvd::Status status
TF_RETURN
Definition: epicsStdio.h:70
LIBCOM_API FILE *epicsStdCall epicsTempFile(void)
Create and open a temporary file.
Definition: epicsTempFile.c:15
LIBCOM_API enum TF_RETURN truncateFile(const char *pFileName, unsigned long size)
Definition: truncateFile.c:27
#define stderr
Definition: epicsStdio.h:32