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/utilities/tuname/tuname.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) 1997
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
 
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <string.h>
34
#include <ctype.h>
35
 
36
#ifndef FS_UTSNAME
37
#define FS_UTSNAME	1
38
#endif
39
 
40
#if FS_UTSNAME
41
#include <sys/types.h>
42
#include <sys/utsname.h>
43
#endif
44
 
45
#include "ossg.h"
46
 
47
 
48
/*
49
    CONVERT A STRING TO LOWER CASE
50
 
51
    This routine converts the string s to lower case, returning the result.
52
*/
53
 
54
static char *to_lower_case
55
    PROTO_N ( ( s ) )
56
    PROTO_T ( char *s )
57
{
58
    int c ;
59
    char *t = s ;
60
    while ( c = ( int ) *t, c != 0 ) {
61
	if ( isascii ( c ) && isupper ( c ) ) {
62
	    *t = ( char ) tolower ( c ) ;
63
	}
64
	t++ ;
65
    }
66
    return ( s ) ;
67
}
68
 
69
 
70
/*
71
    PRINT A MACRO DEFINITION
72
 
73
    This routine prints a macro definition consisting of the prefix p
74
    followed by s, suitably transformed by replacing each non-alphanumeric
75
    character by an underscore.
76
*/
77
 
78
static void define_macro
79
    PROTO_N ( ( p, s ) )
80
    PROTO_T ( CONST char *p X CONST char *s )
81
{
82
    int c ;
83
    IGNORE printf ( "#define %s", p ) ;
84
    while ( c = ( int ) *( s++ ), c != 0 ) {
85
	if ( !isascii ( c ) || !isalnum ( c ) ) c = '_' ;
86
	IGNORE fputc ( c, stdout ) ;
87
    }
88
    IGNORE printf ( " 1\n" ) ;
89
    return ;
90
}
91
 
92
 
93
/*
94
    FIND A MACHINE TYPE
95
 
96
    These routines map the machine type as returned by uname (mapped to
97
    lower case), m, to the standard machine type used in the build.
98
*/
99
 
100
static CONST char *find_aix_machine
101
    PROTO_N ( ( m ) )
102
    PROTO_T ( CONST char *m )
103
{
104
    if ( m [8] == '7' && m [9] == '6' ) return ( "power" ) ;
105
    if ( m [8] == '7' && m [9] == '7' ) return ( "power" ) ;
106
    if ( m [8] == '4' && m [9] == '6' ) return ( "ppc601" ) ;
107
    return ( "unknown" ) ;
108
}
109
 
110
static CONST char *find_hpux_machine
111
    PROTO_N ( ( m ) )
112
    PROTO_T ( CONST char *m )
113
{
114
    if ( strncmp ( m, "9000/", 5 ) == 0 ) {
115
	if ( m [5] == '7' || m [5] == '8' ) return ( "hppa" ) ;
116
	if ( m [5] == '3' || m [5] == '4' ) return ( "680x0" ) ;
117
    }
118
    return ( "unknown" ) ;
119
}
120
 
121
static CONST char *find_irix_machine
122
    PROTO_N ( ( m ) )
123
    PROTO_T ( CONST char *m )
124
{
125
    if ( strcmp ( m, "ip22" ) == 0 ) return ( "mips" ) ;
126
    return ( "unknown" ) ;
127
}
128
 
129
static CONST char *find_linux_machine
130
    PROTO_N ( ( m ) )
131
    PROTO_T ( CONST char *m )
132
{
133
    if ( m [0] == 'i' && strstr ( m, "86" ) ) return ( "80x86" ) ;
134
    return ( "unknown" ) ;
135
}
136
 
137
static CONST char *find_osf1_machine
138
    PROTO_N ( ( m ) )
139
    PROTO_T ( CONST char *m )
140
{
141
    if ( strcmp ( m, "alpha" ) == 0 ) return ( "alpha" ) ;
142
    return ( "unknown" ) ;
143
}
144
 
145
static CONST char *find_sco_machine
146
    PROTO_N ( ( m ) )
147
    PROTO_T ( CONST char *m )
148
{
149
    if ( m [0] == 'i' && strstr ( m, "86" ) ) return ( "80x86" ) ;
150
    return ( "unknown" ) ;
151
}
152
 
153
static CONST char *find_sunos_machine
154
    PROTO_N ( ( m ) )
155
    PROTO_T ( CONST char *m )
156
{
157
    if ( strcmp ( m, "i86pc" ) == 0 ) return ( "80x86" ) ;
158
    if ( strncmp ( m, "sun3", 4 ) == 0 ) return ( "680x0" ) ;
159
    if ( strncmp ( m, "sun4", 4 ) == 0 ) return ( "sparc" ) ;
160
    return ( "unknown" ) ;
161
}
162
 
163
static CONST char *find_ultrix_machine
164
    PROTO_N ( ( m ) )
165
    PROTO_T ( CONST char *m )
