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>
This commit is contained in:
Carlos Rica 2007-07-18 20:31:03 +02:00 committed by Junio C Hamano
parent 6fb73e442a
commit c4fba0a358
4 changed files with 20 additions and 9 deletions

View File

@ -79,8 +79,10 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
size = 1024; size = 1024;
buffer = xmalloc(size); buffer = xmalloc(size);
if (read_pipe(0, &buffer, &size)) if (read_fd(0, &buffer, &size)) {
free(buffer);
die("could not read the input"); die("could not read the input");
}
size = stripspace(buffer, size, 0); size = stripspace(buffer, size, 0);
write_or_die(1, buffer, size); write_or_die(1, buffer, size);

View File

@ -265,7 +265,7 @@ extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat
extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, int); extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, int);
extern int ce_path_match(const struct cache_entry *ce, const char **pathspec); extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path); extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);
extern int read_pipe(int fd, char** return_buf, unsigned long* return_size); extern int read_fd(int fd, char **return_buf, unsigned long *return_size);
extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object); extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);
extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object); extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st); extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);

View File

@ -120,7 +120,7 @@ int main(int argc, char **argv)
setup_git_directory(); setup_git_directory();
if (read_pipe(0, &buffer, &size)) { if (read_fd(0, &buffer, &size)) {
free(buffer); free(buffer);
die("could not read from stdin"); die("could not read from stdin");
} }

View File

@ -2304,27 +2304,36 @@ int has_sha1_file(const unsigned char *sha1)
* *
* returns 0 if anything went fine and -1 otherwise * returns 0 if anything went fine and -1 otherwise
* *
* The buffer is always NUL-terminated, not including it in returned size.
*
* NOTE: both buf and size may change, but even when -1 is returned * NOTE: both buf and size may change, but even when -1 is returned
* you still have to free() it yourself. * you still have to free() it yourself.
*/ */
int read_pipe(int fd, char** return_buf, unsigned long* return_size) int read_fd(int fd, char **return_buf, unsigned long *return_size)
{ {
char* buf = *return_buf; char *buf = *return_buf;
unsigned long size = *return_size; unsigned long size = *return_size;
ssize_t iret; ssize_t iret;
unsigned long off = 0; unsigned long off = 0;
if (!buf || size <= 1) {
size = 1024;
buf = xrealloc(buf, size);
}
do { do {
iret = xread(fd, buf + off, size - off); iret = xread(fd, buf + off, (size - 1) - off);
if (iret > 0) { if (iret > 0) {
off += iret; off += iret;
if (off == size) { if (off == size - 1) {
size *= 2; size = alloc_nr(size);
buf = xrealloc(buf, size); buf = xrealloc(buf, size);
} }
} }
} while (iret > 0); } while (iret > 0);
buf[off] = '\0';
*return_buf = buf; *return_buf = buf;
*return_size = off; *return_size = off;
@ -2339,7 +2348,7 @@ int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
char *buf = xmalloc(size); char *buf = xmalloc(size);
int ret; int ret;
if (read_pipe(fd, &buf, &size)) { if (read_fd(fd, &buf, &size)) {
free(buf); free(buf);
return -1; return -1;
} }