1 /**CFile****************************************************************
2
3 FileName [extraUtilUtil.c]
4
5 SystemName [ABC: Logic synthesis and verification system.]
6
7 PackageName [extra]
8
9 Synopsis [Old SIS utilities.]
10
11 Author [Alan Mishchenko]
12
13 Affiliation [UC Berkeley]
14
15 Date [Ver. 1.0. Started - June 20, 2005.]
16
17 Revision [$Id: extraUtilUtil.c,v 1.0 2003/02/01 00:00:00 alanmi Exp $]
18
19 ***********************************************************************/
20
21 #include <stdio.h>
22 #include <string.h>
23 #include "extra.h"
24
25 ABC_NAMESPACE_IMPL_START
26
27 ////////////////////////////////////////////////////////////////////////
28 /// DECLARATIONS ///
29 ////////////////////////////////////////////////////////////////////////
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
49 ////////////////////////////////////////////////////////////////////////
50 /// FUNCTION DEFINITIONS ///
51 ////////////////////////////////////////////////////////////////////////
52
53 /**Function*************************************************************
54
55 Synopsis [getSoftDataLimit()]
56
57 Description []
58
59 SideEffects []
60
61 SeeAlso []
62
63 ***********************************************************************/
Extra_GetSoftDataLimit()64 int Extra_GetSoftDataLimit()
65 {
66 return EXTRA_RLIMIT_DATA_DEFAULT;
67 }
68
69 /**Function*************************************************************
70
71 Synopsis [util_getopt_reset()]
72
73 Description []
74
75 SideEffects []
76
77 SeeAlso []
78
79 ***********************************************************************/
Extra_UtilGetoptReset()80 void Extra_UtilGetoptReset()
81 {
82 globalUtilOptarg = 0;
83 globalUtilOptind = 0;
84 pScanStr = 0;
85 }
86
87 /**Function*************************************************************
88
89 Synopsis [util_getopt()]
90
91 Description []
92
93 SideEffects []
94
95 SeeAlso []
96
97 ***********************************************************************/
Extra_UtilGetopt(int argc,char * argv[],const char * optstring)98 int Extra_UtilGetopt( int argc, char *argv[], const char *optstring )
99 {
100 register int c;
101 register 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
148 /**Function*************************************************************
149
150 Synopsis [util_print_time()]
151
152 Description []
153
154 SideEffects []
155
156 SeeAlso []
157
158 ***********************************************************************/
Extra_UtilPrintTime(long t)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
168 /**Function*************************************************************
169
170 Synopsis [Extra_UtilStrsav()]
171
172 Description []
173
174 SideEffects []
175
176 SeeAlso []
177
178 ***********************************************************************/
Extra_UtilStrsav(const char * s)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
189 /**Function*************************************************************
190
191 Synopsis [util_tilde_expand()]
192
193 Description [The code contributed by Niklas Sorensson.]
194
195 SideEffects []
196
197 SeeAlso []
198
199 ***********************************************************************/
Extra_UtilTildeExpand(char * fname)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
231 /**Function*************************************************************
232
233 Synopsis [check_file()]
234
235 Description []
236
237 SideEffects []
238
239 SeeAlso []
240
241 ***********************************************************************/
Extra_UtilCheckFile(char * filename,const char * mode)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
258 /**Function*************************************************************
259
260 Synopsis [util_file_search()]
261
262 Description []
263
264 SideEffects []
265
266 SeeAlso []
267
268 ***********************************************************************/
Extra_UtilFileSearch(char * file,char * path,char * mode)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 do {
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 path = ++cp;
308 } while (! quit);
309
310 ABC_FREE(save_path);
311 return 0;
312 }
313
314 /**Function*************************************************************
315
316 Synopsis [MMout_of_memory()]
317
318 Description []
319
320 SideEffects []
321
322 SeeAlso []
323
324 ***********************************************************************/
325 /* MMout_of_memory -- out of memory for lazy people, flush and exit */
Extra_UtilMMout_Of_Memory(long size)326 void Extra_UtilMMout_Of_Memory( long size )
327 {
328 (void) fflush(stdout);
329 (void) fprintf(stderr, "\nout of memory allocating %u bytes\n",
330 (unsigned) size);
331 assert( 0 );
332 exit(1);
333 }
334
335 /**Function*************************************************************
336
337 Synopsis [MMoutOfMemory()]
338
339 Description []
340
341 SideEffects []
342
343 SeeAlso []
344
345 ***********************************************************************/
346 void (*Extra_UtilMMoutOfMemory)( long size ) = (void (*)( long size ))Extra_UtilMMout_Of_Memory;
347
348
349 /**Function*************************************************************
350
351 Synopsis [util_cpu_time()]
352
353 Description []
354
355 SideEffects []
356
357 SeeAlso []
358
359 ***********************************************************************/
Extra_CpuTime()360 abctime Extra_CpuTime()
361 {
362 return Abc_Clock();
363 }
364
365 /**Function*************************************************************
366
367 Synopsis [util_cpu_time()]
368
369 Description []
370
371 SideEffects []
372
373 SeeAlso []
374
375 ***********************************************************************/
376 #if defined(NT) || defined(NT64) || defined(WIN32)
Extra_CpuTimeDouble()377 double Extra_CpuTimeDouble()
378 {
379 return 1.0*Abc_Clock()/CLOCKS_PER_SEC;
380 }
381 #else
382
383 ABC_NAMESPACE_IMPL_END
384
385 #include <sys/time.h>
386 #include <sys/resource.h>
387 #include <unistd.h>
388
389 ABC_NAMESPACE_IMPL_START
390
Extra_CpuTimeDouble()391 double Extra_CpuTimeDouble()
392 {
393 struct rusage ru;
394 getrusage(RUSAGE_SELF, &ru);
395 return (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1000000;
396 }
397 #endif
398
399 /**Function*************************************************************
400
401 Synopsis [Testing memory leaks.]
402
403 Description []
404
405 SideEffects []
406
407 SeeAlso []
408
409 ***********************************************************************/
Extra_MemTest()410 void Extra_MemTest()
411 {
412 // ABC_ALLOC( char, 1002 );
413 }
414
415 ////////////////////////////////////////////////////////////////////////
416 /// END OF FILE ///
417 ////////////////////////////////////////////////////////////////////////
418
419
420 ABC_NAMESPACE_IMPL_END
421
422