OSDN Git Service

* config/i386/i386.md (*strmovsi_1): Simplify asm alternatives.
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / hostnm.c
1 /* Implementation of the HOSTNM intrinsic.
2    Copyright (C) 2005, 2007 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 "libgfortran.h"
32
33 #include <errno.h>
34 #include <string.h>
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h> 
38 #endif
39
40
41 /* Windows32 version */
42 #if defined __MINGW32__ && !defined  HAVE_GETHOSTNAME
43 #define WIN32_LEAN_AND_MEAN
44 #include <windows.h>
45 #include <errno.h>
46
47 static int
48 w32_gethostname (char *name, size_t len)
49 {
50   /* We could try the WinSock API gethostname, but that will
51      fail if WSAStartup function has has not been called.  We don't
52     really need a name that will be understood by socket API, so avoid
53     unnecessary dependence on WinSock libraries by using
54     GetComputerName instead.  */
55
56   /* On Win9x GetComputerName fails if the input size is less
57      than MAX_COMPUTERNAME_LENGTH + 1.  */
58   char buffer[MAX_COMPUTERNAME_LENGTH + 1];
59   DWORD size =  sizeof (buffer);
60
61   if (!GetComputerName (buffer, &size))
62     return -1;
63
64   if ((size = strlen (buffer) + 1)  > len)
65     {
66       errno = EINVAL;
67       /* Truncate as per POSIX spec.  We do not NUL-terminate. */
68       size = len;
69     }
70   memcpy (name, buffer, (size_t) size);
71
72   return 0;
73 }
74
75 #undef gethostname
76 #define gethostname w32_gethostname
77 #define  HAVE_GETHOSTNAME 1
78
79 #endif
80
81
82 /* SUBROUTINE HOSTNM(NAME, STATUS)
83    CHARACTER(len=*), INTENT(OUT) :: NAME
84    INTEGER, INTENT(OUT), OPTIONAL :: STATUS  */
85
86 #ifdef HAVE_GETHOSTNAME
87 extern void hostnm_i4_sub (char *, GFC_INTEGER_4 *, gfc_charlen_type);
88 iexport_proto(hostnm_i4_sub);
89
90 void
91 hostnm_i4_sub (char *name, GFC_INTEGER_4 *status, gfc_charlen_type name_len)
92 {
93   int val, i;
94   char *p;
95
96   memset (name, ' ', name_len);
97   p = gfc_alloca (name_len + 1);
98
99   val = gethostname (p, name_len);
100
101   if (val == 0)
102   {
103     i = -1;
104     while (i < name_len && p[++i] != '\0')
105       name[i] = p[i];
106   }
107
108   if (status != NULL) 
109     *status = (val == 0) ? 0 : errno;
110 }
111 iexport(hostnm_i4_sub);
112
113 extern void hostnm_i8_sub (char *, GFC_INTEGER_8 *, gfc_charlen_type);
114 iexport_proto(hostnm_i8_sub);
115
116 void
117 hostnm_i8_sub (char *name, GFC_INTEGER_8 *status, gfc_charlen_type name_len)
118 {
119   int val, i;
120   char *p;
121
122   memset (name, ' ', name_len);
123   p = gfc_alloca (name_len + 1);
124
125   val = gethostname (p, name_len);
126
127   if (val == 0)
128   {
129     i = -1;
130     while (i < name_len && p[++i] != '\0')
131       name[i] = p[i];
132   }
133
134   if (status != NULL) 
135     *status = (val == 0) ? 0 : errno;
136 }
137 iexport(hostnm_i8_sub);
138
139 extern GFC_INTEGER_4 hostnm (char *, gfc_charlen_type);
140 export_proto(hostnm);
141
142 GFC_INTEGER_4
143 hostnm (char *name, gfc_charlen_type name_len)
144 {
145   GFC_INTEGER_4 val;
146   hostnm_i4_sub (name, &val, name_len);
147   return val;
148 }
149 #endif