ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
extraUtilUtil.c
Go to the documentation of this file.
1
20
21#include <stdio.h>
22#include <string.h>
23#include "extra.h"
24
26
30
31#define EXTRA_RLIMIT_DATA_DEFAULT 67108864 // assume 64MB by default
32
33/* File : getopt.c
34 * Author : Henry Spencer, University of Toronto
35 * Updated: 28 April 1984
36 *
37 * Changes: (R Rudell)
38 * changed index() to strchr();
39 * added getopt_reset() to reset the getopt argument parsing
40 *
41 * Purpose: get option letter from argv.
42 */
43
44const char * globalUtilOptarg; // Global argument pointer (util_optarg)
45int globalUtilOptind = 0; // Global argv index (util_optind)
46
47static const char *pScanStr;
48
52
68
81{
84 pScanStr = 0;
85}
86
98int Extra_UtilGetopt( int argc, char *argv[], const char *optstring )
99{
100 int c;
101 const char *place;
102
103 globalUtilOptarg = NULL;
104
105 if (pScanStr == NULL || *pScanStr == '\0')
106 {
107 if (globalUtilOptind == 0)
109 if (globalUtilOptind >= argc)
110 return EOF;
111 place = argv[globalUtilOptind];
112 if (place[0] != '-' || place[1] == '\0')
113 return EOF;
115 if (place[1] == '-' && place[2] == '\0')
116 return EOF;
117 pScanStr = place+1;
118 }
119
120 c = *pScanStr++;
121 place = strchr(optstring, c);
122 if (place == NULL || c == ':') {
123 (void) fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
124 return '?';
125 }
126 if (*++place == ':')
127 {
128 if (*pScanStr != '\0')
129 {
130 globalUtilOptarg = pScanStr;
131 pScanStr = NULL;
132 }
133 else
134 {
135 if (globalUtilOptind >= argc)
136 {
137 (void) fprintf(stderr, "%s: %c requires an argument\n",
138 argv[0], c);
139 return '?';
140 }
143 }
144 }
145 return c;
146}
147
159char * Extra_UtilPrintTime( long t )
160{
161 static char s[40];
162
163 (void) sprintf(s, "%ld.%02ld sec", t/1000, (t%1000)/10);
164 return s;
165}
166
167
179char * Extra_UtilStrsav( const char *s )
180{
181 if(s == NULL) { /* added 7/95, for robustness */
182 return NULL;
183 }
184 else {
185 return strcpy(ABC_ALLOC(char, strlen(s)+1), s);
186 }
187}
188
200char * Extra_UtilTildeExpand( char *fname )
201{
202 return Extra_UtilStrsav( fname );
203/*
204 int n_tildes = 0;
205 const char* home;
206 char* expanded;
207 int length;
208 int i, j, k;
209
210 for (i = 0; i < (int)strlen(fname); i++)
211 if (fname[i] == '~') n_tildes++;
212
213 home = getenv("HOME");
214 length = n_tildes * strlen(home) + strlen(fname);
215 expanded = ABC_ALLOC(char, length + 1);
216
217 j = 0;
218 for (i = 0; i < (int)strlen(fname); i++){
219 if (fname[i] == '~'){
220 for (k = 0; k < (int)strlen(home); k++)
221 expanded[j++] = home[k];
222 }else
223 expanded[j++] = fname[i];
224 }
225
226 expanded[j] = '\0';
227 return expanded;
228*/
229}
230
242int Extra_UtilCheckFile(char *filename, const char *mode)
243{
244 FILE *fp;
245 int got_file;
246
247 if (strcmp(mode, "x") == 0) {
248 mode = "r";
249 }
250 fp = fopen(filename, mode);
251 got_file = (fp != 0);
252 if (fp != 0) {
253 (void) fclose(fp);
254 }
255 return got_file;
256}
257
269char * Extra_UtilFileSearch(char *file, char *path, char *mode)
270//char *file; // file we're looking for
271//char *path; // search path, colon separated
272//char *mode; // "r", "w", or "x"
273{
274 int quit;
275 char *buffer, *filename, *save_path, *cp;
276
277 if (path == 0 || strcmp(path, "") == 0) {
278 path = "."; /* just look in the current directory */
279 }
280
281 save_path = path = Extra_UtilStrsav(path);
282 quit = 0;
283 for (;;) {
284 cp = strchr(path, ':');
285 if (cp != 0) {
286 *cp = '\0';
287 } else {
288 quit = 1;
289 }
290
291 /* cons up the filename out of the path and file name */
292 if (strcmp(path, ".") == 0) {
293 buffer = Extra_UtilStrsav(file);
294 } else {
295 buffer = ABC_ALLOC(char, strlen(path) + strlen(file) + 4);
296 (void) sprintf(buffer, "%s/%s", path, file);
297 }
299 ABC_FREE(buffer);
300
301 /* see if we can access it */
303 ABC_FREE(save_path);
304 return filename;
305 }
307 if (quit) {
308 break;
309 } else {
310 path = ++cp;
311 }
312 }
313
314 ABC_FREE(save_path);
315 return 0;
316}
317
329/* MMout_of_memory -- out of memory for lazy people, flush and exit */
331{
332 (void) fflush(stdout);
333 (void) fprintf(stderr, "\nout of memory allocating %u bytes\n",
334 (unsigned) size);
335 assert( 0 );
336 exit(1);
337}
338
350void (*Extra_UtilMMoutOfMemory)( long size ) = (void (*)( long size ))Extra_UtilMMout_Of_Memory;
351
352
365{
366 return Abc_Clock();
367}
368
380#if defined(NT) || defined(NT64) || defined(WIN32)
381double Extra_CpuTimeDouble()
382{
383 return 1.0*Abc_Clock()/CLOCKS_PER_SEC;
384}
385#else
386
388
389#include <sys/time.h>
390#include <sys/resource.h>
391#include <unistd.h>
392
394
396{
397/*
398 struct rusage ru;
399 getrusage(RUSAGE_SELF, &ru);
400 return (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1000000;
401*/
402 struct timespec ts;
403 if ( clock_gettime(CLOCK_MONOTONIC, &ts) < 0 )
404 return (double)-1;
405 double res = ((double) ts.tv_sec);
406 res += ((double) ts.tv_nsec) / 1000000000;
407 return res;
408}
409#endif
410
423{
424// ABC_ALLOC( char, 1002 );
425}
426
430
431
433
ABC_INT64_T abctime
Definition abc_global.h:332
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
char * filename
Definition globals.c:40
abctime Extra_CpuTime()
int Extra_GetSoftDataLimit()
FUNCTION DEFINITIONS ///.
ABC_NAMESPACE_IMPL_END ABC_NAMESPACE_IMPL_START double Extra_CpuTimeDouble()
#define EXTRA_RLIMIT_DATA_DEFAULT
DECLARATIONS ///.
char * Extra_UtilStrsav(const char *s)
void Extra_MemTest()
char * Extra_UtilTildeExpand(char *fname)
void Extra_UtilMMout_Of_Memory(long size)
int Extra_UtilGetopt(int argc, char *argv[], const char *optstring)
char * Extra_UtilPrintTime(long t)
int Extra_UtilCheckFile(char *filename, const char *mode)
char * Extra_UtilFileSearch(char *file, char *path, char *mode)
void Extra_UtilGetoptReset()
int globalUtilOptind
const char * globalUtilOptarg
void(* Extra_UtilMMoutOfMemory)(long size)
Definition file.h:23
Definition mode.h:11
#define assert(ex)
Definition util_old.h:213
int strlen()
int strcmp()
char * sprintf()
char * strcpy()
VOID_HACK exit()
char * strchr()