Initial snarf.
[shack.git] / libmojave / cutil / lm_uname_ext.c
blob9a2768381e7c0862e55217f74cf86f96c064a89d
1 /*
2 * System info.
4 * ----------------------------------------------------------------
6 * @begin[license]
7 * Copyright (C) 2003 Mojave Group, Caltech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation,
12 * version 2.1 of the License.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Additional permission is given to link this library with the
24 * OpenSSL project's "OpenSSL" library, and with the OCaml runtime,
25 * and you may distribute the linked executables. See the file
26 * LICENSE.libmojave for more details.
28 * Author: Jason Hickey
29 * @email{jyh@cs.caltech.edu}
30 * @end[license]
32 #include <stdio.h>
33 #include <caml/mlvalues.h>
34 #include <caml/alloc.h>
35 #include <caml/memory.h>
36 #include <caml/fail.h>
38 #ifdef WIN32
39 #include <windows.h>
40 /* Disable some of the warnings */
41 #pragma warning( disable : 4127 4189 4702 4996 )
44 * Fake utsname.
46 struct utsname {
47 char sysname[1024];
48 char nodename[1024];
49 char release[1024];
50 char version[1024];
51 char machine[1024];
55 * uname is undefined as far as I know (jyh).
56 * Get most of the info from the environment.
58 static int uname(struct utsname *name)
60 DWORD len;
61 SYSTEM_INFO sysinfo;
62 OSVERSIONINFO osversion;
63 unsigned ptype;
64 char *osname;
66 /* Ask Win32 for OS info */
67 osversion.dwOSVersionInfoSize = sizeof(osversion);
68 if(GetVersionEx(&osversion) == 0)
69 return -1;
71 /* String version of the osname */
72 osname = (char *) "unknown";
73 switch (osversion.dwPlatformId) {
74 case VER_PLATFORM_WIN32s:
75 osname = (char *) "win32s";
76 break;
77 case VER_PLATFORM_WIN32_WINDOWS:
78 switch (osversion.dwMinorVersion) {
79 case 0:
80 osname = (char *) "Win95";
81 break;
82 case 1:
83 osname = (char *) "Win98";
84 break;
85 default:
86 osname = (char *) "Win9X";
87 break;
89 break;
90 case VER_PLATFORM_WIN32_NT:
91 osname = (char *) "WinNT";
92 break;
95 /* Collect data */
96 strcpy(name->sysname, osname);
97 sprintf(name->version, "%d.%d", (int) osversion.dwMajorVersion, (int) osversion.dwMinorVersion);
98 sprintf(name->release, "%d %s", (int) osversion.dwBuildNumber, osversion.szCSDVersion);
100 /* Computer name */
101 len = sizeof(name->nodename) - 1;
102 GetComputerNameA(name->nodename, &len);
103 name->nodename[len] = 0;
105 /* Machine */
106 GetSystemInfo(&sysinfo);
108 /* CPU type */
109 switch (sysinfo.wProcessorArchitecture) {
110 case PROCESSOR_ARCHITECTURE_INTEL:
111 if(sysinfo.dwProcessorType < 3) /* Shouldn't happen. */
112 ptype = 3;
113 else if(sysinfo.dwProcessorType > 9) /* P4 */
114 ptype = 6;
115 else
116 ptype = sysinfo.dwProcessorType;
117 sprintf(name->machine, "i%d86", ptype);
118 break;
119 case PROCESSOR_ARCHITECTURE_ALPHA:
120 strcpy(name->machine, "alpha");
121 break;
122 case PROCESSOR_ARCHITECTURE_MIPS:
123 strcpy(name->machine, "mips");
124 break;
125 default:
126 strcpy(name->machine, "unknown");
127 break;
130 return 0;
133 #else /* WIN32 */
135 #include <sys/utsname.h>
137 #endif /* !WIN32 */
140 * Allocate a 5-tuple of strings.
142 value lm_uname(value x)
144 CAMLparam1(x);
145 CAMLlocal1(result);
146 struct utsname name;
148 /* Get sysinfo */
149 if(uname(&name) < 0)
150 failwith("uname");
152 /* Copy data */
153 result = alloc_tuple(5);
154 Field(result, 0) = Val_unit;
155 Field(result, 1) = Val_unit;
156 Field(result, 2) = Val_unit;
157 Field(result, 3) = Val_unit;
158 Field(result, 4) = Val_unit;
160 Field(result, 0) = copy_string(name.sysname);
161 Field(result, 1) = copy_string(name.nodename);
162 Field(result, 2) = copy_string(name.release);
163 Field(result, 3) = copy_string(name.version);
164 Field(result, 4) = copy_string(name.machine);
166 /* Return it */
167 CAMLreturn(result);