Simple ACPI/PMU data extraction routine
Richard Hughes
ee21rh at surrey.ac.uk
Mon Jan 17 16:04:53 PST 2005
I've just quickly written this quick routine for getting values such as
"brightness: 5" from ACPI and PMU procfs entries.
ACPI confuses things by sometimes appending a newline, and having a
variable amount of spacing (sometimes tabs, sometimes spaces). I've not
tested it on a PMU file, but there's no reason for it not to work.
I'll paste my sample code here, to see if you can see any obv. oversights
or security faux-pas. Its just past midnight here, so I'll blame that if
the code is bad.
Again, comments as usual please.
#include <stdio.h>
/* finds specific variables in files that have
incorrect string value: nothing
correct string value: answer
incorrect string value: nothing
type entries, typically from ACPI or PMU config files
*/
int getprocfsvalue(char* filename, char* search, char* result)
{
int i, length;
char templine[128];
FILE *file;
file = fopen(filename, "r");
/* file not exists */
if (file==NULL) return -1;
/* find line */
while (fgets(templine, 128, file) != NULL) {
length = strlen(search);
if (strncmp(search, templine, length) == 0) {
/* turn first new line into null, as ACPI appends \n */
for(i=0;i<128-length;i++)
if(templine[i+length] == '\n') {
templine[i+length] = 0;
break;
}
/* eat leading whitespace, which can be \t or space */
for(i=0;i<128-length;i++) {
if((templine[i+length] != ' ') && (templine[i+length] != '\t'))
break;
}
/* copy substring into result */
strncpy(result, templine+length+i, 128);
/* shortcut loop */
break;
}
}
fclose(file);
return 1;
}
int main() {
char filename[1024];
char search[128];
char result[128];
strncpy(filename, "/proc/acpi/battery/BAT1/state", 1024);
strncpy(search, "present rate:", 128);
getprocfsvalue(filename, search, result);
printf("[%s]\n", result);
return 0;
}
--
http://www.hughsie.com/PUBLIC-KEY
_______________________________________________
hal mailing list
hal at lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/hal
More information about the Hal
mailing list