|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|
||||
sysctlbyname() On FreeBSD
I recently bought a Thinkpad T61 (Didn't go for the Dell Vermaden), but all battery indicators I could find for FreeBSD eiter do not work, or don't work as I want them to, creating a simple tray application is simple, but I'm having problems with getting the sysctl.
From the commandline: % sysctl hw.acpi.battery.life 99% means its full, so now from python, from an example found here: Code:
def CheckBatt(tray): libc = ctypes.CDLL('libc.so') size = ctypes.c_uint(0) #libc.sysctlbyname("hw.acpi.battery.life", None, ctypes.byref(size), None, 0) buf = ctypes.create_string_buffer(size.value) libc.sysctlbyname("hw.acpi.battery.life", buf, ctypes.byref(size), None, 0) print buf.value return True Hm, maybe something went wrong with ctypes? Lets try from C: Code:
#include <stdio.h> #include <sys/types.h> #include <sys/sysctl.h> int main() { size_t size; int buf; size = sizeof buf; sysctlbyname("hw.acpi.battery.life", &buf, &size, NULL, 0); printf("`%s'\n", &buf); return 0; } I am not an experienced C programmer, I usually stick to stuff like python, I'm probably doing something stupid, but I can't figure out what. Also, why does the python example linked above use two sysctlbyname() calls? It seems redundant to me...
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
|
|||
What about the following?
Code:
#include <stdio.h> #include <sys/types.h> #include <sys/sysctl.h> int main(void) { int mib[4]; size_t len; char buf[BUFSIZ]; int buflen = BUFSIZ; len = sizeof(mib); if(sysctlnametomib("hw.acpi.battery.life", mib, &len) == -1) { return 1; } if(sysctl(mib, len, buf, &buflen, NULL, 0) == -1) { return 1; } printf("%s\n", buf); } |
|
|||
Let's try that again...
Code:
#include <stdio.h> #include <sys/types.h> #include <sys/sysctl.h> int main(void) { int mib[4], life; size_t miblen, lifelen; miblen = sizeof(mib); lifelen = sizeof(life); if(sysctlnametomib("hw.acpi.battery.life", mib, &miblen) == -1) { return 1; } if(sysctl(mib, miblen, &life, &lifelen, NULL, 0) == -1) { return 1; } printf("%d\n", life); return 0; } |
|
|||
The following, from your original post.. should work.
Code:
#include <stdio.h> #include <sys/types.h> #include <sys/sysctl.h> int main() { size_t size; int buf; size = sizeof buf; sysctlbyname("hw.acpi.battery.life", &buf, &size, NULL, 0); printf("%d\n", buf); return 0; } EDIT: Fixed the above code, should work now. Last edited by BSDfan666; 11th January 2009 at 11:11 PM. |
|
||||
Ok, the problem with the C example was the wrong printf() statement, as anemos pointed out.
The problem with the python example was that I used the wrong type for buf, string instead of int. For those interested, a working python example: Code:
import ctypes libc = ctypes.CDLL('libc.so') size = ctypes.c_size_t() buf = ctypes.c_int() size.value = ctypes.sizeof(buf) libc.sysctlbyname("hw.acpi.battery.life", ctypes.byref(buf), ctypes.byref(size), None, 0) print buf.value
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
|
||||
If you're going to play with external applications: you should use the subprocess module, not commands. I believe the getstatusoutput() function was also moved to subprocess in py26, and commands is kaput in py30; in favor of subprocess.
__________________
My Journal Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''. |
|
||||
For more complex external commands I usually do, but I just thought subprocess was overkill for this.
Didn't know commands was gone in 3 thought, thanks.
__________________
"The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words." -Philip K. Dick |
|
||||
@roddierod
That's what I did, but using ctypes would be a better since it doesn't spawn a shell.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
Thread Tools | |
Display Modes | |
|
|