1 /* Implementation of the STAT and FSTAT intrinsics.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Steven G. Kargl <kargls@comcast.net>.
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
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.
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
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.
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. */
32 #include "libgfortran.h"
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
54 /* SUBROUTINE STAT(FILE, SARRAY, STATUS)
55 CHARACTER(len=*), INTENT(IN) :: FILE
56 INTEGER, INTENT(OUT), :: SARRAY(13)
57 INTEGER, INTENT(OUT), OPTIONAL :: STATUS
59 FUNCTION STAT(FILE, SARRAY)
61 CHARACTER(len=*), INTENT(IN) :: FILE
62 INTEGER, INTENT(OUT), :: SARRAY(13) */
64 extern void stat_i4_sub (char *, gfc_array_i4
*, GFC_INTEGER_4
*,
66 iexport_proto(stat_i4_sub
);
69 stat_i4_sub (char *name
, gfc_array_i4
*sarray
, GFC_INTEGER_4
*status
,
70 gfc_charlen_type name_len
)
76 /* If the rank of the array is not 1, abort. */
77 if (GFC_DESCRIPTOR_RANK (sarray
) != 1)
78 runtime_error ("Array rank of SARRAY is not 1.");
80 /* If the array is too small, abort. */
81 if (sarray
->dim
[0].ubound
+ 1 - sarray
->dim
[0].lbound
< 13)
82 runtime_error ("Array size of SARRAY is too small.");
84 if (sarray
->dim
[0].stride
== 0)
85 sarray
->dim
[0].stride
= 1;
87 /* Trim trailing spaces from name. */
88 while (name_len
> 0 && name
[name_len
- 1] == ' ')
91 /* Make a null terminated copy of the string. */
92 str
= gfc_alloca (name_len
+ 1);
93 memcpy (str
, name
, name_len
);
101 sarray
->data
[0 * sarray
->dim
[0].stride
] = sb
.st_dev
;
104 sarray
->data
[1 * sarray
->dim
[0].stride
] = sb
.st_ino
;
107 sarray
->data
[2 * sarray
->dim
[0].stride
] = sb
.st_mode
;
109 /* Number of (hard) links */
110 sarray
->data
[3 * sarray
->dim
[0].stride
] = sb
.st_nlink
;
113 sarray
->data
[4 * sarray
->dim
[0].stride
] = sb
.st_uid
;
116 sarray
->data
[5 * sarray
->dim
[0].stride
] = sb
.st_gid
;
118 /* ID of device containing directory entry for file (0 if not available) */
119 #if HAVE_STRUCT_STAT_ST_RDEV
120 sarray
->data
[6 * sarray
->dim
[0].stride
] = sb
.st_rdev
;
122 sarray
->data
[6 * sarray
->dim
[0].stride
] = 0;
125 /* File size (bytes) */
126 sarray
->data
[7 * sarray
->dim
[0].stride
] = sb
.st_size
;
128 /* Last access time */
129 sarray
->data
[8 * sarray
->dim
[0].stride
] = sb
.st_atime
;
131 /* Last modification time */
132 sarray
->data
[9 * sarray
->dim
[0].stride
] = sb
.st_mtime
;
134 /* Last file status change time */
135 sarray
->data
[10 * sarray
->dim
[0].stride
] = sb
.st_ctime
;
137 /* Preferred I/O block size (-1 if not available) */
138 #if HAVE_STRUCT_STAT_ST_BLKSIZE
139 sarray
->data
[11 * sarray
->dim
[0].stride
] = sb
.st_blksize
;
141 sarray
->data
[11 * sarray
->dim
[0].stride
] = -1;
144 /* Number of blocks allocated (-1 if not available) */
145 #if HAVE_STRUCT_STAT_ST_BLOCKS
146 sarray
->data
[12 * sarray
->dim
[0].stride
] = sb
.st_blocks
;
148 sarray
->data
[12 * sarray
->dim
[0].stride
] = -1;
153 *status
= (val
== 0) ? 0 : errno
;
155 iexport(stat_i4_sub
);
157 extern void stat_i8_sub (char *, gfc_array_i8
*, GFC_INTEGER_8
*,
159 iexport_proto(stat_i8_sub
);
162 stat_i8_sub (char *name
, gfc_array_i8
*sarray
, GFC_INTEGER_8
*status
,
163 gfc_charlen_type name_len
)
169 /* If the rank of the array is not 1, abort. */
170 if (GFC_DESCRIPTOR_RANK (sarray
) != 1)
171 runtime_error ("Array rank of SARRAY is not 1.");
173 /* If the array is too small, abort. */
174 if (sarray
->dim
[0].ubound
+ 1 - sarray
->dim
[0].lbound
< 13)
175 runtime_error ("Array size of SARRAY is too small.");
177 if (sarray
->dim
[0].stride
== 0)
178 sarray
->dim
[0].stride
= 1;
180 /* Trim trailing spaces from name. */
181 while (name_len
> 0 && name
[name_len
- 1] == ' ')
184 /* Make a null terminated copy of the string. */
185 str
= gfc_alloca (name_len
+ 1);
186 memcpy (str
, name
, name_len
);
187 str
[name_len
] = '\0';
189 val
= stat(str
, &sb
);
194 sarray
->data
[0] = sb
.st_dev
;
197 sarray
->data
[sarray
->dim
[0].stride
] = sb
.st_ino
;
200 sarray
->data
[2 * sarray
->dim
[0].stride
] = sb
.st_mode
;
202 /* Number of (hard) links */
203 sarray
->data
[3 * sarray
->dim
[0].stride
] = sb
.st_nlink
;
206 sarray
->data
[4 * sarray
->dim
[0].stride
] = sb
.st_uid
;
209 sarray
->data
[5 * sarray
->dim
[0].stride
] = sb
.st_gid
;
211 /* ID of device containing directory entry for file (0 if not available) */
212 #if HAVE_STRUCT_STAT_ST_RDEV
213 sarray
->data
[6 * sarray
->dim
[0].stride
] = sb
.st_rdev
;
215 sarray
->data
[6 * sarray
->dim
[0].stride
] = 0;
218 /* File size (bytes) */
219 sarray
->data
[7 * sarray
->dim
[0].stride
] = sb
.st_size
;
221 /* Last access time */
222 sarray
->data
[8 * sarray
->dim
[0].stride
] = sb
.st_atime
;
224 /* Last modification time */
225 sarray
->data
[9 * sarray
->dim
[0].stride
] = sb
.st_mtime
;
227 /* Last file status change time */
228 sarray
->data
[10 * sarray
->dim
[0].stride
] = sb
.st_ctime
;
230 /* Preferred I/O block size (-1 if not available) */
231 #if HAVE_STRUCT_STAT_ST_BLKSIZE
232 sarray
->data
[11 * sarray
->dim
[0].stride
] = sb
.st_blksize
;
234 sarray
->data
[11 * sarray
->dim
[0].stride
] = -1;
237 /* Number of blocks allocated (-1 if not available) */
238 #if HAVE_STRUCT_STAT_ST_BLOCKS
239 sarray
->data
[12 * sarray
->dim
[0].stride
] = sb
.st_blocks
;
241 sarray
->data
[12 * sarray
->dim
[0].stride
] = -1;
246 *status
= (val
== 0) ? 0 : errno
;
248 iexport(stat_i8_sub
);
250 extern GFC_INTEGER_4
stat_i4 (char *, gfc_array_i4
*, gfc_charlen_type
);
251 export_proto(stat_i4
);
254 stat_i4 (char *name
, gfc_array_i4
*sarray
, gfc_charlen_type name_len
)
257 stat_i4_sub (name
, sarray
, &val
, name_len
);
261 extern GFC_INTEGER_8
stat_i8 (char *, gfc_array_i8
*, gfc_charlen_type
);
262 export_proto(stat_i8
);
265 stat_i8 (char *name
, gfc_array_i8
*sarray
, gfc_charlen_type name_len
)
268 stat_i8_sub (name
, sarray
, &val
, name_len
);
273 /* SUBROUTINE FSTAT(UNIT, SARRAY, STATUS)
274 INTEGER, INTENT(IN) :: UNIT
275 INTEGER, INTENT(OUT) :: SARRAY(13)
276 INTEGER, INTENT(OUT), OPTIONAL :: STATUS
278 FUNCTION FSTAT(UNIT, SARRAY)
280 INTEGER, INTENT(IN) :: UNIT
281 INTEGER, INTENT(OUT) :: SARRAY(13) */
283 extern void fstat_i4_sub (GFC_INTEGER_4
*, gfc_array_i4
*, GFC_INTEGER_4
*);
284 iexport_proto(fstat_i4_sub
);
287 fstat_i4_sub (GFC_INTEGER_4
*unit
, gfc_array_i4
*sarray
, GFC_INTEGER_4
*status
)
292 /* If the rank of the array is not 1, abort. */
293 if (GFC_DESCRIPTOR_RANK (sarray
) != 1)
294 runtime_error ("Array rank of SARRAY is not 1.");
296 /* If the array is too small, abort. */
297 if (sarray
->dim
[0].ubound
+ 1 - sarray
->dim
[0].lbound
< 13)
298 runtime_error ("Array size of SARRAY is too small.");
300 if (sarray
->dim
[0].stride
== 0)
301 sarray
->dim
[0].stride
= 1;
303 /* Convert Fortran unit number to C file descriptor. */
304 val
= unit_to_fd (*unit
);
306 val
= fstat(val
, &sb
);
311 sarray
->data
[0 * sarray
->dim
[0].stride
] = sb
.st_dev
;
314 sarray
->data
[1 * sarray
->dim
[0].stride
] = sb
.st_ino
;
317 sarray
->data
[2 * sarray
->dim
[0].stride
] = sb
.st_mode
;
319 /* Number of (hard) links */
320 sarray
->data
[3 * sarray
->dim
[0].stride
] = sb
.st_nlink
;
323 sarray
->data
[4 * sarray
->dim
[0].stride
] = sb
.st_uid
;
326 sarray
->data
[5 * sarray
->dim
[0].stride
] = sb
.st_gid
;
328 /* ID of device containing directory entry for file (0 if not available) */
329 #if HAVE_STRUCT_STAT_ST_RDEV
330 sarray
->data
[6 * sarray
->dim
[0].stride
] = sb
.st_rdev
;
332 sarray
->data
[6 * sarray
->dim
[0].stride
] = 0;
335 /* File size (bytes) */
336 sarray
->data
[7 * sarray
->dim
[0].stride
] = sb
.st_size
;
338 /* Last access time */
339 sarray
->data
[8 * sarray
->dim
[0].stride
] = sb
.st_atime
;
341 /* Last modification time */
342 sarray
->data
[9 * sarray
->dim
[0].stride
] = sb
.st_mtime
;
344 /* Last file status change time */
345 sarray
->data
[10 * sarray
->dim
[0].stride
] = sb
.st_ctime
;
347 /* Preferred I/O block size (-1 if not available) */
348 #if HAVE_STRUCT_STAT_ST_BLKSIZE
349 sarray
->data
[11 * sarray
->dim
[0].stride
] = sb
.st_blksize
;
351 sarray
->data
[11 * sarray
->dim
[0].stride
] = -1;
354 /* Number of blocks allocated (-1 if not available) */
355 #if HAVE_STRUCT_STAT_ST_BLOCKS
356 sarray
->data
[12 * sarray
->dim
[0].stride
] = sb
.st_blocks
;
358 sarray
->data
[12 * sarray
->dim
[0].stride
] = -1;
363 *status
= (val
== 0) ? 0 : errno
;
365 iexport(fstat_i4_sub
);
367 extern void fstat_i8_sub (GFC_INTEGER_8
*, gfc_array_i8
*, GFC_INTEGER_8
*);
368 iexport_proto(fstat_i8_sub
);
371 fstat_i8_sub (GFC_INTEGER_8
*unit
, gfc_array_i8
*sarray
, GFC_INTEGER_8
*status
)
376 /* If the rank of the array is not 1, abort. */
377 if (GFC_DESCRIPTOR_RANK (sarray
) != 1)
378 runtime_error ("Array rank of SARRAY is not 1.");
380 /* If the array is too small, abort. */
381 if (sarray
->dim
[0].ubound
+ 1 - sarray
->dim
[0].lbound
< 13)
382 runtime_error ("Array size of SARRAY is too small.");
384 if (sarray
->dim
[0].stride
== 0)
385 sarray
->dim
[0].stride
= 1;
387 /* Convert Fortran unit number to C file descriptor. */
388 val
= unit_to_fd ((int) *unit
);
390 val
= fstat(val
, &sb
);
395 sarray
->data
[0] = sb
.st_dev
;
398 sarray
->data
[sarray
->dim
[0].stride
] = sb
.st_ino
;
401 sarray
->data
[2 * sarray
->dim
[0].stride
] = sb
.st_mode
;
403 /* Number of (hard) links */
404 sarray
->data
[3 * sarray
->dim
[0].stride
] = sb
.st_nlink
;
407 sarray
->data
[4 * sarray
->dim
[0].stride
] = sb
.st_uid
;
410 sarray
->data
[5 * sarray
->dim
[0].stride
] = sb
.st_gid
;
412 /* ID of device containing directory entry for file (0 if not available) */
413 #if HAVE_STRUCT_STAT_ST_RDEV
414 sarray
->data
[6 * sarray
->dim
[0].stride
] = sb
.st_rdev
;
416 sarray
->data
[6 * sarray
->dim
[0].stride
] = 0;
419 /* File size (bytes) */
420 sarray
->data
[7 * sarray
->dim
[0].stride
] = sb
.st_size
;
422 /* Last access time */
423 sarray
->data
[8 * sarray
->dim
[0].stride
] = sb
.st_atime
;
425 /* Last modification time */
426 sarray
->data
[9 * sarray
->dim
[0].stride
] = sb
.st_mtime
;
428 /* Last file status change time */
429 sarray
->data
[10 * sarray
->dim
[0].stride
] = sb
.st_ctime
;
431 /* Preferred I/O block size (-1 if not available) */
432 #if HAVE_STRUCT_STAT_ST_BLKSIZE
433 sarray
->data
[11 * sarray
->dim
[0].stride
] = sb
.st_blksize
;
435 sarray
->data
[11 * sarray
->dim
[0].stride
] = -1;
438 /* Number of blocks allocated (-1 if not available) */
439 #if HAVE_STRUCT_STAT_ST_BLOCKS
440 sarray
->data
[12 * sarray
->dim
[0].stride
] = sb
.st_blocks
;
442 sarray
->data
[12 * sarray
->dim
[0].stride
] = -1;
447 *status
= (val
== 0) ? 0 : errno
;
449 iexport(fstat_i8_sub
);
451 extern GFC_INTEGER_4
fstat_i4 (GFC_INTEGER_4
*, gfc_array_i4
*);
452 export_proto(fstat_i4
);
455 fstat_i4 (GFC_INTEGER_4
*unit
, gfc_array_i4
*sarray
)
458 fstat_i4_sub (unit
, sarray
, &val
);
462 extern GFC_INTEGER_8
fstat_i8 (GFC_INTEGER_8
*, gfc_array_i8
*);
463 export_proto(fstat_i8
);
466 fstat_i8 (GFC_INTEGER_8
*unit
, gfc_array_i8
*sarray
)
469 fstat_i8_sub (unit
, sarray
, &val
);