OSDN Git Service

93416f9983c2a9a49aec55e6cd0f28f892297420
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / tty.c
1 /* Implementation of the ISATTY and TTYNAM g77 intrinsics.
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 #include "../io/io.h"
34
35 #include <string.h>
36
37 /* LOGICAL FUNCTION ISATTY(UNIT)
38    INTEGER, INTENT(IN) :: UNIT */
39
40 extern GFC_LOGICAL_4 isatty_l4 (int *);
41 export_proto(isatty_l4);
42
43 GFC_LOGICAL_4
44 isatty_l4 (int *unit)
45 {
46   gfc_unit *u;
47   GFC_LOGICAL_4 ret = 0;
48
49   u = find_unit (*unit);
50   if (u != NULL)
51     {
52       ret = (GFC_LOGICAL_4) stream_isatty (u->s);
53       unlock_unit (u);
54     }
55   return ret;
56 }
57
58
59 extern GFC_LOGICAL_8 isatty_l8 (int *);
60 export_proto(isatty_l8);
61
62 GFC_LOGICAL_8
63 isatty_l8 (int *unit)
64 {
65   gfc_unit *u;
66   GFC_LOGICAL_8 ret = 0;
67
68   u = find_unit (*unit);
69   if (u != NULL)
70     {
71       ret = (GFC_LOGICAL_8) stream_isatty (u->s);
72       unlock_unit (u);
73     }
74   return ret;
75 }
76
77
78 /* SUBROUTINE TTYNAM(UNIT,NAME)
79    INTEGER,SCALAR,INTENT(IN) :: UNIT
80    CHARACTER,SCALAR,INTENT(OUT) :: NAME */
81
82 extern void ttynam_sub (int *, char *, gfc_charlen_type);
83 export_proto(ttynam_sub);
84
85 void
86 ttynam_sub (int *unit, char * name, gfc_charlen_type name_len)
87 {
88   gfc_unit *u;
89   char * n;
90   int i;
91
92   memset (name, ' ', name_len);
93   u = find_unit (*unit);
94   if (u != NULL)
95     {
96       n = stream_ttyname (u->s);
97       if (n != NULL)
98         {
99           i = 0;
100           while (*n && i < name_len)
101             name[i++] = *(n++);
102         }
103       unlock_unit (u);
104     }
105 }
106
107
108 extern void ttynam (char **, gfc_charlen_type *, int);
109 export_proto(ttynam);
110
111 void
112 ttynam (char ** name, gfc_charlen_type * name_len, int unit)
113 {
114   gfc_unit *u;
115
116   u = find_unit (unit);
117   if (u != NULL)
118     {
119       *name = stream_ttyname (u->s);
120       if (*name != NULL)
121         {
122           *name_len = strlen (*name);
123           *name = strdup (*name);
124           unlock_unit (u);
125           return;
126         }
127       unlock_unit (u);
128     }
129
130   *name_len = 0;
131   *name = NULL;
132 }