OSDN Git Service

PR libfortran/18966
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / getcwd.c
1 /* Implementation of the GETCWD intrinsic.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Contributed by Steven G. Kargl <kargls@comcast.net>.
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 Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 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 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB.  If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "libgfortran.h"
24
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #endif
31
32 #include <errno.h>
33
34 extern void getcwd_i4_sub (char *, GFC_INTEGER_4 *, gfc_charlen_type);
35 iexport_proto(getcwd_i4_sub);
36
37 void
38 getcwd_i4_sub (char *cwd, GFC_INTEGER_4 *status, gfc_charlen_type cwd_len)
39 {
40   char str[cwd_len + 1], *s;
41   GFC_INTEGER_4 stat;
42
43   memset(cwd, ' ', (size_t) cwd_len);
44
45   if (!getcwd (str, (size_t) cwd_len + 1))
46     stat = errno;
47   else
48     {
49       stat = 0;
50       memcpy (cwd, str, strlen (str));
51     }
52   if (status != NULL) 
53     *status = stat;
54 }
55 iexport(getcwd_i4_sub);
56
57 extern void getcwd_i8_sub (char *, GFC_INTEGER_8 *, gfc_charlen_type);
58 export_proto(getcwd_i8_sub);
59
60 void
61 getcwd_i8_sub (char *cwd, GFC_INTEGER_8 *status, gfc_charlen_type cwd_len)
62 {
63   GFC_INTEGER_4 status4;
64   getcwd_i4_sub (cwd, &status4, cwd_len);
65   if (status)
66     *status = status4;
67 }
68
69 extern GFC_INTEGER_4 PREFIX(getcwd) (char *, gfc_charlen_type);
70 export_proto_np(PREFIX(getcwd));
71
72 GFC_INTEGER_4
73 PREFIX(getcwd) (char *cwd, gfc_charlen_type cwd_len)
74 {
75   GFC_INTEGER_4 status;
76   getcwd_i4_sub (cwd, &status, cwd_len);
77   return status;
78 }