source: trunk/third/sysinfo/gethost.c @ 12269

Revision 12269, 2.4 KB checked in by ghudson, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r12268, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2 * Copyright (c) 1992-1998 Michael A. Cooper.
3 * This software may be freely used and distributed provided it is not
4 * sold for profit or used in part or in whole for commercial gain
5 * without prior written agreement, and the author is credited
6 * appropriately.
7 */
8
9#ifndef lint
10static char *RCSid = "$Revision: 1.1.1.3 $";
11#endif
12
13/*
14 * Get Host name information functions
15 */
16
17#include <netdb.h>                      /* XXX Avoid buggy AIX 3.2.5 cpp */
18#include "defs.h"
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22
23/*
24 * Get our hostname
25 */
26extern char *GetHostName()
27{
28    static char                 Buf[MAXHOSTNAMLEN+1];
29
30    gethostname(Buf, sizeof(Buf));
31
32    return((Buf[0]) ? Buf : (char *) NULL);
33}
34
35/*
36 * Get aliases for this hostname
37 *
38 * Note that this won't work on most systems if your
39 * gethostbyname() call gets its info from DNS since
40 * DNS does not provide this funtionality.
41 */
42extern char *GetHostAliases()
43{
44    static char                 Buf[256];
45    struct hostent             *hp;
46    register char             **pp;
47    char                       *HName;
48    register int                Len;
49
50    if ((HName = GetHostName()) == NULL)
51        return((char *) NULL);
52
53    if ((hp = gethostbyname(HName)) == NULL) {
54        SImsg(SIM_GERR, "Cannot find lookup host info for \"%s\": %s",
55              HName, SYSERR);
56        return((char *) NULL);
57    }
58
59    for (pp = hp->h_aliases, Buf[0] = C_NULL; pp && *pp; ++pp) {
60        Len = strlen(Buf);
61        if ((Len + strlen(*pp) + 1) >= (sizeof(Buf) - Len - 2))
62            break;
63        (void) strcat(Buf, *pp);
64        (void) strcat(Buf, " ");
65    }
66
67    return((Buf[0]) ? Buf : (char *) NULL);
68}
69
70/*
71 * Get addresses for this host
72 */
73extern char *GetHostAddrs()
74{
75    static char                 Buf[256];
76    struct hostent             *hp;
77    register char             **pp;
78    char                       *HName;
79    char                       *AddrStr;
80    register int                Len;
81
82    if ((HName = GetHostName()) == NULL)
83        return((char *) NULL);
84
85    if ((hp = gethostbyname(HName)) == NULL) {
86        SImsg(SIM_GERR, "Cannot find lookup host info for \"%s\": %s",
87              HName, SYSERR);
88        return((char *) NULL);
89    }
90
91    for (pp = hp->h_addr_list, Buf[0] = C_NULL; pp && *pp; ++pp) {
92        if (hp->h_addrtype == AF_INET) {
93            AddrStr = (char *) inet_ntoa(*(struct in_addr *)*pp);
94            if (!AddrStr)
95                continue;
96
97            Len = strlen(Buf);
98            if ((Len + strlen(AddrStr) + 1) >= (sizeof(Buf) - Len - 2))
99                break;
100
101            (void) strcat(Buf, AddrStr);
102            (void) strcat(Buf, " ");
103        }
104    }
105
106    return((Buf[0]) ? Buf : (char *) NULL);
107}
Note: See TracBrowser for help on using the repository browser.