Skip to content

Commit 0a78d61

Browse files
Unique-Usmangitster
authored andcommitted
version: refactor get_uname_info()
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. Mentored-by: Christian Couder <[email protected]> Signed-off-by: Usman Akinyemi <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cdfd081 commit 0a78d61

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

builtin/bugreport.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
#include "diagnose.h"
1313
#include "object-file.h"
1414
#include "setup.h"
15+
#include "version.h"
1516

1617
static void get_system_info(struct strbuf *sys_info)
1718
{
18-
struct utsname uname_info;
1919
char *shell = NULL;
2020

2121
/* get git version from native cmd */
@@ -24,16 +24,7 @@ static void get_system_info(struct strbuf *sys_info)
2424

2525
/* system call for other version info */
2626
strbuf_addstr(sys_info, "uname: ");
27-
if (uname(&uname_info))
28-
strbuf_addf(sys_info, _("uname() failed with error '%s' (%d)\n"),
29-
strerror(errno),
30-
errno);
31-
else
32-
strbuf_addf(sys_info, "%s %s %s %s\n",
33-
uname_info.sysname,
34-
uname_info.release,
35-
uname_info.version,
36-
uname_info.machine);
27+
get_uname_info(sys_info);
3728

3829
strbuf_addstr(sys_info, _("compiler info: "));
3930
get_compiler_info(sys_info);

version.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "version-def.h"
44
#include "strbuf.h"
55
#include "sane-ctype.h"
6+
#include "gettext.h"
67

78
const char git_version_string[] = GIT_VERSION;
89
const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
@@ -47,3 +48,22 @@ const char *git_user_agent_sanitized(void)
4748

4849
return agent;
4950
}
51+
52+
int get_uname_info(struct strbuf *buf)
53+
{
54+
struct utsname uname_info;
55+
56+
if (uname(&uname_info)) {
57+
strbuf_addf(buf, _("uname() failed with error '%s' (%d)\n"),
58+
strerror(errno),
59+
errno);
60+
return -1;
61+
}
62+
63+
strbuf_addf(buf, "%s %s %s %s\n",
64+
uname_info.sysname,
65+
uname_info.release,
66+
uname_info.version,
67+
uname_info.machine);
68+
return 0;
69+
}

version.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ extern const char git_built_from_commit_string[];
77
const char *git_user_agent(void);
88
const char *git_user_agent_sanitized(void);
99

10+
/*
11+
Try to get information about the system using uname(2).
12+
Return -1 and put an error message into 'buf' in case of uname()
13+
error. Return 0 and put uname info into 'buf' otherwise.
14+
*/
15+
int get_uname_info(struct strbuf *buf);
16+
1017
#endif /* VERSION_H */

0 commit comments

Comments
 (0)