* New version 2.21.999
[alpine.git] / pith / osdep / hostname.c
blob12879611e101b51b53fce75d5556706b4ef07909
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: hostname.c 1176 2008-09-29 21:16:42Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2013-2018 Eduardo Chappa
8 * Copyright 2008 University of Washington
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include <system.h>
21 #if HAVE_GETHOSTNAME
23 #elif HAVE_UNAME
25 #include <sys/utsname.h>
27 #elif defined(SYSTEMID)
29 #elif defined(XENIX)
31 #endif
33 #include "hostname.h"
37 /*----------------------------------------------------------------------
38 Call system gethostname
40 Args: hostname -- buffer to return host name in
41 size -- Size of buffer hostname is to be returned in
43 Result: returns 0 if the hostname is correctly set,
44 -1 if not (and errno is set).
45 ----*/
46 int
47 hostname(char *hostname, int size)
49 #if HAVE_GETHOSTNAME
51 if (gethostname(hostname, size))
52 return -1;
54 /* sanity check of hostname string */
55 for (*dn = hname; (*dn > 0x20) && (*dn < 0x7f); ++dn)
58 if (*dn) /* non-ascii in hostname */
59 strncpy(hostname, "unknown", size-1);
61 hostname[size - 1] = '\0';
62 return 0;
64 #elif HAVE_UNAME
66 /** This routine compliments of Scott McGregor at the HP
67 Corporate Computing Center **/
69 int uname(struct utsname *);
70 struct utsname name;
72 (void)uname(&name);
73 (void)strncpy(hostname,name.nodename,size-1);
75 hostname[size - 1] = '\0';
76 return 0;
78 #elif defined(SYSTEMID)
79 char buf[32];
80 FILE *fp;
81 char *p;
83 if ((fp = our_fopen("/etc/systemid", "rb")) != 0) {
84 fgets(buf, sizeof(buf) - 1, fp);
85 fclose(fp);
86 if ((p = strindex(buf, '\n')) != NULL)
87 *p = '\0';
88 (void) strncpy(hostname, buf, size - 1);
89 hostname[size - 1] = '\0';
90 return 0;
93 #elif defined(XENIX)
95 #ifdef DOUNAME
96 /** This routine compliments of Scott McGregor at the HP
97 Corporate Computing Center **/
99 int uname();
100 struct utsname name;
102 (void) uname(&name);
103 (void) strncpy(hostname,name.nodename,size-1);
104 #else
105 (void) strncpy(hostname, HOSTNAME, size-1);
106 #endif /* DOUNAME */
108 hostname[size - 1] = '\0';
109 return 0;
110 #else
111 /* We shouldn't get here except for the windows
112 * case, which currently doesn't use this (as
113 * it appears nothing else does as well)
115 return -1;
117 #endif