Skip to content

Commit 825b922

Browse files
benpeartgitster
authored andcommitted
pkt-line: add packet_read_line_gently()
Add packet_read_line_gently() to enable reading a line without dying on EOF. Signed-off-by: Ben Peart <benpeart@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 974b50c commit 825b922

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

pkt-line.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,18 @@ char *packet_read_line(int fd, int *len_p)
323323
return packet_read_line_generic(fd, NULL, NULL, len_p);
324324
}
325325

326+
int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
327+
{
328+
int len = packet_read(fd, NULL, NULL,
329+
packet_buffer, sizeof(packet_buffer),
330+
PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
331+
if (dst_len)
332+
*dst_len = len;
333+
if (dst_line)
334+
*dst_line = (len > 0) ? packet_buffer : NULL;
335+
return len;
336+
}
337+
326338
char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
327339
{
328340
return packet_read_line_generic(-1, src, src_len, dst_len);

pkt-line.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ int packet_read(int fd, char **src_buffer, size_t *src_len, char
7373
*/
7474
char *packet_read_line(int fd, int *size);
7575

76+
/*
77+
* Convenience wrapper for packet_read that sets the PACKET_READ_GENTLE_ON_EOF
78+
* and CHOMP_NEWLINE options. The return value specifies the number of bytes
79+
* read into the buffer or -1 on truncated input. If the *dst_line parameter
80+
* is not NULL it will return NULL for a flush packet or when the number of
81+
* bytes copied is zero and otherwise points to a static buffer (that may be
82+
* overwritten by subsequent calls). If the size parameter is not NULL, the
83+
* length of the packet is written to it.
84+
*/
85+
int packet_read_line_gently(int fd, int *size, char **dst_line);
86+
7687
/*
7788
* Same as packet_read_line, but read from a buf rather than a descriptor;
7889
* see packet_read for details on how src_* is used.

0 commit comments

Comments
 (0)