hexchat/plugins/sysinfo/unix/parse.c

358 lines
8.3 KiB
C
Raw Permalink Normal View History

2012-10-21 08:44:29 +04:00
/*
* 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
2012-12-23 23:36:54 +04:00
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
2012-10-21 08:44:29 +04:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_LIBPCI
#include <pci/header.h>
#include "pci.h"
#endif
#include <glib.h>
2012-10-21 08:44:29 +04:00
#ifdef __sparc__
#include <dirent.h>
#endif
2012-10-21 08:44:29 +04:00
#include "match.h"
#include "parse.h"
#include "sysinfo.h"
2012-10-21 08:44:29 +04:00
int xs_parse_cpu(char *model, char *vendor, double *freq)
2012-10-21 08:44:29 +04:00
{
#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");
2012-10-21 08:44:29 +04:00
if(fp == NULL)
return 1;
#if defined(__i386__) || defined(__x86_64__)
2012-10-21 08:44:29 +04:00
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__)
2012-10-21 08:44:29 +04:00
{
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';
2012-10-21 08:44:29 +04:00
}
#elif defined( __alpha__)
2012-10-21 08:44:29 +04:00
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__)
2012-10-21 08:44:29 +04:00
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__)
2012-10-21 08:44:29 +04:00
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
2012-10-21 08:44:29 +04:00
fclose(fp);
return 0;
}
2015-02-22 04:52:43 +03:00
gint64 xs_parse_uptime(void)
2012-10-21 08:44:29 +04:00
{
char buffer[bsize];
2015-02-22 04:52:43 +03:00
gint64 uptime = 0;
2012-10-21 08:44:29 +04:00
FILE *fp = fopen("/proc/uptime", "r");
if(fp == NULL)
return 0;
2012-10-21 08:44:29 +04:00
if(fgets(buffer, bsize, fp) != NULL)
2015-02-22 05:01:14 +03:00
uptime = g_ascii_strtoll(buffer, NULL, 0);
2012-10-21 08:44:29 +04:00
fclose(fp);
return uptime;
2012-10-21 08:44:29 +04:00
}
int xs_parse_sound(char *snd_card)
{
#ifndef HAVE_LIBPCI
return 1;
#else
2012-10-21 08:44:29 +04:00
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;
}
2012-10-21 08:44:29 +04:00
else
return 1;
}
2012-10-21 08:44:29 +04:00
while(fgets(buffer, bsize, fp) != NULL)
{
if(isdigit(buffer[0]) || isdigit(buffer[1]))
{
char card_buf[bsize];
2015-02-22 05:01:14 +03:00
gint64 card_id = 0;
2012-10-21 08:44:29 +04:00
pos = strstr(buffer, ":");
2015-02-22 05:01:14 +03:00
card_id = g_ascii_strtoll(buffer, NULL, 0);
2012-10-21 08:44:29 +04:00
if (card_id == 0)
2014-12-18 02:49:59 +03:00
g_snprintf(card_buf, bsize, "%s", pos+2);
2012-10-21 08:44:29 +04:00
else
2015-02-22 05:01:14 +03:00
g_snprintf(card_buf, bsize, "%"G_GINT64_FORMAT": %s", card_id, pos+2);
2012-10-21 08:44:29 +04:00
pos = strstr(card_buf, "\n");
*pos = '\0';
strcat(cards, card_buf);
}
}
strcpy(snd_card, cards);
2012-10-21 08:44:29 +04:00
fclose(fp);
return 0;
#endif
2012-10-21 08:44:29 +04:00
}
int xs_parse_video(char *vid_card)
{
#ifndef HAVE_LIBPCI
return 1;
#else
2012-10-21 08:44:29 +04:00
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
2012-10-21 08:44:29 +04:00
}
int xs_parse_ether(char *ethernet_card)
{
#ifndef HAVE_LIBPCI
return 1;
#else
2012-10-21 08:44:29 +04:00
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
2012-10-21 08:44:29 +04:00
}
int xs_parse_agpbridge(char *agp_bridge)
{
#ifndef HAVE_LIBPCI
return 1;
#else
2012-10-21 08:44:29 +04:00
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
2012-10-21 08:44:29 +04:00
}
int xs_parse_meminfo(unsigned long long *mem_tot, unsigned long long *mem_free, int swap)
{
FILE *fp;
char buffer[bsize];
2012-10-21 08:44:29 +04:00
unsigned long long freemem = 0, buffers = 0, cache = 0;
*mem_tot = 0;
*mem_free = 0;
2012-10-21 08:44:29 +04:00
if((fp = fopen("/proc/meminfo", "r")) == NULL)
return 1;
2012-10-21 08:44:29 +04:00
while(fgets(buffer, bsize, fp) != NULL)
{
if(!swap)
{
2012-10-21 08:44:29 +04:00
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
{
2012-10-21 08:44:29 +04:00
find_match_ll(buffer, "SwapTotal:", mem_tot);
find_match_ll(buffer, "SwapFree:", mem_free);
}
}
if (!swap)
{
2012-10-21 08:44:29 +04:00
*mem_free = freemem + buffers + cache;
}
fclose(fp);
/* Convert to bytes */
*mem_free *= 1000;
*mem_tot *= 1000;
return 0;
2012-10-21 08:44:29 +04:00
}
static void strip_quotes(char *string)
{
2018-09-01 20:01:30 +03:00
size_t len = strlen(string);
if (string[len - 1] == '"')
string[--len] = '\0';
2018-09-01 20:01:30 +03:00
if (string[0] == '"')
memmove(string, string + 1, len);
}
2012-10-21 08:44:29 +04:00
int xs_parse_distro(char *name)
{
FILE *fp = NULL;
char buffer[bsize], *pos = NULL;
2013-08-26 11:58:36 +04:00
if((fp = fopen("/etc/redhat-release", "r")) != NULL)
2012-10-21 08:44:29 +04:00
fgets(buffer, bsize, fp);
2012-10-21 10:11:24 +04:00
else if((fp = fopen("/etc/mageia-release", "r")) != NULL)
fgets(buffer, bsize, fp);
2012-10-21 08:44:29 +04:00
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)
2014-12-18 02:49:59 +03:00
g_snprintf(buffer, bsize, "ArchLinux");
2013-08-26 11:58:36 +04:00
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);
}
2014-12-18 02:49:59 +03:00
g_snprintf(buffer, bsize, "%s \"%s\" %s", id, codename, release);
2013-08-26 11:58:36 +04:00
}
else if((fp = fopen("/etc/debian_version", "r")) != NULL)
{
char release[bsize];
fgets(release, bsize, fp);
2014-12-18 02:49:59 +03:00
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 if((fp = fopen("/etc/os-release", "r")) != NULL)
{
char name[bsize], version[bsize];
strcpy(name, "?");
strcpy(version, "?");
while(fgets(buffer, bsize, fp) != NULL)
{
find_match_char(buffer, "NAME=", name);
find_match_char(buffer, "VERSION=", version);
}
strip_quotes(name);
strip_quotes(version);
g_snprintf(buffer, bsize, "%s %s", name, version);
}
2012-10-21 08:44:29 +04:00
else
2014-12-18 02:49:59 +03:00
g_snprintf(buffer, bsize, "Unknown Distro");
if(fp != NULL)
fclose(fp);
2012-10-21 08:44:29 +04:00
pos=strchr(buffer, '\n');
if(pos != NULL)
*pos = '\0';
2012-10-21 08:44:29 +04:00
strcpy(name, buffer);
return 0;
}