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

[PATCH 2/3] version: refactor get_uname_info()

From
Christian Couder <christian.couder@gmail.com>
Date
Jun 19, 2024, 12:57 UTC
Message-ID
<20240619125708.3719150-3-christian.couder@gmail.com>
In-Reply-To
<20240619125708.3719150-1-christian.couder@gmail.com>

Some code from "builtin/bugreport.c" uses uname(2) to get system information.

Let's refactor this code into a new get_uname_info() function, so that we can reuse it in a following commit.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/bugreport.c | 13 ++-----------
 version.c           | 20 ++++++++++++++++++++
 version.h           |  7 +++++++
 3 files changed, 29 insertions(+), 11 deletions(-)
diff --git a/builtin/bugreport.c b/builtin/bugreport.c
index b3cc77af53..b24f876c41 100644
--- a/builtin/bugreport.c
+++ b/builtin/bugreport.c
@@ -11,10 +11,10 @@
 #include "diagnose.h"
 #include "object-file.h"
 #include "setup.h"
+#include "version.h"
 
 static void get_system_info(struct strbuf *sys_info)
 {
-	struct utsname uname_info;
 	char *shell = NULL;
 
 	/* get git version from native cmd */
@@ -23,16 +23,7 @@ static void get_system_info(struct strbuf *sys_info)
 
 	/* system call for other version info */
 	strbuf_addstr(sys_info, "uname: ");
-	if (uname(&uname_info))
-		strbuf_addf(sys_info, _("uname() failed with error '%s' (%d)\n"),
-			    strerror(errno),
-			    errno);
-	else
-		strbuf_addf(sys_info, "%s %s %s %s\n",
-			    uname_info.sysname,
-			    uname_info.release,
-			    uname_info.version,
-			    uname_info.machine);
+	get_uname_info(sys_info);
 
 	strbuf_addstr(sys_info, _("compiler info: "));
 	get_compiler_info(sys_info);
diff --git a/version.c b/version.c
index 331ee6c372..10b9fa77d1 100644
--- a/version.c
+++ b/version.c
@@ -1,6 +1,7 @@
 #include "git-compat-util.h"
 #include "version.h"
 #include "strbuf.h"
+#include "gettext.h"
 
 const char git_version_string[] = GIT_VERSION;
 const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
@@ -41,3 +42,22 @@ const char *git_user_agent_sanitized(void)
 
 	return agent;
 }
+
+int get_uname_info(struct strbuf *buf)
+{
+	struct utsname uname_info;
+
+	if (uname(&uname_info)) {
+		strbuf_addf(buf, _("uname() failed with error '%s' (%d)\n"),
+			    strerror(errno),
+			    errno);
+		return -1;
+	}
+
+	strbuf_addf(buf, "%s %s %s %s\n",
+		    uname_info.sysname,
+		    uname_info.release,
+		    uname_info.version,
+		    uname_info.machine);
+	return 0;
+}
diff --git a/version.h b/version.h
index 7c62e80577..afe3dbbab7 100644
--- a/version.h
+++ b/version.h
@@ -7,4 +7,11 @@ extern const char git_built_from_commit_string[];
 const char *git_user_agent(void);
 const char *git_user_agent_sanitized(void);
 
+/*
+  Try to get information about the system using uname(2).
+  Return -1 and put an error message into 'buf' in case of uname()
+  error. Return 0 and put uname info into 'buf' otherwise.
+*/
+int get_uname_info(struct strbuf *buf);
+
 #endif /* VERSION_H */
-- 
2.45.2.563.g6aa460b3cb
Previous: Eric SunshineNext: Christian Couder
Message 4 of 22 in “Advertise OS version”
  1. 0/3 Advertise OS versionChristian Couder, Jun 19, 2024
  2. 1/3 version: refactor strbuf_sanitize()Christian Couder, Jun 19, 2024
  3. Eric SunshineJun 19, 2024
  4. 2/3 version: refactor get_uname_info()Christian Couder, Jun 19, 2024
  5. 3/3 connect: advertise OS versionChristian Couder, Jun 19, 2024
  6. Dragan SimicJun 19, 2024
  7. Jeff KingJun 19, 2024
  8. Dragan SimicJun 19, 2024
  9. Christian CouderJun 19, 2024
  10. Dragan SimicJun 19, 2024
  11. rsbecker@nexbridge.comJun 19, 2024
  12. Jeff KingJun 19, 2024
  13. rsbecker@nexbridge.comJun 19, 2024
  14. Dragan SimicJun 19, 2024
  15. Jeff KingJun 19, 2024
  16. rsbecker@nexbridge.comJun 19, 2024
  17. brian m. carlsonJun 19, 2024
  18. Jeff KingJun 20, 2024
  19. Junio C HamanoJun 20, 2024
  20. Usman AkinyemiDec 9, 2024
  21. rsbecker@nexbridge.comDec 9, 2024
  22. Usman AkinyemiDec 10, 2024

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.