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