* testsuite/libstdc++-abi/abi.exp: Add multilib support.
[official-gcc.git] / libgfortran / intrinsics / stat.c
blob11d59a884c347d0947a775e54e3bf01fa26e809b
1 /* Implementation of the STAT and FSTAT intrinsics.
2 Copyright (C) 2004, 2005, 2006, 2007 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
19 executable.)
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. */
31 #include "libgfortran.h"
33 #include <string.h>
34 #include <errno.h>
36 #ifdef HAVE_SYS_STAT_H
37 #include <sys/stat.h>
38 #endif
40 #ifdef HAVE_STDLIB_H
41 #include <stdlib.h>
42 #endif
45 #ifdef HAVE_STAT
47 /* SUBROUTINE STAT(FILE, SARRAY, STATUS)
48 CHARACTER(len=*), INTENT(IN) :: FILE
49 INTEGER, INTENT(OUT), :: SARRAY(13)
50 INTEGER, INTENT(OUT), OPTIONAL :: STATUS
52 FUNCTION STAT(FILE, SARRAY)
53 INTEGER STAT
54 CHARACTER(len=*), INTENT(IN) :: FILE
55 INTEGER, INTENT(OUT), :: SARRAY(13) */
57 /*extern void stat_i4_sub_0 (char *, gfc_array_i4 *, GFC_INTEGER_4 *,
58 gfc_charlen_type, int);
59 internal_proto(stat_i4_sub_0);*/
61 static void
62 stat_i4_sub_0 (char *name, gfc_array_i4 *sarray, GFC_INTEGER_4 *status,
63 gfc_charlen_type name_len, int is_lstat __attribute__ ((unused)))
65 int val;
66 char *str;
67 struct stat sb;
69 /* If the rank of the array is not 1, abort. */
70 if (GFC_DESCRIPTOR_RANK (sarray) != 1)
71 runtime_error ("Array rank of SARRAY is not 1.");
73 /* If the array is too small, abort. */
74 if (sarray->dim[0].ubound + 1 - sarray->dim[0].lbound < 13)
75 runtime_error ("Array size of SARRAY is too small.");
77 /* Trim trailing spaces from name. */
78 while (name_len > 0 && name[name_len - 1] == ' ')
79 name_len--;
81 /* Make a null terminated copy of the string. */
82 str = gfc_alloca (name_len + 1);
83 memcpy (str, name, name_len);
84 str[name_len] = '\0';
86 /* On platforms that don't provide lstat(), we use stat() instead. */
87 #ifdef HAVE_LSTAT
88 if (is_lstat)
89 val = lstat(str, &sb);
90 else
91 #endif
92 val = stat(str, &sb);
94 if (val == 0)
96 /* Device ID */
97 sarray->data[0 * sarray->dim[0].stride] = sb.st_dev;
99 /* Inode number */
100 sarray->data[1 * sarray->dim[0].stride] = sb.st_ino;
102 /* File mode */
103 sarray->data[2 * sarray->dim[0].stride] = sb.st_mode;
105 /* Number of (hard) links */
106 sarray->data[3 * sarray->dim[0].stride] = sb.st_nlink;
108 /* Owner's uid */
109 sarray->data[4 * sarray->dim[0].stride] = sb.st_uid;
111 /* Owner's gid */
112 sarray->data[5 * sarray->dim[0].stride] = sb.st_gid;
114 /* ID of device containing directory entry for file (0 if not available) */
115 #if HAVE_STRUCT_STAT_ST_RDEV
116 sarray->data[6 * sarray->dim[0].stride] = sb.st_rdev;
117 #else
118 sarray->data[6 * sarray->dim[0].stride] = 0;
119 #endif
121 /* File size (bytes) */
122 sarray->data[7 * sarray->dim[0].stride] = sb.st_size;
124 /* Last access time */
125 sarray->data[8 * sarray->dim[0].stride] = sb.st_atime;
127 /* Last modification time */
128 sarray->data[9 * sarray->dim[0].stride] = sb.st_mtime;
130 /* Last file status change time */
131 sarray->data[10 * sarray->dim[0].stride] = sb.st_ctime;
133 /* Preferred I/O block size (-1 if not available) */
134 #if HAVE_STRUCT_STAT_ST_BLKSIZE
135 sarray->data[11 * sarray->dim[0].stride] = sb.st_blksize;
136 #else
137 sarray->data[11 * sarray->dim[0].stride] = -1;
138 #endif
140 /* Number of blocks allocated (-1 if not available) */
141 #if HAVE_STRUCT_STAT_ST_BLOCKS
142 sarray->data[12 * sarray->dim[0].stride] = sb.st_blocks;
143 #else
144 sarray->data[12 * sarray->dim[0].stride] = -1;
145 #endif
148 if (status != NULL)
149 *status = (val == 0) ? 0 : errno;
153 extern void stat_i4_sub (char *, gfc_array_i4 *, GFC_INTEGER_4 *,
154 gfc_charlen_type);
155 iexport_proto(stat_i4_sub);
157 void
158 stat_i4_sub (char *name, gfc_array_i4 *sarray, GFC_INTEGER_4 *status,
159 gfc_charlen_type name_len)
161 stat_i4_sub_0 (name, sarray, status, name_len, 0);
163 iexport(stat_i4_sub);
166 extern void lstat_i4_sub (char *, gfc_array_i4 *, GFC_INTEGER_4 *,
167 gfc_charlen_type);
168 iexport_proto(lstat_i4_sub);
170 void
171 lstat_i4_sub (char *name, gfc_array_i4 *sarray, GFC_INTEGER_4 *status,
172 gfc_charlen_type name_len)
174 stat_i4_sub_0 (name, sarray, status, name_len, 1);
176 iexport(lstat_i4_sub);
180 static void
181 stat_i8_sub_0 (char *name, gfc_array_i8 *sarray, GFC_INTEGER_8 *status,
182 gfc_charlen_type name_len, int is_lstat __attribute__ ((unused)))
184 int val;
185 char *str;
186 struct stat sb;
188 /* If the rank of the array is not 1, abort. */
189 if (GFC_DESCRIPTOR_RANK (sarray) != 1)
190 runtime_error ("Array rank of SARRAY is not 1.");
192 /* If the array is too small, abort. */
193 if (sarray->dim[0].ubound + 1 - sarray->dim[0].lbound < 13)
194 runtime_error ("Array size of SARRAY is too small.");
196 /* Trim trailing spaces from name. */
197 while (name_len > 0 && name[name_len - 1] == ' ')
198 name_len--;
200 /* Make a null terminated copy of the string. */
201 str = gfc_alloca (name_len + 1);
202 memcpy (str, name, name_len);
203 str[name_len] = '\0';
205 /* On platforms that don't provide lstat(), we use stat() instead. */
206 #ifdef HAVE_LSTAT
207 if (is_lstat)
208 val = lstat(str, &sb);
209 else
210 #endif
211 val = stat(str, &sb);
213 if (val == 0)
215 /* Device ID */
216 sarray->data[0] = sb.st_dev;
218 /* Inode number */
219 sarray->data[sarray->dim[0].stride] = sb.st_ino;
221 /* File mode */
222 sarray->data[2 * sarray->dim[0].stride] = sb.st_mode;
224 /* Number of (hard) links */
225 sarray->data[3 * sarray->dim[0].stride] = sb.st_nlink;
227 /* Owner's uid */
228 sarray->data[4 * sarray->dim[0].stride] = sb.st_uid;
230 /* Owner's gid */
231 sarray->data[5 * sarray->dim[0].stride] = sb.st_gid;
233 /* ID of device containing directory entry for file (0 if not available) */
234 #if HAVE_STRUCT_STAT_ST_RDEV
235 sarray->data[6 * sarray->dim[0].stride] = sb.st_rdev;
236 #else
237 sarray->data[6 * sarray->dim[0].stride] = 0;
238 #endif
240 /* File size (bytes) */
241 sarray->data[7 * sarray->dim[0].stride] = sb.st_size;
243 /* Last access time */
244 sarray->data[8 * sarray->dim[0].stride] = sb.st_atime;
246 /* Last modification time */
247 sarray->data[9 * sarray->dim[0].stride] = sb.st_mtime;
249 /* Last file status change time */
250 sarray->data[10 * sarray->dim[0].stride] = sb.st_ctime;
252 /* Preferred I/O block size (-1 if not available) */
253 #if HAVE_STRUCT_STAT_ST_BLKSIZE
254 sarray->data[11 * sarray->dim[0].stride] = sb.st_blksize;
255 #else
256 sarray->data[11 * sarray->dim[0].stride] = -1;
257 #endif
259 /* Number of blocks allocated (-1 if not available) */
260 #if HAVE_STRUCT_STAT_ST_BLOCKS
261 sarray->data[12 * sarray->dim[0].stride] = sb.st_blocks;
262 #else
263 sarray->data[12 * sarray->dim[0].stride] = -1;
264 #endif
267 if (status != NULL)
268 *status = (val == 0) ? 0 : errno;
272 extern void stat_i8_sub (char *, gfc_array_i8 *, GFC_INTEGER_8 *,
273 gfc_charlen_type);
274 iexport_proto(stat_i8_sub);
276 void
277 stat_i8_sub (char *name, gfc_array_i8 *sarray, GFC_INTEGER_8 *status,
278 gfc_charlen_type name_len)
280 stat_i8_sub_0 (name, sarray, status, name_len, 0);
283 iexport(stat_i8_sub);
286 extern void lstat_i8_sub (char *, gfc_array_i8 *, GFC_INTEGER_8 *,
287 gfc_charlen_type);
288 iexport_proto(lstat_i8_sub);
290 void
291 lstat_i8_sub (char *name, gfc_array_i8 *sarray, GFC_INTEGER_8 *status,
292 gfc_charlen_type name_len)
294 stat_i8_sub_0 (name, sarray, status, name_len, 1);
297 iexport(lstat_i8_sub);
300 extern GFC_INTEGER_4 stat_i4 (char *, gfc_array_i4 *, gfc_charlen_type);
301 export_proto(stat_i4);
303 GFC_INTEGER_4
304 stat_i4 (char *name, gfc_array_i4 *sarray, gfc_charlen_type name_len)
306 GFC_INTEGER_4 val;
307 stat_i4_sub (name, sarray, &val, name_len);
308 return val;
311 extern GFC_INTEGER_8 stat_i8 (char *, gfc_array_i8 *, gfc_charlen_type);
312 export_proto(stat_i8);
314 GFC_INTEGER_8
315 stat_i8 (char *name, gfc_array_i8 *sarray, gfc_charlen_type name_len)
317 GFC_INTEGER_8 val;
318 stat_i8_sub (name, sarray, &val, name_len);
319 return val;
323 /* SUBROUTINE LSTAT(FILE, SARRAY, STATUS)
324 CHARACTER(len=*), INTENT(IN) :: FILE
325 INTEGER, INTENT(OUT), :: SARRAY(13)
326 INTEGER, INTENT(OUT), OPTIONAL :: STATUS
328 FUNCTION LSTAT(FILE, SARRAY)
329 INTEGER LSTAT
330 CHARACTER(len=*), INTENT(IN) :: FILE
331 INTEGER, INTENT(OUT), :: SARRAY(13) */
333 extern GFC_INTEGER_4 lstat_i4 (char *, gfc_array_i4 *, gfc_charlen_type);
334 export_proto(lstat_i4);
336 GFC_INTEGER_4
337 lstat_i4 (char *name, gfc_array_i4 *sarray, gfc_charlen_type name_len)
339 GFC_INTEGER_4 val;
340 lstat_i4_sub (name, sarray, &val, name_len);
341 return val;
344 extern GFC_INTEGER_8 lstat_i8 (char *, gfc_array_i8 *, gfc_charlen_type);
345 export_proto(lstat_i8);
347 GFC_INTEGER_8
348 lstat_i8 (char *name, gfc_array_i8 *sarray, gfc_charlen_type name_len)
350 GFC_INTEGER_8 val;
351 lstat_i8_sub (name, sarray, &val, name_len);
352 return val;
355 #endif
358 #ifdef HAVE_FSTAT
360 /* SUBROUTINE FSTAT(UNIT, SARRAY, STATUS)
361 INTEGER, INTENT(IN) :: UNIT
362 INTEGER, INTENT(OUT) :: SARRAY(13)
363 INTEGER, INTENT(OUT), OPTIONAL :: STATUS
365 FUNCTION FSTAT(UNIT, SARRAY)
366 INTEGER FSTAT
367 INTEGER, INTENT(IN) :: UNIT
368 INTEGER, INTENT(OUT) :: SARRAY(13) */
370 extern void fstat_i4_sub (GFC_INTEGER_4 *, gfc_array_i4 *, GFC_INTEGER_4 *);
371 iexport_proto(fstat_i4_sub);
373 void
374 fstat_i4_sub (GFC_INTEGER_4 *unit, gfc_array_i4 *sarray, GFC_INTEGER_4 *status)
376 int val;
377 struct stat sb;
379 /* If the rank of the array is not 1, abort. */
380 if (GFC_DESCRIPTOR_RANK (sarray) != 1)
381 runtime_error ("Array rank of SARRAY is not 1.");
383 /* If the array is too small, abort. */
384 if (sarray->dim[0].ubound + 1 - sarray->dim[0].lbound < 13)
385 runtime_error ("Array size of SARRAY is too small.");
387 /* Convert Fortran unit number to C file descriptor. */
388 val = unit_to_fd (*unit);
389 if (val >= 0)
390 val = fstat(val, &sb);
392 if (val == 0)
394 /* Device ID */
395 sarray->data[0 * sarray->dim[0].stride] = sb.st_dev;
397 /* Inode number */
398 sarray->data[1 * sarray->dim[0].stride] = sb.st_ino;
400 /* File mode */
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;
406 /* Owner's uid */
407 sarray->data[4 * sarray->dim[0].stride] = sb.st_uid;
409 /* Owner's gid */
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;
415 #else
416 sarray->data[6 * sarray->dim[0].stride] = 0;
417 #endif
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;
434 #else
435 sarray->data[11 * sarray->dim[0].stride] = -1;
436 #endif
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;
441 #else
442 sarray->data[12 * sarray->dim[0].stride] = -1;
443 #endif
446 if (status != NULL)
447 *status = (val == 0) ? 0 : errno;
449 iexport(fstat_i4_sub);
451 extern void fstat_i8_sub (GFC_INTEGER_8 *, gfc_array_i8 *, GFC_INTEGER_8 *);
452 iexport_proto(fstat_i8_sub);
454 void
455 fstat_i8_sub (GFC_INTEGER_8 *unit, gfc_array_i8 *sarray, GFC_INTEGER_8 *status)
457 int val;
458 struct stat sb;
460 /* If the rank of the array is not 1, abort. */
461 if (GFC_DESCRIPTOR_RANK (sarray) != 1)
462 runtime_error ("Array rank of SARRAY is not 1.");
464 /* If the array is too small, abort. */
465 if (sarray->dim[0].ubound + 1 - sarray->dim[0].lbound < 13)
466 runtime_error ("Array size of SARRAY is too small.");
468 /* Convert Fortran unit number to C file descriptor. */
469 val = unit_to_fd ((int) *unit);
470 if (val >= 0)
471 val = fstat(val, &sb);
473 if (val == 0)
475 /* Device ID */
476 sarray->data[0] = sb.st_dev;
478 /* Inode number */
479 sarray->data[sarray->dim[0].stride] = sb.st_ino;
481 /* File mode */
482 sarray->data[2 * sarray->dim[0].stride] = sb.st_mode;
484 /* Number of (hard) links */
485 sarray->data[3 * sarray->dim[0].stride] = sb.st_nlink;
487 /* Owner's uid */
488 sarray->data[4 * sarray->dim[0].stride] = sb.st_uid;
490 /* Owner's gid */
491 sarray->data[5 * sarray->dim[0].stride] = sb.st_gid;
493 /* ID of device containing directory entry for file (0 if not available) */
494 #if HAVE_STRUCT_STAT_ST_RDEV
495 sarray->data[6 * sarray->dim[0].stride] = sb.st_rdev;
496 #else
497 sarray->data[6 * sarray->dim[0].stride] = 0;
498 #endif
500 /* File size (bytes) */
501 sarray->data[7 * sarray->dim[0].stride] = sb.st_size;
503 /* Last access time */
504 sarray->data[8 * sarray->dim[0].stride] = sb.st_atime;
506 /* Last modification time */
507 sarray->data[9 * sarray->dim[0].stride] = sb.st_mtime;
509 /* Last file status change time */
510 sarray->data[10 * sarray->dim[0].stride] = sb.st_ctime;
512 /* Preferred I/O block size (-1 if not available) */
513 #if HAVE_STRUCT_STAT_ST_BLKSIZE
514 sarray->data[11 * sarray->dim[0].stride] = sb.st_blksize;
515 #else
516 sarray->data[11 * sarray->dim[0].stride] = -1;
517 #endif
519 /* Number of blocks allocated (-1 if not available) */
520 #if HAVE_STRUCT_STAT_ST_BLOCKS
521 sarray->data[12 * sarray->dim[0].stride] = sb.st_blocks;
522 #else
523 sarray->data[12 * sarray->dim[0].stride] = -1;
524 #endif
527 if (status != NULL)
528 *status = (val == 0) ? 0 : errno;
530 iexport(fstat_i8_sub);
532 extern GFC_INTEGER_4 fstat_i4 (GFC_INTEGER_4 *, gfc_array_i4 *);
533 export_proto(fstat_i4);
535 GFC_INTEGER_4
536 fstat_i4 (GFC_INTEGER_4 *unit, gfc_array_i4 *sarray)
538 GFC_INTEGER_4 val;
539 fstat_i4_sub (unit, sarray, &val);
540 return val;
543 extern GFC_INTEGER_8 fstat_i8 (GFC_INTEGER_8 *, gfc_array_i8 *);
544 export_proto(fstat_i8);
546 GFC_INTEGER_8
547 fstat_i8 (GFC_INTEGER_8 *unit, gfc_array_i8 *sarray)
549 GFC_INTEGER_8 val;
550 fstat_i8_sub (unit, sarray, &val);
551 return val;
554 #endif