summary refs log tree commit diff stats
path: root/plugins/sysinfo/unix/parse.c
blob: 3059906809bf71fc35942782b560c830f8867438 (plain) (blame)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * parse.c - parsing functions for X-Sys
 * by mike9010
 * Copyright (C) 2003, 2004, 2005 Michael Shoup
 * Copyright (C) 2005, 2006, 2007 Tony Vroon
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <pci/header.h>
#include <glib.h>

#ifdef __sparc__
#include <dirent.h>
#endif

#include "pci.h"
#include "match.h"
#include "parse.h"
#include "sysinfo.h"

int xs_parse_cpu(char *model, char *vendor, double *freq)
{
#if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) || defined(__alpha__) || defined(__ia64__) || defined(__parisc__) || defined(__sparc__)
	char buffer[bsize];
#endif
	FILE *fp;

	fp = fopen("/proc/cpuinfo", "r");
	if(fp == NULL)
		return 1;

#if defined(__i386__) || defined(__x86_64__)

	while(fgets(buffer, bsize, fp) != NULL)
	{
		find_match_char(buffer, "model name", model);
		find_match_char(buffer, "vendor_id", vendor);
		find_match_double(buffer, "cpu MHz", freq);
	}

#elif defined(__powerpc__)
	{
		char *pos;

		while(fgets(buffer, bsize, fp) != NULL)
		{
			find_match_char(buffer, "cpu", model);
			find_match_char(buffer, "machine", vendor);
			find_match_double(buffer, "clock", freq);
		}
		pos = strstr(model, ",");
		if (pos != NULL)
		    *pos = '\0';
	}
#elif defined( __alpha__)

	while(fgets(buffer, bsize, fp) != NULL)
	{
		find_match_char(buffer, "cpu model", model);
		find_match_char(buffer, "system type", vendor);
		find_match_double(buffer, "cycle frequency [Hz]", freq);
	}
	*freq = *freq / 1000000;

#elif defined(__ia64__)

	while(fgets(buffer, bsize, fp) != NULL)
	{
		find_match_char(buffer, "model", model);
		find_match_char(buffer, "vendor", vendor);
		find_match_double(buffer, "cpu MHz", freq);
	}

#elif defined(__parisc__)

	while(fgets(buffer, bsize, fp) != NULL)
	{
		find_match_char(buffer, "cpu  ", model);
		find_match_char(buffer, "cpu family", vendor);
		find_match_double(buffer, "cpu MHz", freq);
	}

#elif defined(__sparc__)
    {
	    DIR *dir;
	    struct dirent *entry;
	    FILE *fp2;

	    while(fgets(buffer, bsize, fp) != NULL)
	    {
		    find_match_char(buffer, "cpu	", model);
		    find_match_char(buffer, "type	", vendor);
		    find_match_double_hex(buffer, "Cpu0ClkTck", freq);
	    }
	    *freq = *freq / 1000000;
    }
#else

    fclose(fp);
    return 1; /* Unsupported */

#endif

	fclose(fp);
	return 0;
}

gint64 xs_parse_uptime(void)
{
	char buffer[bsize];
	gint64 uptime = 0;
	FILE *fp = fopen("/proc/uptime", "r");
	if(fp == NULL)
		return 0;

	if(fgets(buffer, bsize, fp) != NULL)
		uptime = g_ascii_strtoll(buffer, NULL, 0);

	fclose(fp);

	return uptime;
}

int xs_parse_sound(char *snd_card)
{
#ifndef HAVE_LIBPCI
	return 1;
#else
	char buffer[bsize], cards[bsize] = "\0", vendor[7] = "\0", device[7] = "\0", *pos;
	u16 class = PCI_CLASS_MULTIMEDIA_AUDIO;

	FILE *fp = NULL;
	if((fp = fopen("/proc/asound/cards", "r"))== NULL)
	{
		if (pci_find_by_class(&class, vendor, device) == 0)
		{
			pci_find_fullname(snd_card, vendor, device);
			return 0;
		}
		else
			return 1;
	}


	while(fgets(buffer, bsize, fp) != NULL)
	{
		if(isdigit(buffer[0]) || isdigit(buffer[1]))
		{
			char card_buf[bsize];
			gint64 card_id = 0;
			pos = strstr(buffer, ":");
			card_id = g_ascii_strtoll(buffer, NULL, 0);
			if (card_id == 0)
				g_snprintf(card_buf, bsize, "%s", pos+2);
			else
				g_snprintf(card_buf, bsize, "%"G_GINT64_FORMAT": %s", card_id, pos+2);
			pos = strstr(card_buf, "\n");
			*pos = '\0';
			strcat(cards, card_buf);
		}
	}

	strcpy(snd_card, cards);

	fclose(fp);
	return 0;
#endif
}

