Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – /branches/tendra4/src/installers/680x0/common/utility.c – Rev 2

Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1996
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
/*
30
			    VERSION INFORMATION
31
			    ===================
32
 
33
--------------------------------------------------------------------------
34
$Header: /u/g/release/CVSROOT/Source/src/installers/680x0/common/utility.c,v 1.1.1.1 1998/01/17 15:55:49 release Exp $
35
--------------------------------------------------------------------------
36
$Log: utility.c,v $
37
 * Revision 1.1.1.1  1998/01/17  15:55:49  release
38
 * First version to be checked into rolling release.
39
 *
40
Revision 1.1.1.1  1997/10/13 12:43:00  ma
41
First version.
42
 
43
Revision 1.3  1997/10/13 08:50:16  ma
44
Made all pl_tests for general proc & exception handling pass.
45
 
46
Revision 1.2  1997/03/20 12:52:20  ma
47
Removed #if 0 and #endif around used definitions.
48
 
49
Revision 1.1.1.1  1997/03/14 07:50:19  ma
50
Imported from DRA
51
 
52
 * Revision 1.1.1.1  1996/09/20  10:56:59  john
53
 *
54
 * Revision 1.1.1.1  1996/03/26  15:45:18  john
55
 *
56
 * Revision 1.4  94/11/08  11:24:26  11:24:26  ra (Robert Andrews)
57
 * Changed error reporting format slightly.
58
 *
59
 * Revision 1.3  94/02/21  16:05:37  16:05:37  ra (Robert Andrews)
60
 * Clear up a long-int confusion.
61
 *
62
 * Revision 1.2  93/04/19  13:37:53  13:37:53  ra (Robert Andrews)
63
 * Line numbers in error reports were wrong.
64
 *
65
 * Revision 1.1  93/02/22  17:16:55  17:16:55  ra (Robert Andrews)
66
 * Initial revision
67
 *
68
--------------------------------------------------------------------------
69
*/
70
 
71
 
72
#include "config.h"
73
#if FS_STDARG
74
#include <stdarg.h>
75
#else
76
#include <varargs.h>
77
#endif
78
#include "common_types.h"
79
#include "basicread.h"
80
#include "instrs.h"
81
#include "utility.h"
82
extern long crt_line_num ;
83
extern char *crt_fname ;
84
extern char *progname ;
85
long total_calloced ;
86
#if 0
87
/* Makes automatically generated makefile work */
88
#include "xalloc.c"
89
#endif
90
 
91
 
92
/*
93
    FIND THE BASENAME OF A FILE
94
 
95
    The string nm is analysed and a pointer to the character after the
96
    last '/' is returned.
97
*/
98
 
99
char *basename
100
    PROTO_N ( ( nm ) )
101
    PROTO_T ( char *nm )
102
{
103
    char *bn = nm ;
104
    for ( ; *nm ; nm++ ) if ( *nm == '/' ) bn = nm + 1 ;
105
    return ( bn ) ;
106
}
107
 
108
 
109
/*
110
    FIND THE FIRST NONZERO BIT
111
 
112
    This routine returns the bit number of the least significant set
113
    bit in n.  For 0 it returns -1.
114
*/
115
 
116
int bit_one
117
    PROTO_N ( ( n ) )
118
    PROTO_T ( bitpattern n )
119
{
120
    int c = 0 ;
121
    bitpattern m ;
122
    for ( m = n ; m ; m >>= 1, c++ ) if ( m & 1 ) return ( c ) ;
123
    return ( -1 ) ;
124
}
125
 
126
 
127
/*
128
    FIND THE NUMBER OF SET BITS
129
 
130
    The number of set bits in n is returned.
131
*/
132
 
133
int bits_in
134
    PROTO_N ( ( n ) )
135
    PROTO_T ( bitpattern n )
136
{
137
    /* Table of bits in : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F */
138
    static int b [16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 } ;
139
    int c = 0 ;
140
    while ( n ) {
141
	c += b [ n & 0xf ] ;
142
	n >>= 4 ;
143
    }
144
    return ( c ) ;
145
}
146
 
147
 
