BRL-CAD
gethostname.c
Go to the documentation of this file.
1 /* G E T H O S T N A M E . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include "bnetwork.h"
24 #include "bio.h"
25 
26 #include "bu/log.h"
27 #include "bu/str.h"
28 
29 int
30 bu_gethostname(char *hostname, size_t hostlen)
31 {
32  int status;
33 
34 #ifdef HAVE_WINSOCK_H
35  /**
36  * Windows requires winsock networking library be initialized
37  * before the hostname can be retrieved.
38  */
39  WSADATA wsaData;
40  if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
41  bu_log("ERROR: unable to initialize networking\n");
42 #endif
43 
44 #if defined(HAVE_WORKING_GETHOSTNAME_FUNCTION)
45  status = gethostname(hostname, hostlen);
46 #else
47 
48  /* gethostname is POSIX but not a C99. fallback to nothing. */
49  bu_strlcpy(hostname, "unknown", hostlen);
50  status = 0; /* no error */
51 
52 #endif
53 
54 #ifdef HAVE_WINSOCK_H
55  WSACleanup();
56 #endif
57 
58  return status;
59 }
60 
61 /*
62  * Local Variables:
63  * mode: C
64  * tab-width: 8
65  * indent-tabs-mode: t
66  * c-file-style: "stroustrup"
67  * End:
68  * ex: shiftwidth=4 tabstop=8
69  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int bu_gethostname(char *hostname, size_t hostlen)
Definition: gethostname.c:30
Header file for the BRL-CAD common definitions.
#define bu_strlcpy(dst, src, size)
Definition: str.h:60