summaryrefslogtreecommitdiffstats
path: root/src/misc/util/util.h
blob: 0b1473472cfde6c302dbc75b61d5dac0f5b149ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**CHeaderFile*****************************************************************

  FileName    [ util.h ]

  PackageName [ util ]

  Synopsis    [ Very low-level utilities ]

  Description [ Includes file access, pipes, forks, time, and temporary file
          access. ]

  Author      [ Stephen Edwards <sedwards@eecs.berkeley.edu> and many others]

  Copyright   [Copyright (c) 1994-1996 The Regents of the Univ. of California.
  All rights reserved.

  Permission is hereby granted, without written agreement and without license
  or royalty fees, to use, copy, modify, and distribute this software and its
  documentation for any purpose, provided that the above copyright notice and
  the following two paragraphs appear in all copies of this software.

  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN
  "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
  MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.]

  Revision    [$Id: util.h,v 1.11 1998/05/04 02:05:08 hsv Exp $]

******************************************************************************/

#ifndef _UTIL
#define _UTIL

////////////////////////////////////////////
#include "leaks.h"
////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//////////////// added by alanmi, November 22, 2001 ////////////////
//#include <ctype.h>
#include <time.h>
#include <signal.h>

#ifndef SIGALRM
#define SIGALRM SIGINT
#endif

#ifndef SIGSTOP
#define SIGSTOP SIGINT
#endif

#ifndef SIGXCPU
#define SIGXCPU SIGINT
#endif

#ifndef SIGUSR1
#define SIGUSR1 SIGINT
#endif

#ifndef SIGKILL
#define SIGKILL SIGINT
#endif

#ifndef HUGE
#define HUGE HUGE_VAL
#endif

#if defined(__STDC__)
#define SIGNAL_FN       void
#endif

#define alarm(n)    ((void)0)
#define kill(a,b)   ((void)0)

#define  random  rand
#define srandom srand
////////////////////////////////////////////////////////////////////


#if HAVE_UNISTD_H
#  include <unistd.h>
#endif

#if HAVE_SYS_TYPES_H
#  include <sys/types.h>
#endif

#if HAVE_VARARGS_H
///////////////////////////////////////
#undef __STDC__
#  include <varargs.h>
#define __STDC__ 1
////////////////// alanmi Feb 03, 2001
#endif

#ifndef STDC_HEADERS
#define STDC_HEADERS 1
#endif

#ifndef HAVE_STRCHR
#define HAVE_STRCHR 1
#endif

#ifndef HAVE_GETENV
#define HAVE_GETENV 1
#endif

#if STDC_HEADERS
//#  include <stdlib.h>
#  include <string.h>
#else
#  ifdef HAVE_STRCHR
char * strchr();
int strcmp();
#  else
#    define strchr index
#  endif
#  ifdef HAVE_GETENV
char * getenv();
#  endif
#endif /* STDC_HEADERS */

#if HAVE_ERRNO_H
#  include <errno.h>
#endif

/*
 * Ensure we have reasonable assert() and fail() functions
 */

#ifndef HAVE_ASSERT_H
#define HAVE_ASSERT_H 1
#endif

#if HAVE_ASSERT_H
#  include <assert.h>
#else
#  ifdef NDEBUG
#    define assert(ex) ;
#  else
#    define assert(ex) {\
    if (! (ex)) {\
    (void) fprintf(stderr,\
        "Assertion failed: file %s, line %d\n\"%s\"\n",\
        __FILE__, __LINE__, "ex");\
    (void) fflush(stdout);\
    abort();\
    }\
}
#  endif
#endif

#define fail(why) {\
    (void) fprintf(stderr, "Fatal error: file %s, line %d\n%s\n",\
    __FILE__, __LINE__, why);\
    (void) fflush(stdout);\
    abort();\
}

/*
 * Support for ANSI function prototypes in non-ANSI compilers
 *
 * Usage:
 *   extern int foo ARGS((char *, double))
 */

#ifndef ARGS
#  ifdef __STDC__
#     define ARGS(args)    args
#  else
#     define ARGS(args) ()
# endif
#endif

#ifndef NULLARGS
#  ifdef __STDC__
#    define NULLARGS    (void)
#  else
#    define NULLARGS    ()
#  endif
#endif

