/src/PROJ/curl/lib/curl_gethostname.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #include "curl_gethostname.h" |
28 | | |
29 | | /* |
30 | | * Curl_gethostname() is a wrapper around gethostname() which allows |
31 | | * overriding the hostname that the function would normally return. |
32 | | * This capability is used by the test suite to verify exact matching |
33 | | * of NTLM authentication, which exercises libcurl's MD4 and DES code |
34 | | * as well as by the SMTP module when a hostname is not provided. |
35 | | * |
36 | | * For libcurl debug enabled builds hostname overriding takes place |
37 | | * when environment variable CURL_GETHOSTNAME is set, using the value |
38 | | * held by the variable to override returned hostname. |
39 | | * |
40 | | * Note: The function always returns the un-qualified hostname rather |
41 | | * than being provider dependent. |
42 | | */ |
43 | | |
44 | | int Curl_gethostname(char * const name, GETHOSTNAME_TYPE_ARG2 namelen) |
45 | 0 | { |
46 | | #ifndef HAVE_GETHOSTNAME |
47 | | |
48 | | /* Allow compilation and return failure when unavailable */ |
49 | | (void) name; |
50 | | (void) namelen; |
51 | | return -1; |
52 | | |
53 | | #else |
54 | 0 | int err; |
55 | 0 | char *dot; |
56 | |
|
57 | | #ifdef DEBUGBUILD |
58 | | |
59 | | /* Override hostname when environment variable CURL_GETHOSTNAME is set */ |
60 | | const char *force_hostname = getenv("CURL_GETHOSTNAME"); |
61 | | if(force_hostname) { |
62 | | if(strlen(force_hostname) < (size_t)namelen) |
63 | | strcpy(name, force_hostname); |
64 | | else |
65 | | return 1; /* can't do it */ |
66 | | err = 0; |
67 | | } |
68 | | else { |
69 | | name[0] = '\0'; |
70 | | err = gethostname(name, namelen); |
71 | | } |
72 | | |
73 | | #else /* DEBUGBUILD */ |
74 | |
|
75 | 0 | name[0] = '\0'; |
76 | | #ifdef __AMIGA__ |
77 | | err = gethostname((unsigned char *)name, namelen); |
78 | | #else |
79 | 0 | err = gethostname(name, namelen); |
80 | 0 | #endif |
81 | |
|
82 | 0 | #endif |
83 | |
|
84 | 0 | name[namelen - 1] = '\0'; |
85 | |
|
86 | 0 | if(err) |
87 | 0 | return err; |
88 | | |
89 | | /* Truncate domain, leave only machine name */ |
90 | 0 | dot = strchr(name, '.'); |
91 | 0 | if(dot) |
92 | 0 | *dot = '\0'; |
93 | |
|
94 | 0 | return 0; |
95 | 0 | #endif |
96 | |
|
97 | 0 | } |