Pārlūkot izejas kodu

compat: Add a strndup replacement

Jean-Baptiste Kempf 9 gadi atpakaļ
vecāks
revīzija
0c50ec4e27
4 mainītis faili ar 55 papildinājumiem un 0 dzēšanām
  1. 5 0
      compat/compat.h
  2. 47 0
      compat/strndup.c
  3. 1 0
      configure.ac
  4. 2 0
      src/smb_session.c

+ 5 - 0
compat/compat.h

@@ -30,6 +30,7 @@
 
 #include "config.h"
 
+#include <stdlib.h>
 #if !defined HAVE_STRLCPY && !defined HAVE_LIBBSD
 size_t strlcpy(char *dst, const char *src, size_t siz);
 #endif
@@ -44,6 +45,10 @@ typedef enum {
 int clock_gettime(clockid_t clk_id, struct timespec *tp);
 #endif
 
+#if !defined HAVE_STRNDUP
+char *strndup(const char *str, size_t n);
+#endif
+
 #ifndef O_NONBLOCK
 # define O_NONBLOCK 0
 #endif

+ 47 - 0
compat/strndup.c

@@ -0,0 +1,47 @@
+/*      $NetBSD: strndup.c,v 1.3 2007/01/14 23:41:24 cbiere Exp $       */
+
+/*
+ * Copyright (c) 1988, 1993
+ *    The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+char *
+strndup(const char *str, size_t n)
+{
+    size_t len;
+    char *copy;
+
+    len = strnlen(str, n);
+    if ((copy = malloc(len + 1)) == NULL)
+        return (NULL);
+    memcpy(copy, str, len);
+    copy[len] = '\0';
+    return (copy);
+}

+ 1 - 0
configure.ac

@@ -89,6 +89,7 @@ AC_SEARCH_LIBS([strlcpy], [bsd], [
 ])
 AC_SEARCH_LIBS([clock_gettime], [rt pthread])
 AC_REPLACE_FUNCS([strlcpy])
+AC_REPLACE_FUNCS([strndup])
 AC_REPLACE_FUNCS([clock_gettime])
 
 AC_CHECK_HEADERS([bsd/string.h langinfo.h alloca.h sys/queue.h arpa/inet.h])

+ 2 - 0
src/smb_session.c

@@ -28,6 +28,8 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#include "compat.h"
+
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>