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