diff options
author | Hans de Goede <hdegoede@redhat.com> | 2014-02-17 13:16:54 +0100 |
---|---|---|
committer | Chris Wilson <chris@chris-wilson.co.uk> | 2014-02-17 18:38:54 +0000 |
commit | e860b3eaedcb5d56745d5bcc9ce720e9a0e9c293 (patch) | |
tree | e654d73e13e9977cf57de88a1c15b89c6121690d /tools | |
parent | b5229c6e15964e723a7bb99d3c2c20a2fa168e3b (diff) |
backlight-helper: Simplify reading the level from stdin
Since the helper is a standalone app, the usual xserver rules of not using
stdio because of signal handling don't apply.
And since the helper does run with elevated rights, it is important to keep
the code KISS so that it can be audited easily.
This commit replaces the hard to read "raw" read loop with a much simpler
loop using fgets, improving readability of the code.
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/backlight_helper.c | 29 |
1 files changed, 7 insertions, 22 deletions
diff --git a/tools/backlight_helper.c b/tools/backlight_helper.c index fc16fcea..11abebc2 100644 --- a/tools/backlight_helper.c +++ b/tools/backlight_helper.c @@ -9,7 +9,7 @@ int main(int argc, char *argv[]) { struct stat st; - char buf[1024], *b = buf; + char buf[1024]; int len, fd; if (argc != 2) { @@ -24,27 +24,12 @@ int main(int argc, char *argv[]) return 1; } - while ((len = read(0, b, sizeof(buf) - (b - buf) - 1)) > 0) { - len += b - buf; - buf[len] = '\0'; - - b = buf; - do { - char *end = strchr(b, '\n'); - if (end == NULL) - break; - - ++end; - if (write(fd, b, end - b) != end - b) { - fprintf(stderr, "Failed to update backlight interface '%s'\n", argv[1]); - return 2; - } - - b = end; - } while (1); - - memmove(buf, b, len = buf + len - b); - b = buf + len; + while (fgets(buf, sizeof(buf), stdin)) { + len = strlen(buf); + if (write(fd, buf, len) != len) { + fprintf(stderr, "Failed to update backlight interface '%s'\n", argv[1]); + return 2; + } } return 0; |