git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/4] strbuf: add new function strbuf_getwholeline()

From
Brandon Casey <casey@nrlssc.navy.mil>
Date
Aug 5, 2009, 03:31 UTC
Message-ID
<r3l_p2g-BpVHWKE-kMWIRydJaW0FHLKBpE497REXzOgqPjLUFjPkJ-YKp1tkrIs3CwcppURiH8o@cipher.nrlssc.navy.mil>
In-Reply-To
<r3l_p2g-BpVHWKE-kMWIRzBGUCnzo9_l7hOHzYLG_4X6oEjXrJ4rJdB10yXPT2jmJJ7ppBmr-x8@cipher.nrlssc.navy.mil>
From: Brandon Casey <drafnel@gmail.com>

This function is just like strbuf_getline() except it retains the line-termination character. This function will be used by the mailinfo and mailsplit builtins which require the entire line for parsing.

Signed-off-by: Brandon Casey <drafnel@gmail.com>
---
 strbuf.c |   15 ++++++++++++---
 strbuf.h |    1 +
 2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index f03d117..a6153dc 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -322,7 +322,7 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
 	return -1;
 }
 
-int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
+int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 {
 	int ch;
 
@@ -332,10 +332,10 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
 
 	strbuf_reset(sb);
 	while ((ch = fgetc(fp)) != EOF) {
-		if (ch == term)
-			break;
 		strbuf_grow(sb, 1);
 		sb->buf[sb->len++] = ch;
+		if (ch == term)
+			break;
 	}
 	if (ch == EOF && sb->len == 0)
 		return EOF;
@@ -344,6 +344,15 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
 	return 0;
 }
 
+int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
+{
+	if (strbuf_getwholeline(sb, fp, term))
+		return EOF;
+	if (sb->buf[sb->len-1] == term)
+		strbuf_setlen(sb, sb->len-1);
+	return 0;
+}
+
 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
 {
 	int fd, len;
diff --git a/strbuf.h b/strbuf.h
index eaa8704..d05e056 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -126,6 +126,7 @@ extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
 extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
 extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
 
+extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
 extern int strbuf_getline(struct strbuf *, FILE *, int);
 
 extern void stripspace(struct strbuf *buf, int skip_comments);
-- 
1.6.4
Previous: Brandon CaseyNext: Brandon Casey
Message 29 of 33 in “Please make git-am handle \r\n-damaged patches”
  1. H. Peter AnvinAug 3, 2009
  2. Junio C HamanoAug 3, 2009
  3. Wesley J. LandakerAug 3, 2009
  4. H. Peter AnvinAug 3, 2009
  5. H. Peter AnvinAug 3, 2009
  6. Sverre RabbelierAug 3, 2009
  7. H. Peter AnvinAug 3, 2009
  8. Paolo BonziniAug 4, 2009
  9. Alex RiesenAug 4, 2009
  10. Junio C HamanoAug 4, 2009
  11. Let mailsplit and mailinfo handle mails with CRLF line-endingsAlex Riesen, Aug 4, 2009
  12. Sverre RabbelierAug 4, 2009
  13. Brandon CaseyAug 4, 2009
  14. Alex RiesenAug 4, 2009
  15. Let mailsplit and mailinfo handle mails with CRLF line-endingsAlex Riesen, Aug 4, 2009
  16. Brandon CaseyAug 4, 2009
  17. Brandon CaseyAug 4, 2009
  18. Alex RiesenAug 4, 2009
  19. Alex RiesenAug 4, 2009
  20. Alex RiesenAug 4, 2009
  21. Brandon CaseyAug 4, 2009
  22. Erik Faye-LundAug 4, 2009
  23. Alex RiesenAug 4, 2009
  24. Erik Faye-LundAug 4, 2009
  25. Tony FinchAug 5, 2009
  26. Nanako ShiraishiAug 4, 2009
  27. Junio C HamanoAug 4, 2009
  28. 0/4 Re: Please make git-am handle \r\n-damaged patchesBrandon Casey, Aug 5, 2009
  29. 1/4 strbuf: add new function strbuf_getwholeline()Brandon Casey, Aug 5, 2009
  30. 2/4 builtin-mailinfo,builtin-mailsplit: use strbufsBrandon Casey, Aug 5, 2009
  31. 3/4 builtin-mailsplit.c: remove read_line_with_nul() since it is no longer usedBrandon Casey, Aug 5, 2009
  32. 4/4 Allow mailsplit (and hence git-am) to handle mails with CRLF line-endingsBrandon Casey, Aug 5, 2009
  33. Alex RiesenAug 5, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.