main/Makefile.am: support to extract version information from (git) tarballs created...
[enigma2.git] / tools / libopen.c
index 8725f55a477b6c4150cfa6650827898f217e51f0..f6b935ee5d15ddf2853718f5ddcee7fbc4017a1a 100644 (file)
@@ -1,4 +1,3 @@
-#define _GNU_SOURCE
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
@@ -11,7 +10,8 @@
 
 int open64(const char *pathname, int flags, ...)
 {
-       static int (*libc_open64) (const char* pathname, int flags, ...);
+       typedef int (*FUNC_PTR) (const char* pathname, int flags, ...);
+       static FUNC_PTR libc_open64;
        int fd=-1;
        if (!libc_open64)
        {
@@ -23,7 +23,7 @@ int open64(const char *pathname, int flags, ...)
                        fputs(dlerror(), stderr);
                        exit(1);
                }
-               libc_open64 = dlsym(handle, "open64");
+               libc_open64 = (FUNC_PTR) dlsym(handle, "open64");
                if ((error = dlerror()) != NULL) {
                        fprintf(stderr, "%s\n", error);
                        exit(1);
@@ -45,9 +45,11 @@ int open64(const char *pathname, int flags, ...)
        return fd;
 }
 
+#if _FILE_OFFSET_BITS != 64
 int open(const char *pathname, int flags, ...)
 {
-       static int (*libc_open) (const char* pathname, int flags, ...);
+       typedef int (*FUNC_PTR) (const char* pathname, int flags, ...);
+       static FUNC_PTR libc_open;
        int fd=-1;
        if (!libc_open)
        {
@@ -59,7 +61,7 @@ int open(const char *pathname, int flags, ...)
                        fputs(dlerror(), stderr);
                        exit(1);
                }
-               libc_open = dlsym(handle, "open");
+               libc_open = (FUNC_PTR) dlsym(handle, "open");
                if ((error = dlerror()) != NULL) {
                        fprintf(stderr, "%s\n", error);
                        exit(1);
@@ -80,10 +82,12 @@ int open(const char *pathname, int flags, ...)
        }
        return fd;
 }
+#endif
 
 FILE *fopen64(const char *pathname, const char *mode)
 {
-       static FILE *(*libc_fopen64) (const char* pathname, const char *mode);
+       typedef FILE *(*FUNC_PTR) (const char* pathname, const char *mode);
+       static FUNC_PTR libc_fopen64;
        FILE *f=0;
        if (!libc_fopen64)
        {
@@ -95,7 +99,7 @@ FILE *fopen64(const char *pathname, const char *mode)
                        fputs(dlerror(), stderr);
                        exit(1);
                }
-               libc_fopen64 = dlsym(handle, "fopen64");
+               libc_fopen64 = (FUNC_PTR) dlsym(handle, "fopen64");
                if ((error = dlerror()) != NULL) {
                        fprintf(stderr, "%s\n", error);
                        exit(1);
@@ -118,9 +122,11 @@ FILE *fopen64(const char *pathname, const char *mode)
        return f;
 }
 
+#if _FILE_OFFSET_BITS != 64
 FILE *fopen(const char *pathname, const char *mode)
 {
-       static FILE *(*libc_fopen) (const char* pathname, const char *mode);
+       typedef FILE *(*FUNC_PTR) (const char* pathname, const char *mode);
+       static FUNC_PTR libc_fopen;
        FILE *f=0;
        if (!libc_fopen)
        {
@@ -132,7 +138,7 @@ FILE *fopen(const char *pathname, const char *mode)
                        fputs(dlerror(), stderr);
                        exit(1);
                }
-               libc_fopen = dlsym(handle, "fopen");
+               libc_fopen = (FUNC_PTR) dlsym(handle, "fopen");
                if ((error = dlerror()) != NULL) {
                        fprintf(stderr, "%s\n", error);
                        exit(1);
@@ -154,10 +160,12 @@ FILE *fopen(const char *pathname, const char *mode)
        }
        return f;
 }
+#endif
 
 int socket(int domain, int type, int protocol)
 {
-       static int (*libc_socket) (int domain, int type, int protocol);
+       typedef int (*FUNC_PTR) (int domain, int type, int protocol);
+       static FUNC_PTR libc_socket;
        int fd=-1;
        if (!libc_socket)
        {
@@ -169,7 +177,7 @@ int socket(int domain, int type, int protocol)
                        fputs(dlerror(), stderr);
                        exit(1);
                }
-               libc_socket = dlsym(handle, "socket");
+               libc_socket = (FUNC_PTR) dlsym(handle, "socket");
                if ((error = dlerror()) != NULL) {
                        fprintf(stderr, "%s\n", error);
                        exit(1);
@@ -191,9 +199,53 @@ int socket(int domain, int type, int protocol)
        return fd;
 }
 
+int socketpair(int d, int type, int protocol, int sv[2])
+{
+       typedef int (*FUNC_PTR) (int d, int type, int protocol, int sv[2]);
+       static FUNC_PTR libc_socketpair;
+       int ret=-1;
+       if (!libc_socketpair)
+       {
+               void *handle;
+               char *error;
+               handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
+               if (!handle)
+               {
+                       fputs(dlerror(), stderr);
+                       exit(1);
+               }
+               libc_socketpair = (FUNC_PTR) dlsym(handle, "socketpair");
+               if ((error = dlerror()) != NULL) {
+                       fprintf(stderr, "%s\n", error);
+                       exit(1);
+               }
+       }
+       ret = libc_socketpair(d, type, protocol, sv);
+       if (!ret)
+       {
+               int fd_flags = fcntl(sv[0], F_GETFD, 0);
+               if (fd_flags >= 0)
+               {
+                       fd_flags |= FD_CLOEXEC;
+                       fcntl(sv[0], F_SETFD, fd_flags);
+               }
+               fd_flags = fcntl(sv[1], F_GETFD, 0);
+               if (fd_flags >= 0)
+               {
+                       fd_flags |= FD_CLOEXEC;
+                       fcntl(sv[1], F_SETFD, fd_flags);
+               }
+#ifdef DEBUG
+               fprintf(stdout, "socketpair fd %d %d\n", sv[0], sv[1]);
+#endif
+       }
+       return ret;
+}
+
 int pipe(int modus[2])
 {
-       static int (*libc_pipe) (int modus[2]);
+       typedef int (*FUNC_PTR) (int modus[2]);
+       static FUNC_PTR libc_pipe;
        int ret=-1;
        if (!libc_pipe)
        {
@@ -205,7 +257,7 @@ int pipe(int modus[2])
                        fputs(dlerror(), stderr);
                        exit(1);
                }
-               libc_pipe = dlsym(handle, "pipe");
+               libc_pipe = (FUNC_PTR) dlsym(handle, "pipe");
                if ((error = dlerror()) != NULL) {
                        fprintf(stderr, "%s\n", error);
                        exit(1);