From 439ed71e6ec8e9e32d3f7f15bde24539d5258891 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Fri, 11 Oct 2024 11:45:21 +0200 Subject: [PATCH] oopsie --- Makefile | 6 +++++- ambientd.c | 11 +++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 8099614..033d775 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,8 @@ CC = gcc -CFLAGS = -Wall -Wextra -pedantic -std=c99 +CFLAGS = -Wall -Wextra -pedantic -std=c99 -O2 +PREFIX = /usr/local ambientd: ambientd.c + +install: + cp ambientd ${PREFIX}/bin/ diff --git a/ambientd.c b/ambientd.c index 6cacd5b..740e47b 100644 --- a/ambientd.c +++ b/ambientd.c @@ -29,20 +29,21 @@ float min_brightness_percent = 0.05; // returns the luminance value in percentage between 0 and 1 int get_luminance_raw (void) { + freopen(NULL, "r", luminance_fp); const char *x = fgets(STR_BUFFER, sizeof(STR_BUFFER), luminance_fp); if (x == NULL) { - fprintf(stderr, "Could not read luminance value: %s\n", strerror(errno)); return -1; } int luminance = strtol(STR_BUFFER, NULL, 10); - rewind(luminance_fp); return luminance; } +// read a string from a file and convert it to int, should only be used for +// positive numbers since negative numbers mean an error int read_int(FILE *fp) { - fflush(fp); + freopen(NULL, "r", fp); const char *p = fgets(STR_BUFFER, sizeof(STR_BUFFER), fp); if (p == NULL) { return -1; @@ -51,7 +52,6 @@ int read_int(FILE *fp) if (r == LONG_MIN || r == LONG_MAX) { return -1; } - rewind(fp); return r; } @@ -101,6 +101,9 @@ int main (void) } int x = get_luminance_raw(); + if (x < 0) { + fprintf(stderr, "Could not read luminance value: %s\n", strerror(errno)); + } int luminance_samples[MEDIAN_SAMPLES] = {x, x, x, x}; int idx = 0; int user_offset = 0;