166
{
167
    if ( strcmp ( m, "risc" ) == 0 ) return ( "mips" ) ;
168
    if ( strcmp ( m, "vax" ) == 0 ) return ( "vax" ) ;
169
    return ( "unknown" ) ;
170
}
171
 
172
static CONST char *find_unix_sv_machine
173
    PROTO_N ( ( m ) )
174
    PROTO_T ( CONST char *m )
175
{
176
    if ( m [0] == 'i' && m [2] == '8' && m [3] == '6' ) return ( "80x86" ) ;
177
    return ( "unknown" ) ;
178
}
179
 
180
 
181
/*
182
    FIND SCO RELEASE NUMBER
183
 
184
    On SCO the release number is not returned by uname.  Instead there is
185
    a function __scoinfo which returns the necessary information.  This
186
    routine finds the release number.
187
*/
188
 
189
static CONST char *find_sco_release
190
    PROTO_N ( ( buff ) )
191
    PROTO_T ( char *buff )
192
{
193
#if defined ( _M_XENIX ) && defined ( _TWO_USER )
194
    struct scoutsname us ;
195
    extern int __scoinfo PROTO_S ( ( struct scoutsname *, int ) ) ;
196
    if ( __scoinfo ( &us, ( int ) sizeof ( us ) ) != -1 ) {
197
	char *v = strchr ( us.release, 'v' ) ;
198
	if ( v ) {
199
	    /* The release is the part following the 'v' */
200
	    IGNORE strcpy ( buff, v + 1 ) ;
201
	    return ( buff ) ;
202
	}
203
    }
204
#endif
205
    UNUSED ( buff ) ;
206
    return ( "unknown" ) ;
207
}
208
 
209
 
210
/*
211
    FIND LINUX EXECUTABLE FORMAT
212
 
213
    This routine finds whether the given version of linux supports the ELF
214
    or the a.out executable format.  It does this by examining the magic
215
    number at the start of an executable file, nm.
216
*/
217
 
218
static CONST char *find_linux_format
219
    PROTO_N ( ( nm ) )
220
    PROTO_T ( CONST char *nm )
221
{
222
    FILE *f = fopen ( nm, "rb" ) ;
223
    if ( f != NULL ) {
224
	char m [4] ;
225
	if ( fread ( m, sizeof ( char ), sizeof ( m ), f ) == 4 ) {
226
	    if ( m [0] == 0x7f ) {
227
		if ( m [1] == 'E' && m [2] == 'L' && m [3] == 'F' ) {
228
		    IGNORE fclose ( f ) ;
229
		    return ( "elf" ) ;
230
		}
231
	    }
232
	}
233
	IGNORE fclose ( f ) ;
234
    }
235
    return ( "aout" ) ;
236
}
237
 
238
 
239
/*
240
    MAIN ROUTINE
241
 
242
    This program is a rationalised form of uname which tries to give the
243
    operating system type, the operating system version and the processor
244
    type in a consistent form.  It may need extending to cover new
245
    operating systems.
246
*/
247
 
248
int main
249
    PROTO_N ( ( argc, argv ) )
250
    PROTO_T ( int argc X char **argv )
