From c7f133ca984946c74cffbcab2c95be90f615bf41 Mon Sep 17 00:00:00 2001 From: Andi Shyti Date: Tue, 28 May 2013 00:43:03 +0200 Subject: apds990x_proxy: fix errno usage. This patch fixes one of the most common errors about errno usage: if (somecall() == -1) { printf("somecall() failed\n"); if (errno == ...) { ... } } printf may change the value of errno, so that the errno value in the if statement is not anymore referring to 'somecall()' function. The correct usage should be: if (somecall() == -1) { int errsv = errno; printf("somecall() failed\n"); if (errsv == ...) { ... } } Signed-off-by: Andi Shyti --- drivers/misc/apds990x_proxy.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/drivers/misc/apds990x_proxy.c b/drivers/misc/apds990x_proxy.c index f5e701c..ba6ea3d 100644 --- a/drivers/misc/apds990x_proxy.c +++ b/drivers/misc/apds990x_proxy.c @@ -47,25 +47,30 @@ int write_file(char *fname, const char *val) { int fd; ssize_t ret; + int retval = 0; fd = open(fname, O_WRONLY); if (fd < 0) { + retval = errno; fprintf(stderr, "%s: %s\n", fname, strerror(errno)); - return errno; + return retval; } ret = write(fd, val, 1); if (ret != 1) { + retval = errno; fprintf(stderr, "%s: %s\n", fname, strerror(errno)); close(fd); - return errno; + return retval; } fd = close(fd); - if (fd < 0) + if (fd < 0) { + retval = errno; fprintf(stderr, "%s: %s\n", fname, strerror(errno)); + } - return errno; + return retval; } int read_proxy(void) -- cgit v1.2.3