148
/*
149
    BIT PATTERNS
150
 
151
    lo_bits [n] is the number with its bottom n bits set and the rest
152
    zero.  hi_bits [n] is the number with its top n bits set and the rest
153
    zero.
154
*/
155
 
156
bitpattern lo_bits [] = { 0,
157
    0x00000001, 0x00000003, 0x00000007, 0x0000000f,
158
    0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
159
    0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
160
    0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
161
    0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff,
162
    0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff,
163
    0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff,
164
    0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff } ;
165
 
166
bitpattern hi_bits [] = { 0,
167
    0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
168
    0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
169
    0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
170
    0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
171
    0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
172
    0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
173
    0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
174
    0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff } ;
175
 
176
 
177
/*
178
    NUMBER OF ERRORS
179
 
180
    errors is the number of errors which have occurred.  max_errors is
181
    the maximum number of errors that will be tolerated before exiting.
182
*/
183
 
184
int errors = 0 ;
185
#ifdef EBUG
186
int max_errors = 10 ;
187
#else
188
int max_errors = 1 ;
189
#endif
190
 
191
 
192
/*
193
    PRINT AN ERROR REPORT
194
 
195
    This routine prints the error report s together with any additional
196
    arguments.
197
*/
198
 
199
void error
200
    PROTO_V ( ( char *s, ... ) )
201
{
202
    char c ;
203
    char *p ;
204
    va_list args ;
205
#if FS_STDARG
206
    va_start ( args, s ) ;
207
    p = s ;
208
#else
209
    va_start ( args ) ;
210
    p = va_arg ( args, char * ) ;
211
#endif
212
    if ( progname ) fprintf ( stderr, "%s : ", progname ) ;
213
    fprintf ( stderr, "Error : " ) ;
214
    c = *p ;
215
    if ( c >= 'a' && c <= 'z' ) {
216
	c += ( 'A' - 'a' ) ;
217
	fputc ( c, stderr ) ;
218
	p++ ;
219
    }
220
    vfprintf ( stderr, p, args ) ;
221
    if ( crt_line_num != -1 && crt_fname ) {
222
	fprintf ( stderr, ", %s, line %ld", crt_fname, crt_line_num ) ;
223
    }
224
    fprintf ( stderr, ".\n" ) ;
225
    va_end ( args ) ;
226
#ifdef IGNORE_ERRORS
227
    return ;
228
#endif
229
    if ( max_errors == 0 ) exit ( EXIT_FAILURE ) ;
230
    if ( ++errors > max_errors ) {
231
	fprintf ( stderr, "%s : Too many errors.\n", progname ) ;
232
	exit ( EXIT_FAILURE ) ;
233
    }
234
    return ;
235
}
236
 
237
/*
238
    PRINT A SIMPLE ERROR
239
 
240
    This routine prints the simple error report s.
241
*/
242
 
243
void failer
244
    PROTO_N ( ( s ) )
245
    PROTO_T ( char *s )
246
{
247
    error ( s ) ;
248
    return ;
249
}
250
 
251
 
252
/*
253
    PRINT A WARNING
254
 
255
    This routine prints the warning message s together with any additional
256
    arguments.
257
*/
258
 
259
void warning
260
    PROTO_V ( ( char *s, ... ) )
261
{
262
    char c ;
263
    char *p ;
264
    va_list args ;
265
#if FS_STDARG
266
    va_start ( args, s ) ;
267
    p = s ;
268
#else
269
    va_start ( args ) ;
270
    p = va_arg ( args, char * ) ;
271
#endif
272
    if ( progname ) ( void ) fprintf ( stderr, "%s : ", progname ) ;
273
    fprintf ( stderr, "Warning : " ) ;
274
    c = *p ;
275
    if ( c >= 'a' && c <= 'z' ) {
276
	c += ( 'A' - 'a' ) ;
277
	fputc ( c, stderr ) ;
278
	p++ ;
279
    }
280
    ( void ) vfprintf ( stderr, p, args ) ;
281
    ( void ) fprintf ( stderr, ".\n" ) ;
282
    va_end ( args ) ;
283
    return ;
284
}
285
 
286
 
287
/*
288
    MEMORY HACK VALUE
289
*/
290
 
291
#define memhack 0
292
 
