xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/tokens.c (revision 1618:8c9a4f31d225)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1618Srie  * Common Development and Distribution License (the "License").
6*1618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*1618Srie 
220Sstevel@tonic-gate /*
23*1618Srie  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*1618Srie  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #define	 sysinfo	_sysinfo
290Sstevel@tonic-gate #define	 strdup		_strdup
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include	<sys/systeminfo.h>
320Sstevel@tonic-gate #include	<sys/utsname.h>
330Sstevel@tonic-gate #include	<limits.h>
340Sstevel@tonic-gate #include	<strings.h>
350Sstevel@tonic-gate #include	"_conv.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * Isalist(1) expansion.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * Obtain the native instruction sets executable on this platform and unpack
410Sstevel@tonic-gate  * each element into the isalist descriptor.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate Isa_desc *
440Sstevel@tonic-gate conv_isalist(void)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 	char		info[SYS_NMLN], * list, * ptr, * optr;
470Sstevel@tonic-gate 	Isa_desc *	desc;
480Sstevel@tonic-gate 	Isa_opt *	opt;
490Sstevel@tonic-gate 	long		size;
500Sstevel@tonic-gate 	int		no;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	if ((desc = calloc(1, sizeof (Isa_desc))) == 0)
530Sstevel@tonic-gate 		return (0);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	/*
560Sstevel@tonic-gate 	 * If we can't get the isalist() perhaps we've gone back to a release
570Sstevel@tonic-gate 	 * too old to support it - silently ignore.
580Sstevel@tonic-gate 	 */
590Sstevel@tonic-gate 	if ((size = sysinfo(SI_ISALIST, info, SYS_NMLN)) == -1)
600Sstevel@tonic-gate 		return (desc);
610Sstevel@tonic-gate 	desc->isa_listsz = (size_t)size;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	/*
640Sstevel@tonic-gate 	 * Duplicate the isalist string in preparation for breaking it up.
650Sstevel@tonic-gate 	 */
660Sstevel@tonic-gate 	if ((list = strdup(info)) == 0)
670Sstevel@tonic-gate 		return (desc);
680Sstevel@tonic-gate 	desc->isa_list = list;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	/*
710Sstevel@tonic-gate 	 * Determine the number of instruction sets and use this to size the
720Sstevel@tonic-gate 	 * isalist option table.
730Sstevel@tonic-gate 	 */
740Sstevel@tonic-gate 	for (no = 1, ptr = list; *ptr; ptr++) {
750Sstevel@tonic-gate 		if (*ptr == ' ')
760Sstevel@tonic-gate 			no++;
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate 	if ((opt = malloc(no * sizeof (Isa_opt))) == 0)
790Sstevel@tonic-gate 		return (desc);
800Sstevel@tonic-gate 	desc->isa_opt = opt;
810Sstevel@tonic-gate 	desc->isa_optno = no;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	/*
840Sstevel@tonic-gate 	 * Unpack the instruction set list.
850Sstevel@tonic-gate 	 */
860Sstevel@tonic-gate 	for (optr = ptr = list; *ptr; ptr++) {
870Sstevel@tonic-gate 		if (*ptr != ' ')
880Sstevel@tonic-gate 			continue;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 		opt->isa_name = optr;
910Sstevel@tonic-gate 		opt->isa_namesz = ptr - optr;
920Sstevel@tonic-gate 		opt++;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 		*ptr = '\0';
950Sstevel@tonic-gate 		optr = ptr + 1;
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 	opt->isa_name = optr;
980Sstevel@tonic-gate 	opt->isa_namesz = ptr - optr;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	return (desc);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * uname(2) expansion.
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * Obtain the information that identifies the current operating system and
1070Sstevel@tonic-gate  * unpack those elements we're interested in (presently name and release).
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate Uts_desc *
1100Sstevel@tonic-gate conv_uts(void)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	struct utsname	utsname;
1130Sstevel@tonic-gate 	Uts_desc *	desc;
1140Sstevel@tonic-gate 	size_t		size;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if ((desc = calloc(1, sizeof (Uts_desc))) == 0)
1170Sstevel@tonic-gate 		return (0);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	/*
1200Sstevel@tonic-gate 	 * If we can't get the uname(2) silently ignore.
1210Sstevel@tonic-gate 	 */
1220Sstevel@tonic-gate 	if (uname(&utsname) == -1)
1230Sstevel@tonic-gate 		return (desc);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	/*
1260Sstevel@tonic-gate 	 * Duplicate the operating system name and release components.
1270Sstevel@tonic-gate 	 */
1280Sstevel@tonic-gate 	size = strlen(utsname.sysname);
1290Sstevel@tonic-gate 	if ((desc->uts_osname = malloc(size + 1)) == 0)
1300Sstevel@tonic-gate 		return (desc);
1310Sstevel@tonic-gate 	desc->uts_osnamesz = size;
1320Sstevel@tonic-gate 	(void) strncpy(desc->uts_osname, utsname.sysname, size);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	size = strlen(utsname.release);
1350Sstevel@tonic-gate 	if ((desc->uts_osrel = malloc(size + 1)) == 0)
1360Sstevel@tonic-gate 		return (0);
1370Sstevel@tonic-gate 	desc->uts_osrelsz = size;
1380Sstevel@tonic-gate 	(void) strncpy(desc->uts_osrel, utsname.release, size);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	return (desc);
1410Sstevel@tonic-gate }
142