1 /* Implementation of the STAT and FSTAT intrinsics.
2 Copyright (C) 2004, 2005, 2006 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 /* Trim trailing spaces from name. */
85 while (name_len > 0 && name[name_len - 1] == ' ')
88 /* Make a null terminated copy of the string. */
89 str = gfc_alloca (name_len + 1);
90 memcpy (str, name, name_len);
98 sarray->data[0 * sarray->dim[0].stride] = sb.st_dev;
101 sarray->data[1 * sarray->dim[0].stride] = sb.st_ino;
104 sarray->data[2 * sarray->dim[0].stride] = sb.st_mode;
106 /* Number of (hard) links */
107 sarray->data[3 * sarray->dim[0].stride] = sb.st_nlink;
110 sarray->data[4 * sarray->dim[0].stride] = sb.st_uid;
113 sarray->data[5 * sarray->dim[0].stride] = sb.st_gid;
115 /* ID of device containing directory entry for file (0 if not available) */
116 #if HAVE_STRUCT_STAT_ST_RDEV
117 sarray->data[6 * sarray->dim[0].stride] = sb.st_rdev;
119 sarray->data[6 * sarray->dim[0].stride] = 0;
122 /* File size (bytes) */
123 sarray->data[7 * sarray->dim[0].stride] = sb.st_size;
125 /* Last access time */
126 sarray->data[8 * sarray->dim[0].stride] = sb.st_atime;
128 /* Last modification time */
129 sarray->data[9 * sarray->dim[0].stride] = sb.st_mtime;
131 /* Last file status change time */
132 sarray->data[10 * sarray->dim[0].stride] = sb.st_ctime;
134 /* Preferred I/O block size (-1 if not available) */
135 #if HAVE_STRUCT_STAT_ST_BLKSIZE
136 sarray->data[11 * sarray->dim[0].stride] = sb.st_blksize;
138 sarray->data[11 * sarray->dim[0].stride] = -1;
141 /* Number of blocks allocated (-1 if not available) */
142 #if HAVE_STRUCT_STAT_ST_BLOCKS
143 sarray->data[12 * sarray->dim[0].stride] = sb.st_blocks;
145 sarray->data[12 * sarray->dim[0].stride] = -1;
150 *status = (val == 0) ? 0 : errno;
152 iexport(stat_i4_sub);
154 extern void stat_i8_sub (char *, gfc_array_i8 *, GFC_INTEGER_8 *,
156 iexport_proto(stat_i8_sub);
159 stat_i8_sub (char *name, gfc_array_i8 *sarray, GFC_INTEGER_8 *status,
160 gfc_charlen_type name_len)
166 /* If the rank of the array is not 1, abort. */
167 if (GFC_DESCRIPTOR_RANK (sarray) != 1)
168 runtime_error ("Array rank of SARRAY is not 1.");
170 /* If the array is too small, abort. */
171 if (sarray->dim[0].ubound + 1 - sarray->dim[0].lbound < 13)
172 runtime_error ("Array size of SARRAY is too small.");
174 /* Trim trailing spaces from name. */
175 while (name_len > 0 && name[name_len - 1] == ' ')
178 /* Make a null terminated copy of the string. */
179 str = gfc_alloca (name_len + 1);
180 memcpy (str, name, name_len);
181 str[name_len] = '\0';
183 val = stat(str, &sb);
188 sarray->data[0] = sb.st_dev;
191 sarray->data[sarray->dim[0].stride] = sb.st_ino;
194 sarray->data[2 * sarray->dim[0].stride] = sb.st_mode;
196 /* Number of (hard) links */
197 sarray->data[3 * sarray->dim[0].stride] = sb.st_nlink;
200 sarray->data[4 * sarray->dim[0].stride] = sb.st_uid;
203 sarray->data[5 * sarray->dim[0].stride] = sb.st_gid;
205 /* ID of device containing directory entry for file (0 if not available) */
206 #if HAVE_STRUCT_STAT_ST_RDEV
207 sarray->data[6 * sarray->dim[0].stride] = sb.st_rdev;
209 sarray->data[6 * sarray->dim[0].stride] = 0;
212 /* File size (bytes) */
213 sarray->data[7 * sarray->dim[0].stride] = sb.st_size;
215 /* Last access time */
216 sarray->data[8 * sarray->dim[0].stride] = sb.st_atime;
218 /* Last modification time */
219 sarray->data[9 * sarray->dim[0].stride] = sb.st_mtime;
221 /* Last file status change time */
222 sarray->data[10 * sarray->dim[0].stride] = sb.st_ctime;
224 /* Preferred I/O block size (-1 if not available) */
225 #if HAVE_STRUCT_STAT_ST_BLKSIZE
226 sarray->data[11 * sarray->dim[0].stride] = sb.st_blksize;
228 sarray->data[11 * sarray->dim[0].stride] = -1;
231 /* Number of blocks allocated (-1 if not available) */
232 #if HAVE_STRUCT_STAT_ST_BLOCKS
233 sarray->data[12 * sarray->dim[0].stride] = sb.st_blocks;
235 sarray->data[12 * sarray->dim[0].stride] = -1;
240 *status = (val == 0) ? 0 : errno;
242 iexport(stat_i8_sub);
244 extern GFC_INTEGER_4 stat_i4 (char *, gfc_array_i4 *, gfc_charlen_type);
245 export_proto(stat_i4);
248 stat_i4 (char *name, gfc_array_i4 *sarray, gfc_charlen_type name_len)
251 stat_i4_sub (name, sarray, &val, name_len);
255 extern GFC_INTEGER_8 stat_i8 (char *, gfc_array_i8 *, gfc_charlen_type);
256 export_proto(stat_i8);
259 stat_i8 (char *name, gfc_array_i8 *sarray, gfc_charlen_type name_len)
262 stat_i8_sub (name, sarray, &val, name_len);
267 /* SUBROUTINE FSTAT(UNIT, SARRAY, STATUS)
268 INTEGER, INTENT(IN) :: UNIT
269 INTEGER, INTENT(OUT) :: SARRAY(13)
270 INTEGER, INTENT(OUT), OPTIONAL :: STATUS
272 FUNCTION FSTAT(UNIT, SARRAY)
274 INTEGER, INTENT(IN) :: UNIT
275 INTEGER, INTENT(OUT) :: SARRAY(13) */
277 extern void fstat_i4_sub (GFC_INTEGER_4 *, gfc_array_i4 *, GFC_INTEGER_4 *);
278 iexport_proto(fstat_i4_sub);
281 fstat_i4_sub (GFC_INTEGER_4 *unit, gfc_array_i4 *sarray, GFC_INTEGER_4 *status)
286 /* If the rank of the array is not 1, abort. */
287 if (GFC_DESCRIPTOR_RANK (sarray) != 1)
288 runtime_error ("Array rank of SARRAY is not 1.");
290 /* If the array is too small, abort. */
291 if (sarray->dim[0].ubound + 1 - sarray->dim[0].lbound < 13)
292 runtime_error ("Array size of SARRAY is too small.");
294 /* Convert Fortran unit number to C file descriptor. */
295 val = unit_to_fd (*unit);
297 val = fstat(val, &sb);
302 sarray->data[0 * sarray->dim[0].stride] = sb.st_dev;
305 sarray->data[1 * sarray->dim[0].stride] = sb.st_ino;
308 sarray->data[2 * sarray->dim[0].stride] = sb.st_mode;
310 /* Number of (hard) links */
311 sarray->data[3 * sarray->dim[0].stride] = sb.st_nlink;
314 sarray->data[4 * sarray->dim[0].stride] = sb.st_uid;
317 sarray->data[5 * sarray->dim[0].stride] = sb.st_gid;
319 /* ID of device containing directory entry for file (0 if not available) */
320 #if HAVE_STRUCT_STAT_ST_RDEV
321 sarray->data[6 * sarray->dim[0].stride] = sb.st_rdev;
323 sarray->data[6 * sarray->dim[0].stride] = 0;
326 /* File size (bytes) */
327 sarray->data[7 * sarray->dim[0].stride] = sb.st_size;
329 /* Last access time */
330 sarray->data[8 * sarray->dim[0].stride] = sb.st_atime;
332 /* Last modification time */
333 sarray->data[9 * sarray->dim[0].stride] = sb.st_mtime;
335 /* Last file status change time */
336 sarray->data[10 * sarray->dim[0].stride] = sb.st_ctime;
338 /* Preferred I/O block size (-1 if not available) */
339 #if HAVE_STRUCT_STAT_ST_BLKSIZE
340 sarray->data[11 * sarray->dim[0].stride] = sb.st_blksize;
342 sarray->data[11 * sarray->dim[0].stride] = -1;
345 /* Number of blocks allocated (-1 if not available) */
346 #if HAVE_STRUCT_STAT_ST_BLOCKS
347 sarray->data[12 * sarray->dim[0].stride] = sb.st_blocks;
349 sarray->data[12 * sarray->dim[0].stride] = -1;
354 *status = (val == 0) ? 0 : errno;
356 iexport(fstat_i4_sub);
358 extern void fstat_i8_sub (GFC_INTEGER_8 *, gfc_array_i8 *, GFC_INTEGER_8 *);
359 iexport_proto(fstat_i8_sub);
362 fstat_i8_sub (GFC_INTEGER_8 *unit, gfc_array_i8 *sarray, GFC_INTEGER_8 *status)
367 /* If the rank of the array is not 1, abort. */
368 if (GFC_DESCRIPTOR_RANK (sarray) != 1)
369 runtime_error ("Array rank of SARRAY is not 1.");
371 /* If the array is too small, abort. */
372 if (sarray->dim[0].ubound + 1 - sarray->dim[0].lbound < 13)
373 runtime_error ("Array size of SARRAY is too small.");
375 /* Convert Fortran unit number to C file descriptor. */
376 val = unit_to_fd ((int) *unit);
378 val = fstat(val, &sb);
383 sarray->data[0] = sb.st_dev;
386 sarray->data[sarray->dim[0].stride] = sb.st_ino;
389 sarray->data[2 * sarray->dim[0].stride] = sb.st_mode;
391 /* Number of (hard) links */
392 sarray->data[3 * sarray->dim[0].stride] = sb.st_nlink;
395 sarray->data[4 * sarray->dim[0].stride] = sb.st_uid;
398 sarray->data[5 * sarray->dim[0].stride] = sb.st_gid;
400 /* ID of device containing directory entry for file (0 if not available) */
401 #if HAVE_STRUCT_STAT_ST_RDEV
402 sarray->data[6 * sarray->dim[0].stride] = sb.st_rdev;
404 sarray->data[6 * sarray->dim[0].stride] = 0;
407 /* File size (bytes) */
408 sarray->data[7 * sarray->dim[0].stride] = sb.st_size;
410 /* Last access time */
411 sarray->data[8 * sarray->dim[0].stride] = sb.st_atime;
413 /* Last modification time */
414 sarray->data[9 * sarray->dim[0].stride] = sb.st_mtime;
416 /* Last file status change time */
417 sarray->data[10 * sarray->dim[0].stride] = sb.st_ctime;
419 /* Preferred I/O block size (-1 if not available) */
420 #if HAVE_STRUCT_STAT_ST_BLKSIZE
421 sarray->data[11 * sarray->dim[0].stride] = sb.st_blksize;
423 sarray->data[11 * sarray->dim[0].stride] = -1;
426 /* Number of blocks allocated (-1 if not available) */
427 #if HAVE_STRUCT_STAT_ST_BLOCKS
428 sarray->data[12 * sarray->dim[0].stride] = sb.st_blocks;
430 sarray->data[12 * sarray->dim[0].stride] = -1;
435 *status = (val == 0) ? 0 : errno;
437 iexport(fstat_i8_sub);
439 extern GFC_INTEGER_4 fstat_i4 (GFC_INTEGER_4 *, gfc_array_i4 *);
440 export_proto(fstat_i4);
443 fstat_i4 (GFC_INTEGER_4 *unit, gfc_array_i4 *sarray)
446 fstat_i4_sub (unit, sarray, &val);
450 extern GFC_INTEGER_8 fstat_i8 (GFC_INTEGER_8 *, gfc_array_i8 *);
451 export_proto(fstat_i8);
454 fstat_i8 (GFC_INTEGER_8 *unit, gfc_array_i8 *sarray)
457 fstat_i8_sub (unit, sarray, &val);