@matheusbsbs / matheus-bagliano / commits / c4fba0a358
Fork 0

Rename read_pipe() with read_fd() and make its buffer nul-terminated.

The new name is closer to the purpose of the function. A NUL-terminated buffer makes things easier when callers need that. Since the function returns only the memory written with data, almost always allocating more space than needed because final size is unknown, an extra NUL terminating the buffer is harmless. It is not included in the returned size, so the function remains working as before. Also, now the function allows the buffer passed to be NULL at first, and alloc_nr is now used for growing the buffer, instead size=*2. Signed-off-by: Carlos Rica <jasampler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlos Rica committed Jul 18, 2007 at 20:31 UTC c4fba0a358d43fdb2301dd122d68c49dd8471d71
4 files changed +20 -9
builtin-stripspace.c
+3 -1
@@ -79,8 +79,10 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
79
80 size = 1024;
81 buffer = xmalloc(size);
82 - if (read_pipe(0, &buffer, &size))
82 + if (read_fd(0, &buffer, &size)) {
83 + free(buffer);
84 die("could not read the input");
85 + }
86
87 size = stripspace(buffer, size, 0);
88 write_or_die(1, buffer, size);
cache.h
+1 -1
@@ -265,7 +265,7 @@ extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat
265 extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, int);
266 extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
267 extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);
268 -extern int read_pipe(int fd, char** return_buf, unsigned long* return_size);
268 +extern int read_fd(int fd, char **return_buf, unsigned long *return_size);
269 extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);
270 extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
271 extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
mktag.c
+1 -1
@@ -120,7 +120,7 @@ int main(int argc, char **argv)
120
121 setup_git_directory();
122
123 - if (read_pipe(0, &buffer, &size)) {
123 + if (read_fd(0, &buffer, &size)) {
124 free(buffer);
125 die("could not read from stdin");
126 }
sha1_file.c
+15 -6
@@ -2304,27 +2304,36 @@ int has_sha1_file(const unsigned char *sha1)
2304 *
2305 * returns 0 if anything went fine and -1 otherwise
2306 *
2307 + * The buffer is always NUL-terminated, not including it in returned size.
2308 + *
2309 * NOTE: both buf and size may change, but even when -1 is returned
2310 * you still have to free() it yourself.
2311 */
2310 -int read_pipe(int fd, char** return_buf, unsigned long* return_size)
2312 +int read_fd(int fd, char **return_buf, unsigned long *return_size)
2313 {
2312 - char* buf = *return_buf;
2314 + char *buf = *return_buf;
2315 unsigned long size = *return_size;
2316 ssize_t iret;
2317 unsigned long off = 0;
2318
2319 + if (!buf || size <= 1) {
2320 + size = 1024;
2321 + buf = xrealloc(buf, size);
2322 + }
2323 +
2324 do {
2318 - iret = xread(fd, buf + off, size - off);
2325 + iret = xread(fd, buf + off, (size - 1) - off);
2326 if (iret > 0) {
2327 off += iret;
2321 - if (off == size) {
2322 - size *= 2;
2328 + if (off == size - 1) {
2329 + size = alloc_nr(size);
2330 buf = xrealloc(buf, size);
2331 }
2332 }
2333 } while (iret > 0);
2334
2335 + buf[off] = '\0';
2336 +
2337 *return_buf = buf;
2338 *return_size = off;
2339
@@ -2339,7 +2348,7 @@ int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
2348 char *buf = xmalloc(size);
2349 int ret;
2350
2342 - if (read_pipe(fd, &buf, &size)) {
2351 + if (read_fd(fd, &buf, &size)) {
2352 free(buf);
2353 return -1;
2354 }