libreplace: some systems don't have memmem()
[samba.git] / lib / replace / replace.c
index 0683f556eb8f5fafd48f5bfbfeff0a062026187c..17fd46bcc89828b5bd85ce7573c68fafe106c04c 100644 (file)
 #include "system/locale.h"
 #include "system/wait.h"
 
+#ifdef _WIN32
+#define mkdir(d,m) _mkdir(d)
+#endif
+
 void replace_dummy(void);
 void replace_dummy(void) {}
 
@@ -355,7 +359,7 @@ char *rep_strndup(const char *s, size_t n)
 }
 #endif
 
-#ifndef HAVE_WAITPID
+#if !defined(HAVE_WAITPID) && defined(HAVE_WAIT4)
 int rep_waitpid(pid_t pid,int *status,int options)
 {
   return wait4(pid, status, options, NULL);
@@ -368,7 +372,8 @@ int rep_seteuid(uid_t euid)
 #ifdef HAVE_SETRESUID
        return setresuid(-1, euid, -1);
 #else
-#  error "You need a seteuid function"
+       errno = ENOSYS;
+       return -1;
 #endif
 }
 #endif
@@ -379,7 +384,8 @@ int rep_setegid(gid_t egid)
 #ifdef HAVE_SETRESGID
        return setresgid(-1, egid, -1);
 #else
-#  error "You need a setegid function"
+       errno = ENOSYS;
+       return -1;
 #endif
 }
 #endif
@@ -616,6 +622,14 @@ int rep_utimes(const char *filename, const struct timeval tv[2])
 }
 #endif
 
+#ifndef HAVE_DUP2
+int rep_dup2(int oldfd, int newfd) 
+{
+       errno = ENOSYS;
+       return -1;
+}
+#endif
+
 #ifndef HAVE_CHOWN
 /**
 chown isn't used much but OS/2 doesn't have it
@@ -667,3 +681,26 @@ char *rep_realpath(const char *path, char *resolved_path)
        return NULL;
 }
 #endif
+
+
+#ifndef HAVE_MEMMEM
+void *rep_memmem(const void *haystack, size_t haystacklen,
+                const void *needle, size_t needlelen)
+{
+       if (needlelen == 0) {
+               return discard_const(haystack);
+       }
+       while (haystacklen >= needlelen) {
+               char *p = memchr(haystack, *(const char *)needle,
+                                haystacklen-(needlelen-1));
+               if (!p) return NULL;
+               if (memcmp(p, needle, needlelen) == 0) {
+                       return p;
+               }
+               haystack = p+1;
+               haystacklen -= (p - (const char *)haystack) + 1;
+       }
+       return NULL;
+}
+#endif
+