Path: blob/master/Utilities/cmcurl/lib/curl_gethostname.c
3153 views
/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22***************************************************************************/2324#include "curl_setup.h"2526#include "curl_gethostname.h"2728/*29* Curl_gethostname() is a wrapper around gethostname() which allows30* overriding the hostname that the function would normally return.31* This capability is used by the test suite to verify exact matching32* of NTLM authentication, which exercises libcurl's MD4 and DES code33* as well as by the SMTP module when a hostname is not provided.34*35* For libcurl debug enabled builds hostname overriding takes place36* when environment variable CURL_GETHOSTNAME is set, using the value37* held by the variable to override returned hostname.38*39* Note: The function always returns the un-qualified hostname rather40* than being provider dependent.41*/4243int Curl_gethostname(char * const name, GETHOSTNAME_TYPE_ARG2 namelen)44{45#ifndef HAVE_GETHOSTNAME4647/* Allow compilation and return failure when unavailable */48(void) name;49(void) namelen;50return -1;5152#else53int err;54char *dot;5556#ifdef DEBUGBUILD5758/* Override hostname when environment variable CURL_GETHOSTNAME is set */59const char *force_hostname = getenv("CURL_GETHOSTNAME");60if(force_hostname) {61if(strlen(force_hostname) < (size_t)namelen)62strcpy(name, force_hostname);63else64return 1; /* can't do it */65err = 0;66}67else {68name[0] = '\0';69err = gethostname(name, namelen);70}7172#else /* DEBUGBUILD */7374name[0] = '\0';75#ifdef __AMIGA__76err = gethostname((unsigned char *)name, namelen);77#else78err = gethostname(name, namelen);79#endif8081#endif8283name[namelen - 1] = '\0';8485if(err)86return err;8788/* Truncate domain, leave only machine name */89dot = strchr(name, '.');90if(dot)91*dot = '\0';9293return 0;94#endif9596}979899