From: hackbard Date: Wed, 18 Aug 2004 09:06:40 +0000 (+0000) Subject: initial checkin - doesnt work by now! X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=my-code%2Facpi.git initial checkin - doesnt work by now! --- 49c3a5c79c83695ee603c9d2b890d68cfe7b9619 diff --git a/print_info.c b/print_info.c new file mode 100644 index 0000000..8e7ce80 --- /dev/null +++ b/print_info.c @@ -0,0 +1,62 @@ +/* write battery/thermal status to a file ... */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#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; +}