abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
utilFile.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [utilFile.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Temporary file utilities.]
8 
9  Synopsis [Temporary file utilities.]
10 
11  Author [Niklas Een]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - September 29, 2010.]
16 
17  Revision [$Id: utilFile.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 
28 #if defined(_MSC_VER) || defined(__MINGW32__)
29 #include <windows.h>
30 #include <process.h>
31 #include <io.h>
32 #else
33 #include <unistd.h>
34 #endif
35 
36 #include "abc_global.h"
37 
39 
40 ////////////////////////////////////////////////////////////////////////
41 /// DECLARATIONS ///
42 ////////////////////////////////////////////////////////////////////////
43 
44 ////////////////////////////////////////////////////////////////////////
45 /// FUNCTION DEFINITIONS ///
46 ////////////////////////////////////////////////////////////////////////
47 
48 /**Function*************************************************************
49 
50  Synopsis []
51 
52  Description []
53 
54  SideEffects []
55 
56  SeeAlso []
57 
58 ***********************************************************************/
59 static ABC_UINT64_T realTimeAbs() // -- absolute time in nano-seconds
60 {
61 #if defined(_MSC_VER)
62  LARGE_INTEGER f, t;
63  double realTime_freq;
64  int ok;
65 
66  ok = QueryPerformanceFrequency(&f); assert(ok);
67  realTime_freq = 1.0 / (__int64)(((ABC_UINT64_T)f.LowPart) | ((ABC_UINT64_T)f.HighPart << 32));
68 
69  ok = QueryPerformanceCounter(&t); assert(ok);
70  return (ABC_UINT64_T)(__int64)(((__int64)(((ABC_UINT64_T)t.LowPart | ((ABC_UINT64_T)t.HighPart << 32))) * realTime_freq * 1000000000));
71 #else
72  return 0;
73 #endif
74 }
75 
76 /**Function*************************************************************
77 
78  Synopsis [Opens a temporary file.]
79 
80  Description [Opens a temporary file with given prefix and returns file
81  descriptor (-1 on failure) and a string containing the name of the file
82  (to be freed by caller).]
83 
84  SideEffects []
85 
86  SeeAlso []
87 
88 ***********************************************************************/
89 int tmpFile(const char* prefix, const char* suffix, char** out_name)
90 {
91 #if defined(_MSC_VER) || defined(__MINGW32__)
92  int i, fd;
93  *out_name = (char*)malloc(strlen(prefix) + strlen(suffix) + 27);
94  for (i = 0; i < 10; i++){
95  sprintf(*out_name, "%s%I64X%d%s", prefix, realTimeAbs(), _getpid(), suffix);
96  fd = _open(*out_name, O_CREAT | O_EXCL | O_BINARY | O_RDWR, _S_IREAD | _S_IWRITE);
97  if (fd == -1){
98  free(*out_name);
99  *out_name = NULL;
100  }
101  return fd;
102  }
103  assert(0); // -- could not open temporary file
104  return 0;
105 #else
106  int fd;
107  *out_name = (char*)malloc(strlen(prefix) + strlen(suffix) + 7);
108  assert(*out_name != NULL);
109  sprintf(*out_name, "%sXXXXXX", prefix);
110  fd = mkstemp(*out_name);
111  if (fd == -1){
112  free(*out_name);
113  *out_name = NULL;
114  }else{
115  // Kludge:
116  close(fd);
117  unlink(*out_name);
118  strcat(*out_name, suffix);
119  fd = open(*out_name, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE);
120  if (fd == -1){
121  free(*out_name);
122  *out_name = NULL;
123  }
124  }
125  return fd;
126 #endif
127 }
128 
129 /**Function*************************************************************
130 
131  Synopsis []
132 
133  Description []
134 
135  SideEffects []
136 
137  SeeAlso []
138 
139 ***********************************************************************/
140 /*
141 int main(int argc, char** argv)
142 {
143  char* tmp_filename;
144  int fd = tmpFile("__abctmp_", &tmp_filename);
145  FILE* out = fdopen(fd, "wb");
146 
147  fprintf(out, "This file contains important information.\n");
148  fclose(out);
149 
150  printf("Created: %s\n", tmp_filename);
151  free(tmp_filename);
152 
153  return 0;
154 }
155 */
156 
157 /**Function*************************************************************
158 
159  Synopsis []
160 
161  Description []
162 
163  SideEffects []
164 
165  SeeAlso []
166 
167 ***********************************************************************/
168 char* vnsprintf(const char* format, va_list args)
169 {
170  unsigned n;
171  char* ret;
172  va_list args_copy;
173 
174  static FILE* dummy_file = NULL;
175  if (!dummy_file)
176  {
177 #if !defined(_MSC_VER) && !defined(__MINGW32)
178  dummy_file = fopen("/dev/null", "wb");
179 #else
180  dummy_file = fopen("NUL", "wb");
181 #endif
182  }
183 
184 #if defined(__va_copy)
185  __va_copy(args_copy, args);
186 #else
187  #if defined(va_copy)
188  va_copy(args_copy, args);
189  #else
190  args_copy = args;
191  #endif
192 #endif
193  n = vfprintf(dummy_file, format, args);
194  ret = ABC_ALLOC( char, n + 1 );
195  ret[n] = (char)255;
196  args = args_copy;
197  vsprintf(ret, format, args);
198 #if !defined(__va_copy) && defined(va_copy)
199  va_end(args_copy);
200 #endif
201  assert(ret[n] == 0);
202  return ret;
203 }
204 
205 char* nsprintf(const char* format, ...)
206 {
207  char* ret;
208  va_list args;
209  va_start(args, format);
210  ret = vnsprintf(format, args);
211  va_end(args);
212  return ret;
213 }
214 
215 ////////////////////////////////////////////////////////////////////////
216 /// END OF FILE ///
217 ////////////////////////////////////////////////////////////////////////
218 
219 
221 
char * malloc()
VOID_HACK free()
int tmpFile(const char *prefix, const char *suffix, char **out_name)
Definition: utilFile.c:89
char * vnsprintf(const char *format, va_list args)
Definition: utilFile.c:168
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
char * nsprintf(const char *format,...)
Definition: utilFile.c:205
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
char * sprintf()
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
char * strcat()
static ABC_NAMESPACE_IMPL_START ABC_UINT64_T realTimeAbs()
DECLARATIONS ///.
Definition: utilFile.c:59
#define assert(ex)
Definition: util_old.h:213
int strlen()