ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
utilFile.c File Reference
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "abc_global.h"
Include dependency graph for utilFile.c:

Go to the source code of this file.

Functions

int tmpFile (const char *prefix, const char *suffix, char **out_name)
 
char * vnsprintf (const char *format, va_list args)
 
char * nsprintf (const char *format,...)
 

Function Documentation

◆ nsprintf()

char * nsprintf ( const char * format,
... )

Definition at line 233 of file utilFile.c.

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}
char * vnsprintf(const char *format, va_list args)
Definition utilFile.c:196
Here is the call graph for this function:

◆ tmpFile()

int tmpFile ( const char * prefix,
const char * suffix,
char ** out_name )

Function*************************************************************

Synopsis [Opens a temporary file.]

Description [Opens a temporary file with given prefix and returns file descriptor (-1 on failure) and a string containing the name of the file (to be freed by caller).]

SideEffects []

SeeAlso []

Definition at line 106 of file utilFile.c.

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}
#define assert(ex)
Definition util_old.h:213
int strlen()
char * sprintf()
VOID_HACK free()
char * strcat()
char * malloc()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vnsprintf()

char * vnsprintf ( const char * format,
va_list args )

Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso [] Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 196 of file utilFile.c.

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}
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
Here is the caller graph for this function: