xref: /illumos-gate/usr/src/cmd/sgs/libconv/common/tokens.c (revision 55d6cb5d63bcf69dfa47b8c41c770a2d34f169b0)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/systeminfo.h>
30 #include <sys/utsname.h>
31 #include <limits.h>
32 #include <strings.h>
33 #include "_conv.h"
34 
35 /*
36  * Isalist(1) expansion.
37  *
38  * Obtain the native instruction sets executable on this platform and unpack
39  * each element into the isalist descriptor.
40  */
41 Isa_desc *
42 conv_isalist(void)
43 {
44 	char		info[SYS_NMLN], * list, * ptr, * optr;
45 	Isa_desc *	desc;
46 	Isa_opt *	opt;
47 	long		size;
48 	int		no;
49 
50 	if ((desc = calloc(1, sizeof (Isa_desc))) == 0)
51 		return (0);
52 
53 	/*
54 	 * If we can't get the isalist() perhaps we've gone back to a release
55 	 * too old to support it - silently ignore.
56 	 */
57 	if ((size = sysinfo(SI_ISALIST, info, SYS_NMLN)) == -1)
58 		return (desc);
59 	desc->isa_listsz = (size_t)size;
60 
61 	/*
62 	 * Duplicate the isalist string in preparation for breaking it up.
63 	 */
64 	if ((list = strdup(info)) == 0)
65 		return (desc);
66 	desc->isa_list = list;
67 
68 	/*
69 	 * Determine the number of instruction sets and use this to size the
70 	 * isalist option table.
71 	 */
72 	for (no = 1, ptr = list; *ptr; ptr++) {
73 		if (*ptr == ' ')
74 			no++;
75 	}
76 	if ((opt = malloc(no * sizeof (Isa_opt))) == 0)
77 		return (desc);
78 	desc->isa_opt = opt;
79 	desc->isa_optno = no;
80 
81 	/*
82 	 * Unpack the instruction set list.
83 	 */
84 	for (optr = ptr = list; *ptr; ptr++) {
85 		if (*ptr != ' ')
86 			continue;
87 
88 		opt->isa_name = optr;
89 		opt->isa_namesz = ptr - optr;
90 		opt++;
91 
92 		*ptr = '\0';
93 		optr = ptr + 1;
94 	}
95 	opt->isa_name = optr;
96 	opt->isa_namesz = ptr - optr;
97 
98 	return (desc);
99 }
100 
101 /*
102  * uname(2) expansion.
103  *
104  * Obtain the information that identifies the current operating system and
105  * unpack those elements we're interested in (presently name and release).
106  */
107 Uts_desc *
108 conv_uts(void)
109 {
110 	struct utsname	utsname;
111 	Uts_desc *	desc;
112 	size_t		size;
113 
114 	if ((desc = calloc(1, sizeof (Uts_desc))) == 0)
115 		return (0);
116 
117 	/*
118 	 * If we can't get the uname(2) silently ignore.
119 	 */
120 	if (uname(&utsname) == -1)
121 		return (desc);
122 
123 	/*
124 	 * Duplicate the operating system name and release components.
125 	 */
126 	size = strlen(utsname.sysname);
127 	if ((desc->uts_osname = malloc(size + 1)) == 0)
128 		return (desc);
129 	desc->uts_osnamesz = size;
130 	(void) strncpy(desc->uts_osname, utsname.sysname, size);
131 
132 	size = strlen(utsname.release);
133 	if ((desc->uts_osrel = malloc(size + 1)) == 0)
134 		return (0);
135 	desc->uts_osrelsz = size;
136 	(void) strncpy(desc->uts_osrel, utsname.release, size);
137 
138 	return (desc);
139 }
140