7
7
#include "cache.h"
8
8
#include "builtin.h"
9
9
#include "string-list.h"
10
+ #include "strbuf.h"
10
11
11
12
static const char git_mailsplit_usage [] =
12
13
"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)
42
43
return 1 ;
43
44
}
44
45
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 ;
47
47
48
48
/* We cannot use fgets() because our lines can contain NULs */
49
49
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)
71
71
static int split_one (FILE * mbox , const char * name , int allow_bare )
72
72
{
73
73
FILE * output = NULL ;
74
- int len = strlen (buf );
75
74
int fd ;
76
75
int status = 0 ;
77
- int is_bare = !is_from_line (buf , len );
76
+ int is_bare = !is_from_line (buf . buf , buf . len );
78
77
79
78
if (is_bare && !allow_bare )
80
79
goto corrupt ;
@@ -88,20 +87,17 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
88
87
* "From " and having something that looks like a date format.
89
88
*/
90
89
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 )
94
91
die_errno ("cannot write output" );
95
92
96
- len = read_line_with_nul (buf , sizeof (buf ), mbox );
97
- if (len == 0 ) {
93
+ if (strbuf_getwholeline (& buf , mbox , '\n' )) {
98
94
if (feof (mbox )) {
99
95
status = 1 ;
100
96
break ;
101
97
}
102
98
die_errno ("cannot read mbox" );
103
99
}
104
- if (!is_partial && ! is_bare && is_from_line (buf , len ))
100
+ if (!is_bare && is_from_line (buf . buf , buf . len ))
105
101
break ; /* done with one message */
106
102
}
107
103
fclose (output );
@@ -166,7 +162,7 @@ static int split_maildir(const char *maildir, const char *dir,
166
162
goto out ;
167
163
}
168
164
169
- if (fgets ( buf , sizeof ( buf ), f ) == NULL ) {
165
+ if (strbuf_getwholeline ( & buf , f , '\n' ) ) {
170
166
error ("cannot read mail %s (%s)" , file , strerror (errno ));
171
167
goto out ;
172
168
}
@@ -203,7 +199,7 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,
203
199
} while (isspace (peek ));
204
200
ungetc (peek , f );
205
201
206
- if (fgets ( buf , sizeof ( buf ), f ) == NULL ) {
202
+ if (strbuf_getwholeline ( & buf , f , '\n' ) ) {
207
203
/* empty stdin is OK */
208
204
if (f != stdin ) {
209
205
error ("cannot read mbox %s" , file );
0 commit comments