/*
 * A little support for C++ compilers
 */

#ifdef __cplusplus
#  define EXTERN    extern "C"
#else
#  define EXTERN    extern
#endif

/* 
 * Support to define unused varibles
 */
#if (__GNUC__ >2 || __GNUC_MINOR__ >=7) && !defined(UNUSED)
#define UNUSED __attribute__ ((unused))
#else
#define UNUSED
#endif

/*
 * A neater way to define zero pointers
 *
 * Usage:
 *  int * fred;
 *  fred = NIL(int);
 */

#define NIL(type)        ((type *) 0)

/*
 * Left over from SIS and Octtools:
 */

//#define USE_MM
// uncommented this line,
// because to detect memory leaks, we need to work with malloc(), etc directly
#define USE_MM



#ifdef USE_MM
/*
 *  assumes the memory manager is libmm.a (a deprecated (?) Octtools library)
 *    - allows malloc(0) or realloc(obj, 0)
 *    - catches out of memory (and calls MMout_of_memory())
 *    - catch free(0) and realloc(0, size) in the macros
 */
#  define ALLOC(type, num)    \
    ((type *) malloc(sizeof(type) * (num)))
#  define REALLOC(type, obj, num)    \
    (obj) ? ((type *) realloc((char *) obj, sizeof(type) * (num))) : \
        ((type *) malloc(sizeof(type) * (num)))
#  define FREE(obj)        \
    ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
#else
/*
 *  enforce strict semantics on the memory allocator
 */
#  define ALLOC(type, num)    \
    ((type *) MMalloc((long) sizeof(type) * (long) (num)))
#  define REALLOC(type, obj, num)    \
    ((type *) MMrealloc((char *) (obj), (long) sizeof(type) * (long) (num)))
#  define FREE(obj)        \
    ((obj) ? (free((void *) (obj)), (obj) = 0) : 0)
EXTERN void MMout_of_memory ARGS((long));
EXTERN char *MMalloc ARGS((long));
EXTERN char *MMrealloc ARGS((char *, long));
EXTERN void MMfree ARGS((char *));
#endif

#ifndef TRUE
#  define TRUE 1
#endif

#ifndef FALSE
#  define FALSE 0
#endif

#ifndef ABS
#  define ABS(a)            ((a) < 0 ? -(a) : (a))
#endif

#ifndef MAX
#  define MAX(a,b)        ((a) > (b) ? (a) : (b))
#endif

#ifndef MIN
#  define MIN(a,b)        ((a) < (b) ? (a) : (b))
#endif

#define ptime()         util_cpu_time()
#define print_time(t)   util_print_time(t)

#ifndef HUGE_VAL
#  ifndef HUGE
#    define HUGE  8.9884656743115790e+307
#  endif
#  define HUGE_VAL HUGE
#endif

#ifndef MAXINT
#  define MAXINT (1 << 30)
#endif

EXTERN void util_print_cpu_stats ARGS((FILE *));
EXTERN long util_cpu_time ARGS((void));
EXTERN void util_getopt_reset ARGS((void));
EXTERN int util_getopt ARGS((int, char **, char *));
EXTERN char *util_path_search ARGS((char *));
EXTERN char *util_file_search ARGS((char *, char *, char *));
EXTERN char *util_print_time ARGS((long));
EXTERN int util_save_image ARGS((char *, char *));
EXTERN char *util_strsav ARGS((char *));
EXTERN char *util_inttostr ARGS((int));
EXTERN char *util_strcat3 ARGS((char *, char *, char *));
EXTERN char *util_strcat4 ARGS((char *, char *, char *, char *));
EXTERN int util_do_nothing ARGS((void));
EXTERN char *util_tilde_expand ARGS((char *));
EXTERN char *util_tempnam ARGS((char *, char *));
EXTERN FILE *util_tmpfile ARGS((void));
EXTERN void util_srandom ARGS((long));
EXTERN long util_random ARGS(());
EXTERN int getSoftDataLimit ARGS(());

/*
 * Global variables for util_getopt()
 */

extern int util_optind;
extern char *util_optarg;

/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Function prototypes                                                       */
/*---------------------------------------------------------------------------*/

/**AutomaticEnd***************************************************************/

#endif /* _UTIL */