293
/*
294
    ALLOCATE A BLOCK OF MEMORY
295
 
296
    A pointer to a block of memory of size sz is returned.  Failure to
297
    allocate this memory gives an immediate fatal error.
298
*/
299
 
300
voidstar xmalloc
301
    PROTO_N ( ( sz ) )
302
    PROTO_T ( size_t sz )
303
{
304
    voidstar res ;
305
    if ( sz == 0 ) return ( null ) ;
306
    res = ( voidstar ) malloc ( sz + memhack ) ;
307
    if ( res == null ) {
308
	error ( "Can't allocate memory" ) ;
309
	exit ( EXIT_FAILURE ) ;
310
    }
311
#ifdef MEM_DEBUG
312
    printf ( "%d (malloc, %d bytes)\n", res, sz ) ;
313
    fflush ( stdout ) ;
314
#endif
315
    return ( res ) ;
316
}
317
 
318
 
319
/*
320
    ALLOCATE ROOM IN MEMORY
321
 
322
    A pointer to a block of memory of size n * sz is returned.  This
323
    memory is initialized to 0.  Failure to allocate memory gives an
324
    immediate fatal error.
325
*/
326
 
327
voidstar xcalloc
328
    PROTO_N ( ( n, sz ) )
329
    PROTO_T ( int n X size_t sz )
330
{
331
    voidstar res ;
332
    if ( n == 0 || sz == 0 ) return ( null ) ;
333
    if ( sz == sizeof ( char ) && n < 100 ) {
334
	/* Be careful not to free character arrays */
335
	static char *cbuffer = null ;
336
	static size_t cbuffsz = 0 ;
337
	if ( n + memhack >= cbuffsz ) {
338
	    cbuffsz = 2000 ;
339
	    cbuffer = ( char * ) calloc ( cbuffsz, sizeof ( char ) ) ;
340
	    if ( cbuffer == null ) {
341
		error ( "Can't allocate memory" ) ;
342
		exit ( EXIT_FAILURE ) ;
343
	    }
344
	}
345
	res = ( voidstar ) cbuffer ;
346
	cbuffer += ( n + memhack ) ;
347
	cbuffsz -= ( n + memhack ) ;
348
    } else {
349
	res = ( voidstar ) calloc ( n + memhack, sz ) ;
350
	if ( res == null ) {
351
	    error ( "Can't allocate memory" ) ;
352
	    exit ( EXIT_FAILURE ) ;
353
	}
354
    }
355
#ifdef MEM_DEBUG
356
    printf ( "%d (calloc, %d bytes)\n", res, n * sz ) ;
357
    fflush ( stdout ) ;
358
#endif
359
    return ( res ) ;
360
}
361
 
362
 
363
/*
364
    REALLOCATE A BLOCK OF MEMORY
365
 
366
    The previously allocated memory pointed to by p is reallocated
367
    to size n.  A pointer to the new block of memory is returned.
368
    Failure to allocate memory gives an immediate fatal error.
369
*/
370
 
371
voidstar xrealloc
372
    PROTO_N ( ( p, sz ) )
373
    PROTO_T ( voidstar p X size_t sz )
374
{
375
    voidstar res ;
376
    if ( p == null ) return ( xmalloc ( sz ) ) ;
377
    if ( sz == 0 ) return ( null ) ;
378
    res = ( voidstar ) realloc ( p, sz + memhack ) ;
379
    if ( res == null ) {
380
	error ( "Can't reallocate memory" ) ;
381
	exit ( EXIT_FAILURE ) ;
382
    }
383
#ifdef MEM_DEBUG
384
    printf ( "%d (realloc, %d bytes)\n", res, sz ) ;
385
    fflush ( stdout ) ;
386
#endif
387
    return ( res ) ;
388
}
389
 
390
 
391
/*
392
    FREE A BLOCK OF MEMORY
393
 
394
    The block of memory pointed to by p is returned to free.  p must
395
    previously have been allocated using one of the routines above.
396
*/
397
 
398
void xfree
399
    PROTO_N ( ( p ) )
400
    PROTO_T ( voidstar p )
401
{
402
    if ( p == null ) return ;
403
    free ( p ) ;
404
    return ;
405
}
406