summaryrefslogtreecommitdiff
path: root/file.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-09-16 09:24:44 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-09-16 09:24:44 +0000
commit315cbef8358eb4f1fae3faba90c459054ad2a2c0 (patch)
treeefdfccec210226e9df20b9e87b71dd55670a82bd /file.c
parentf3dd7c49630a771acafd2d4f358ff2e7a74b4d15 (diff)
file.c: reduce xmalloc
* file.c (rb_readlink): read symlink in the result string directly. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36984 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'file.c')
-rw-r--r--file.c12
1 files changed, 5 insertions, 7 deletions
diff --git a/file.c b/file.c
index ba7fea7b0f..6940142653 100644
--- a/file.c
+++ b/file.c
@@ -2443,7 +2443,6 @@ rb_file_s_readlink(VALUE klass, VALUE path)
static VALUE
rb_readlink(VALUE path)
{
- char *buf;
int size = 100;
ssize_t rv;
VALUE v;
@@ -2451,21 +2450,20 @@ rb_readlink(VALUE path)
rb_secure(2);
FilePathValue(path);
path = rb_str_encode_ospath(path);
- buf = xmalloc(size);
- while ((rv = readlink(RSTRING_PTR(path), buf, size)) == size
+ v = rb_enc_str_new(0, size, rb_filesystem_encoding());
+ while ((rv = readlink(RSTRING_PTR(path), RSTRING_PTR(v), size)) == size
#ifdef _AIX
|| (rv < 0 && errno == ERANGE) /* quirky behavior of GPFS */
#endif
) {
+ rb_str_modify_expand(v, size);
size *= 2;
- buf = xrealloc(buf, size);
}
if (rv < 0) {
- xfree(buf);
+ rb_str_resize(v, 0);
rb_sys_fail_path(path);
}
- v = rb_filesystem_str_new(buf, rv);
- xfree(buf);
+ rb_str_resize(v, rv);
return v;
}