diff options
author | Andi Shyti <andi.shyti@nokia.com> | 2013-05-28 00:51:14 +0200 |
---|---|---|
committer | Andi Shyti <andi.shyti@nokia.com> | 2013-05-28 00:51:14 +0200 |
commit | c7370f16cd6b7b1f64e023d2b349b0b378ee1e63 (patch) | |
tree | 224fac6346d049399255a714e49d1318c315265e | |
parent | c7f133ca984946c74cffbcab2c95be90f615bf41 (diff) |
apds990x_proxy: fix file access order
A sysfs file is a special file which prints an EOF at any
reading. Therefore it's not possible to open it once, read it
whenever is needed (since an EOF is received after the first
reading) and then close it.
The sequence
fd = open(pathname, flags);
while (1)
read(fd, buf, count);
close(fd);
should be
while (1) {
fd = open(pathname, flags);
read(fd, buf, count);
close(fd);
}
Signed-off-by: Andi Shyti <andi.shyti@nokia.com>
-rw-r--r-- | drivers/misc/apds990x_proxy.c | 42 |
1 files changed, 29 insertions, 13 deletions
diff --git a/drivers/misc/apds990x_proxy.c b/drivers/misc/apds990x_proxy.c index ba6ea3d..5474f5e 100644 --- a/drivers/misc/apds990x_proxy.c +++ b/drivers/misc/apds990x_proxy.c @@ -31,6 +31,8 @@ struct device { unsigned int n; + int prox; + char *power; char *prox_en; char *prox_raw; @@ -75,31 +77,45 @@ int write_file(char *fname, const char *val) int read_proxy(void) { - int i, fd; + unsigned int i; + int fd, retval = 0; ssize_t ret; char val[6]; - fd = open(apds990x_dev.prox_raw, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "%s: %s\n", apds990x_dev.prox_raw, + for (i = 0; i < apds990x_dev.n; i++) { + sleep(1); + + fd = open(apds990x_dev.prox_raw, O_RDONLY); + if (fd < 0) { + retval = errno; + fprintf(stderr, "%s: %s\n", apds990x_dev.prox_raw, strerror(errno)); - return errno; - } + return retval; + } - for (i = 0; i < apds990x_dev.n; i++) { - ret = read(fd, val, 5); - if (!ret) { + ret = read(fd, val, sizeof(val)); + if (ret < 0) { + retval = errno; fprintf(stderr, "%s: %s\n", apds990x_dev.prox_raw, strerror(errno)); break; } - val[ret] = '\0'; - printf("%s\n", val); - sleep(1); + /* replace EOF with \n */ + val[ret-1] = '\n'; + apds990x_dev.prox = (int) strtol(val, NULL, 10); + printf("%d\n", apds990x_dev.prox); + + fd = close(fd); + if (fd < 0) { + retval = errno; + fprintf(stderr, "%s: %s\n", apds990x_dev.prox_raw, + strerror(errno)); + break; + } } - return errno; + return retval; } void print_usage(char *name) |