diff --git a/.gitignore b/.gitignore index bf100ce..2a68601 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ a.out *.o fake-values +essai diff --git a/Makefile b/Makefile index a8c0bbc..30b1d1c 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,10 @@ CC = gcc +essai: essai.c funcs.o Makefile + gcc -Wall $< funcs.o -o $@ + + funcs.o: funcs.c funcs.h Makefile gcc -Wall -c $< diff --git a/essai.c b/essai.c new file mode 100644 index 0000000..0ad7c09 --- /dev/null +++ b/essai.c @@ -0,0 +1,38 @@ +/* + * essai.c + */ + +#include +#include +#include + +#include "funcs.h" + +int verbosity; + +/* --------------------------------------------------------------- */ + +int main(int argc, char *argv[]) +{ +int opt, foo; +int type = 0; +double loads[3]; + +while ((opt = getopt(argc, argv, "vst:")) != -1) { + switch (opt) { + case 'v': verbosity++; break; + case 's': srand(getpid()); break; + case 't': type = atoi(optarg); break; + default: break; + } + } + +foo = get_loadavg(loads); +if (foo) fprintf(stderr, "get loadavg -> %d\n", foo); + +printf("%f %f %f %f\n", dtime(), loads[0], loads[1], loads[2]); + +return 0; +} + +/* --------------------------------------------------------------- */ diff --git a/fake-values.c b/fake-values.c index 8572d7d..292e8f3 100644 --- a/fake-values.c +++ b/fake-values.c @@ -1,5 +1,5 @@ /* - * fake-values.c + * fake-values.c */ #include @@ -30,7 +30,6 @@ if (verbosity > 1) { fprintf(stderr, "fake values - %s %s\n", __DATE__, __TIME__); } - printf("%f %d\n", dtime(), random1000(type)); return 0; diff --git a/funcs.c b/funcs.c index 9270f5c..6e65490 100644 --- a/funcs.c +++ b/funcs.c @@ -48,18 +48,25 @@ return (double)tv.tv_sec + (double)tv.tv_usec / 1e6; /* --------------------------------------------------------------- */ -double get_loadavg(int foo) +int get_loadavg(double *where) { FILE *fp; +double loads[3]; +int foo; if ( ! (fp=fopen("/proc/loadavg", "r")) ) { perror("read loadavg"); - return -42.51; + return -1; } +foo = fscanf(fp, "%lf %lf %lf", loads, loads+1, loads+2); +if (3 != foo) fprintf(stderr, "%s : read %d vals\n", __func__, foo); + +memcpy(where, loads, 3 * sizeof(double)); + fclose(fp); -return -42.51; +return 0; } /* --------------------------------------------------------------- */ diff --git a/funcs.h b/funcs.h index e142ca8..29556fe 100644 --- a/funcs.h +++ b/funcs.h @@ -4,4 +4,5 @@ int random1000(int mode); double dtime(void); +int get_loadavg(double *where);