OSDN Git Service

7ea2a1002a3d337934770c460ef8ded41383ba83
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / unlink.c
1 /* Implementation of the UNLINK 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 /* SUBROUTINE UNLINK(NAME, STATUS)
35    CHARACTER(LEN= ), INTENT(IN) :: NAME
36    INTEGER, INTENT(OUT), OPTIONAL :: STATUS)  */
37
38 void
39 prefix(unlink_i4_sub) (char * name, GFC_INTEGER_4 * status,
40                        gfc_charlen_type name_len)
41 {
42
43   char *str, *s;
44   GFC_INTEGER_4 stat;
45
46   /* Trim trailing spaces from name.  */
47   while (name_len > 0 && name[name_len - 1] == ' ')
48     name_len--;
49
50   /* Make a null terminated copy of the string.  */
51   str = gfc_alloca (name_len + 1);
52   memcpy (str, name, name_len);
53   str[name_len] = '\0'; 
54
55   stat = unlink (str);
56
57   if (status != NULL) 
58     *status = (stat == 0) ? stat : errno;
59 }
60
61
62 void
63 prefix(unlink_i8_sub) (char * name, GFC_INTEGER_8 * status,
64                                gfc_charlen_type name_len)
65 {
66   GFC_INTEGER_4 status4;
67   prefix (unlink_i4_sub) (name, &status4, name_len);
68   if (status)
69     *status = status4;
70 }
71
72
73 /* INTEGER FUNCTION UNLINK(NAME)
74    CHARACTER(LEN= ), INTENT(IN) :: NAME  */
75
76 GFC_INTEGER_4
77 prefix(unlink) (char * name, gfc_charlen_type name_len)
78 {
79   GFC_INTEGER_4 status;
80   prefix(unlink_i4_sub) (name, &status, name_len);
81   return status;
82 }