From: Hans de Goede Subject: [PATCH 4/4] backlight-helper: Simplify reading the level from stdin Date: Mon, 17 Feb 2014 13:16:54 +0100 Message-ID: <1392639414-3909-5-git-send-email-hdegoede@redhat.com> References: <1392639414-3909-1-git-send-email-hdegoede@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by gabe.freedesktop.org (Postfix) with ESMTP id A5858FA721 for ; Mon, 17 Feb 2014 04:17:02 -0800 (PST) Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s1HCH20p021500 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 17 Feb 2014 07:17:02 -0500 In-Reply-To: <1392639414-3909-1-git-send-email-hdegoede@redhat.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: intel-gfx-bounces@lists.freedesktop.org Errors-To: intel-gfx-bounces@lists.freedesktop.org To: intel-gfx@lists.freedesktop.org Cc: peter.hutterer@redhat.com List-Id: intel-gfx@lists.freedesktop.org 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 --- tools/backlight_helper.c | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/tools/backlight_helper.c b/tools/backlight_helper.c index fc16fce..11abebc 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; -- 1.8.5.3