From 1b5dd78a4ff7c866b7a21d72bd9245505fe1d826 Mon Sep 17 00:00:00 2001 From: fadhil riyanto Date: Sun, 18 Aug 2024 10:27:18 +0700 Subject: test file Signed-off-by: fadhil riyanto --- bat0.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ inotify.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 bat0.c create mode 100644 inotify.c diff --git a/bat0.c b/bat0.c new file mode 100644 index 0000000..316a29a --- /dev/null +++ b/bat0.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +volatile int need_exit = 0; + +static __off_t getfile_size(int fd) +{ + struct stat st; + int ret; + + ret = fstat(fd, &st); + if (ret == -1) { + perror("fstat"); + return -1; + } + + return st.st_size; +} + +static int open_bat0(const char* bat0dir) +{ + return open(bat0dir, O_RDONLY); +} + +static char* read_bat0(int fd) +{ + off_t size = getfile_size(fd); + char *buf = (char*)malloc(size); + memset(buf, '\0', size); + read(fd, buf, size); + return buf; +} + +static int close_bat0(int fd) +{ + return close(fd); +} + +void signal_cb(int signal) +{ + need_exit = 1; +} + +int main() +{ + signal(SIGINT, signal_cb); + + while (!need_exit) { + int fd = open_bat0("/sys/class/power_supply/BAT0/capacity"); + char* bat0data = read_bat0(fd); + + printf("%s\n", bat0data); + free(bat0data); + + close_bat0(fd); + sleep(3); + } +} \ No newline at end of file diff --git a/inotify.c b/inotify.c new file mode 100644 index 0000000..6059d20 --- /dev/null +++ b/inotify.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +typedef int FD ; + +int main() { + FD filed = open("/tmp/test_inotify", O_RDWR ); + char buf[128]; + + if( !filed ) { + printf("Openfile error\n"); + exit(-1); + } + + int nbytes; + while(1) { + nbytes = read(filed, buf, 16); + printf("read %d bytes from file.\n", nbytes); + if(nbytes > 0) { + printf("%s\n", buf); // split buffer by new line. + } + sleep(1); + } + return 0; +} -- cgit v1.2.3