replace SPDX identifier GPL-2.0+ with GPL-2.0-or-later.
[gd/wireshark/.git] / version_info.c
1 /* version_info.c
2  * Routines to report version information for Wireshark programs
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include <config.h>
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <locale.h>
17
18 #ifdef _WIN32
19 #include <windows.h>
20 #elif __APPLE__
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
23 #elif __linux__
24 #include <sys/sysinfo.h>
25 #endif
26
27 #include <glib.h>
28
29 #ifdef HAVE_ZLIB
30 #include <zlib.h>
31 #endif
32
33 #include "version.h"
34
35 #include "version_info.h"
36
37 #include <wsutil/cpu_info.h>
38 #include <wsutil/copyright_info.h>
39 #include <wsutil/os_version_info.h>
40 #include <wsutil/ws_printf.h> /* ws_debug_printf */
41 #include <wsutil/plugins.h>
42
43 /*
44  * If the string doesn't end with a newline, append one.
45  * Then word-wrap it to 80 columns.
46  */
47 static void
48 end_string(GString *str)
49 {
50         size_t point;
51         char *p, *q;
52
53         point = str->len;
54         if (point == 0 || str->str[point - 1] != '\n')
55                 g_string_append(str, "\n");
56         p = str->str;
57         while (*p != '\0') {
58                 q = strchr(p, '\n');
59                 if (q - p > 80) {
60                         /*
61                          * Break at or before this point.
62                          */
63                         q = p + 80;
64                         while (q > p && *q != ' ')
65                                 q--;
66                         if (q != p)
67                                 *q = '\n';
68                 }
69                 p = q + 1;
70         }
71 }
72
73 static const gchar *
74 get_zlib_compiled_version_info(void)
75 {
76 #ifdef HAVE_ZLIB
77 #ifdef ZLIB_VERSION
78         return "with zlib "ZLIB_VERSION;
79 #else
80         return "with zlib (version unknown)";
81 #endif /* ZLIB_VERSION */
82 #else
83         return "without zlib";
84 #endif /* HAVE_ZLIB */
85 }
86
87 /*
88  * Get various library compile-time versions, put them in a GString,
89  * and return the GString.
90  *
91  * "prepend_info" is called at the start to prepend any additional
92  * information before the standard library information.
93  *
94  * "append_info" is called at the end to append any additional
95  * information after the standard library information.  This is
96  * required in order to, for example, put the Portaudio information
97  * at the end of the string, as we currently don't use Portaudio in
98  * TShark.
99  */
100 GString *
101 get_compiled_version_info(void (*prepend_info)(GString *),
102                           void (*append_info)(GString *))
103 {
104         GString *str;
105
106         str = g_string_new("Compiled ");
107
108         if (sizeof(str) == 4)
109                 g_string_append(str, "(32-bit) ");
110         else
111                 g_string_append(str, "(64-bit) ");
112
113         if (prepend_info) {
114                 (*prepend_info)(str);
115                 g_string_append(str, ", ");
116         }
117
118         /* GLIB */
119         g_string_append(str, "with ");
120         g_string_append_printf(str,
121 #ifdef GLIB_MAJOR_VERSION
122             "GLib %d.%d.%d", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION,
123             GLIB_MICRO_VERSION);
124 #else
125             "GLib (version unknown)");
126 #endif
127
128         g_string_append_printf(str, ", %s", get_zlib_compiled_version_info());
129
130         /* Additional application-dependent information */
131         if (append_info)
132                 (*append_info)(str);
133         g_string_append(str, ".");
134
135         end_string(str);
136
137         return str;
138 }
139
140 static void
141 get_mem_info(GString *str)
142 {
143         gint64 memsize = 0;
144
145 #ifdef _WIN32
146         MEMORYSTATUSEX statex;
147
148         statex.dwLength = sizeof (statex);
149
150         if (GlobalMemoryStatusEx(&statex))
151                 memsize = statex.ullTotalPhys;
152 #elif __APPLE__
153         size_t len = sizeof(memsize);
154         sysctlbyname("hw.memsize", &memsize, &len, NULL, 0);
155 #elif __linux__
156         struct sysinfo info;
157         if (sysinfo(&info) == 0)
158                 memsize = info.totalram * info.mem_unit;
159 #endif
160
161         if (memsize > 0)
162                 g_string_append_printf(str, ", with ""%" G_GINT64_MODIFIER "d" " MB of physical memory", memsize/(1024*1024));
163 }
164
165 /*
166  * Get compiler information, and append it to the GString.
167  */
168 static void
169 get_compiler_info(GString *str)
170 {
171         /*
172          * See https://sourceforge.net/apps/mediawiki/predef/index.php?title=Compilers
173          * information on various defined strings.
174          *
175          * GCC's __VERSION__ is a nice text string for humans to
176          * read.  The page at sourceforge.net largely describes
177          * numeric #defines that encode the version; if the compiler
178          * doesn't also offer a nice printable string, we try prettifying
179          * the number somehow.
180          */
181 #if defined(__GNUC__) && defined(__VERSION__)
182         /*
183          * Clang and llvm-gcc also define __GNUC__ and __VERSION__;
184          * distinguish between them.
185          */
186 #if defined(__clang__)
187         g_string_append_printf(str, "\n\nBuilt using clang %s.\n", __VERSION__);
188 #elif defined(__llvm__)
189         g_string_append_printf(str, "\n\nBuilt using llvm-gcc %s.\n", __VERSION__);
190 #else /* boring old GCC */
191         g_string_append_printf(str, "\n\nBuilt using gcc %s.\n", __VERSION__);
192 #endif /* llvm */
193 #elif defined(__HP_aCC)
194         g_string_append_printf(str, "\n\nBuilt using HP aCC %d.\n", __HP_aCC);
195 #elif defined(__xlC__)
196         g_string_append_printf(str, "\n\nBuilt using IBM XL C %d.%d\n",
197             (__xlC__ >> 8) & 0xFF, __xlC__ & 0xFF);
198 #ifdef __IBMC__
199         if ((__IBMC__ % 10) != 0)
200                 g_string_append_printf(str, " patch %d", __IBMC__ % 10);
201 #endif /* __IBMC__ */
202         g_string_append_printf(str, "\n");
203 #elif defined(__INTEL_COMPILER)
204         g_string_append_printf(str, "\n\nBuilt using Intel C %d.%d",
205             __INTEL_COMPILER / 100, (__INTEL_COMPILER / 10) % 10);
206         if ((__INTEL_COMPILER % 10) != 0)
207                 g_string_append_printf(str, " patch %d", __INTEL_COMPILER % 10);
208 #ifdef __INTEL_COMPILER_BUILD_DATE
209         g_string_sprinta(str, ", compiler built %04d-%02d-%02d",
210             __INTEL_COMPILER_BUILD_DATE / 10000,
211             (__INTEL_COMPILER_BUILD_DATE / 100) % 100,
212             __INTEL_COMPILER_BUILD_DATE % 100);
213 #endif /* __INTEL_COMPILER_BUILD_DATE */
214         g_string_append_printf(str, "\n");
215 #elif defined(_MSC_FULL_VER)
216 # if _MSC_FULL_VER > 99999999
217         /* Quote from the web:
218          * Bakersfield: DevDiv's upper management determines the scheduling of new major versions.
219          * They also decided to increment the product version from 12 (for VS 2013) to 14 (for VS 2015).
220          * However, the C++ compiler's version incremented normally, from 18 to 19.
221          * (It's larger because the C++ compiler predates the "Visual" in Visual C++.)
222          * XXX? Should we just output the compiler version?
223          */
224     int compiler_major_version = (_MSC_FULL_VER / 10000000), visual_studio_ver;
225
226     if (compiler_major_version < 19) {
227         visual_studio_ver = compiler_major_version - 6;
228     }else{
229         visual_studio_ver = compiler_major_version - 5;
230     }
231
232     g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d",
233                    visual_studio_ver,
234                                (_MSC_FULL_VER / 100000) % 100);
235 #  if (_MSC_FULL_VER % 100000) != 0
236         g_string_append_printf(str, " build %d",
237                                _MSC_FULL_VER % 100000);
238 #  endif
239 # else
240         g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d",
241                                (_MSC_FULL_VER / 1000000) - 6,
242                                (_MSC_FULL_VER / 10000) % 100);
243 #  if (_MSC_FULL_VER % 10000) != 0
244         g_string_append_printf(str, " build %d",
245                                _MSC_FULL_VER % 10000);
246 #  endif
247 # endif
248         g_string_append_printf(str, "\n");
249 #elif defined(_MSC_VER)
250         /* _MSC_FULL_VER not defined, but _MSC_VER defined */
251         g_string_append_printf(str, "\n\nBuilt using Microsoft Visual C++ %d.%d\n",
252             (_MSC_VER / 100) - 6, _MSC_VER % 100);
253 #elif defined(__SUNPRO_C)
254         g_string_append_printf(str, "\n\nBuilt using Sun C %d.%d",
255             (__SUNPRO_C >> 8) & 0xF, (__SUNPRO_C >> 4) & 0xF);
256         if ((__SUNPRO_C & 0xF) != 0)
257                 g_string_append_printf(str, " patch %d", __SUNPRO_C & 0xF);
258         g_string_append_printf(str, "\n");
259 #endif
260 }
261
262 /* XXX - is the setlocale() return string opaque? For glibc the separator is ';' */
263 static gchar *
264 get_locale(void)
265 {
266         const gchar *lang;
267         gchar **locv, *loc;
268
269         lang = setlocale(LC_ALL, NULL);
270         if (lang == NULL) {
271                 return NULL;
272         }
273
274         locv = g_strsplit(lang, ";", -1);
275         loc = g_strjoinv(", ", locv);
276         g_strfreev(locv);
277         return loc;
278 }
279
280 /*
281  * Get various library run-time versions, and the OS version, and append
282  * them to the specified GString.
283  *
284  * "additional_info" is called at the end to append any additional
285  * information; this is required in order to, for example, put the
286  * Portaudio information at the end of the string, as we currently
287  * don't use Portaudio in TShark.
288  */
289 GString *
290 get_runtime_version_info(void (*additional_info)(GString *))
291 {
292         GString *str;
293         gchar *lang;
294
295         str = g_string_new("Running on ");
296
297         get_os_version_info(str);
298
299         /* CPU Info */
300         get_cpu_info(str);
301
302         /* Get info about installed memory */
303         get_mem_info(str);
304
305         /*
306          * Locale.
307          *
308          * This returns the C language's locale information; this
309          * returns the locale that's actually in effect, even if
310          * it doesn't happen to match the settings of any of the
311          * locale environment variables.
312          *
313          * On Windows get_locale returns the full language, country
314          * name, and code page, e.g. "English_United States.1252":
315          * https://msdn.microsoft.com/en-us/library/x99tb11d.aspx
316          */
317         if ((lang = get_locale()) != NULL) {
318                 g_string_append_printf(str, ", with locale %s", lang);
319                 g_free(lang);
320         }
321         else {
322                 g_string_append(str, ", with default locale");
323         }
324
325
326         /* Additional application-dependent information */
327         if (additional_info)
328                 (*additional_info)(str);
329
330         /* zlib */
331 #if defined(HAVE_ZLIB) && !defined(_WIN32)
332         g_string_append_printf(str, ", with zlib %s", zlibVersion());
333 #endif
334
335         /* plugins */
336 #ifdef HAVE_PLUGINS
337         if (g_module_supported()) {
338                 g_string_append_printf(str, ", binary plugins supported (%d loaded)", plugins_get_count());
339         }
340         else {
341                 g_string_append(str, ", binary plugins not supported");
342         }
343 #endif
344
345         g_string_append(str, ".");
346
347         /* Compiler info */
348         get_compiler_info(str);
349
350         end_string(str);
351
352         return str;
353 }
354
355 void
356 show_version(const gchar *prog_name_str, GString *comp_info_str,
357              GString *runtime_info_str)
358 {
359         ws_debug_printf("%s %s\n"
360                "\n"
361                "%s"
362                "\n"
363                "%s"
364                "\n"
365                "%s",
366                prog_name_str, get_ws_vcs_version_info(), get_copyright_info(),
367                comp_info_str->str, runtime_info_str->str);
368 }
369
370 /*
371  * Return a version number string for Wireshark, including, for builds
372  * from a tree checked out from Wireshark's version control system,
373  * something identifying what version was checked out.
374  */
375 const char *
376 get_ws_vcs_version_info(void)
377 {
378 #ifdef VCSVERSION
379         return VERSION " (" VCSVERSION ")";
380 #else
381         return VERSION;
382 #endif
383 }
384
385 void
386 get_ws_version_number(int *major, int *minor, int *micro)
387 {
388         if (major)
389                 *major = VERSION_MAJOR;
390         if (minor)
391                 *minor = VERSION_MINOR;
392         if (micro)
393                 *micro = VERSION_MICRO;
394 }
395
396 /*
397  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
398  *
399  * Local variables:
400  * c-basic-offset: 8
401  * tab-width: 8
402  * indent-tabs-mode: t
403  * End:
404  *
405  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
406  * :indentSize=8:tabSize=8:noTabs=false:
407  */