]> hackdaworld.org Git - my-code/acpi.git/blob - print_info.c
initial checkin - doesnt work by now!
[my-code/acpi.git] / print_info.c
1 /* write battery/thermal status to a file ... */
2
3 #define _GNU_SOURCE
4 #include <stdio.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <sys/select.h>
9 #include <unistd.h>
10
11 #define BATFILE "/proc/acpi/battery/BAT0/state"
12
13 int main(int argc,char **argv) {
14
15         int msgfd;
16         int batfd;
17         char batfile[64];
18         struct timeval to;
19         char buf[512];
20         char buf_o[512];
21         int i;
22
23         if(argc<2) {
24                 puts("file to read bat status from not specified,");
25                 printf("using %s.\n",BATFILE);
26                 strcpy(batfile,BATFILE);
27         }
28         else {
29                 printf("using %s ...\n",argv[1]);
30                 strncpy(batfile,argv[1],64-1);
31         }
32
33         if((batfd=open(batfile,O_RDONLY))<0) {
34                 printf("unable to open file %s\n",batfile);
35                 return -1;
36         }
37
38         if((msgfd=open("/var/log/battery",O_WRONLY))<0) {
39                 puts("unable to open file /var/log/battery");
40                 return -1;
41         }
42
43         to.tv_sec=2;
44         to.tv_usec=0;
45
46         while(1) {
47                 if(select(1,NULL,NULL,NULL,&to)<0) {
48                         puts("select call failed");
49                         return -1;
50                 }
51
52                 i=read(batfd,buf,512);
53                 lseek(batfd,0,SEEK_SET);
54                 buf[i]='\0';
55                 if(strncmp(buf_o,buf,i)) {
56                         dprintf(msgfd,"%s",buf);
57                         strcpy(buf_o,buf);
58                 }
59         }
60
61         return 1;
62 }