Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   (View Single Post)  
Old 11th January 2009
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
Join Date: Apr 2008
Location: Dutchman living in the UK
Posts: 2,198
Default 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
hw.acpi.battery.life: 99

99% means its full, so now from python, from an example found here:

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
output: `c'

Hm, maybe something went wrong with ctypes? Lets try from C:

#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>

        size_t size;
        int buf;
        size = sizeof buf;

        sysctlbyname("hw.acpi.battery.life", &buf, &size, NULL, 0);

        printf("`%s'\n", &buf);
        return 0;
Output: `c' ... So the problem isn't in ctypes, but probably in my use of sysctlbyname() (?)

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.
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 08:15 AM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick