ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
utilFile.c
Go to the documentation of this file.
1
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// Handle legacy macros
29#if !defined(S_IREAD)
30#if defined(S_IRUSR)
31#define S_IREAD S_IRUSR
32#else
33#error S_IREAD is undefined
34#endif
35#endif
36
37#if !defined(S_IWRITE)
38#if defined(S_IWUSR)
39#define S_IWRITE S_IWUSR
40#else
41#error S_IWRITE is undefined
42#endif
43#endif
44
45#if defined(_MSC_VER) || defined(__MINGW32__)
46#include <windows.h>
47#include <process.h>
48#include <io.h>
49#else
50#include <unistd.h>
51#endif
52
53#include "abc_global.h"
54
56
60
64
76static ABC_UINT64_T realTimeAbs() // -- absolute time in nano-seconds
77{
78#if defined(_MSC_VER)
79 LARGE_INTEGER f, t;
80 double realTime_freq;
81 int ok;
82
83 ok = QueryPerformanceFrequency(&f); assert(ok);
84 realTime_freq = 1.0 / (__int64)(((ABC_UINT64_T)f.LowPart) | ((ABC_UINT64_T)f.HighPart << 32));
85
86 ok = QueryPerformanceCounter(&t); assert(ok);
87 return (ABC_UINT64_T)(__int64)(((__int64)(((ABC_UINT64_T)t.LowPart | ((ABC_UINT64_T)t.HighPart << 32))) * realTime_freq * 1000000000));
88#else
89 return 0;
90#endif
91}
92
106int tmpFile(const char* prefix, const char* suffix, char** out_name)
107{
108#if defined(_MSC_VER) || defined(__MINGW32__)
109 int i, fd;
110 *out_name = (char*)malloc(strlen(prefix) + strlen(suffix) + 27);
111 for (i = 0; i < 10; i++){
112 sprintf(*out_name, "%s%I64X%d%s", prefix, realTimeAbs(), _getpid(), suffix);
113 fd = _open(*out_name, O_CREAT | O_EXCL | O_BINARY | O_RDWR, _S_IREAD | _S_IWRITE);
114 if (fd == -1){
115 free(*out_name);
116 *out_name = NULL;
117 }
118 return fd;
119 }
120 assert(0); // -- could not open temporary file
121 return 0;
122#elif defined(__wasm)
123 static int seq = 0; // no risk of collision since we're in a sandbox
124 int fd;
125 *out_name = (char*)malloc(strlen(prefix) + strlen(suffix) + 9);
126 sprintf(*out_name, "%s%08d%s", prefix, seq++, suffix);
127 fd = open(*out_name, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE);
128 if (fd == -1){
129 free(*out_name);
130 *out_name = NULL;
131 }
132 return fd;
133#else
134 int fd;
135 *out_name = (char*)malloc(strlen(prefix) + strlen(suffix) + 7);
136 assert(*out_name != NULL);
137 sprintf(*out_name, "%sXXXXXX", prefix);
138 fd = mkstemp(*out_name);
139 if (fd == -1){
140 free(*out_name);
141 *out_name = NULL;
142 }else{
143 // Kludge:
144 close(fd);
145 unlink(*out_name);
146 strcat(*out_name, suffix);
147 fd = open(*out_name, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE);
148 if (fd == -1){
149 free(*out_name);
150 *out_name = NULL;
151 }
152 }
153 return fd;
154#endif
155}
156
168/*
169int main(int argc, char** argv)
170{
171 char* tmp_filename;
172 int fd = tmpFile("__abctmp_", &tmp_filename);
173 FILE* out = fdopen(fd, "wb");
174
175 fprintf(out, "This file contains important information.\n");
176 fclose(out);
177
178 printf("Created: %s\n", tmp_filename);
179 free(tmp_filename);
180
181 return 0;
182}
183*/
184
196char* vnsprintf(const char* format, va_list args)
197{
198 unsigned n;
199 char* ret;
200 va_list args_copy;
201
202 static FILE* dummy_file = NULL;
203 if (!dummy_file)
204 {
205#if !defined(_MSC_VER) && !defined(__MINGW32)
206 dummy_file = fopen("/dev/null", "wb");
207#else
208 dummy_file = fopen("NUL", "wb");
209#endif
210 }
211
212#if defined(__va_copy)
213 __va_copy(args_copy, args);
214#else
215 #if defined(va_copy)
216 va_copy(args_copy, args);
217 #else
218 args_copy = args;
219 #endif
220#endif
221 n = vfprintf(dummy_file, format, args);
222 ret = ABC_ALLOC( char, n + 1 );
223 ret[n] = (char)255;
224 args = args_copy;
225 vsprintf(ret, format, args);
226#if !defined(__va_copy) && defined(va_copy)
227 va_end(args_copy);
228#endif
229 assert(ret[n] == 0);
230 return ret;
231}
232
233char* nsprintf(const char* format, ...)
234{
235 char* ret;
236 va_list args;
237 va_start(args, format);
238 ret = vnsprintf(format, args);
239 va_end(args);
240 return ret;
241}
242
246
247
249
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
char * vnsprintf(const char *format, va_list args)
Definition utilFile.c:196
int tmpFile(const char *prefix, const char *suffix, char **out_name)
Definition utilFile.c:106
char * nsprintf(const char *format,...)
Definition utilFile.c:233
#define assert(ex)
Definition util_old.h:213
int strlen()
char * sprintf()
VOID_HACK free()
char * strcat()
char * malloc()