Skip to content

Commit

Permalink
code refactoring, move variables to top and reformat ifdefs
Browse files Browse the repository at this point in the history
  • Loading branch information
woodruffw committed Dec 7, 2014
1 parent 314f51d commit 25c894d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 81 deletions.
100 changes: 50 additions & 50 deletions src/plat/bsd/detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
void detect_distro(char *str)
{
struct utsname distro_info;

uname(&distro_info);
snprintf(str, MAX_STRLEN, "%s", distro_info.sysname);

Expand All @@ -52,6 +53,7 @@ void detect_distro(char *str)
void detect_arch(char *str)
{
struct utsname arch_info;

uname(&arch_info);
safe_strncpy(str, arch_info.machine, MAX_STRLEN);

Expand All @@ -66,10 +68,10 @@ void detect_host(char *str)
{
char *given_user = "Unknown";
char given_host[MAX_STRLEN] = "Unknown";
struct utsname host_info;

given_user = getlogin();

struct utsname host_info;
uname(&host_info);
safe_strncpy(given_host, host_info.nodename, MAX_STRLEN);

Expand All @@ -85,6 +87,7 @@ void detect_host(char *str)
void detect_kernel(char *str)
{
struct utsname kern_info;

uname(&kern_info);
snprintf(str, MAX_STRLEN, "%s", kern_info.release);

Expand All @@ -98,39 +101,36 @@ void detect_kernel(char *str)
void detect_uptime(char *str)
{
long uptime = 0;

#if !defined(__NetBSD__)
long currtime = 0, boottime = 0;
#endif

#if !defined(__NetBSD__)
long currtime = 0, boottime = 0;
#endif
FILE *uptime_file;

int secs = 0;
int mins = 0;
int hrs = 0;
int days = 0;

#if defined(__NetBSD__)
uptime_file = popen("cut -d ' ' -f 1 < /proc/uptime", "r");
fscanf(uptime_file, "%ld", &uptime);
pclose(uptime_file);
#elif defined(__FreeBSD__) || defined(__DragonFly__)
uptime_file = popen("sysctl -n kern.boottime | cut -d '=' -f 2 | cut -d ',' -f 1", "r");
fscanf(uptime_file, "%ld", &boottime); /* get boottime in secs */
pclose(uptime_file);
#if defined(__NetBSD__)
uptime_file = popen("cut -d ' ' -f 1 < /proc/uptime", "r");
fscanf(uptime_file, "%ld", &uptime);
pclose(uptime_file);
#elif defined(__FreeBSD__) || defined(__DragonFly__)
uptime_file = popen("sysctl -n kern.boottime | cut -d '=' -f 2 | cut -d ',' -f 1", "r");
fscanf(uptime_file, "%ld", &boottime); /* get boottime in secs */
pclose(uptime_file);

currtime = time(NULL);
currtime = time(NULL);

uptime = currtime - boottime;
#elif defined(__OpenBSD__)
uptime_file = popen("sysctl -n kern.boottime", "r");
fscanf(uptime_file, "%ld", &boottime); /* get boottime in secs */
pclose(uptime_file);
uptime = currtime - boottime;
#elif defined(__OpenBSD__)
uptime_file = popen("sysctl -n kern.boottime", "r");
fscanf(uptime_file, "%ld", &boottime); /* get boottime in secs */
pclose(uptime_file);

currtime = time(NULL);
currtime = time(NULL);

uptime = currtime - boottime;
#endif
uptime = currtime - boottime;
#endif

split_uptime(uptime, &secs, &mins, &hrs, &days);

Expand All @@ -149,23 +149,25 @@ void detect_uptime(char *str)
void detect_pkgs(char *str, const char *distro_str)
{
int packages = 0;
#if defined(__FreeBSD__) || defined(__OpenBSD__)
FILE *pkgs_file;
#endif

#if defined(__FreeBSD__)
FILE *pkgs_file;
pkgs_file = popen("pkg info | wc -l", "r");
fscanf(pkgs_file, "%d", &packages);
pclose(pkgs_file);
#elif defined(__OpenBSD__)
FILE *pkgs_file;
pkgs_file = popen("pkg_info | wc -l", "r");
fscanf(pkgs_file, "%d", &packages);
pclose(pkgs_file);
#else
safe_strncpy(str, "Not Found", MAX_STRLEN);

if (error)
ERROR_OUT("Error: ", "Could not find packages on current OS.");
#endif
#if defined(__FreeBSD__)
pkgs_file = popen("pkg info | wc -l", "r");
fscanf(pkgs_file, "%d", &packages);
pclose(pkgs_file);
#elif defined(__OpenBSD__)
pkgs_file = popen("pkg_info | wc -l", "r");
fscanf(pkgs_file, "%d", &packages);
pclose(pkgs_file);
#else
safe_strncpy(str, "Not Found", MAX_STRLEN);

if (error)
ERROR_OUT("Error: ", "Could not find packages on current OS.");
#endif

snprintf(str, MAX_STRLEN, "%d", packages);

Expand All @@ -180,15 +182,15 @@ void detect_cpu(char *str)
{
FILE *cpu_file;

#if defined(__NetBSD__)
cpu_file = popen("awk 'BEGIN{FS=\":\"} /model name/ { print $2; exit }' /proc/cpuinfo | sed -e 's/ @/\\n/' -e 's/^ *//g' -e 's/ *$//g' | head -1 | tr -d '\\n'", "r");
fgets(str, MAX_STRLEN, cpu_file);
pclose(cpu_file);
#else
cpu_file = popen("sysctl -n hw.model | tr -d '\\n'", "r");
fgets(str, MAX_STRLEN, cpu_file);
pclose(cpu_file);
#endif
#if defined(__NetBSD__)
cpu_file = popen("awk 'BEGIN{FS=\":\"} /model name/ { print $2; exit }' /proc/cpuinfo | sed -e 's/ @/\\n/' -e 's/^ *//g' -e 's/ *$//g' | head -1 | tr -d '\\n'", "r");
fgets(str, MAX_STRLEN, cpu_file);
pclose(cpu_file);
#else
cpu_file = popen("sysctl -n hw.model | tr -d '\\n'", "r");
fgets(str, MAX_STRLEN, cpu_file);
pclose(cpu_file);
#endif

return;
}
Expand Down Expand Up @@ -239,7 +241,6 @@ void detect_disk(char *str)
void detect_mem(char *str)
{
FILE *mem_file;

long long total_mem = 0;

mem_file = popen("sysctl -n hw.physmem", "r");
Expand Down Expand Up @@ -433,7 +434,6 @@ void detect_wm_theme(char *str, const char *wm_str)
void detect_gtk(char *str)
{
FILE *gtk_file;

char gtk2_str[MAX_STRLEN] = "Unknown";
char gtk3_str[MAX_STRLEN] = "Unknown";
char gtk_icons_str[MAX_STRLEN] = "Unknown";
Expand Down
39 changes: 20 additions & 19 deletions src/plat/darwin/detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,29 @@
*/
void detect_distro(char *str)
{
#if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1070
int ver_maj, ver_min, ver_bug;
#else
FILE *distro_file;
#endif

/*
Use this:
https://www.opensource.apple.com/source/DarwinTools/DarwinTools-1/sw_vers.c
*/
#if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1070
int ver_maj, ver_min, ver_bug;
Gestalt(gestaltSystemVersionMajor, (SInt32 *) &ver_maj);
Gestalt(gestaltSystemVersionMinor, (SInt32 *) &ver_min);
Gestalt(gestaltSystemVersionBugFix, (SInt32 *) &ver_bug);

snprintf(str, MAX_STRLEN, "Max OS X %d.%d.%d", ver_maj, ver_min, ver_bug);
#else
FILE *distro_file;
#if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1070
Gestalt(gestaltSystemVersionMajor, (SInt32 *) &ver_maj);
Gestalt(gestaltSystemVersionMinor, (SInt32 *) &ver_min);
Gestalt(gestaltSystemVersionBugFix, (SInt32 *) &ver_bug);

distro_file = popen("sw_vers -productVersion | tr -d '\\n'", "r");
fgets(distro_name_str, MAX_STRLEN, distro_file);
pclose(distro_file);
snprintf(str, MAX_STRLEN, "Max OS X %d.%d.%d", ver_maj, ver_min, ver_bug);
#else
distro_file = popen("sw_vers -productVersion | tr -d '\\n'", "r");
fgets(distro_name_str, MAX_STRLEN, distro_file);
pclose(distro_file);

snprintf(str, MAX_STRLEN, "Mac OS X %s", distro_name_str);
#endif
snprintf(str, MAX_STRLEN, "Mac OS X %s", distro_name_str);
#endif

return;
}
Expand All @@ -70,6 +73,7 @@ void detect_distro(char *str)
void detect_arch(char *str)
{
struct utsname arch_info;

uname(&arch_info);
safe_strncpy(str, arch_info.machine, MAX_STRLEN);

Expand All @@ -84,10 +88,9 @@ void detect_host(char *str)
{
char *given_user = "Unknown";
char given_host[MAX_STRLEN] = "Unknown";
struct utsname host_info;

given_user = getlogin();

struct utsname host_info;
uname(&host_info);
safe_strncpy(given_host, host_info.nodename, MAX_STRLEN);

Expand All @@ -103,6 +106,7 @@ void detect_host(char *str)
void detect_kernel(char *str)
{
struct utsname kern_info;

uname(&kern_info);
snprintf(str, MAX_STRLEN, "%s %s", kern_info.sysname, kern_info.release);

Expand All @@ -116,7 +120,6 @@ void detect_kernel(char *str)
void detect_uptime(char *str)
{
long long uptime = 0;

int secs = 0;
int mins = 0;
int hrs = 0;
Expand Down Expand Up @@ -210,7 +213,6 @@ void detect_gpu(char *str)
void detect_disk(char *str)
{
struct statfs disk_info;

long disk_total = 0, disk_used = 0, disk_percentage = 0;

if (!(statfs(getenv("HOME"), &disk_info)))
Expand All @@ -235,7 +237,6 @@ void detect_disk(char *str)
void detect_mem(char *str)
{
FILE *mem_file;

long long total_mem = 0;
long long free_mem = 0;
long long used_mem = 0;
Expand Down
15 changes: 8 additions & 7 deletions src/plat/sun/detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
void detect_distro(char *str)
{
struct utsname distro_info;

uname(&distro_info);
snprintf(str, MAX_STRLEN, "%s", distro_info.sysname);

Expand All @@ -50,6 +51,7 @@ void detect_distro(char *str)
void detect_arch(char *str)
{
struct utsname arch_info;

uname(&arch_info);
safe_strncpy(str, arch_info.machine, MAX_STRLEN);

Expand All @@ -64,10 +66,9 @@ void detect_host(char *str)
{
char *given_user = "Unknown";
char given_host[MAX_STRLEN] = "Unknown";

given_user = getlogin(); /* getlogin is apparently buggy on linux, so this might be changed */

struct utsname host_info;

given_user = getlogin();
uname(&host_info);
safe_strncpy(given_host, host_info.nodename, MAX_STRLEN);

Expand All @@ -83,6 +84,7 @@ void detect_host(char *str)
void detect_kernel(char *str)
{
struct utsname kern_info;

uname(&kern_info);
snprintf(str, MAX_STRLEN, "%s", kern_info.release);

Expand All @@ -96,14 +98,13 @@ void detect_kernel(char *str)
void detect_uptime(char *str)
{
long uptime = 0, currtime = 0, boottime = 0;

int secs = 0;
int mins = 0;
int hrs = 0;
int days = 0;
struct utmpx *ent;

currtime = time(NULL);
struct utmpx *ent;

while ((ent = getutxent()))
{
Expand Down Expand Up @@ -289,12 +290,12 @@ void detect_res(char *str)
{
int width = 0;
int height = 0;

Display *disp;
Screen *screen;

if ((disp = XOpenDisplay(NULL)))
{
Screen *screen = XDefaultScreenOfDisplay(disp);
screen = XDefaultScreenOfDisplay(disp);
width = WidthOfScreen(screen);
height = HeightOfScreen(screen);
snprintf(str, MAX_STRLEN, "%dx%d", width, height);
Expand Down
Loading

0 comments on commit 25c894d

Please sign in to comment.