Move the routine to get a CPU information string to wsutil.
[metze/wireshark/wip.git] / version_info.c
1 /* version_info.c
2  * Routines to report version information for stuff used by Wireshark
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #ifdef HAVE_LIBZ
31 #include <zlib.h>       /* to get the libz version number */
32 #endif
33
34 #include "version_info.h"
35 #include "capture-pcap-util.h"
36 #include <wsutil/os_version_info.h>
37 #include <wsutil/compiler_info.h>
38 #include <wsutil/cpu_info.h>
39
40 #include "version.h"
41
42 #ifdef HAVE_WINDOWS_H
43 #include <windows.h>
44 #endif
45
46 #ifdef HAVE_LIBCAP
47 # include <sys/capability.h>
48 #endif
49
50 #ifdef GITVERSION
51         const char *wireshark_gitversion = " (" GITVERSION " from " GITBRANCH ")";
52 #else
53         const char *wireshark_gitversion = "";
54 #endif
55
56 /*
57  * If the string doesn't end with a newline, append one.
58  * Then word-wrap it to 80 columns.
59  */
60 static void
61 end_string(GString *str)
62 {
63         size_t point;
64         char *p, *q;
65
66         point = str->len;
67         if (point == 0 || str->str[point - 1] != '\n')
68                 g_string_append(str, "\n");
69         p = str->str;
70         while (*p != '\0') {
71                 q = strchr(p, '\n');
72                 if (q - p > 80) {
73                         /*
74                          * Break at or before this point.
75                          */
76                         q = p + 80;
77                         while (q > p && *q != ' ')
78                                 q--;
79                         if (q != p)
80                                 *q = '\n';
81                 }
82                 p = q + 1;
83         }
84 }
85
86 /*
87  * Get various library compile-time versions and append them to
88  * the specified GString.
89  *
90  * "additional_info" is called at the end to append any additional
91  * information; this is required in order to, for example, put the
92  * Portaudio information at the end of the string, as we currently
93  * don't use Portaudio in TShark.
94  */
95 void
96 get_compiled_version_info(GString *str, void (*prepend_info)(GString *),
97                           void (*append_info)(GString *))
98 {
99         if (sizeof(str) == 4)
100                 g_string_append(str, "(32-bit) ");
101         else
102                 g_string_append(str, "(64-bit) ");
103
104         if (prepend_info)
105                 (*prepend_info)(str);
106
107         /* GLIB */
108         g_string_append(str, "with ");
109         g_string_append_printf(str,
110 #ifdef GLIB_MAJOR_VERSION
111             "GLib %d.%d.%d", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION,
112             GLIB_MICRO_VERSION);
113 #else
114             "GLib (version unknown)");
115 #endif
116
117         /* Libpcap */
118         g_string_append(str, ", ");
119         get_compiled_pcap_version(str);
120
121         /* LIBZ */
122         g_string_append(str, ", ");
123 #ifdef HAVE_LIBZ
124         g_string_append(str, "with libz ");
125 #ifdef ZLIB_VERSION
126         g_string_append(str, ZLIB_VERSION);
127 #else /* ZLIB_VERSION */
128         g_string_append(str, "(version unknown)");
129 #endif /* ZLIB_VERSION */
130 #else /* HAVE_LIBZ */
131         g_string_append(str, "without libz");
132 #endif /* HAVE_LIBZ */
133
134 #ifndef _WIN32
135         /* This is UN*X-only. */
136         /* LIBCAP */
137         g_string_append(str, ", ");
138 #ifdef HAVE_LIBCAP
139         g_string_append(str, "with POSIX capabilities");
140 #ifdef _LINUX_CAPABILITY_VERSION
141         g_string_append(str, " (Linux)");
142 #endif /* _LINUX_CAPABILITY_VERSION */
143 #else /* HAVE_LIBCAP */
144         g_string_append(str, "without POSIX capabilities");
145 #endif /* HAVE_LIBCAP */
146 #endif /* _WIN32 */
147
148 #ifdef __linux__
149         /* This is a Linux-specific library. */
150         /* LIBNL */
151         g_string_append(str, ", ");
152 #if defined(HAVE_LIBNL1)
153         g_string_append(str, "with libnl 1");
154 #elif defined(HAVE_LIBNL2)
155         g_string_append(str, "with libnl 2");
156 #elif defined(HAVE_LIBNL3)
157         g_string_append(str, "with libnl 3");
158 #else /* no libnl */
159         g_string_append(str, "without libnl");
160 #endif /* libnl version */
161 #endif /* __linux__ */
162
163         /* Additional application-dependent information */
164         if (append_info)
165                 (*append_info)(str);
166         g_string_append(str, ".");
167
168         end_string(str);
169 }
170
171 static void get_mem_info(GString *str _U_)
172 {
173 #if defined(_WIN32)
174         MEMORYSTATUSEX statex;
175
176         statex.dwLength = sizeof (statex);
177
178         if(GlobalMemoryStatusEx (&statex))
179                 g_string_append_printf(str, ", with ""%" G_GINT64_MODIFIER "d" "MB of physical memory.\n", statex.ullTotalPhys/(1024*1024));
180 #endif
181
182 }
183
184 /*
185  * Get various library run-time versions, and the OS version, and append
186  * them to the specified GString.
187  */
188 void
189 get_runtime_version_info(GString *str, void (*additional_info)(GString *))
190 {
191 #ifndef _WIN32
192         gchar *lang;
193 #endif
194
195         g_string_append(str, "on ");
196
197         get_os_version_info(str);
198
199 #ifndef _WIN32
200         /* Locale */
201         if ((lang = getenv ("LANG")) != NULL)
202                 g_string_append_printf(str, ", with locale %s", lang);
203         else
204                 g_string_append(str, ", without locale");
205 #endif
206
207         /* Libpcap */
208         g_string_append(str, ", ");
209         get_runtime_pcap_version(str);
210
211         /* zlib */
212 #if defined(HAVE_LIBZ) && !defined(_WIN32)
213         g_string_append_printf(str, ", with libz %s", zlibVersion());
214 #endif
215
216         /* Additional application-dependent information */
217         if (additional_info)
218                 (*additional_info)(str);
219
220         g_string_append(str, ".");
221
222         /* CPU Info */
223         get_cpu_info(str);
224
225         /* Get info about installed memory Windows only */
226         get_mem_info(str);
227
228         /* Compiler info */
229         get_compiler_info(str);
230
231         end_string(str);
232 }
233
234 #if defined(_WIN32)
235 /*
236  * Get the major OS version.
237  */
238 /* XXX - Should this return the minor version as well, e.g. 0x00050002? */
239 guint32
240 get_os_major_version()
241 {
242         OSVERSIONINFO info;
243         info.dwOSVersionInfoSize = sizeof info;
244         if (GetVersionEx(&info)) {
245                 return info.dwMajorVersion;
246         }
247         return 0;
248 }
249 #endif
250
251 /*
252  * Editor modelines
253  *
254  * Local Variables:
255  * c-basic-offset: 8
256  * tab-width: 8
257  * indent-tabs-mode: t
258  * End:
259  *
260  * ex: set shiftwidth=8 tabstop=8 noexpandtab:
261  * :indentSize=8:tabSize=8:noTabs=false:
262  */