libreplace: fixed declaration of dprintf() on FreeBSD (cherry picked from commit...
[samba.git] / lib / replace / replace.c
index a648391b23d4ebd2c859f044400174ef22e969f9..b8e4e46c529c0c524e32c18ee41c76e0f3a621ca 100644 (file)
@@ -359,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);
@@ -372,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
@@ -383,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
@@ -679,3 +681,57 @@ 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
+
+#ifndef HAVE_VDPRINTF
+int rep_vdprintf(int fd, const char *format, va_list ap)
+{
+       char *s = NULL;
+       int ret;
+
+       vasprintf(&s, format, ap);
+       if (s == NULL) {
+               errno = ENOMEM;
+               return -1;
+       }
+       ret = write(fd, s, strlen(s));
+       free(s);
+       return ret;
+}
+#endif
+
+#ifndef HAVE_DPRINTF
+int rep_dprintf(int fd, const char *format, ...)
+{
+       int ret;
+       va_list ap;
+
+       va_start(ap, format);
+       ret = vdprintf(fd, format, ap);
+       va_end(ap);
+
+       return ret;
+}
+#endif
+