summaryrefslogtreecommitdiffstats
path: root/lib/uname.c
blob: d4778a8e3d218ec6093427b5caa86fabc7b0ce3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* uname replacement.
   Copyright (C) 2009-2020 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

/* Specification.  */
#include <sys/utsname.h>

/* This file provides an implementation only for the native Windows API.  */
#if defined _WIN32 && ! defined __CYGWIN__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <windows.h>

/* Mingw headers don't have all the platform codes.  */
#ifndef VER_PLATFORM_WIN32_CE
# define VER_PLATFORM_WIN32_CE 3
#endif

/* Some headers don't have all the processor architecture codes.  */
#ifndef PROCESSOR_ARCHITECTURE_AMD64
# define PROCESSOR_ARCHITECTURE_AMD64 9
#endif
#ifndef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
# define PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 10
#endif

/* Mingw headers don't have the latest processor codes.  */
#ifndef PROCESSOR_AMD_X8664
# define PROCESSOR_AMD_X8664 8664
#endif

int
uname (struct utsname *buf)
{
  OSVERSIONINFO version;
  OSVERSIONINFOEX versionex;
  BOOL have_versionex; /* indicates whether versionex is filled */
  const char *super_version;

  /* Preparation: Fill version and, if possible, also versionex.
     But try to call GetVersionEx only once in the common case.  */
  versionex.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
  have_versionex = GetVersionEx ((OSVERSIONINFO *) &versionex);
  if (have_versionex)
    {
      /* We know that OSVERSIONINFO is a subset of OSVERSIONINFOEX.  */
      memcpy (&version, &versionex, sizeof (OSVERSIONINFO));
    }
  else
    {
      version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
      if (!GetVersionEx (&version))
        abort ();
    }

  /* Fill in nodename.  */
  if (gethostname (buf->nodename, sizeof (buf->nodename)) < 0)
    strcpy (buf->nodename, "localhost");

  /* Determine major-major Windows version.  */
  if (version.dwPlatformId == VER_PLATFORM_WIN32_NT)
    {
      /* Windows NT or newer.  */
      super_version = "NT";
    }
  else if (version.dwPlatformId == VER_PLATFORM_WIN32_CE)
    {
      /* Windows CE or Embedded CE.  */
      super_version = "CE";
    }
  else if (version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
    {
      /* Windows 95/98/ME.  */
      switch (version.dwMinorVersion)
        {
        case 0:
          super_version = "95";
          break;
        case 10:
          super_version = "98";
          break;
        case 90:
          super_version = "ME";
          break;
        default:
          super_version = "";
          break;
        }
    }
  else
    super_version = "";

  /* Fill in sysname.  */
#ifdef __MINGW32__
  /* Returns a string compatible with the MSYS uname.exe program,
     so that no further changes are needed to GNU config.guess.
     For example,
       $ ./uname.exe -s      => MINGW32_NT-5.1
   */
  sprintf (buf->sysname, "MINGW32_%s-%u.%u", super_version,
           (unsigned int) version.dwMajorVersion,
           (unsigned int) version.dwMinorVersion);
#else
  sprintf (buf->sysname, "Windows%s", super_version);
#endif

  /* Fill in release, version.  */
  /* The MSYS uname.exe programs uses strings from a modified Cygwin runtime:
       $ ./uname.exe -r      => 1.0.11(0.46/3/2)
       $ ./uname.exe -v      => 2008-08-25 23:40
     There is no point in imitating this behaviour.  */
  if (version.dwPlatformId == VER_PLATFORM_WIN32_NT)
    {
      /* Windows NT or newer.  */
      struct windows_version
        {
          int major;
          int minor;
          unsigned int server_offset;
          const char *name;
        };

      /* Storing the workstation and server version names in a single
         stream does not waste memory when they are the same.  These
         macros abstract the representation.  VERSION1 is used if
         version.wProductType does not matter, VERSION2 if it does.  */
      #define VERSION1(major, minor, name) \
        { major, minor, 0, name }
      #define VERSION2(major, minor, workstation, server) \
        { major, minor, sizeof workstation, workstation "\0" server }
      static const struct windows_version versions[] =
        {
          VERSION2 (3, -1, "Windows NT Workstation", "Windows NT Server"),
          VERSION2 (4, -1, "Windows NT Workstation", "Windows NT Server"),
          VERSION1 (5, 0, "Windows 2000"),
          VERSION1 (5, 1, "Windows XP"),
          VERSION1 (5, 2, "Windows Server 2003"),
          VERSION2 (6, 0, "Windows Vista", "Windows Server 2008"),
          VERSION2 (6, 1, "Windows 7", "Windows Server 2008 R2"),
          VERSION2 (-1, -1, "Windows", "Windows Server")
        };
      const char *base;
      const struct windows_version *v = versions;

      /* Find a version that matches ours.  The last element is a
         wildcard that always ends the loop.  */
      while ((v->major != version.dwMajorVersion && v->major != -1)
             || (v->minor != version.dwMinorVersion && v->minor != -1))
        v++;

      if (have_versionex && versionex.wProductType != VER_NT_WORKSTATION)
        base = v->name + v->server_offset;
      else
        base = v->name;
      if (v->major == -1 || v->minor == -1)
        sprintf (buf->release, "%s %u.%u",
                 base,
                 (unsigned int) version.dwMajorVersion,
                 (unsigned int) version.dwMinorVersion);
      else
        strcpy (buf->release, base);
    }
  else if (version.dwPlatformId == VER_PLATFORM_WIN32_CE)
    {
      /* Windows CE or Embedded CE.  */
      sprintf (buf->release, "Windows CE %u.%u",
               (unsigned int) version.dwMajorVersion,
               (unsigned int) version.dwMinorVersion);
    }
  else
    {
      /* Windows 95/98/ME.  */
      sprintf (buf->release, "Windows %s", super_version);
    }
  strcpy (buf->version, version.szCSDVersion);

  /* Fill in machine.  */
  {
    SYSTEM_INFO info;

    GetSystemInfo (&info);
    /* Check for Windows NT or CE, since the info.wProcessorLevel is
       garbage on Windows 95. */
    if (version.dwPlatformId == VER_PLATFORM_WIN32_NT
        || version.dwPlatformId == VER_PLATFORM_WIN32_CE)
      {
        /* Windows NT or newer, or Windows CE or Embedded CE.  */
        switch (info.wProcessorArchitecture)
          {
          case PROCESSOR_ARCHITECTURE_AMD64:
            strcpy (buf->machine, "x86_64");
            break;
          case PROCESSOR_ARCHITECTURE_IA64:
            strcpy (buf->machine, "ia64");
            break;
          case PROCESSOR_ARCHITECTURE_INTEL:
            strcpy (buf->machine, "i386");
            if (info.wProcessorLevel >= 3)
              buf->machine[1] =
                '0' + (info.wProcessorLevel <= 6 ? info.wProcessorLevel : 6);
            break;
          case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
            strcpy (buf->machine, "i686");
            break;
          case PROCESSOR_ARCHITECTURE_MIPS:
            strcpy (buf->machine, "mips");
            break;
          case PROCESSOR_ARCHITECTURE_ALPHA:
          case PROCESSOR_ARCHITECTURE_ALPHA64:
            strcpy (buf->machine, "alpha");
            break;
          case PROCESSOR_ARCHITECTURE_PPC:
            strcpy (buf->machine, "powerpc");
            break;
          case PROCESSOR_ARCHITECTURE_SHX:
            strcpy (buf->machine, "sh");
            break;
          case PROCESSOR_ARCHITECTURE_ARM:
            strcpy (buf->machine, "arm");
            break;
          default:
            strcpy (buf->machine, "unknown");
            break;
          }
      }
    else
      {
        /* Windows 95/98/ME.  */
        switch (info.dwProcessorType)
          {
          case PROCESSOR_AMD_X8664:
            strcpy (buf->machine, "x86_64");
            break;
          case PROCESSOR_INTEL_IA64:
            strcpy (buf->machine, "ia64");
            break;
          default:
            if (info.dwProcessorType % 100 == 86)
              sprintf (buf->machine, "i%u",
                       (unsigned int) info.dwProcessorType);
            else
              strcpy (buf->machine, "unknown");
            break;
          }
      }
  }

  return 0;
}

#endif