OSDN Git Service

Configure cleanup.
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / hostnm.c
1 /* Implementation of the HOSTNM intrinsic.
2    Copyright (C) 2005, 2007, 2009 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 3 of the License, or (at your option) any later version.
11
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.
16
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.
20
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/>.  */
25
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
35
36 /* Windows32 version */
37 #if defined __MINGW32__ && !defined  HAVE_GETHOSTNAME
38 #define WIN32_LEAN_AND_MEAN
39 #include <windows.h>
40 #include <errno.h>
41
42 static int
43 w32_gethostname (char *name, size_t len)
44 {
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.  */
50
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);
55
56   if (!GetComputerName (buffer, &size))
57     return -1;
58
59   if ((size = strlen (buffer) + 1)  > len)
60     {
61       errno = EINVAL;
62       /* Truncate as per POSIX spec.  We do not NUL-terminate. */
63       size = len;
64     }
65   memcpy (name, buffer, (size_t) size);
66
67   return 0;
68 }
69
70 #undef gethostname
71 #define gethostname w32_gethostname
72 #define  HAVE_GETHOSTNAME 1
73
74 #endif
75
76
77 /* SUBROUTINE HOSTNM(NAME, STATUS)
78    CHARACTER(len=*), INTENT(OUT) :: NAME
79    INTEGER, INTENT(OUT), OPTIONAL :: STATUS  */
80
81 #ifdef HAVE_GETHOSTNAME
82 extern void hostnm_i4_sub (char *, GFC_INTEGER_4 *, gfc_charlen_type);
83 iexport_proto(hostnm_i4_sub);
84
85 void
86 hostnm_i4_sub (char *name, GFC_INTEGER_4 *status, gfc_charlen_type name_len)
87 {
88   int val, i;
89   char *p;
90
91   memset (name, ' ', name_len);
92   p = gfc_alloca (name_len + 1);
93
94   val = gethostname (p, name_len);
95
96   if (val == 0)
97   {
98     i = -1;
99     while (i < name_len && p[++i] != '\0')
100       name[i] = p[i];
101   }
102
103   if (status != NULL) 
104     *status = (val == 0) ? 0 : errno;
105 }
106 iexport(hostnm_i4_sub);
107
108 extern void hostnm_i8_sub (char *, GFC_INTEGER_8 *, gfc_charlen_type);
109 iexport_proto(hostnm_i8_sub);
110
111 void
112 hostnm_i8_sub (char *name, GFC_INTEGER_8 *status, gfc_charlen_type name_len)
113 {
114   int val, i;
115   char *p;
116
117   memset (name, ' ', name_len);
118   p = gfc_alloca (name_len + 1);
119
120   val = gethostname (p, name_len);
121
122   if (val == 0)
123   {
124     i = -1;
125     while (i < name_len && p[++i] != '\0')
126       name[i] = p[i];
127   }
128
129   if (status != NULL) 
130     *status = (val == 0) ? 0 : errno;
131 }
132 iexport(hostnm_i8_sub);
133
134 extern GFC_INTEGER_4 hostnm (char *, gfc_charlen_type);
135 export_proto(hostnm);
136
137 GFC_INTEGER_4
138 hostnm (char *name, gfc_charlen_type name_len)
139 {
140   GFC_INTEGER_4 val;
141   hostnm_i4_sub (name, &val, name_len);
142   return val;
143 }
144 #endif