From cb9fe43a946ea1a285e34793659996dfa620f8b8 Mon Sep 17 00:00:00 2001 From: Jakub Filo Date: Tue, 7 Sep 2021 22:53:41 +0200 Subject: [PATCH] Archiving statusbar C sources --- .../statusbar items - C sources/README.md | 1 + examples/statusbar items - C sources/cpu.c | 18 +++ .../statusbar items - C sources/hoststat.c | 104 ++++++++++++++++++ .../statusbar items - C sources/loadavg.c | 16 +++ examples/statusbar items - C sources/mem.c | 12 ++ 5 files changed, 151 insertions(+) create mode 100644 examples/statusbar items - C sources/README.md create mode 100644 examples/statusbar items - C sources/cpu.c create mode 100644 examples/statusbar items - C sources/hoststat.c create mode 100644 examples/statusbar items - C sources/loadavg.c create mode 100644 examples/statusbar items - C sources/mem.c diff --git a/examples/statusbar items - C sources/README.md b/examples/statusbar items - C sources/README.md new file mode 100644 index 0000000..be893f6 --- /dev/null +++ b/examples/statusbar items - C sources/README.md @@ -0,0 +1 @@ +These small C programs were supposed to make statusbar in i3wm keep system load down to even 0.00 on my very old machine, where even calling a "cat" or "grep" repeatedly every few seconds caused measurable system load. diff --git a/examples/statusbar items - C sources/cpu.c b/examples/statusbar items - C sources/cpu.c new file mode 100644 index 0000000..efef4ef --- /dev/null +++ b/examples/statusbar items - C sources/cpu.c @@ -0,0 +1,18 @@ +#include +#include + +int main(void) +{ + long double a[4], b[4], loadavg; + FILE *fp; + fp = fopen("/proc/stat","r"); + fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&a[0],&a[1],&a[2],&a[3]); + fclose(fp); + sleep(1); + fp = fopen("/proc/stat","r"); + fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&b[0],&b[1],&b[2],&b[3]); + fclose(fp); + loadavg = ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3])); + printf("%*d\n",3,(int)(loadavg*100)); + return(0); +} diff --git a/examples/statusbar items - C sources/hoststat.c b/examples/statusbar items - C sources/hoststat.c new file mode 100644 index 0000000..7e2ab01 --- /dev/null +++ b/examples/statusbar items - C sources/hoststat.c @@ -0,0 +1,104 @@ +#include +#include + +void func1() +{ + FILE *fp; + char string[255]; + fp = fopen("/media/usb/lbu.last","r"); + if(fp == NULL){ + printf("lbu down"); + } + char string2[255]; + fscanf(fp,"%*s %*s %*s %s %*s %*s %s", string, string2); + fclose(fp); + printf(" LBU commit %s %-10s \n",string,string2); +} + +void func2() +{ + char string[3][5]; + FILE *fp; + fp = fopen("/proc/loadavg","r"); + fscanf(fp,"%s %s %s", string[1],string[2],string[3]); + fclose(fp); + printf(" Load averages %s %s %s \n",string[1],string[2],string[3]); +} + +void func3() +{ + char string[3][5]; + FILE *fp; + fp = fopen("/proc/loadavg","r"); + fscanf(fp,"%s %s %s", string[1],string[2],string[3]); + fclose(fp); + printf(" Load averages %s %s %s \n",string[1],string[2],string[3]); +} + +// main is a control loop +int main(void) +{ + FILE *fp = fopen("/tmp/hoststat.sock","r+"); + if(fp == NULL){ + printf("socket lost"); + fp = fopen("/tmp/hoststat.sock","w+"); + fclose(fp); + return 1; + } + char tmpsock[50]; + fgets(tmpsock,50,fp); + fclose(fp); + fp = fopen("/tmp/hoststat.sock","w"); + if (strcmp(tmpsock,"step0\n")==0) + { + func1(); + fprintf(fp,"step1\n"); + return 0; + } + if (strcmp(tmpsock,"step1\n")==0){ + func1(); + fprintf(fp,"step2\n"); + return 0; + } + if (strcmp(tmpsock,"step2\n")==0){ + func2(); + fprintf(fp,"step3\n"); + return 0; + } + if (strcmp(tmpsock,"step3\n")==0){ + func2(); + fprintf(fp,"step4\n"); + return 0; + } + if (strcmp(tmpsock,"step4\n")==0){ + func2(); + fprintf(fp,"step5\n"); + return 0; + } + if (strcmp(tmpsock,"step5\n")==0){ + func3(); + fprintf(fp,"step6\n"); + return 0; + } + if (strcmp(tmpsock,"step6\n")==0){ + func3(); + fprintf(fp,"step7\n"); + return 0; + } + if (strcmp(tmpsock,"step7\n")==0){ + func3(); + fprintf(fp,"step8\n"); + return 0; + } + if (strcmp(tmpsock,"step8\n")==0){ + func1(); + fprintf(fp,"step0\n"); + return 0; + } + else + { + printf("Starting up"); + fprintf(fp,"step0\n"); + return 0; + } +} diff --git a/examples/statusbar items - C sources/loadavg.c b/examples/statusbar items - C sources/loadavg.c new file mode 100644 index 0000000..45f7a4d --- /dev/null +++ b/examples/statusbar items - C sources/loadavg.c @@ -0,0 +1,16 @@ +#include + +int main (void) +{ + float loadavg; + FILE *fp; + fp = fopen("/proc/loadavg","r"); + fscanf(fp,"%f",&loadavg); + fclose(fp); + printf("%.2f",loadavg); + if ( loadavg > 2.0 ) + { + printf("\a"); + } + return(0); +} diff --git a/examples/statusbar items - C sources/mem.c b/examples/statusbar items - C sources/mem.c new file mode 100644 index 0000000..6135db4 --- /dev/null +++ b/examples/statusbar items - C sources/mem.c @@ -0,0 +1,12 @@ +#include + +int main(void) +{ + unsigned long long availmem; + FILE *fp; + fp = fopen("/proc/meminfo","r"); + fscanf(fp,"%*s %*s %*s %*s %*s %*s %*s %llu", &availmem); + fclose(fp); + printf("%llu MB free\n",availmem/1024); + return(0); +}