@@ -36,66 +36,39 @@ char* get_default_hostname(void) {
3636 return strdup (FALLBACK_HOSTNAME );
3737}
3838
39- char * gethostname_malloc (void ) {
39+ int gethostname_full (GetHostnameFlags flags , char * * ret ) {
40+ _cleanup_free_ char * buf = NULL , * fallback = NULL ;
4041 struct utsname u ;
4142 const char * s ;
4243
43- /* This call tries to return something useful, either the actual hostname
44- * or it makes something up. The only reason it might fail is OOM.
45- * It might even return "localhost" if that's set. */
44+ assert (ret );
4645
4746 assert_se (uname (& u ) >= 0 );
4847
4948 s = u .nodename ;
50- if (isempty (s ) || streq (s , "(none)" ))
51- return get_default_hostname ();
52-
53- return strdup (s );
54- }
55-
56- char * gethostname_short_malloc (void ) {
57- struct utsname u ;
58- const char * s ;
59- _cleanup_free_ char * f = NULL ;
60-
61- /* Like above, but kills the FQDN part if present. */
62-
63- assert_se (uname (& u ) >= 0 );
64-
65- s = u .nodename ;
66- if (isempty (s ) || streq (s , "(none)" ) || s [0 ] == '.' ) {
67- s = f = get_default_hostname ();
49+ if (isempty (s ) ||
50+ (!FLAGS_SET (flags , GET_HOSTNAME_ALLOW_NONE ) && streq (s , "(none)" )) ||
51+ (!FLAGS_SET (flags , GET_HOSTNAME_ALLOW_LOCALHOST ) && is_localhost (s )) ||
52+ (FLAGS_SET (flags , GET_HOSTNAME_SHORT ) && s [0 ] == '.' )) {
53+ if (!FLAGS_SET (flags , GET_HOSTNAME_FALLBACK_DEFAULT ))
54+ return - ENXIO ;
55+
56+ s = fallback = get_default_hostname ();
6857 if (!s )
69- return NULL ;
58+ return - ENOMEM ;
7059
71- assert (s [0 ] != '.' );
60+ if (FLAGS_SET (flags , GET_HOSTNAME_SHORT ) && s [0 ] == '.' )
61+ return - ENXIO ;
7262 }
7363
74- return strndup (s , strcspn (s , "." ));
75- }
76-
77- int gethostname_strict (char * * ret ) {
78- struct utsname u ;
79- char * k ;
80-
81- /* This call will rather fail than make up a name. It will not return "localhost" either. */
82-
83- assert_se (uname (& u ) >= 0 );
84-
85- if (isempty (u .nodename ))
86- return - ENXIO ;
87-
88- if (streq (u .nodename , "(none)" ))
89- return - ENXIO ;
90-
91- if (is_localhost (u .nodename ))
92- return - ENXIO ;
93-
94- k = strdup (u .nodename );
95- if (!k )
64+ if (FLAGS_SET (flags , GET_HOSTNAME_SHORT ))
65+ buf = strndup (s , strcspn (s , "." ));
66+ else
67+ buf = strdup (s );
68+ if (!buf )
9669 return - ENOMEM ;
9770
98- * ret = k ;
71+ * ret = TAKE_PTR ( buf ) ;
9972 return 0 ;
10073}
10174
0 commit comments