int xs_parse_video(char *vid_card)
{
#ifndef HAVE_LIBPCI
	return 1;
#else
	char vendor[7] = "\0", device[7] = "\0";
	u16 class = PCI_CLASS_DISPLAY_VGA;
	if (pci_find_by_class(&class, vendor, device))
		return 1;
	else
		pci_find_fullname(vid_card, vendor, device);
	return 0;
#endif
}

int xs_parse_ether(char *ethernet_card)
{
#ifndef HAVE_LIBPCI
	return 1;
#else
	char vendor[7] = "\0", device[7] = "\0";
	u16 class = PCI_CLASS_NETWORK_ETHERNET;
	if (pci_find_by_class(&class, vendor, device))
		return 1;
	else
		pci_find_fullname(ethernet_card, vendor, device);
	return 0;
#endif
}

int xs_parse_agpbridge(char *agp_bridge)
{
#ifndef HAVE_LIBPCI
	return 1;
#else
	char vendor[7] = "\0", device[7] = "\0";
	u16 class = PCI_CLASS_BRIDGE_HOST;
	if (pci_find_by_class(&class, vendor, device))
		return 1;
	else
		pci_find_fullname(agp_bridge, vendor, device);
	return 0;
#endif
}

int xs_parse_meminfo(unsigned long long *mem_tot, unsigned long long *mem_free, int swap)
{
	FILE *fp;
	char buffer[bsize];
	unsigned long long freemem = 0, buffers = 0, cache = 0;
	*mem_tot = 0;
	*mem_free = 0;

	if((fp = fopen("/proc/meminfo", "r")) == NULL)
		return 1;

	while(fgets(buffer, bsize, fp) != NULL)
	{
		if(!swap)
		{
			find_match_ll(buffer, "MemTotal:", mem_tot);
			find_match_ll(buffer, "MemFree:", &freemem);
			find_match_ll(buffer, "Buffers:", &buffers);
			find_match_ll(buffer, "Cached:", &cache);
		}
		else
		{
			find_match_ll(buffer, "SwapTotal:", mem_tot);
			find_match_ll(buffer, "SwapFree:", mem_free);
		}
	}
	if (!swap)
	{
		*mem_free = freemem + buffers + cache;
	}
	fclose(fp);

	/* Convert to bytes */
	*mem_free *= 1000;
	*mem_tot *= 1000;
	return 0;
}

int xs_parse_distro(char *name)
{
	FILE *fp = NULL;
	char buffer[bsize], *pos = NULL;

	if((fp = fopen("/etc/redhat-release", "r")) != NULL)
		fgets(buffer, bsize, fp);
	else if((fp = fopen("/etc/mageia-release", "r")) != NULL)
		fgets(buffer, bsize, fp);
	else if((fp = fopen("/etc/slackware-version", "r")) != NULL)
		fgets(buffer, bsize, fp);
	else if((fp = fopen("/etc/mandrake-release", "r")) != NULL)
		fgets(buffer, bsize, fp);
	else if((fp = fopen("/etc/SuSE-release", "r")) != NULL)
		fgets(buffer, bsize, fp);
	else if((fp = fopen("/etc/turbolinux-release", "r")) != NULL)
		fgets(buffer, bsize, fp);
	else if((fp = fopen("/etc/arch-release", "r")) != NULL)
		g_snprintf(buffer, bsize, "ArchLinux");
	else if((fp = fopen("/etc/lsb-release", "r")) != NULL)
	{
		char id[bsize], codename[bsize], release[bsize];
		strcpy(id, "?");
		strcpy(codename, "?");
		strcpy(release, "?");
		while(fgets(buffer, bsize, fp) != NULL)
		{
			find_match_char(buffer, "DISTRIB_ID", id);
			find_match_char(buffer, "DISTRIB_CODENAME", codename);
			find_match_char(buffer, "DISTRIB_RELEASE", release);
		}
		g_snprintf(buffer, bsize, "%s \"%s\" %s", id, codename, release);
	}
	else if((fp = fopen("/etc/debian_version", "r")) != NULL)
	{
		char release[bsize];
		fgets(release, bsize, fp);
		g_snprintf(buffer, bsize, "Debian %s", release);
	}
	else if((fp = fopen("/etc/portage/make.conf", "r")) != NULL ||
			(fp = fopen("/etc/make.conf", "r")) != NULL)
	{
		char keywords[bsize];
		while(fgets(buffer, bsize, fp) != NULL)
			find_match_char(buffer, "ACCEPT_KEYWORDS", keywords);
		/* cppcheck-suppress uninitvar */
		if (strstr(keywords, "\"") == NULL)
			g_snprintf(buffer, bsize, "Gentoo Linux (stable)");
		else
			g_snprintf(buffer, bsize, "Gentoo Linux %s", keywords);
	}
	else
		g_snprintf(buffer, bsize, "Unknown Distro");
	if(fp != NULL)
		fclose(fp);

	pos=strchr(buffer, '\n');
	if(pos != NULL)
		*pos = '\0';
	strcpy(name, buffer);
	return 0;
}