xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/tokens.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *	Copyright (c) 1999,2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  *	All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #define	 sysinfo	_sysinfo
29*0Sstevel@tonic-gate #define	 strdup		_strdup
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include	<sys/systeminfo.h>
32*0Sstevel@tonic-gate #include	<sys/utsname.h>
33*0Sstevel@tonic-gate #include	<limits.h>
34*0Sstevel@tonic-gate #include	<strings.h>
35*0Sstevel@tonic-gate #include	"_conv.h"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * Isalist(1) expansion.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * Obtain the native instruction sets executable on this platform and unpack
41*0Sstevel@tonic-gate  * each element into the isalist descriptor.
42*0Sstevel@tonic-gate  */
43*0Sstevel@tonic-gate Isa_desc *
44*0Sstevel@tonic-gate conv_isalist(void)
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate 	char		info[SYS_NMLN], * list, * ptr, * optr;
47*0Sstevel@tonic-gate 	Isa_desc *	desc;
48*0Sstevel@tonic-gate 	Isa_opt *	opt;
49*0Sstevel@tonic-gate 	long		size;
50*0Sstevel@tonic-gate 	int		no;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	if ((desc = calloc(1, sizeof (Isa_desc))) == 0)
53*0Sstevel@tonic-gate 		return (0);
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	/*
56*0Sstevel@tonic-gate 	 * If we can't get the isalist() perhaps we've gone back to a release
57*0Sstevel@tonic-gate 	 * too old to support it - silently ignore.
58*0Sstevel@tonic-gate 	 */
59*0Sstevel@tonic-gate 	if ((size = sysinfo(SI_ISALIST, info, SYS_NMLN)) == -1)
60*0Sstevel@tonic-gate 		return (desc);
61*0Sstevel@tonic-gate 	desc->isa_listsz = (size_t)size;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	/*
64*0Sstevel@tonic-gate 	 * Duplicate the isalist string in preparation for breaking it up.
65*0Sstevel@tonic-gate 	 */
66*0Sstevel@tonic-gate 	if ((list = strdup(info)) == 0)
67*0Sstevel@tonic-gate 		return (desc);
68*0Sstevel@tonic-gate 	desc->isa_list = list;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	/*
71*0Sstevel@tonic-gate 	 * Determine the number of instruction sets and use this to size the
72*0Sstevel@tonic-gate 	 * isalist option table.
73*0Sstevel@tonic-gate 	 */
74*0Sstevel@tonic-gate 	for (no = 1, ptr = list; *ptr; ptr++) {
75*0Sstevel@tonic-gate 		if (*ptr == ' ')
76*0Sstevel@tonic-gate 			no++;
77*0Sstevel@tonic-gate 	}
78*0Sstevel@tonic-gate 	if ((opt = malloc(no * sizeof (Isa_opt))) == 0)
79*0Sstevel@tonic-gate 		return (desc);
80*0Sstevel@tonic-gate 	desc->isa_opt = opt;
81*0Sstevel@tonic-gate 	desc->isa_optno = no;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	/*
84*0Sstevel@tonic-gate 	 * Unpack the instruction set list.
85*0Sstevel@tonic-gate 	 */
86*0Sstevel@tonic-gate 	for (optr = ptr = list; *ptr; ptr++) {
87*0Sstevel@tonic-gate 		if (*ptr != ' ')
88*0Sstevel@tonic-gate 			continue;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 		opt->isa_name = optr;
91*0Sstevel@tonic-gate 		opt->isa_namesz = ptr - optr;
92*0Sstevel@tonic-gate 		opt++;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 		*ptr = '\0';
95*0Sstevel@tonic-gate 		optr = ptr + 1;
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 	opt->isa_name = optr;
98*0Sstevel@tonic-gate 	opt->isa_namesz = ptr - optr;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	return (desc);
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate /*
104*0Sstevel@tonic-gate  * uname(2) expansion.
105*0Sstevel@tonic-gate  *
106*0Sstevel@tonic-gate  * Obtain the information that identifies the current operating system and
107*0Sstevel@tonic-gate  * unpack those elements we're interested in (presently name and release).
108*0Sstevel@tonic-gate  */
109*0Sstevel@tonic-gate Uts_desc *
110*0Sstevel@tonic-gate conv_uts(void)
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	struct utsname	utsname;
113*0Sstevel@tonic-gate 	Uts_desc *	desc;
114*0Sstevel@tonic-gate 	size_t		size;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	if ((desc = calloc(1, sizeof (Uts_desc))) == 0)
117*0Sstevel@tonic-gate 		return (0);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	/*
120*0Sstevel@tonic-gate 	 * If we can't get the uname(2) silently ignore.
121*0Sstevel@tonic-gate 	 */
122*0Sstevel@tonic-gate 	if (uname(&utsname) == -1)
123*0Sstevel@tonic-gate 		return (desc);
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	/*
126*0Sstevel@tonic-gate 	 * Duplicate the operating system name and release components.
127*0Sstevel@tonic-gate 	 */
128*0Sstevel@tonic-gate 	size = strlen(utsname.sysname);
129*0Sstevel@tonic-gate 	if ((desc->uts_osname = malloc(size + 1)) == 0)
130*0Sstevel@tonic-gate 		return (desc);
131*0Sstevel@tonic-gate 	desc->uts_osnamesz = size;
132*0Sstevel@tonic-gate 	(void) strncpy(desc->uts_osname, utsname.sysname, size);
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	size = strlen(utsname.release);
135*0Sstevel@tonic-gate 	if ((desc->uts_osrel = malloc(size + 1)) == 0)
136*0Sstevel@tonic-gate 		return (0);
137*0Sstevel@tonic-gate 	desc->uts_osrelsz = size;
138*0Sstevel@tonic-gate 	(void) strncpy(desc->uts_osrel, utsname.release, size);
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	return (desc);
141*0Sstevel@tonic-gate }
142