1 1.1 mrg /* Implementation of the HOSTNM intrinsic. 2 1.1.1.3 mrg Copyright (C) 2005-2022 Free Software Foundation, Inc. 3 1.1 mrg Contributed by Franois-Xavier Coudert <coudert (at) clipper.ens.fr> 4 1.1 mrg 5 1.1 mrg This file is part of the GNU Fortran runtime library (libgfortran). 6 1.1 mrg 7 1.1 mrg Libgfortran is free software; you can redistribute it and/or 8 1.1 mrg modify it under the terms of the GNU General Public 9 1.1 mrg License as published by the Free Software Foundation; either 10 1.1 mrg version 3 of the License, or (at your option) any later version. 11 1.1 mrg 12 1.1 mrg Libgfortran is distributed in the hope that it will be useful, 13 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 mrg GNU General Public License for more details. 16 1.1 mrg 17 1.1 mrg Under Section 7 of GPL version 3, you are granted additional 18 1.1 mrg permissions described in the GCC Runtime Library Exception, version 19 1.1 mrg 3.1, as published by the Free Software Foundation. 20 1.1 mrg 21 1.1 mrg You should have received a copy of the GNU General Public License and 22 1.1 mrg a copy of the GCC Runtime Library Exception along with this program; 23 1.1 mrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24 1.1 mrg <http://www.gnu.org/licenses/>. */ 25 1.1 mrg 26 1.1 mrg #include "libgfortran.h" 27 1.1 mrg 28 1.1 mrg #include <errno.h> 29 1.1 mrg #include <string.h> 30 1.1 mrg 31 1.1 mrg #ifdef HAVE_UNISTD_H 32 1.1 mrg #include <unistd.h> 33 1.1 mrg #endif 34 1.1 mrg 35 1.1 mrg #include <limits.h> 36 1.1 mrg 37 1.1 mrg #ifndef HOST_NAME_MAX 38 1.1 mrg #define HOST_NAME_MAX 255 39 1.1 mrg #endif 40 1.1 mrg 41 1.1 mrg 42 1.1 mrg /* Windows32 version */ 43 1.1 mrg #if defined __MINGW32__ && !defined HAVE_GETHOSTNAME 44 1.1 mrg #define WIN32_LEAN_AND_MEAN 45 1.1 mrg #include <windows.h> 46 1.1 mrg #include <errno.h> 47 1.1 mrg 48 1.1 mrg static int 49 1.1 mrg w32_gethostname (char *name, size_t len) 50 1.1 mrg { 51 1.1 mrg /* We could try the WinSock API gethostname, but that will 52 1.1 mrg fail if WSAStartup function has has not been called. We don't 53 1.1 mrg really need a name that will be understood by socket API, so avoid 54 1.1 mrg unnecessary dependence on WinSock libraries by using 55 1.1 mrg GetComputerName instead. */ 56 1.1 mrg 57 1.1 mrg /* On Win9x GetComputerName fails if the input size is less 58 1.1 mrg than MAX_COMPUTERNAME_LENGTH + 1. */ 59 1.1 mrg char buffer[MAX_COMPUTERNAME_LENGTH + 1]; 60 1.1 mrg DWORD size = sizeof (buffer); 61 1.1 mrg 62 1.1 mrg if (!GetComputerName (buffer, &size)) 63 1.1 mrg return -1; 64 1.1 mrg 65 1.1 mrg if ((size = strlen (buffer) + 1) > len) 66 1.1 mrg { 67 1.1 mrg errno = EINVAL; 68 1.1 mrg /* Truncate as per POSIX spec. We do not NUL-terminate. */ 69 1.1 mrg size = len; 70 1.1 mrg } 71 1.1 mrg memcpy (name, buffer, (size_t) size); 72 1.1 mrg 73 1.1 mrg return 0; 74 1.1 mrg } 75 1.1 mrg 76 1.1 mrg #undef gethostname 77 1.1 mrg #define gethostname w32_gethostname 78 1.1 mrg #define HAVE_GETHOSTNAME 1 79 1.1 mrg 80 1.1 mrg #endif 81 1.1 mrg 82 1.1 mrg 83 1.1 mrg /* SUBROUTINE HOSTNM(NAME, STATUS) 84 1.1 mrg CHARACTER(len=*), INTENT(OUT) :: NAME 85 1.1 mrg INTEGER, INTENT(OUT), OPTIONAL :: STATUS */ 86 1.1 mrg 87 1.1 mrg #ifdef HAVE_GETHOSTNAME 88 1.1 mrg static int 89 1.1 mrg hostnm_0 (char *name, gfc_charlen_type name_len) 90 1.1 mrg { 91 1.1 mrg char p[HOST_NAME_MAX + 1]; 92 1.1 mrg int val; 93 1.1 mrg 94 1.1 mrg memset (name, ' ', name_len); 95 1.1 mrg 96 1.1 mrg size_t reqlen = sizeof (p) > (size_t) name_len + 1 97 1.1 mrg ? (size_t) name_len + 1: sizeof (p); 98 1.1 mrg val = gethostname (p, reqlen); 99 1.1 mrg 100 1.1 mrg if (val == 0) 101 1.1 mrg { 102 1.1 mrg for (gfc_charlen_type i = 0; i < name_len && p[i] != '\0'; i++) 103 1.1 mrg name[i] = p[i]; 104 1.1 mrg } 105 1.1 mrg 106 1.1 mrg return ((val == 0) ? 0 : errno); 107 1.1 mrg } 108 1.1 mrg 109 1.1 mrg extern void hostnm_i4_sub (char *, GFC_INTEGER_4 *, gfc_charlen_type); 110 1.1 mrg iexport_proto(hostnm_i4_sub); 111 1.1 mrg 112 1.1 mrg void 113 1.1 mrg hostnm_i4_sub (char *name, GFC_INTEGER_4 *status, gfc_charlen_type name_len) 114 1.1 mrg { 115 1.1 mrg int val = hostnm_0 (name, name_len); 116 1.1 mrg if (status != NULL) 117 1.1 mrg *status = val; 118 1.1 mrg } 119 1.1 mrg iexport(hostnm_i4_sub); 120 1.1 mrg 121 1.1 mrg extern void hostnm_i8_sub (char *, GFC_INTEGER_8 *, gfc_charlen_type); 122 1.1 mrg iexport_proto(hostnm_i8_sub); 123 1.1 mrg 124 1.1 mrg void 125 1.1 mrg hostnm_i8_sub (char *name, GFC_INTEGER_8 *status, gfc_charlen_type name_len) 126 1.1 mrg { 127 1.1 mrg int val = hostnm_0 (name, name_len); 128 1.1 mrg if (status != NULL) 129 1.1 mrg *status = val; 130 1.1 mrg } 131 1.1 mrg iexport(hostnm_i8_sub); 132 1.1 mrg 133 1.1 mrg extern GFC_INTEGER_4 hostnm (char *, gfc_charlen_type); 134 1.1 mrg export_proto(hostnm); 135 1.1 mrg 136 1.1 mrg GFC_INTEGER_4 137 1.1 mrg hostnm (char *name, gfc_charlen_type name_len) 138 1.1 mrg { 139 1.1 mrg return hostnm_0 (name, name_len); 140 1.1 mrg } 141 1.1 mrg #endif 142