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