annotate libgfortran/intrinsics/hostnm.c @ 138:fc828634a951

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