+/* write battery/thermal status to a file ... */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/select.h>
+#include <unistd.h>
+
+#define BATFILE "/proc/acpi/battery/BAT0/state"
+
+int main(int argc,char **argv) {
+
+ int msgfd;
+ int batfd;
+ char batfile[64];
+ struct timeval to;
+ char buf[512];
+ char buf_o[512];
+ int i;
+
+ if(argc<2) {
+ puts("file to read bat status from not specified,");
+ printf("using %s.\n",BATFILE);
+ strcpy(batfile,BATFILE);
+ }
+ else {
+ printf("using %s ...\n",argv[1]);
+ strncpy(batfile,argv[1],64-1);
+ }
+
+ if((batfd=open(batfile,O_RDONLY))<0) {
+ printf("unable to open file %s\n",batfile);
+ return -1;
+ }
+
+ if((msgfd=open("/var/log/battery",O_WRONLY))<0) {
+ puts("unable to open file /var/log/battery");
+ return -1;
+ }
+
+ to.tv_sec=2;
+ to.tv_usec=0;
+
+ while(1) {
+ if(select(1,NULL,NULL,NULL,&to)<0) {
+ puts("select call failed");
+ return -1;
+ }
+
+ i=read(batfd,buf,512);
+ lseek(batfd,0,SEEK_SET);
+ buf[i]='\0';
+ if(strncmp(buf_o,buf,i)) {
+ dprintf(msgfd,"%s",buf);
+ strcpy(buf_o,buf);
+ }
+ }
+
+ return 1;
+}