Browse Source

Archiving statusbar C sources

master
plantroon user 1 year ago
parent
commit
c537ce363e
  1. 1
      examples/statusbar items - C sources/README.md
  2. 18
      examples/statusbar items - C sources/cpu.c
  3. 104
      examples/statusbar items - C sources/hoststat.c
  4. 16
      examples/statusbar items - C sources/loadavg.c
  5. 12
      examples/statusbar items - C sources/mem.c

1
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.

18
examples/statusbar items - C sources/cpu.c

@ -0,0 +1,18 @@
#include <stdio.h>
#include <unistd.h>
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);
}

104
examples/statusbar items - C sources/hoststat.c

@ -0,0 +1,104 @@
#include <stdio.h>
#include <string.h>
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;
}
}

16
examples/statusbar items - C sources/loadavg.c

@ -0,0 +1,16 @@
#include <stdio.h>
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);
}

12
examples/statusbar items - C sources/mem.c

@ -0,0 +1,12 @@
#include <stdio.h>
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);
}
Loading…
Cancel
Save