]> granicus.if.org Git - neomutt/commitdiff
add strndup.c strnlen.c
authorKarel Zak <kzak@redhat.com>
Thu, 6 Sep 2012 07:31:54 +0000 (09:31 +0200)
committerRichard Russon <rich@flatcap.org>
Mon, 4 Apr 2016 02:32:55 +0000 (03:32 +0100)
Reported-by: Vladimir.Marek@oracle.com
Signed-off-by: Karel Zak <kzak@redhat.com>
configure.ac
protos.h
strndup.c [new file with mode: 0644]
strnlen.c [new file with mode: 0644]

index fe18ecce4e09bb23ce93762a409df8d82b3e00eb..c84fcb714a7d06490105363e1d70b3da20ed991f 100644 (file)
@@ -354,7 +354,7 @@ AC_CHECK_TYPE(ssize_t, int)
 
 AC_CHECK_FUNCS(fgetpos memmove setegid srand48 strerror)
 
-AC_REPLACE_FUNCS([setenv strcasecmp strdup strsep strtok_r wcscasecmp])
+AC_REPLACE_FUNCS([setenv strcasecmp strdup strndup strnlen strsep strtok_r wcscasecmp])
 AC_REPLACE_FUNCS([strcasestr mkdtemp])
 
 AC_CHECK_FUNC(getopt)
index 8e5f7aa9d6ba86bdfb531680103eee1d40ca7ef9..2f849cf11118c1bfd2e12dbc3c4b3f8f92bc5b7f 100644 (file)
--- a/protos.h
+++ b/protos.h
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 1996-2000,2007,2010,2013 Michael R. Elkins <me@mutt.org>
+ * Copyright (C) 2013 Karel Zak <kzak@redhat.com>
  * 
  *     This program is free software; you can redistribute it and/or modify
  *     it under the terms of the GNU General Public License as published by
@@ -565,3 +566,11 @@ char *strcasestr (const char *, const char *);
 #ifndef HAVE_MKDTEMP
 char *mkdtemp (char *tmpl);
 #endif
+
+#ifndef HAVE_STRNLEN
+size_t strnlen(const char *s, size_t maxlen);
+#endif
+
+#ifndef HAVE_STRNDUP
+char *strndup(const char *s, size_t n);
+#endif
diff --git a/strndup.c b/strndup.c
new file mode 100644 (file)
index 0000000..67c1a5e
--- /dev/null
+++ b/strndup.c
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2013 Karel Zak <kzak@redhat.com>
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "mutt.h"
+
+char *strndup(const char *s, size_t n)
+{
+       size_t len = strnlen(s, n);
+       char *new = (char *) malloc((len + 1) * sizeof(char));
+       if (!new)
+               return NULL;
+       new[len] = '\0';
+       return (char *) memcpy(new, s, len);
+}
diff --git a/strnlen.c b/strnlen.c
new file mode 100644 (file)
index 0000000..278a988
--- /dev/null
+++ b/strnlen.c
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2013 Karel Zak <kzak@redhat.com>
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "mutt.h"
+
+size_t strnlen(const char *s, size_t maxlen)
+{
+        int i;
+
+        for (i = 0; i < maxlen; i++) {
+                if (s[i] == '\0')
+                        return i + 1;
+        }
+        return maxlen;
+}