251
{
252
    char buff [50] ;
253
    CONST char *sysname = "unknown" ;
254
    CONST char *nodename = "anonymous" ;
255
    CONST char *release = "unknown" ;
256
    CONST char *version = "unknown" ;
257
    CONST char *machine = "unknown" ;
258
    CONST char *execform = "default" ;
259
 
260
    /* Call uname to find system information */
261
#if FS_UTSNAME
262
    struct utsname un ;
263
    if ( uname ( &un ) != -1 ) {
264
	sysname = to_lower_case ( un.sysname ) ;
265
	nodename = to_lower_case ( un.nodename ) ;
266
	release = un.release ;
267
	version = un.version ;
268
	machine = to_lower_case ( un.machine ) ;
269
    }
270
#endif
271
 
272
#ifdef DEBUG
273
    /* Print debugging information */
274
    IGNORE fprintf ( stderr, "sysname = \"%s\"\n", sysname ) ;
275
    IGNORE fprintf ( stderr, "nodename = \"%s\"\n", nodename ) ;
276
    IGNORE fprintf ( stderr, "release = \"%s\"\n", release ) ;
277
    IGNORE fprintf ( stderr, "version = \"%s\"\n", version ) ;
278
    IGNORE fprintf ( stderr, "machine = \"%s\"\n", machine ) ;
279
#endif
280
 
281
    /* Examine system information */
282
    if ( strcmp ( sysname, "aix" ) == 0 ) {
283
	machine = find_aix_machine ( machine ) ;
284
	IGNORE sprintf ( buff, "%s.%s", version, release ) ;
285
	release = buff ;
286
 
287
    } else if ( strcmp ( sysname, "freebsd" ) == 0 ) {
288
	machine = find_linux_machine ( machine ) ;
289
 
290
    } else if ( strcmp ( sysname, "hp-ux" ) == 0 ) {
291
	sysname = "hpux" ;
292
	machine = find_hpux_machine ( machine ) ;
293
 
294
    } else if ( strcmp ( sysname, "irix" ) == 0 ) {
295
	machine = find_irix_machine ( machine ) ;
296
 
297
    } else if ( strcmp ( sysname, "linux" ) == 0 ) {
298
	machine = find_linux_machine ( machine ) ;
299
	execform = find_linux_format ( argv [0] ) ;
300
	if ( strcmp ( execform, "aout" ) == 0 ) {
301
	    /* Mark a.out versions */
302
	    IGNORE sprintf ( buff, "%sA", release ) ;
303
	    release = buff ;
304
	}
305
 
306
    } else if ( strcmp ( sysname, "msdos" ) == 0 ) {
307
	machine = find_sco_machine ( machine ) ;
308
 
309
    } else if ( strcmp ( sysname, "osf1" ) == 0 ) {
310
	machine = find_osf1_machine ( machine ) ;
311
 
312
    } else if ( strcmp ( sysname, "sco" ) == 0 ) {
313
	/* This doesn't actually happen - see below */
314
	release = find_sco_release ( buff ) ;
315
	machine = find_sco_machine ( machine ) ;
316
 
317
    } else if ( strcmp ( sysname, "sunos" ) == 0 ) {
318
	if ( release [0] == '5' ) {
319
	    /* SunOS 5.x is really Solaris 2.x */
320
	    sysname = "solaris" ;
321
	    IGNORE sprintf ( buff, "2%s", release + 1 ) ;
322
	    release = buff ;
323
	}
324
	machine = find_sunos_machine ( machine ) ;
325
 
326
    } else if ( strcmp ( sysname, "ultrix" ) == 0 ) {
327
	machine = find_ultrix_machine ( machine ) ;
328
 
329
    } else if ( strcmp ( sysname, "unix_sv" ) == 0 ) {
330
	IGNORE sprintf ( buff, "svr%s", release ) ;
331
	sysname = buff ;
332
	release = version ;
333
	machine = find_unix_sv_machine ( machine ) ;
334
 
335
    } else if ( strcmp ( sysname, nodename ) == 0 ) {
336
	/* This is a bug on SCO */
337
	sysname = "sco" ;
338
	release = find_sco_release ( buff ) ;
339
	machine = find_sco_machine ( machine ) ;
340
 
341
    } else if ( strncmp ( sysname, "cygwin32", 8 ) == 0 ) {
342
	sysname = "cygwin32" ;
343
	machine = find_linux_machine ( machine ) ;
344
 
345
    } else {
346
	/* Unknown operating system */
347
	sysname = "unknown" ;
348
	machine = "unknown" ;
349
	release = "unknown" ;
350
    }
351
 
352
    /* Print host type */
353
    if ( argc > 2 ) {
354
	IGNORE fprintf ( stderr, "tcc_host: Too many arguments.\n" ) ;
355
	argc = 2 ;
356
    }
357
    if ( argc == 2 ) {
358
	char *arg = argv [1] ;
359
	if ( strcmp ( arg, "-a" ) == 0 ) {
360
	    IGNORE printf ( "%s %s %s\n", sysname, release, machine ) ;
361
	    return ( 0 ) ;
362
	}
363
	if ( strcmp ( arg, "-d" ) == 0 ) {
364
	    IGNORE printf ( "%s/%s/%s\n", sysname, release, machine ) ;
365
	    return ( 0 ) ;
366
	}
367
	if ( strcmp ( arg, "-e" ) == 0 ) {
368
	    IGNORE printf ( "%s\n", execform ) ;
369
	    return ( 0 ) ;
370
	}
371
	if ( strcmp ( arg, "-h" ) == 0 ) {
372
	    IGNORE printf ( "%s\n", nodename ) ;
373
	    return ( 0 ) ;
374
	}
375
	if ( strcmp ( arg, "-m" ) == 0 ) {
376
	    IGNORE printf ( "%s\n", machine ) ;
377
	    return ( 0 ) ;
378
	}
379
	if ( strcmp ( arg, "-r" ) == 0 ) {
380
	    IGNORE printf ( "%s\n", release ) ;
381
	    return ( 0 ) ;
382
	}
383
	if ( strcmp ( arg, "-s" ) == 0 ) {
384
	    IGNORE printf ( "%s\n", sysname ) ;
385
	    return ( 0 ) ;
386
	}
387
	if ( strcmp ( arg, "-D" ) == 0 ) {
388
	    define_macro ( "FS_OS_", sysname ) ;
389
	    define_macro ( "FS_OS_VERS_", release ) ;
390
	    define_macro ( "FS_CPU_", machine ) ;
391
	    define_macro ( "FS_EXEC_", execform ) ;
392
	    return ( 0 ) ;
393
	}
394
	IGNORE fprintf ( stderr, "tcc_host: Unknown argument '%s'.\n", arg ) ;
395
    }
396
    IGNORE printf ( "%s %s %s\n", sysname, release, machine ) ;
397
    return ( 0 ) ;
398
}