OSDN Git Service

PR libfortran/31607
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / hostnm.c
1 /* Implementation of the HOSTNM intrinsic.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3    Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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 2 of the License, or (at your option) any later version.
11
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file.  (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
20
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING.  If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA.  */
30
31 #include "config.h"
32 #include "libgfortran.h"
33
34 #include <errno.h>
35 #include <string.h>
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h> 
39 #endif
40
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
48 static int
49 w32_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
82
83 /* SUBROUTINE HOSTNM(NAME, STATUS)
84    CHARACTER(len=*), INTENT(OUT) :: NAME
85    INTEGER, INTENT(OUT), OPTIONAL :: STATUS  */
86
87 #ifdef HAVE_GETHOSTNAME
88 extern void hostnm_i4_sub (char *, GFC_INTEGER_4 *, gfc_charlen_type);
89 iexport_proto(hostnm_i4_sub);
90
91 void
92 hostnm_i4_sub (char *name, GFC_INTEGER_4 *status, gfc_charlen_type name_len)
93 {
94   int val, i;
95   char *p;
96
97   memset (name, ' ', name_len);
98   p = gfc_alloca (name_len + 1);
99
100   val = gethostname (p, name_len);
101
102   if (val == 0)
103   {
104     i = -1;
105     while (i < name_len && p[++i] != '\0')
106       name[i] = p[i];
107   }
108
109   if (status != NULL) 
110     *status = (val == 0) ? 0 : errno;
111 }
112 iexport(hostnm_i4_sub);
113
114 extern void hostnm_i8_sub (char *, GFC_INTEGER_8 *, gfc_charlen_type);
115 iexport_proto(hostnm_i8_sub);
116
117 void
118 hostnm_i8_sub (char *name, GFC_INTEGER_8 *status, gfc_charlen_type name_len)
119 {
120   int val, i;
121   char *p;
122
123   memset (name, ' ', name_len);
124   p = gfc_alloca (name_len + 1);
125
126   val = gethostname (p, name_len);
127
128   if (val == 0)
129   {
130     i = -1;
131     while (i < name_len && p[++i] != '\0')
132       name[i] = p[i];
133   }
134
135   if (status != NULL) 
136     *status = (val == 0) ? 0 : errno;
137 }
138 iexport(hostnm_i8_sub);
139
140 extern GFC_INTEGER_4 hostnm (char *, gfc_charlen_type);
141 export_proto(hostnm);
142
143 GFC_INTEGER_4
144 hostnm (char *name, gfc_charlen_type name_len)
145 {
146   GFC_INTEGER_4 val;
147   hostnm_i4_sub (name, &val, name_len);
148   return val;
149 }
150 #endif