abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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"

Go to the source code of this file.

Functions

static
ABC_NAMESPACE_IMPL_START
ABC_UINT64_T 
realTimeAbs ()
 DECLARATIONS ///. More...
 
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

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

Definition at line 205 of file utilFile.c.

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 }
char * vnsprintf(const char *format, va_list args)
Definition: utilFile.c:168
static ABC_NAMESPACE_IMPL_START ABC_UINT64_T realTimeAbs ( )
static

DECLARATIONS ///.

CFile****************************************************************

FileName [utilFile.c]

SystemName [ABC: Logic synthesis and verification system.]

PackageName [Temporary file utilities.]

Synopsis [Temporary file utilities.]

Author [Niklas Een]

Affiliation [UC Berkeley]

Date [Ver. 1.0. Started - September 29, 2010.]

Revision [

Id:
utilFile.c,v 1.00 2005/06/20 00:00:00 alanmi Exp

]FUNCTION DEFINITIONS /// Function*************************************************************

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 59 of file utilFile.c.

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 }
#define assert(ex)
Definition: util_old.h:213
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 89 of file utilFile.c.

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 }
char * malloc()
VOID_HACK free()
char * sprintf()
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()
char* vnsprintf ( const char *  format,
va_list  args 
)

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

Synopsis []

Description []

SideEffects []

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 168 of file utilFile.c.

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 }
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
#define assert(ex)
Definition: util_old.h:213