aboutsummaryrefslogtreecommitdiff
path: root/libbb/safe_gethostname.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/safe_gethostname.c')
-rw-r--r--libbb/safe_gethostname.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/libbb/safe_gethostname.c b/libbb/safe_gethostname.c
index bdb989631..233a6a418 100644
--- a/libbb/safe_gethostname.c
+++ b/libbb/safe_gethostname.c
@@ -25,7 +25,9 @@
25 */ 25 */
26 26
27#include "libbb.h" 27#include "libbb.h"
28#if defined(__linux__)
28#include <sys/utsname.h> 29#include <sys/utsname.h>
30#endif
29 31
30/* 32/*
31 * On success return the current malloced and NUL terminated hostname. 33 * On success return the current malloced and NUL terminated hostname.
@@ -35,6 +37,7 @@
35 */ 37 */
36char* FAST_FUNC safe_gethostname(void) 38char* FAST_FUNC safe_gethostname(void)
37{ 39{
40#if defined(__linux__)
38 struct utsname uts; 41 struct utsname uts;
39 42
40 /* The length of the arrays in a struct utsname is unspecified; 43 /* The length of the arrays in a struct utsname is unspecified;
@@ -49,6 +52,13 @@ char* FAST_FUNC safe_gethostname(void)
49 /* Uname can fail only if you pass a bad pointer to it. */ 52 /* Uname can fail only if you pass a bad pointer to it. */
50 uname(&uts); 53 uname(&uts);
51 return xstrndup(!uts.nodename[0] ? "?" : uts.nodename, sizeof(uts.nodename)); 54 return xstrndup(!uts.nodename[0] ? "?" : uts.nodename, sizeof(uts.nodename));
55#else
56 /* We really don't care about people with host names wider than most screens */
57 char buf[256];
58 int r = gethostname(buf, sizeof(buf));
59 buf[sizeof(buf)-1] = '\0';
60 return xstrdup(r < 0 ? "?" : buf);
61#endif
52} 62}
53 63
54/* 64/*
@@ -66,9 +76,12 @@ char* FAST_FUNC safe_getdomainname(void)
66 return xstrndup(!uts.domainname[0] ? "?" : uts.domainname, sizeof(uts.domainname)); 76 return xstrndup(!uts.domainname[0] ? "?" : uts.domainname, sizeof(uts.domainname));
67#else 77#else
68 /* We really don't care about people with domain names wider than most screens */ 78 /* We really don't care about people with domain names wider than most screens */
79 /*
69 char buf[256]; 80 char buf[256];
70 int r = getdomainname(buf, sizeof(buf)); 81 int r = getdomainname(buf, sizeof(buf));
71 buf[sizeof(buf)-1] = '\0'; 82 buf[sizeof(buf)-1] = '\0';
72 return xstrdup(r < 0 ? "?" : buf); 83 return xstrdup(r < 0 ? "?" : buf);
84 */
85 return xstrdup("?");
73#endif 86#endif
74} 87}