1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
From 753f86fb11b4d3dbeee152cdc1187e86467e1af6 Mon Sep 17 00:00:00 2001
From: yangfl <yangfl@users.noreply.github.com>
Date: Sun, 9 Mar 2025 01:54:21 +0800
Subject: [PATCH 4/4] Use minGlue-Linux.h as glue file
---
minGlue.h | 38 +++++++++++++++++++++++++++++---------
1 file changed, 29 insertions(+), 9 deletions(-)
diff --git a/minGlue.h b/minGlue.h
index 2d47d90..906f8fb 100644
--- a/minGlue.h
+++ b/minGlue.h
@@ -1,25 +1,45 @@
-/* Glue functions for the minIni library, based on the C/C++ stdio library
+/* Glue functions for the minIni library, based on the C/C++ stdio library and
+ * using BSD-style file locking to "serialize" concurrent accesses to an INI
+ * file. It presumes GCC compiler extensions.
*
- * Or better said: this file contains macros that maps the function interface
- * used by minIni to the standard C/C++ file I/O functions.
- *
- * By CompuPhase, 2008-2014
+ * By CompuPhase, 2020
* This "glue file" is in the public domain. It is distributed without
* warranties or conditions of any kind, either express or implied.
*/
/* map required file I/O types and functions to the standard C library */
#include <stdio.h>
+#include <unistd.h>
+#include <sys/file.h>
#define INI_FILETYPE FILE*
-#define ini_openread(filename,file) ((*(file) = fopen((filename),"rb")) != NULL)
-#define ini_openwrite(filename,file) ((*(file) = fopen((filename),"wb")) != NULL)
-#define ini_openrewrite(filename,file) ((*(file) = fopen((filename),"r+b")) != NULL)
+
+static inline int ini_openread(const char *filename, INI_FILETYPE *file) {
+ if ((*file = fopen((filename), "r")) == NULL)
+ return 0;
+ return flock(fileno(*file), LOCK_SH) == 0;
+}
+
+static inline int ini_openwrite(const char *filename, INI_FILETYPE *file) {
+ if ((*file = fopen((filename), "r+")) == NULL
+ && (*file = fopen((filename), "w")) == NULL)
+ return 0;
+ if (flock(fileno(*file), LOCK_EX) < 0)
+ return 0;
+ return ftruncate(fileno(*file), 0) == 0;
+}
+
+#define INI_OPENREWRITE
+static inline int ini_openrewrite(const char *filename, INI_FILETYPE *file) {
+ if ((*file = fopen((filename), "r+")) == NULL)
+ return 0;
+ return flock(fileno(*file), LOCK_EX) == 0;
+}
+
#define ini_close(file) (fclose(*(file)) == 0)
#define ini_read(buffer,size,file) (fgets((buffer),(size),*(file)) != NULL)
#define ini_write(buffer,file) (fputs((buffer),*(file)) >= 0)
#define ini_rename(source,dest) (rename((source), (dest)) == 0)
-#define ini_remove(filename) (remove(filename) == 0)
#define INI_FILEPOS long int
#define ini_tell(file,pos) (*(pos) = ftell(*(file)))
--
2.47.2
|