abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
extraUtilUtil.c
Go to the documentation of this file.
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 
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 ***********************************************************************/
65 {
67 }
68 
69 /**Function*************************************************************
70 
71  Synopsis [util_getopt_reset()]
72 
73  Description []
74 
75  SideEffects []
76 
77  SeeAlso []
78 
79 ***********************************************************************/
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 ***********************************************************************/
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') {
107  if (globalUtilOptind >= argc) return EOF;
108  place = argv[globalUtilOptind];
109  if (place[0] != '-' || place[1] == '\0') return EOF;
111  if (place[1] == '-' && place[2] == '\0') return EOF;
112  pScanStr = place+1;
113  }
114 
115  c = *pScanStr++;
116  place = strchr(optstring, c);
117  if (place == NULL || c == ':') {
118  (void) fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
119  return '?';
120  }
121  if (*++place == ':') {
122  if (*pScanStr != '\0') {
124  pScanStr = NULL;
125  } else {
126  if (globalUtilOptind >= argc) {
127  (void) fprintf(stderr, "%s: %c requires an argument\n",
128  argv[0], c);
129  return '?';
130  }
133  }
134  }
135  return c;
136 }
137 
138 /**Function*************************************************************
139 
140  Synopsis [util_print_time()]
141 
142  Description []
143 
144  SideEffects []
145 
146  SeeAlso []
147 
148 ***********************************************************************/
149 char * Extra_UtilPrintTime( long t )
150 {
151  static char s[40];
152 
153  (void) sprintf(s, "%ld.%02ld sec", t/1000, (t%1000)/10);
154  return s;
155 }
156 
157 
158 /**Function*************************************************************
159 
160  Synopsis [Extra_UtilStrsav()]
161 
162  Description []
163 
164  SideEffects []
165 
166  SeeAlso []
167 
168 ***********************************************************************/
169 char * Extra_UtilStrsav( const char *s )
170 {
171  if(s == NULL) { /* added 7/95, for robustness */
172  return NULL;
173  }
174  else {
175  return strcpy(ABC_ALLOC(char, strlen(s)+1), s);
176  }
177 }
178 
179 /**Function*************************************************************
180 
181  Synopsis [util_tilde_expand()]
182 
183  Description [The code contributed by Niklas Sorensson.]
184 
185  SideEffects []
186 
187  SeeAlso []
188 
189 ***********************************************************************/
190 char * Extra_UtilTildeExpand( char *fname )
191 {
192  return Extra_UtilStrsav( fname );
193 /*
194  int n_tildes = 0;
195  const char* home;
196  char* expanded;
197  int length;
198  int i, j, k;
199 
200  for (i = 0; i < (int)strlen(fname); i++)
201  if (fname[i] == '~') n_tildes++;
202 
203  home = getenv("HOME");
204  length = n_tildes * strlen(home) + strlen(fname);
205  expanded = ABC_ALLOC(char, length + 1);
206 
207  j = 0;
208  for (i = 0; i < (int)strlen(fname); i++){
209  if (fname[i] == '~'){
210  for (k = 0; k < (int)strlen(home); k++)
211  expanded[j++] = home[k];
212  }else
213  expanded[j++] = fname[i];
214  }
215 
216  expanded[j] = '\0';
217  return expanded;
218 */
219 }
220 
221 /**Function*************************************************************
222 
223  Synopsis [check_file()]
224 
225  Description []
226 
227  SideEffects []
228 
229  SeeAlso []
230 
231 ***********************************************************************/
232 int Extra_UtilCheckFile(char *filename, const char *mode)
233 {
234  FILE *fp;
235  int got_file;
236 
237  if (strcmp(mode, "x") == 0) {
238  mode = "r";
239  }
240  fp = fopen(filename, mode);
241  got_file = (fp != 0);
242  if (fp != 0) {
243  (void) fclose(fp);
244  }
245  return got_file;
246 }
247 
248 /**Function*************************************************************
249 
250  Synopsis [util_file_search()]
251 
252  Description []
253 
254  SideEffects []
255 
256  SeeAlso []
257 
258 ***********************************************************************/
259 char * Extra_UtilFileSearch(char *file, char *path, char *mode)
260 //char *file; // file we're looking for
261 //char *path; // search path, colon separated
262 //char *mode; // "r", "w", or "x"
263 {
264  int quit;
265  char *buffer, *filename, *save_path, *cp;
266 
267  if (path == 0 || strcmp(path, "") == 0) {
268  path = "."; /* just look in the current directory */
269  }
270 
271  save_path = path = Extra_UtilStrsav(path);
272  quit = 0;
273  do {
274  cp = strchr(path, ':');
275  if (cp != 0) {
276  *cp = '\0';
277  } else {
278  quit = 1;
279  }
280 
281  /* cons up the filename out of the path and file name */
282  if (strcmp(path, ".") == 0) {
283  buffer = Extra_UtilStrsav(file);
284  } else {
285  buffer = ABC_ALLOC(char, strlen(path) + strlen(file) + 4);
286  (void) sprintf(buffer, "%s/%s", path, file);
287  }
288  filename = Extra_UtilTildeExpand(buffer);
289  ABC_FREE(buffer);
290 
291  /* see if we can access it */
292  if (Extra_UtilCheckFile(filename, mode)) {
293  ABC_FREE(save_path);
294  return filename;
295  }
296  ABC_FREE(filename);
297  path = ++cp;
298  } while (! quit);
299 
300  ABC_FREE(save_path);
301  return 0;
302 }
303 
304 /**Function*************************************************************
305 
306  Synopsis [MMout_of_memory()]
307 
308  Description []
309 
310  SideEffects []
311 
312  SeeAlso []
313 
314 ***********************************************************************/
315 /* MMout_of_memory -- out of memory for lazy people, flush and exit */
317 {
318  (void) fflush(stdout);
319  (void) fprintf(stderr, "\nout of memory allocating %u bytes\n",
320  (unsigned) size);
321  assert( 0 );
322  exit(1);
323 }
324 
325 /**Function*************************************************************
326 
327  Synopsis [MMoutOfMemory()]
328 
329  Description []
330 
331  SideEffects []
332 
333  SeeAlso []
334 
335 ***********************************************************************/
336 void (*Extra_UtilMMoutOfMemory)( long size ) = (void (*)( long size ))Extra_UtilMMout_Of_Memory;
337 
338 
339 /**Function*************************************************************
340 
341  Synopsis [util_cpu_time()]
342 
343  Description []
344 
345  SideEffects []
346 
347  SeeAlso []
348 
349 ***********************************************************************/
351 {
352  return Abc_Clock();
353 }
354 
355 /**Function*************************************************************
356 
357  Synopsis [util_cpu_time()]
358 
359  Description []
360 
361  SideEffects []
362 
363  SeeAlso []
364 
365 ***********************************************************************/
366 #if defined(NT) || defined(NT64) || defined(WIN32)
367 double Extra_CpuTimeDouble()
368 {
369  return 1.0*Abc_Clock()/CLOCKS_PER_SEC;
370 }
371 #else
372 
374 
375 #include <sys/time.h>
376 #include <sys/resource.h>
377 #include <unistd.h>
378 
380 
382 {
383  struct rusage ru;
384  getrusage(RUSAGE_SELF, &ru);
385  return (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1000000;
386 }
387 #endif
388 
389 /**Function*************************************************************
390 
391  Synopsis [Testing memory leaks.]
392 
393  Description []
394 
395  SideEffects []
396 
397  SeeAlso []
398 
399 ***********************************************************************/
401 {
402 // ABC_ALLOC( char, 1002 );
403 }
404 
405 ////////////////////////////////////////////////////////////////////////
406 /// END OF FILE ///
407 ////////////////////////////////////////////////////////////////////////
408 
409 
411 
char * filename
Definition: globals.c:40
VOID_HACK exit()
char * Extra_UtilTildeExpand(char *fname)
const char * globalUtilOptarg
Definition: extraUtilUtil.c:44
#define EXTRA_RLIMIT_DATA_DEFAULT
DECLARATIONS ///.
Definition: extraUtilUtil.c:31
int globalUtilOptind
Definition: extraUtilUtil.c:45
int Extra_UtilGetopt(int argc, char *argv[], const char *optstring)
Definition: extraUtilUtil.c:98
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
char * Extra_UtilStrsav(const char *s)
static abctime Abc_Clock()
Definition: abc_global.h:279
void Extra_UtilMMout_Of_Memory(long size)
int strcmp()
abctime Extra_CpuTime()
char * strchr()
void Extra_MemTest()
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
void Extra_UtilGetoptReset()
Definition: extraUtilUtil.c:80
static int size
Definition: cuddSign.c:86
int Extra_GetSoftDataLimit()
FUNCTION DEFINITIONS ///.
Definition: extraUtilUtil.c:64
char * sprintf()
char * Extra_UtilPrintTime(long t)
char * strcpy()
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static const char * pScanStr
Definition: extraUtilUtil.c:47
ABC_NAMESPACE_IMPL_END ABC_NAMESPACE_IMPL_START double Extra_CpuTimeDouble()
#define ABC_FREE(obj)
Definition: abc_global.h:232
char * Extra_UtilFileSearch(char *file, char *path, char *mode)
int Extra_UtilCheckFile(char *filename, const char *mode)
void(* Extra_UtilMMoutOfMemory)(long size)
#define assert(ex)
Definition: util_old.h:213
int strlen()
ABC_INT64_T abctime
Definition: abc_global.h:278