Skip to content

Commit c8f373a

Browse files
drafnelgitster
authored andcommitted
builtin-mailinfo,builtin-mailsplit: use strbufs
There should be no functional change. Just the necessary changes and simplifications associated with calling strbuf_getwholeline() rather than an internal function or fgets. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c7e4f0d commit c8f373a

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

builtin-mailinfo.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,6 @@ static void handle_filter(struct strbuf *line)
765765

766766
static void handle_body(void)
767767
{
768-
int len = 0;
769768
struct strbuf prev = STRBUF_INIT;
770769

771770
/* Skip up to the first boundary */
@@ -775,8 +774,6 @@ static void handle_body(void)
775774
}
776775

777776
do {
778-
strbuf_setlen(&line, line.len + len);
779-
780777
/* process any boundary lines */
781778
if (*content_top && is_multipart_boundary(&line)) {
782779
/* flush any leftover */
@@ -832,10 +829,7 @@ static void handle_body(void)
832829
handle_filter(&line);
833830
}
834831

835-
strbuf_reset(&line);
836-
if (strbuf_avail(&line) < 100)
837-
strbuf_grow(&line, 100);
838-
} while ((len = read_line_with_nul(line.buf, strbuf_avail(&line), fin)));
832+
} while (!strbuf_getwholeline(&line, fin, '\n'));
839833

840834
handle_body_out:
841835
strbuf_release(&prev);

builtin-mailsplit.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "cache.h"
88
#include "builtin.h"
99
#include "string-list.h"
10+
#include "strbuf.h"
1011

1112
static const char git_mailsplit_usage[] =
1213
"git mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> [<mbox>|<Maildir>...]";
@@ -42,8 +43,7 @@ static int is_from_line(const char *line, int len)
4243
return 1;
4344
}
4445

45-
/* Could be as small as 64, enough to hold a Unix "From " line. */
46-
static char buf[4096];
46+
static struct strbuf buf = STRBUF_INIT;
4747

4848
/* We cannot use fgets() because our lines can contain NULs */
4949
int read_line_with_nul(char *buf, int size, FILE *in)
@@ -71,10 +71,9 @@ int read_line_with_nul(char *buf, int size, FILE *in)
7171
static int split_one(FILE *mbox, const char *name, int allow_bare)
7272
{
7373
FILE *output = NULL;
74-
int len = strlen(buf);
7574
int fd;
7675
int status = 0;
77-
int is_bare = !is_from_line(buf, len);
76+
int is_bare = !is_from_line(buf.buf, buf.len);
7877

7978
if (is_bare && !allow_bare)
8079
goto corrupt;
@@ -88,20 +87,17 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
8887
* "From " and having something that looks like a date format.
8988
*/
9089
for (;;) {
91-
int is_partial = len && buf[len-1] != '\n';
92-
93-
if (fwrite(buf, 1, len, output) != len)
90+
if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
9491
die_errno("cannot write output");
9592

96-
len = read_line_with_nul(buf, sizeof(buf), mbox);
97-
if (len == 0) {
93+
if (strbuf_getwholeline(&buf, mbox, '\n')) {
9894
if (feof(mbox)) {
9995
status = 1;
10096
break;
10197
}
10298
die_errno("cannot read mbox");
10399
}
104-
if (!is_partial && !is_bare && is_from_line(buf, len))
100+
if (!is_bare && is_from_line(buf.buf, buf.len))
105101
break; /* done with one message */
106102
}
107103
fclose(output);
@@ -166,7 +162,7 @@ static int split_maildir(const char *maildir, const char *dir,
166162
goto out;
167163
}
168164

169-
if (fgets(buf, sizeof(buf), f) == NULL) {
165+
if (strbuf_getwholeline(&buf, f, '\n')) {
170166
error("cannot read mail %s (%s)", file, strerror(errno));
171167
goto out;
172168
}
@@ -203,7 +199,7 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,
203199
} while (isspace(peek));
204200
ungetc(peek, f);
205201

206-
if (fgets(buf, sizeof(buf), f) == NULL) {
202+
if (strbuf_getwholeline(&buf, f, '\n')) {
207203
/* empty stdin is OK */
208204
if (f != stdin) {
209205
error("cannot read mbox %s", file);

0 commit comments

Comments
 (0)