From 07681e184be7214479ac8ef553617e26a20aab06 Mon Sep 17 00:00:00 2001 From: su8 Date: Sat, 1 Sep 2018 22:40:21 +0200 Subject: [PATCH] Add new variable sysctlbyname for FreeBSD --- src/core.cc | 6 ++++++ src/freebsd.cc | 28 ++++++++++++++++++++++++++-- src/freebsd.h | 30 +++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/core.cc b/src/core.cc index 7d9697e9..07a6e719 100644 --- a/src/core.cc +++ b/src/core.cc @@ -837,6 +837,12 @@ struct text_object *construct_text_object(char *s, const char *arg, long line, END OBJ(mouse_speed, 0) obj->callbacks.print = &print_mouse_speed; #endif /* BUILD_X11 */ +#ifdef __FreeBSD__ + END OBJ(sysctlbyname, 0) obj->data.s = STRNDUP_ARG; + obj->callbacks.print = &print_sysctlbyname; + obj->callbacks.free = &gen_free_opaque; +#endif /* __FreeBSD__ */ + END OBJ(password, 0) obj->data.s = strndup(arg ? arg : "20", text_buffer_size.get(*state)); obj->callbacks.print = &print_password; obj->callbacks.free = &gen_free_opaque; diff --git a/src/freebsd.cc b/src/freebsd.cc index 76490414..4a122724 100644 --- a/src/freebsd.cc +++ b/src/freebsd.cc @@ -25,7 +25,6 @@ * along with this program. If not, see . * */ - #include "config.h" #include @@ -34,9 +33,9 @@ #include #include #include -#include #include #include +#include #include #include @@ -704,3 +703,28 @@ int get_entropy_poolsize(unsigned int *val) { (void)val; return 1; } + +void print_sysctlbyname(struct text_object *obj, char *p, unsigned int p_max_size) { + u_int val[3] = {0}; + char buf[256] = {""}; + size_t len = sizeof(val); + size_t len2 = sizeof(buf); + + if (0 == strcmp(obj->data.s, "")) { + snprintf(p, p_max_size, "%s", "sysctlbyname takes an argument"); + return; + } + + if (0 != sysctlbyname(obj->data.s, &val, &len, NULL, 0)) { + if (0 != sysctlbyname(obj->data.s, &buf, &len2, NULL, 0)) { + snprintf(p, p_max_size, "%s", ""); + return; + } + } + + if (0 != strcmp(buf, "")) { + snprintf(p, p_max_size, "%s", buf); + } else { + snprintf(p, p_max_size, "%lu", (unsigned long)val[0]); + } +} diff --git a/src/freebsd.h b/src/freebsd.h index b0987540..7680e1b1 100644 --- a/src/freebsd.h +++ b/src/freebsd.h @@ -1,4 +1,31 @@ -/* */ +/* + * + * Conky, a system monitor, based on torsmo + * + * Any original torsmo code is licensed under the BSD license + * + * All code written since the fork of torsmo is licensed under the GPL + * + * Please see COPYING for details + * + * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen + * Copyright (c) 2005-2018 Brenden Matthews, Philip Kovacs, et. al. + * (see AUTHORS) + * All rights reserved. + * + * 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 3 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, see . + * + */ #ifndef FREEBSD_H_ #define FREEBSD_H_ @@ -16,5 +43,6 @@ int get_entropy_avail(unsigned int *); int get_entropy_poolsize(unsigned int *); +void print_sysctlbyname(struct text_object *, char *, unsigned int); #endif /*FREEBSD_H_*/