summaryrefslogtreecommitdiff
path: root/src/libstddjb
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2024-04-16 12:57:52 +0000
committerLaurent Bercot <ska@appnovation.com>2024-04-16 12:57:52 +0000
commit825b80eaa311b0aec4c00614f38f3eb63949d5a7 (patch)
tree624f702487d9acdd3cc85060cfe2edc388cf7096 /src/libstddjb
parentbfc53cfde4fdda343647678fcd21b38a44f2de08 (diff)
downloadskalibs-825b80eaa311b0aec4c00614f38f3eb63949d5a7.tar.xz
Tentative support for old MacOS with no O_CLOEXEC
Also add a prototype for gol, not finished yet. Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libstddjb')
-rw-r--r--src/libstddjb/coe.c2
-rw-r--r--src/libstddjb/fd_ensure_open.c2
-rw-r--r--src/libstddjb/fd_islocked.c2
-rw-r--r--src/libstddjb/fd_lock.c2
-rw-r--r--src/libstddjb/fd_unlock.c2
-rw-r--r--src/libstddjb/filecopy_unsafe.c2
-rw-r--r--src/libstddjb/gol.c52
-rw-r--r--src/libstddjb/gol_argv.c26
-rw-r--r--src/libstddjb/hiercopy_internal.c2
-rw-r--r--src/libstddjb/ipc_accept.c2
-rw-r--r--src/libstddjb/ipc_bind_reuse_lock_perms.c2
-rw-r--r--src/libstddjb/ndelay_off.c2
-rw-r--r--src/libstddjb/ndelay_on.c2
-rw-r--r--src/libstddjb/open2.c28
-rw-r--r--src/libstddjb/open3.c28
-rw-r--r--src/libstddjb/open_append.c2
-rw-r--r--src/libstddjb/open_create.c2
-rw-r--r--src/libstddjb/open_excl.c2
-rw-r--r--src/libstddjb/open_read.c2
-rw-r--r--src/libstddjb/open_trunc.c2
-rw-r--r--src/libstddjb/open_write.c2
-rw-r--r--src/libstddjb/openb_read.c2
-rw-r--r--src/libstddjb/openbc_read.c2
-rw-r--r--src/libstddjb/openc_append.c2
-rw-r--r--src/libstddjb/openc_create.c2
-rw-r--r--src/libstddjb/openc_excl.c2
-rw-r--r--src/libstddjb/openc_read.c2
-rw-r--r--src/libstddjb/openc_trunc.c2
-rw-r--r--src/libstddjb/openc_write.c2
-rw-r--r--src/libstddjb/pipe_internal.c2
-rw-r--r--src/libstddjb/socket_accept4.c2
-rw-r--r--src/libstddjb/socket_accept6.c2
-rw-r--r--src/libstddjb/socket_internal.c2
-rw-r--r--src/libstddjb/socketpair_internal.c3
-rw-r--r--src/libstddjb/uncoe.c2
35 files changed, 162 insertions, 35 deletions
diff --git a/src/libstddjb/coe.c b/src/libstddjb/coe.c
index a32c4ef..469a7c5 100644
--- a/src/libstddjb/coe.c
+++ b/src/libstddjb/coe.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int coe (int fd)
diff --git a/src/libstddjb/fd_ensure_open.c b/src/libstddjb/fd_ensure_open.c
index 4c1b63c..2867da1 100644
--- a/src/libstddjb/fd_ensure_open.c
+++ b/src/libstddjb/fd_ensure_open.c
@@ -1,8 +1,8 @@
/* ISC license. */
#include <errno.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int fd_ensure_open (int fd, int w)
diff --git a/src/libstddjb/fd_islocked.c b/src/libstddjb/fd_islocked.c
index 50603c0..6089ee8 100644
--- a/src/libstddjb/fd_islocked.c
+++ b/src/libstddjb/fd_islocked.c
@@ -1,8 +1,8 @@
/* ISC license. */
-#include <fcntl.h>
#include <unistd.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int fd_islocked (int fd)
diff --git a/src/libstddjb/fd_lock.c b/src/libstddjb/fd_lock.c
index a4ebaa5..e5d8a42 100644
--- a/src/libstddjb/fd_lock.c
+++ b/src/libstddjb/fd_lock.c
@@ -1,10 +1,10 @@
/* ISC license. */
-#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <skalibs/error.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int fd_lock (int fd, int w, int nb)
diff --git a/src/libstddjb/fd_unlock.c b/src/libstddjb/fd_unlock.c
index 0e6ac0e..64984b9 100644
--- a/src/libstddjb/fd_unlock.c
+++ b/src/libstddjb/fd_unlock.c
@@ -1,9 +1,9 @@
/* ISC license. */
-#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
void fd_unlock (int fd)
diff --git a/src/libstddjb/filecopy_unsafe.c b/src/libstddjb/filecopy_unsafe.c
index a6410c1..41f33bd 100644
--- a/src/libstddjb/filecopy_unsafe.c
+++ b/src/libstddjb/filecopy_unsafe.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int filecopy_unsafe (char const *src, char const *dst, unsigned int mode)
diff --git a/src/libstddjb/gol.c b/src/libstddjb/gol.c
new file mode 100644
index 0000000..e61d7f3
--- /dev/null
+++ b/src/libstddjb/gol.c
@@ -0,0 +1,52 @@
+/* ISC license. */
+
+#include <string.h>
+
+#include <skalibs/gol.h>
+
+int gol (char const *const *argv, unsigned int argc, gol_bool const *b, unsigned int bn, gol_arg const *a, unsigned int an, uint64_t *br, char const **ar, int *problem)
+{
+ for (unsigned int i = 0 ; i < argc ; i++)
+ {
+ if (argv[i][0] != '-' || !argv[i][1]) return i ;
+ if (argv[i][1] == '-')
+ {
+ unsigned int j = 0 ;
+ char const *x ;
+ if (!argv[i][2]) return i+1 ;
+ x = strchr(argv[i]+2, '=') ;
+ if (x)
+ {
+ size_t len = x - argv[i] - 2 ;
+ for (; j < an ; j++) if (!strncmp(argv[i] + 2, a[j].lo, len) && !a[j].lo[len]) break ;
+ if (j >= an) return (*problem = -1, -1-i) ;
+ ar[a[j].i] = x + 1 ;
+ }
+ else
+ {
+ for (; j < bn ; j++) if (!strcmp(argv[i] + 2, b[j].lo)) break ;
+ if (j >= bn) return (*problem = -1, -1-i) ;
+ if (b[j].set) *br |= b[j].mask ; else *br &= b[j].mask ;
+ }
+ }
+ else
+ {
+ char const *p = argv[i] + 1 ;
+ for (; !*p ; p++)
+ {
+ unsigned int j = 0 ;
+ for (; j < an ; j++) if (*p == a[j].so) break ;
+ if (j < an)
+ {
+ if (p[1]) ar[a[j].i] == p + 1 ;
+ else if (i+1 < argc && strcmp(argv[i+1], "--")) ar[a[j].i] = argv[++i] ;
+ break ;
+ }
+ for (j = 0 ; j < bn ; j++) if (*p == b[j].so) break ;
+ if (j >= bn) return (*problem = p - argv[i], -1-i) ;
+ if (b[j].set) *br |= b[j].mask ; else *br &= b[j].mask ;
+ }
+ }
+ }
+ return argc ;
+}
diff --git a/src/libstddjb/gol_argv.c b/src/libstddjb/gol_argv.c
new file mode 100644
index 0000000..2538f5a
--- /dev/null
+++ b/src/libstddjb/gol_argv.c
@@ -0,0 +1,26 @@
+/* ISC license. */
+
+#include <string.h>
+
+#include <skalibs/gol.h>
+#include <skalibs/strerr.h>
+
+unsigned int gol_argv (int argc, char const *const *argv, gol_bool const *b, unsigned int bn, gol_arg const *a, unsigned int an, uint64_t *br, char const **ar)
+{
+ int problem = 0 ;
+ int r ;
+ if (!argc) strerr_diefu1x(100, "gol: invalid argv") ;
+ if (argc == 1) return 1 ;
+ r = gol(argv + 1, argc - 1, b, bn, a, an, br, ar, &problem) ;
+
+ if (r < 0)
+ {
+ if (problem >= 0)
+ {
+ char s[2] = { argv[-r][problem], 0 } ;
+ strerr_dief4x(100, "unrecognized ", "short", "option: ", s) ;
+ }
+ else strerr_dief3x(100, "unrecognized ", "option: ", argv[-r]) ;
+ }
+ else return r + 1 ;
+}
diff --git a/src/libstddjb/hiercopy_internal.c b/src/libstddjb/hiercopy_internal.c
index c512463..5d216a3 100644
--- a/src/libstddjb/hiercopy_internal.c
+++ b/src/libstddjb/hiercopy_internal.c
@@ -4,10 +4,10 @@
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
-#include <fcntl.h>
#include <skalibs/stralloc.h>
#include <skalibs/direntry.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
static int dircopy (char const *src, char const *dst, mode_t mode, stralloc *tmp, unsigned int options)
diff --git a/src/libstddjb/ipc_accept.c b/src/libstddjb/ipc_accept.c
index 9d5d82c..83886ff 100644
--- a/src/libstddjb/ipc_accept.c
+++ b/src/libstddjb/ipc_accept.c
@@ -6,9 +6,9 @@
#include <sys/un.h>
#include <string.h>
#include <errno.h>
-#include <fcntl.h>
#include <skalibs/bytestr.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
#include <skalibs/socket.h>
diff --git a/src/libstddjb/ipc_bind_reuse_lock_perms.c b/src/libstddjb/ipc_bind_reuse_lock_perms.c
index 6e4d9ff..f47ba19 100644
--- a/src/libstddjb/ipc_bind_reuse_lock_perms.c
+++ b/src/libstddjb/ipc_bind_reuse_lock_perms.c
@@ -2,13 +2,13 @@
#include <skalibs/nonposix.h>
-#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/stat.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
#include <skalibs/socket.h>
diff --git a/src/libstddjb/ndelay_off.c b/src/libstddjb/ndelay_off.c
index 5ea68c6..1d23f08 100644
--- a/src/libstddjb/ndelay_off.c
+++ b/src/libstddjb/ndelay_off.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int ndelay_off (int fd)
diff --git a/src/libstddjb/ndelay_on.c b/src/libstddjb/ndelay_on.c
index 1bf9527..2a899b8 100644
--- a/src/libstddjb/ndelay_on.c
+++ b/src/libstddjb/ndelay_on.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int ndelay_on (int fd)
diff --git a/src/libstddjb/open2.c b/src/libstddjb/open2.c
index 1d2682e..0f23183 100644
--- a/src/libstddjb/open2.c
+++ b/src/libstddjb/open2.c
@@ -1,14 +1,38 @@
/* ISC license. */
+#include <skalibs/sysdeps.h>
#include <skalibs/nonposix.h>
+
#include <sys/stat.h>
-#include <fcntl.h>
#include <errno.h>
+#include <skalibs/fcntl.h>
+#include <skalibs/djbunix.h>
+
+#ifdef SKALIBS_HASOCLOEXEC
+
int open2 (char const *s, unsigned int flags)
{
int r ;
do r = open(s, flags) ;
- while ((r == -1) && (errno == EINTR)) ;
+ while (r == -1 && errno == EINTR) ;
return r ;
}
+
+#else
+
+int open2 (char const *s, unsigned int flags)
+{
+ int fd ;
+ do fd = open(s, flags & ~O_CLOEXEC) ;
+ while (fd == -1 && errno == EINTR) ;
+ if (fd == -1) return -1 ;
+ if (flags & O_CLOEXEC && coe(fd) == -1)
+ {
+ fd_close(fd) ;
+ return -1 ;
+ }
+ return fd ;
+}
+
+#endif
diff --git a/src/libstddjb/open3.c b/src/libstddjb/open3.c
index 21fd904..a9ff9e6 100644
--- a/src/libstddjb/open3.c
+++ b/src/libstddjb/open3.c
@@ -1,14 +1,38 @@
/* ISC license. */
+#include <skalibs/sysdeps.h>
#include <skalibs/nonposix.h>
+
#include <sys/stat.h>
-#include <fcntl.h>
#include <errno.h>
+#include <skalibs/fcntl.h>
+#include <skalibs/djbunix.h>
+
+#ifdef SKALIBS_HASOCLOEXEC
+
int open3 (char const *s, unsigned int flags, unsigned int mode)
{
int r ;
do r = open(s, flags, mode) ;
- while ((r == -1) && (errno == EINTR)) ;
+ while (r == -1 && errno == EINTR) ;
return r ;
}
+
+#else
+
+int open3 (char const *s, unsigned int flags, unsigned int mode)
+{
+ int fd ;
+ do fd = open(s, flags & ~O_CLOEXEC, mode) ;
+ while (fd == -1 && errno == EINTR) ;
+ if (fd == -1) return -1 ;
+ if (flags & O_CLOEXEC && coe(fd) == -1)
+ {
+ fd_close(fd) ;
+ return -1 ;
+ }
+ return fd ;
+}
+
+#endif
diff --git a/src/libstddjb/open_append.c b/src/libstddjb/open_append.c
index 7486312..74f1bca 100644
--- a/src/libstddjb/open_append.c
+++ b/src/libstddjb/open_append.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int open_append (char const *fn)
diff --git a/src/libstddjb/open_create.c b/src/libstddjb/open_create.c
index db072d1..6eeb57a 100644
--- a/src/libstddjb/open_create.c
+++ b/src/libstddjb/open_create.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int open_create (char const *fn)
diff --git a/src/libstddjb/open_excl.c b/src/libstddjb/open_excl.c
index 83c7770..e93d194 100644
--- a/src/libstddjb/open_excl.c
+++ b/src/libstddjb/open_excl.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int open_excl (char const *fn)
diff --git a/src/libstddjb/open_read.c b/src/libstddjb/open_read.c
index cc1b191..327dd08 100644
--- a/src/libstddjb/open_read.c
+++ b/src/libstddjb/open_read.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int open_read (char const *fn)
diff --git a/src/libstddjb/open_trunc.c b/src/libstddjb/open_trunc.c
index b34e3c2..8333934 100644
--- a/src/libstddjb/open_trunc.c
+++ b/src/libstddjb/open_trunc.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int open_trunc (char const *fn)
diff --git a/src/libstddjb/open_write.c b/src/libstddjb/open_write.c
index 42a92c3..14045c7 100644
--- a/src/libstddjb/open_write.c
+++ b/src/libstddjb/open_write.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int open_write (char const *fn)
diff --git a/src/libstddjb/openb_read.c b/src/libstddjb/openb_read.c
index 19ec073..cd7925a 100644
--- a/src/libstddjb/openb_read.c
+++ b/src/libstddjb/openb_read.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int openb_read (char const *fn)
diff --git a/src/libstddjb/openbc_read.c b/src/libstddjb/openbc_read.c
index 4783780..bd4713e 100644
--- a/src/libstddjb/openbc_read.c
+++ b/src/libstddjb/openbc_read.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int openbc_read (char const *fn)
diff --git a/src/libstddjb/openc_append.c b/src/libstddjb/openc_append.c
index 9765aa6..e0375dd 100644
--- a/src/libstddjb/openc_append.c
+++ b/src/libstddjb/openc_append.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int openc_append (char const *fn)
diff --git a/src/libstddjb/openc_create.c b/src/libstddjb/openc_create.c
index 2c0dd86..98e535b 100644
--- a/src/libstddjb/openc_create.c
+++ b/src/libstddjb/openc_create.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int openc_create (char const *fn)
diff --git a/src/libstddjb/openc_excl.c b/src/libstddjb/openc_excl.c
index 9a45913..97e5531 100644
--- a/src/libstddjb/openc_excl.c
+++ b/src/libstddjb/openc_excl.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int openc_excl (char const *fn)
diff --git a/src/libstddjb/openc_read.c b/src/libstddjb/openc_read.c
index 57a23bc..6cb77fe 100644
--- a/src/libstddjb/openc_read.c
+++ b/src/libstddjb/openc_read.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int openc_read (char const *fn)
diff --git a/src/libstddjb/openc_trunc.c b/src/libstddjb/openc_trunc.c
index ca39160..bda44be 100644
--- a/src/libstddjb/openc_trunc.c
+++ b/src/libstddjb/openc_trunc.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int openc_trunc (char const *fn)
diff --git a/src/libstddjb/openc_write.c b/src/libstddjb/openc_write.c
index 518c460..4310cfb 100644
--- a/src/libstddjb/openc_write.c
+++ b/src/libstddjb/openc_write.c
@@ -1,7 +1,7 @@
/* ISC license. */
#include <skalibs/nonposix.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int openc_write (char const *fn)
diff --git a/src/libstddjb/pipe_internal.c b/src/libstddjb/pipe_internal.c
index dc7cfe6..972f595 100644
--- a/src/libstddjb/pipe_internal.c
+++ b/src/libstddjb/pipe_internal.c
@@ -17,8 +17,8 @@ int pipe_internal (int *p, unsigned int flags)
#else
#include <unistd.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int pipe_internal (int *p, unsigned int flags)
diff --git a/src/libstddjb/socket_accept4.c b/src/libstddjb/socket_accept4.c
index 36f3dc2..0c11023 100644
--- a/src/libstddjb/socket_accept4.c
+++ b/src/libstddjb/socket_accept4.c
@@ -7,9 +7,9 @@
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
-#include <fcntl.h>
#include <skalibs/uint16.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
#include <skalibs/socket.h>
diff --git a/src/libstddjb/socket_accept6.c b/src/libstddjb/socket_accept6.c
index 2c18540..229178d 100644
--- a/src/libstddjb/socket_accept6.c
+++ b/src/libstddjb/socket_accept6.c
@@ -7,9 +7,9 @@
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
-#include <fcntl.h>
#include <skalibs/uint16.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
#include <skalibs/ip46.h>
#include <skalibs/socket.h>
diff --git a/src/libstddjb/socket_internal.c b/src/libstddjb/socket_internal.c
index 39c6e38..3371b9a 100644
--- a/src/libstddjb/socket_internal.c
+++ b/src/libstddjb/socket_internal.c
@@ -4,8 +4,8 @@
#include <skalibs/nonposix.h>
#include <sys/socket.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
#include <skalibs/socket.h>
diff --git a/src/libstddjb/socketpair_internal.c b/src/libstddjb/socketpair_internal.c
index 0cedce2..8e46374 100644
--- a/src/libstddjb/socketpair_internal.c
+++ b/src/libstddjb/socketpair_internal.c
@@ -2,10 +2,11 @@
#include <skalibs/sysdeps.h>
#include <skalibs/nonposix.h>
+
#include <sys/socket.h>
#include <errno.h>
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
#include <skalibs/socket.h>
diff --git a/src/libstddjb/uncoe.c b/src/libstddjb/uncoe.c
index be7b90c..55804ff 100644
--- a/src/libstddjb/uncoe.c
+++ b/src/libstddjb/uncoe.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include <fcntl.h>
+#include <skalibs/fcntl.h>
#include <skalibs/djbunix.h>
int uncoe (int fd)