summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandall Leeds <randall@apache.org>2011-11-30 02:20:27 -0800
committerRandall Leeds <randall@apache.org>2012-01-07 19:42:56 -0800
commit74d4c91f0dcbfbb9772f7f8b71df9628ad242f53 (patch)
tree2ae9e581746ad1eb19352e98a285e7d4bf791710
parent2e60492716658da175f499340678189e1213254c (diff)
downloadcouchdb-74d4c91f0dcbfbb9772f7f8b71df9628ad242f53.tar.gz
avoid unneccesary calls to strlen
-rw-r--r--src/couchdb/priv/couch_js/util.c18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/couchdb/priv/couch_js/util.c b/src/couchdb/priv/couch_js/util.c
index 3076856a4..f45ee38d8 100644
--- a/src/couchdb/priv/couch_js/util.c
+++ b/src/couchdb/priv/couch_js/util.c
@@ -20,11 +20,12 @@
#include "utf8.h"
-char*
-slurp_file(char* buf, const char* file)
+size_t
+slurp_file(const char* file, char** outbuf_p)
{
FILE* fp;
char fbuf[16384];
+ char *buf = NULL;
char* tmp;
size_t nread = 0;
size_t buflen = 0;
@@ -41,16 +42,13 @@ slurp_file(char* buf, const char* file)
while((nread = fread(fbuf, 1, 16384, fp)) > 0) {
if(buf == NULL) {
- buflen = nread;
buf = (char*) malloc(nread + 1);
if(buf == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(3);
}
- memcpy(buf, fbuf, buflen);
- buf[buflen] = '\0';
+ memcpy(buf, fbuf, nread);
} else {
- buflen = strlen(buf);
tmp = (char*) malloc(buflen + nread + 1);
if(tmp == NULL) {
fprintf(stderr, "Out of memory.\n");
@@ -58,12 +56,14 @@ slurp_file(char* buf, const char* file)
}
memcpy(tmp, buf, buflen);
memcpy(tmp+buflen, fbuf, nread);
- tmp[buflen+nread] = '\0';
free(buf);
buf = tmp;
}
+ buflen += nread;
+ buf[buflen] = '\0';
}
- return buf;
+ *outbuf_p = buf;
+ return buflen + 1;
}
couch_args*
@@ -104,7 +104,7 @@ couch_parse_args(int argc, const char* argv[])
}
while(i < argc) {
- args->script = slurp_file(args->script, argv[i]);
+ slurp_file(argv[i], &args->script);
if(args->script_name == NULL) {
if(strcmp(argv[i], "-") == 0) {
args->script_name = "<stdin>";