summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-11-04 07:50:19 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-11-04 07:50:19 +0000
commit8df1863dcbd9e19b5e7f60c2e06e088105c975f5 (patch)
tree3995ac160a013bba6bfd37203e3687bbf8539ac3 /ext
parent3553a86eb51365cc524cf5c549b37770448d550d (diff)
downloadruby-8df1863dcbd9e19b5e7f60c2e06e088105c975f5.tar.gz
tkutil.c: strndup
* ext/tk/tkutil/tkutil.c (strndup): fallback definition. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52449 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/tk/tkutil/tkutil.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/ext/tk/tkutil/tkutil.c b/ext/tk/tkutil/tkutil.c
index 2bfb6bf50a..5d6613c7c2 100644
--- a/ext/tk/tkutil/tkutil.c
+++ b/ext/tk/tkutil/tkutil.c
@@ -90,6 +90,24 @@ static unsigned long CALLBACK_ID_NUM = 0;
/*************************************/
+#ifndef HAVE_STRNDUP
+static char * strndup _((const char *, size_t));
+static char *
+strndup(ptr, len)
+ const char *ptr;
+ size_t len;
+{
+ char *newptr = malloc(len + 1);
+ if (newptr) {
+ memcpy(newptr, ptr, len);
+ newptr[len] = '\0';
+ }
+ return newptr;
+}
+#endif
+
+/*************************************/
+
#if defined(HAVE_RB_OBJ_INSTANCE_EXEC) && !defined(RUBY_VM)
extern VALUE rb_obj_instance_exec _((int, VALUE*, VALUE));
#endif
@@ -1593,17 +1611,8 @@ cbsubst_table_setup(argc, argv, self)
chr = (unsigned char)(0x80 + idx);
subst_inf->keylen[chr] = RSTRING_LEN(infp[0]);
-#if HAVE_STRNDUP
subst_inf->key[chr] = strndup(RSTRING_PTR(infp[0]),
RSTRING_LEN(infp[0]));
-#else
- subst_inf->key[chr] = malloc(RSTRING_LEN(infp[0]) + 1);
- if (subst_inf->key[chr]) {
- strncpy(subst_inf->key[chr], RSTRING_PTR(infp[0]),
- RSTRING_LEN(infp[0]) + 1);
- subst_inf->key[chr][RSTRING_LEN(infp[0])] = '\0';
- }
-#endif
subst_inf->type[chr] = NUM2CHR(infp[1]);
subst_inf->full_subst_length += (subst_inf->keylen[chr] + 2);