don't bail out on locking errors, when flock() is not supported
[project/uci.git] / util.c
diff --git a/util.c b/util.c
index e56992e..f0b2094 100644 (file)
--- a/util.c
+++ b/util.c
@@ -23,6 +23,7 @@
 #include <unistd.h>
 #include <ctype.h>
 #include <fcntl.h>
+#include <errno.h>
 
 #define LINEBUF        32
 #define LINEBUF_MAX    4096
@@ -86,7 +87,7 @@ __plugin bool uci_validate_str(const char *str, bool name)
                return false;
 
        while (*str) {
-               char c = *str;
+               unsigned char c = *str;
                if (!isalnum(c) && c != '_') {
                        if (name || (c < 33) || (c > 126))
                                return false;
@@ -114,8 +115,9 @@ static inline bool uci_validate_name(const char *str)
 bool uci_validate_text(const char *str)
 {
        while (*str) {
-               if ((*str == '\r') || (*str == '\n') ||
-                       ((*str < 32) && (*str != '\t')))
+               unsigned char c = *str;
+               if ((c == '\r') || (c == '\n') ||
+                       ((c < 32) && (c != '\t')))
                        return false;
                str++;
        }
@@ -452,7 +454,8 @@ static FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int
        if (fd < 0)
                goto error;
 
-       if (flock(fd, (write ? LOCK_EX : LOCK_SH)) < 0)
+       ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
+       if ((ret < 0) && (errno != ENOSYS))
                goto error;
 
        ret = lseek(fd, 0, pos);
@@ -477,6 +480,7 @@ static void uci_close_stream(FILE *stream)
        if (!stream)
                return;
 
+       fflush(stream);
        fd = fileno(stream);
        flock(fd, LOCK_UN);
        fclose(stream);