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

[PATCH v3 1/3] Introduce die_errno() that appends strerror(errno) to die()

From
Thomas Rast <trast@student.ethz.ch>
Date
Jun 8, 2009, 21:02 UTC
Message-ID
<5f43aa9d0b69e99600a73bddd4c93217608ab661.1244494647.git.trast@student.ethz.ch>
In-Reply-To
<cover.1244494646.git.trast@student.ethz.ch>

There are many calls to die() that do, or should, report strerror(errno) to indicate how the syscall they guard failed. Introduce a small helper function for this case.

Note:
- POSIX says vsnprintf can modify errno in some unlikely cases, so we
  have to use errno early.
- We take some care to pass the original format to die_routine(), in
  case someone wants to call die_errno() with custom format
  characters.
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 git-compat-util.h |    1 +
 usage.c           |   12 ++++++++++++
 2 files changed, 13 insertions(+), 0 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index f25f7f1..0366cde 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -162,6 +162,7 @@
 /* General helper functions */
 extern void usage(const char *err) NORETURN;
 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
+extern void die_errno(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
 
diff --git a/usage.c b/usage.c
index 820d09f..fd936a1 100644
--- a/usage.c
+++ b/usage.c
@@ -60,6 +60,18 @@ void die(const char *err, ...)
 	va_end(params);
 }
 
+void die_errno(const char *fmt, ...)
+{
+	va_list params;
+	char fmt_with_err[1024];
+
+	snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, strerror(errno));
+
+	va_start(params, fmt);
+	die_routine(fmt_with_err, params);
+	va_end(params);
+}
+
 int error(const char *err, ...)
 {
 	va_list params;
-- 
1.6.3.2.333.g27636
Previous: Thomas RastNext: Thomas Rast
Message 2 of 14 in “die_errno()”
  1. 0/3 die_errno()Thomas Rast, Jun 8, 2009
  2. 1/3 Introduce die_errno() that appends strerror(errno) to die()Thomas Rast, Jun 8, 2009
  3. 2/3 Convert existing die(..., strerror(errno)) to die_errno()Thomas Rast, Jun 8, 2009
  4. 3/3 Use die_errno() instead of die() when checking syscallsThomas Rast, Jun 8, 2009
  5. Jeff KingJun 8, 2009
  6. Thomas RastJun 9, 2009
  7. Jeff KingJun 9, 2009
  8. Andreas EricssonJun 9, 2009
  9. 0/4 die_errno()Thomas Rast, Jun 27, 2009
  10. 1/4 Introduce die_errno() that appends strerror(errno) to die()Thomas Rast, Jun 27, 2009
  11. 2/4 die_errno(): double % in strerror() output just in caseThomas Rast, Jun 27, 2009
  12. 3/4 Convert existing die(..., strerror(errno)) to die_errno()Thomas Rast, Jun 27, 2009
  13. 4/4 Use die_errno() instead of die() when checking syscallsThomas Rast, Jun 27, 2009
  14. Junio C HamanoJun 27, 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.