* Fix an error in compilation when Alpine is not built with S/MIME
[alpine.git] / pith / osdep / hostname.c
blob6ba445d3285d1182878c0355de6659d80bdb5020
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 2008 University of Washington
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * ========================================================================
18 #include <system.h>
20 #if HAVE_GETHOSTNAME
22 #elif HAVE_UNAME
24 #include <sys/utsname.h>
26 #elif defined(SYSTEMID)
28 #elif defined(XENIX)
30 #endif
32 #include "hostname.h"
36 /*----------------------------------------------------------------------
37 Call system gethostname
39 Args: hostname -- buffer to return host name in
40 size -- Size of buffer hostname is to be returned in
42 Result: returns 0 if the hostname is correctly set,
43 -1 if not (and errno is set).
44 ----*/
45 int
46 hostname(char *hostname, int size)
48 #if HAVE_GETHOSTNAME
50 if (gethostname(hostname, size))
51 return -1;
53 /* sanity check of hostname string */
54 for (*dn = hname; (*dn > 0x20) && (*dn < 0x7f); ++dn)
57 if (*dn) /* non-ascii in hostname */
58 strncpy(hostname, "unknown", size-1);
60 hostname[size - 1] = '\0';
61 return 0;
63 #elif HAVE_UNAME
65 /** This routine compliments of Scott McGregor at the HP
66 Corporate Computing Center **/
68 int uname(struct utsname *);
69 struct utsname name;
71 (void)uname(&name);
72 (void)strncpy(hostname,name.nodename,size-1);
74 hostname[size - 1] = '\0';
75 return 0;
77 #elif defined(SYSTEMID)
78 char buf[32];
79 FILE *fp;
80 char *p;
82 if ((fp = our_fopen("/etc/systemid", "rb")) != 0) {
83 fgets(buf, sizeof(buf) - 1, fp);
84 fclose(fp);
85 if ((p = strindex(buf, '\n')) != NULL)
86 *p = '\0';
87 (void) strncpy(hostname, buf, size - 1);
88 hostname[size - 1] = '\0';
89 return 0;
92 #elif defined(XENIX)
94 #ifdef DOUNAME
95 /** This routine compliments of Scott McGregor at the HP
96 Corporate Computing Center **/
98 int uname();
99 struct utsname name;
101 (void) uname(&name);
102 (void) strncpy(hostname,name.nodename,size-1);
103 #else
104 (void) strncpy(hostname, HOSTNAME, size-1);
105 #endif /* DOUNAME */
107 hostname[size - 1] = '\0';
108 return 0;
109 #else
110 /* We shouldn't get here except for the windows
111 * case, which currently doesn't use this (as
112 * it appears nothing else does as well)
114 return -1;
116 #endif