1627f7eb2Smrg /* Implementation of the HOSTNM intrinsic.
2*4c3eb207Smrg Copyright (C) 2005-2020 Free Software Foundation, Inc.
3627f7eb2Smrg Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
4627f7eb2Smrg
5627f7eb2Smrg This file is part of the GNU Fortran runtime library (libgfortran).
6627f7eb2Smrg
7627f7eb2Smrg Libgfortran is free software; you can redistribute it and/or
8627f7eb2Smrg modify it under the terms of the GNU General Public
9627f7eb2Smrg License as published by the Free Software Foundation; either
10627f7eb2Smrg version 3 of the License, or (at your option) any later version.
11627f7eb2Smrg
12627f7eb2Smrg Libgfortran is distributed in the hope that it will be useful,
13627f7eb2Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
14627f7eb2Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15627f7eb2Smrg GNU General Public License for more details.
16627f7eb2Smrg
17627f7eb2Smrg Under Section 7 of GPL version 3, you are granted additional
18627f7eb2Smrg permissions described in the GCC Runtime Library Exception, version
19627f7eb2Smrg 3.1, as published by the Free Software Foundation.
20627f7eb2Smrg
21627f7eb2Smrg You should have received a copy of the GNU General Public License and
22627f7eb2Smrg a copy of the GCC Runtime Library Exception along with this program;
23627f7eb2Smrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24627f7eb2Smrg <http://www.gnu.org/licenses/>. */
25627f7eb2Smrg
26627f7eb2Smrg #include "libgfortran.h"
27627f7eb2Smrg
28627f7eb2Smrg #include <errno.h>
29627f7eb2Smrg #include <string.h>
30627f7eb2Smrg
31627f7eb2Smrg #ifdef HAVE_UNISTD_H
32627f7eb2Smrg #include <unistd.h>
33627f7eb2Smrg #endif
34627f7eb2Smrg
35627f7eb2Smrg #include <limits.h>
36627f7eb2Smrg
37627f7eb2Smrg #ifndef HOST_NAME_MAX
38627f7eb2Smrg #define HOST_NAME_MAX 255
39627f7eb2Smrg #endif
40627f7eb2Smrg
41627f7eb2Smrg
42627f7eb2Smrg /* Windows32 version */
43627f7eb2Smrg #if defined __MINGW32__ && !defined HAVE_GETHOSTNAME
44627f7eb2Smrg #define WIN32_LEAN_AND_MEAN
45627f7eb2Smrg #include <windows.h>
46627f7eb2Smrg #include <errno.h>
47627f7eb2Smrg
48627f7eb2Smrg static int
w32_gethostname(char * name,size_t len)49627f7eb2Smrg w32_gethostname (char *name, size_t len)
50627f7eb2Smrg {
51627f7eb2Smrg /* We could try the WinSock API gethostname, but that will
52627f7eb2Smrg fail if WSAStartup function has has not been called. We don't
53627f7eb2Smrg really need a name that will be understood by socket API, so avoid
54627f7eb2Smrg unnecessary dependence on WinSock libraries by using
55627f7eb2Smrg GetComputerName instead. */
56627f7eb2Smrg
57627f7eb2Smrg /* On Win9x GetComputerName fails if the input size is less
58627f7eb2Smrg than MAX_COMPUTERNAME_LENGTH + 1. */
59627f7eb2Smrg char buffer[MAX_COMPUTERNAME_LENGTH + 1];
60627f7eb2Smrg DWORD size = sizeof (buffer);
61627f7eb2Smrg
62627f7eb2Smrg if (!GetComputerName (buffer, &size))
63627f7eb2Smrg return -1;
64627f7eb2Smrg
65627f7eb2Smrg if ((size = strlen (buffer) + 1) > len)
66627f7eb2Smrg {
67627f7eb2Smrg errno = EINVAL;
68627f7eb2Smrg /* Truncate as per POSIX spec. We do not NUL-terminate. */
69627f7eb2Smrg size = len;
70627f7eb2Smrg }
71627f7eb2Smrg memcpy (name, buffer, (size_t) size);
72627f7eb2Smrg
73627f7eb2Smrg return 0;
74627f7eb2Smrg }
75627f7eb2Smrg
76627f7eb2Smrg #undef gethostname
77627f7eb2Smrg #define gethostname w32_gethostname
78627f7eb2Smrg #define HAVE_GETHOSTNAME 1
79627f7eb2Smrg
80627f7eb2Smrg #endif
81627f7eb2Smrg
82627f7eb2Smrg
83627f7eb2Smrg /* SUBROUTINE HOSTNM(NAME, STATUS)
84627f7eb2Smrg CHARACTER(len=*), INTENT(OUT) :: NAME
85627f7eb2Smrg INTEGER, INTENT(OUT), OPTIONAL :: STATUS */
86627f7eb2Smrg
87627f7eb2Smrg #ifdef HAVE_GETHOSTNAME
88627f7eb2Smrg static int
hostnm_0(char * name,gfc_charlen_type name_len)89627f7eb2Smrg hostnm_0 (char *name, gfc_charlen_type name_len)
90627f7eb2Smrg {
91627f7eb2Smrg char p[HOST_NAME_MAX + 1];
92627f7eb2Smrg int val;
93627f7eb2Smrg
94627f7eb2Smrg memset (name, ' ', name_len);
95627f7eb2Smrg
96627f7eb2Smrg size_t reqlen = sizeof (p) > (size_t) name_len + 1
97627f7eb2Smrg ? (size_t) name_len + 1: sizeof (p);
98627f7eb2Smrg val = gethostname (p, reqlen);
99627f7eb2Smrg
100627f7eb2Smrg if (val == 0)
101627f7eb2Smrg {
102627f7eb2Smrg for (gfc_charlen_type i = 0; i < name_len && p[i] != '\0'; i++)
103627f7eb2Smrg name[i] = p[i];
104627f7eb2Smrg }
105627f7eb2Smrg
106627f7eb2Smrg return ((val == 0) ? 0 : errno);
107627f7eb2Smrg }
108627f7eb2Smrg
109627f7eb2Smrg extern void hostnm_i4_sub (char *, GFC_INTEGER_4 *, gfc_charlen_type);
110627f7eb2Smrg iexport_proto(hostnm_i4_sub);
111627f7eb2Smrg
112627f7eb2Smrg void
hostnm_i4_sub(char * name,GFC_INTEGER_4 * status,gfc_charlen_type name_len)113627f7eb2Smrg hostnm_i4_sub (char *name, GFC_INTEGER_4 *status, gfc_charlen_type name_len)
114627f7eb2Smrg {
115627f7eb2Smrg int val = hostnm_0 (name, name_len);
116627f7eb2Smrg if (status != NULL)
117627f7eb2Smrg *status = val;
118627f7eb2Smrg }
119627f7eb2Smrg iexport(hostnm_i4_sub);
120627f7eb2Smrg
121627f7eb2Smrg extern void hostnm_i8_sub (char *, GFC_INTEGER_8 *, gfc_charlen_type);
122627f7eb2Smrg iexport_proto(hostnm_i8_sub);
123627f7eb2Smrg
124627f7eb2Smrg void
hostnm_i8_sub(char * name,GFC_INTEGER_8 * status,gfc_charlen_type name_len)125627f7eb2Smrg hostnm_i8_sub (char *name, GFC_INTEGER_8 *status, gfc_charlen_type name_len)
126627f7eb2Smrg {
127627f7eb2Smrg int val = hostnm_0 (name, name_len);
128627f7eb2Smrg if (status != NULL)
129627f7eb2Smrg *status = val;
130627f7eb2Smrg }
131627f7eb2Smrg iexport(hostnm_i8_sub);
132627f7eb2Smrg
133627f7eb2Smrg extern GFC_INTEGER_4 hostnm (char *, gfc_charlen_type);
134627f7eb2Smrg export_proto(hostnm);
135627f7eb2Smrg
136627f7eb2Smrg GFC_INTEGER_4
hostnm(char * name,gfc_charlen_type name_len)137627f7eb2Smrg hostnm (char *name, gfc_charlen_type name_len)
138627f7eb2Smrg {
139627f7eb2Smrg return hostnm_0 (name, name_len);
140627f7eb2Smrg }
141627f7eb2Smrg #endif
142