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
25
ABC_NAMESPACE_IMPL_START
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
44
const
char
*
globalUtilOptarg
;
// Global argument pointer (util_optarg)
45
int
globalUtilOptind
= 0;
// Global argv index (util_optind)
46
47
static
const
char
*pScanStr;
48
52
64
int
Extra_GetSoftDataLimit
()
65
{
66
return
EXTRA_RLIMIT_DATA_DEFAULT
;
67
}
68
80
void
Extra_UtilGetoptReset
()
81
{
82
globalUtilOptarg
= 0;
83
globalUtilOptind
= 0;
84
pScanStr = 0;
85
}
86
98
int
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)
108
globalUtilOptind
++;
109
if
(
globalUtilOptind
>= argc)
110
return
EOF;
111
place = argv[
globalUtilOptind
];
112
if
(place[0] !=
'-'
|| place[1] ==
'\0'
)
113
return
EOF;
114
globalUtilOptind
++;
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
}
141
globalUtilOptarg
= argv[
globalUtilOptind
];
142
globalUtilOptind
++;
143
}
144
}
145
return
c;
146
}
147
159
char
*
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
179
char
*
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
200
char
*
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
242
int
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
269
char
*
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
}
298
filename
=
Extra_UtilTildeExpand
(buffer);
299
ABC_FREE
(buffer);
300
301
/* see if we can access it */
302
if
(
Extra_UtilCheckFile
(
filename
,
mode
)) {
303
ABC_FREE
(save_path);
304
return
filename
;
305
}
306
ABC_FREE
(
filename
);
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 */
330
void
Extra_UtilMMout_Of_Memory
(
long
size )
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
350
void (*
Extra_UtilMMoutOfMemory
)(
long
size ) = (
void
(*)(
long
size ))
Extra_UtilMMout_Of_Memory
;
351
352
364
abctime
Extra_CpuTime
()
365
{
366
return
Abc_Clock();
367
}
368
380
#if defined(NT) || defined(NT64) || defined(WIN32)
381
double
Extra_CpuTimeDouble
()
382
{
383
return
1.0*Abc_Clock()/CLOCKS_PER_SEC;
384
}
385
#else
386
387
ABC_NAMESPACE_IMPL_END
388
389
#include <sys/time.h>
390
#include <sys/resource.h>
391
#include <unistd.h>
392
393
ABC_NAMESPACE_IMPL_START
394
395
double
Extra_CpuTimeDouble
()
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
422
void
Extra_MemTest
()
423
{
424
// ABC_ALLOC( char, 1002 );
425
}
426
430
431
432
ABC_NAMESPACE_IMPL_END
433
abctime
ABC_INT64_T abctime
Definition
abc_global.h:332
ABC_ALLOC
#define ABC_ALLOC(type, num)
Definition
abc_global.h:264
ABC_FREE
#define ABC_FREE(obj)
Definition
abc_global.h:267
ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_START
Definition
abc_namespaces.h:54
ABC_NAMESPACE_IMPL_END
#define ABC_NAMESPACE_IMPL_END
Definition
abc_namespaces.h:55
filename
char * filename
Definition
globals.c:40
Extra_CpuTime
abctime Extra_CpuTime()
Definition
extraUtilUtil.c:364
Extra_GetSoftDataLimit
int Extra_GetSoftDataLimit()
FUNCTION DEFINITIONS ///.
Definition
extraUtilUtil.c:64
Extra_CpuTimeDouble
ABC_NAMESPACE_IMPL_END ABC_NAMESPACE_IMPL_START double Extra_CpuTimeDouble()
Definition
extraUtilUtil.c:395
EXTRA_RLIMIT_DATA_DEFAULT
#define EXTRA_RLIMIT_DATA_DEFAULT
DECLARATIONS ///.
Definition
extraUtilUtil.c:31
Extra_UtilStrsav
char * Extra_UtilStrsav(const char *s)
Definition
extraUtilUtil.c:179
Extra_MemTest
void Extra_MemTest()
Definition
extraUtilUtil.c:422
Extra_UtilTildeExpand
char * Extra_UtilTildeExpand(char *fname)
Definition
extraUtilUtil.c:200
Extra_UtilMMout_Of_Memory
void Extra_UtilMMout_Of_Memory(long size)
Definition
extraUtilUtil.c:330
Extra_UtilGetopt
int Extra_UtilGetopt(int argc, char *argv[], const char *optstring)
Definition
extraUtilUtil.c:98
Extra_UtilPrintTime
char * Extra_UtilPrintTime(long t)
Definition
extraUtilUtil.c:159
Extra_UtilCheckFile
int Extra_UtilCheckFile(char *filename, const char *mode)
Definition
extraUtilUtil.c:242
Extra_UtilFileSearch
char * Extra_UtilFileSearch(char *file, char *path, char *mode)
Definition
extraUtilUtil.c:269
Extra_UtilGetoptReset
void Extra_UtilGetoptReset()
Definition
extraUtilUtil.c:80
extra.h
globalUtilOptind
int globalUtilOptind
Definition
extraUtilUtil.c:45
globalUtilOptarg
const char * globalUtilOptarg
Definition
extraUtilUtil.c:44
Extra_UtilMMoutOfMemory
void(* Extra_UtilMMoutOfMemory)(long size)
Definition
extraUtilUtil.c:350
file
Definition
file.h:23
mode
Definition
mode.h:11
assert
#define assert(ex)
Definition
util_old.h:213
strlen
int strlen()
strcmp
int strcmp()
sprintf
char * sprintf()
strcpy
char * strcpy()
exit
VOID_HACK exit()
strchr
char * strchr()
src
misc
extra
extraUtilUtil.c
Generated by Doxygen 1.13.2 © 2025 EPTansuo. All rights reserved.
鲁ICP备2021046540号