• R/O
  • SSH
  • HTTPS

yash: Commit


Commit MetaInfo

Revision3913 (tree)
Time2018-11-24 14:13:11
Authormagicant

Log Message

Use strdup if available

The strdup function is mandatory in POSIX.1-2008 and many systems
support it. Let's take advantage of it.

Change Summary

Incremental Difference

--- yash/trunk/configure (revision 3912)
+++ yash/trunk/configure (revision 3913)
@@ -868,6 +868,29 @@
868868 defconfigh "HAVE_STRNLEN"
869869 fi
870870
871+# check for strdup
872+checking 'for strdup'
873+cat >"${tempsrc}" <<END
874+${confighdefs}
875+#include <string.h>
876+#ifndef strdup
877+char *strdup(const char*);
878+#endif
879+int main(void) {
880+ char *dup = strdup("12345");
881+ if (!dup) return 1;
882+ int cmp = strcmp(dup, "12345");
883+ free(dup);
884+ return cmp != 0;
885+}
886+END
887+trymake && tryexec
888+checked
889+if [ x"${checkresult}" = x"yes" ]
890+then
891+ defconfigh "HAVE_STRDUP"
892+fi
893+
871894 # check for wcsnlen
872895 checking 'for wcsnlen'
873896 cat >"${tempsrc}" <<END
--- yash/trunk/util.c (revision 3912)
+++ yash/trunk/util.c (revision 3913)
@@ -60,6 +60,7 @@
6060 }
6161 #endif
6262
63+#if !HAVE_STRDUP
6364 /* Returns a newly malloced copy of the specified string.
6465 * Aborts the program on malloc failure. */
6566 char *xstrdup(const char *s)
@@ -70,6 +71,7 @@
7071 result[len] = '\0';
7172 return memcpy(result, s, len);
7273 }
74+#endif
7375
7476 #if !HAVE_WCSNLEN
7577 /* Returns min(maxlen, wcslen(s)). */
--- yash/trunk/util.h (revision 3912)
+++ yash/trunk/util.h (revision 3913)
@@ -194,13 +194,21 @@
194194 # ifndef strnlen
195195 extern size_t strnlen(const char *s, size_t maxlen);
196196 # endif
197-# define xstrnlen(s,maxlen) strnlen(s,maxlen)
197+# define xstrnlen strnlen
198198 #endif
199+
200+#if HAVE_STRDUP
201+# ifndef strdup
202+extern char *strdup(const char *s);
203+# endif
204+# define xstrdup strdup
205+#endif
206+
199207 #if HAVE_WCSNLEN
200208 # ifndef wcsnlen
201209 extern size_t wcsnlen(const wchar_t *s, size_t maxlen);
202210 # endif
203-# define xwcsnlen(s,maxlen) wcsnlen(s,maxlen)
211+# define xwcsnlen wcsnlen
204212 #endif
205213
206214 /* Returns a newly malloced copy of the specified string.
Show on old repository browser