From 5877f7b5c077df92983553d455318b26096e8bf0 Mon Sep 17 00:00:00 2001 From: Thomas Martitz Date: Sun, 29 Jun 2014 12:23:48 +0200 Subject: open_utf8: Actually use the result of read/write. This silences warnings on some compilers but is anyway a good idea. Change-Id: Ib566ab59a5d1cb433da466f3ce0c32ff150eb52e --- apps/misc.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/apps/misc.c b/apps/misc.c index 6c589b99e4..f847023c31 100644 --- a/apps/misc.c +++ b/apps/misc.c @@ -1111,6 +1111,7 @@ int split_string(char *str, const char split_char, char *vector[], const int vec int open_utf8(const char* pathname, int flags) { + ssize_t ret; int fd; unsigned char bom[BOM_UTF_8_SIZE]; @@ -1120,16 +1121,23 @@ int open_utf8(const char* pathname, int flags) if(flags & (O_TRUNC | O_WRONLY)) { - write(fd, BOM_UTF_8, BOM_UTF_8_SIZE); + ret = write(fd, BOM_UTF_8, BOM_UTF_8_SIZE); } else { - read(fd, bom, BOM_UTF_8_SIZE); + ret = read(fd, bom, BOM_UTF_8_SIZE); /* check for BOM */ - if(memcmp(bom, BOM_UTF_8, BOM_UTF_8_SIZE)) - lseek(fd, 0, SEEK_SET); + if (ret == BOM_UTF_8_SIZE) + { + if(memcmp(bom, BOM_UTF_8, BOM_UTF_8_SIZE)) + lseek(fd, 0, SEEK_SET); + } } - return fd; + /* read or write failure, do not continue */ + if (ret < 0) + close(fd); + + return ret >= 0 ? fd : -1; } -- cgit v1.2.3