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