]> hackdaworld.org Git - my-code/acpi.git/commitdiff
initial checkin - doesnt work by now! master origin
authorhackbard <hackbard>
Wed, 18 Aug 2004 09:06:40 +0000 (09:06 +0000)
committerhackbard <hackbard>
Wed, 18 Aug 2004 09:06:40 +0000 (09:06 +0000)
print_info.c [new file with mode: 0644]

diff --git a/print_info.c b/print_info.c
new file mode 100644 (file)
index 0000000..8e7ce80
--- /dev/null
@@ -0,0 +1,62 @@
+/* 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;
+}