This is Unofficial EPICS BASE Doxygen Site
epicsTempFile.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 is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 
10 /*
11  * Author: Jeff Hill
12  */
13 
14 #include <stdlib.h>
15 #include <string.h>
16 #include <io.h>
17 #include <fcntl.h>
18 #include <sys/stat.h>
19 
20 #include "epicsTempFile.h"
21 
22 /*
23  * epicsTmpFile
24  *
25  * allow the teporary file directory to be set with the
26  * TMP environment varianble
27  */
28 LIBCOM_API FILE * epicsStdCall epicsTempFile ()
29 {
30  char * pName = _tempnam("c:\\tmp", "epics");
31  if (pName) {
32  /* We use open followed by fdopen so that the _O_EXCL
33  * flag can be used. This causes detection of a race
34  * condition where two programs end up receiving the
35  * same temporary file name.
36  *
37  * _O_CREAT create if non-existant
38  * _O_EXCL file must not exist
39  * _O_RDWR read and write the file
40  * _O_TEMPORARY delete file on close
41  * _O_BINARY no translation
42  * _O_SHORT_LIVED avoid flush to disk
43  */
44  int openFlag = _O_CREAT | _O_EXCL | _O_RDWR |
45  _O_SHORT_LIVED | _O_BINARY | _O_TEMPORARY;
46  int fd = open(pName, openFlag, _S_IWRITE);
47  FILE * pNewFile = 0;
48  if (fd >=0) {
49  pNewFile = _fdopen(fd, "w+b");
50  }
51  else {
52  printf("Temporary file \"%s\" open failed because "
53  "\"%s\"\n", pName, strerror(errno));
54  }
55  free(pName);
56  return pNewFile;
57  }
58  return 0;
59 }
#define printf
Definition: epicsStdio.h:41
LIBCOM_API FILE *epicsStdCall epicsTempFile(void)
Create and open a temporary file.
Definition: epicsTempFile.c:15
OS-independent way to create temporary files.