17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
245aefb655Srie  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*7257d1b4Sraf #include <sys/systeminfo.h>
28*7257d1b4Sraf #include <sys/utsname.h>
29*7257d1b4Sraf #include <limits.h>
30*7257d1b4Sraf #include <strings.h>
31*7257d1b4Sraf #include "_conv.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * Isalist(1) expansion.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * Obtain the native instruction sets executable on this platform and unpack
377c478bd9Sstevel@tonic-gate  * each element into the isalist descriptor.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate Isa_desc *
conv_isalist(void)407c478bd9Sstevel@tonic-gate conv_isalist(void)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	char		info[SYS_NMLN], * list, * ptr, * optr;
437c478bd9Sstevel@tonic-gate 	Isa_desc *	desc;
447c478bd9Sstevel@tonic-gate 	Isa_opt *	opt;
457c478bd9Sstevel@tonic-gate 	long		size;
467c478bd9Sstevel@tonic-gate 	int		no;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	if ((desc = calloc(1, sizeof (Isa_desc))) == 0)
497c478bd9Sstevel@tonic-gate 		return (0);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	/*
527c478bd9Sstevel@tonic-gate 	 * If we can't get the isalist() perhaps we've gone back to a release
537c478bd9Sstevel@tonic-gate 	 * too old to support it - silently ignore.
547c478bd9Sstevel@tonic-gate 	 */
557c478bd9Sstevel@tonic-gate 	if ((size = sysinfo(SI_ISALIST, info, SYS_NMLN)) == -1)
567c478bd9Sstevel@tonic-gate 		return (desc);
577c478bd9Sstevel@tonic-gate 	desc->isa_listsz = (size_t)size;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/*
607c478bd9Sstevel@tonic-gate 	 * Duplicate the isalist string in preparation for breaking it up.
617c478bd9Sstevel@tonic-gate 	 */
627c478bd9Sstevel@tonic-gate 	if ((list = strdup(info)) == 0)
637c478bd9Sstevel@tonic-gate 		return (desc);
647c478bd9Sstevel@tonic-gate 	desc->isa_list = list;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	/*
677c478bd9Sstevel@tonic-gate 	 * Determine the number of instruction sets and use this to size the
687c478bd9Sstevel@tonic-gate 	 * isalist option table.
697c478bd9Sstevel@tonic-gate 	 */
707c478bd9Sstevel@tonic-gate 	for (no = 1, ptr = list; *ptr; ptr++) {
717c478bd9Sstevel@tonic-gate 		if (*ptr == ' ')
727c478bd9Sstevel@tonic-gate 			no++;
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	if ((opt = malloc(no * sizeof (Isa_opt))) == 0)
757c478bd9Sstevel@tonic-gate 		return (desc);
767c478bd9Sstevel@tonic-gate 	desc->isa_opt = opt;
777c478bd9Sstevel@tonic-gate 	desc->isa_optno = no;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * Unpack the instruction set list.
817c478bd9Sstevel@tonic-gate 	 */
827c478bd9Sstevel@tonic-gate 	for (optr = ptr = list; *ptr; ptr++) {
837c478bd9Sstevel@tonic-gate 		if (*ptr != ' ')
847c478bd9Sstevel@tonic-gate 			continue;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		opt->isa_name = optr;
877c478bd9Sstevel@tonic-gate 		opt->isa_namesz = ptr - optr;
887c478bd9Sstevel@tonic-gate 		opt++;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		*ptr = '\0';
917c478bd9Sstevel@tonic-gate 		optr = ptr + 1;
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 	opt->isa_name = optr;
947c478bd9Sstevel@tonic-gate 	opt->isa_namesz = ptr - optr;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	return (desc);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * uname(2) expansion.
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  * Obtain the information that identifies the current operating system and
1037c478bd9Sstevel@tonic-gate  * unpack those elements we're interested in (presently name and release).
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate Uts_desc *
conv_uts(void)1067c478bd9Sstevel@tonic-gate conv_uts(void)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	struct utsname	utsname;
1097c478bd9Sstevel@tonic-gate 	Uts_desc *	desc;
1107c478bd9Sstevel@tonic-gate 	size_t		size;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if ((desc = calloc(1, sizeof (Uts_desc))) == 0)
1137c478bd9Sstevel@tonic-gate 		return (0);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * If we can't get the uname(2) silently ignore.
1177c478bd9Sstevel@tonic-gate 	 */
1187c478bd9Sstevel@tonic-gate 	if (uname(&utsname) == -1)
1197c478bd9Sstevel@tonic-gate 		return (desc);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * Duplicate the operating system name and release components.
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	size = strlen(utsname.sysname);
1257c478bd9Sstevel@tonic-gate 	if ((desc->uts_osname = malloc(size + 1)) == 0)
1267c478bd9Sstevel@tonic-gate 		return (desc);
1277c478bd9Sstevel@tonic-gate 	desc->uts_osnamesz = size;
1287c478bd9Sstevel@tonic-gate 	(void) strncpy(desc->uts_osname, utsname.sysname, size);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	size = strlen(utsname.release);
1317c478bd9Sstevel@tonic-gate 	if ((desc->uts_osrel = malloc(size + 1)) == 0)
1327c478bd9Sstevel@tonic-gate 		return (0);
1337c478bd9Sstevel@tonic-gate 	desc->uts_osrelsz = size;
1347c478bd9Sstevel@tonic-gate 	(void) strncpy(desc->uts_osrel, utsname.release, size);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	return (desc);
1377c478bd9Sstevel@tonic-gate }
138