* global.c (global_alloc): Make it static.
[official-gcc.git] / libgfortran / intrinsics / stat.c
blobac91204522eef8c06c32aad5ad4fea07b412851f
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
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 "config.h"
32 #include "libgfortran.h"
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #include <sys/stat.h>
40 #endif
42 #ifdef HAVE_STDLIB_H
43 #include <stdlib.h>
44 #endif
46 #ifdef HAVE_STRING_H
47 #include <string.h>
48 #endif
50 #include <errno.h>
52 #include "../io/io.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)
60 INTEGER STAT
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 *,
65 gfc_charlen_type);
66 iexport_proto(stat_i4_sub);
68 void
69 stat_i4_sub (char *name, gfc_array_i4 *sarray, GFC_INTEGER_4 *status,
70 gfc_charlen_type name_len)
72 int val;
73 char *str;
74 struct stat sb;
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] == ' ')
89 name_len--;
91 /* Make a null terminated copy of the string. */
92 str = gfc_alloca (name_len + 1);
93 memcpy (str, name, name_len);
94 str[name_len] = '\0';
96 val = stat(str, &sb);
98 if (val == 0)
100 /* Device ID */
101 sarray->data[0 * sarray->dim[0].stride] = sb.st_dev;
103 /* Inode number */
104 sarray->data[1 * sarray->dim[0].stride] = sb.st_ino;
106 /* File mode */
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;
112 /* Owner's uid */
113 sarray->data[4 * sarray->dim[0].stride] = sb.st_uid;
115 /* Owner's gid */
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;
121 #else
122 sarray->data[6 * sarray->dim[0].stride] = 0;
123 #endif
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;
140 #else
141 sarray->data[11 * sarray->dim[0].stride] = -1;
142 #endif
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;
147 #else
148 sarray->data[12 * sarray->dim[0].stride] = -1;
149 #endif
152 if (status != NULL)
153 *status = (val == 0) ? 0 : errno;
155 iexport(stat_i4_sub);
157 extern void stat_i8_sub (char *, gfc_array_i8 *, GFC_INTEGER_8 *,
158 gfc_charlen_type);
159 iexport_proto(stat_i8_sub);
161 void
162 stat_i8_sub (char *name, gfc_array_i8 *sarray, GFC_INTEGER_8 *status,
163 gfc_charlen_type name_len)
165 int val;
166 char *str;
167 struct stat sb;
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] == ' ')
182 name_len--;
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);
191 if (val == 0)
193 /* Device ID */
194 sarray->data[0] = sb.st_dev;
196 /* Inode number */
197 sarray->data[sarray->dim[0].stride] = sb.st_ino;
199 /* File mode */
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;
205 /* Owner's uid */
206 sarray->data[4 * sarray->dim[0].stride] = sb.st_uid;
208 /* Owner's gid */
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;
214 #else
215 sarray->data[6 * sarray->dim[0].stride] = 0;
216 #endif
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;
233 #else
234 sarray->data[11 * sarray->dim[0].stride] = -1;
235 #endif
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;
240 #else
241 sarray->data[12 * sarray->dim[0].stride] = -1;
242 #endif
245 if (status != NULL)
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);
253 GFC_INTEGER_4
254 stat_i4 (char *name, gfc_array_i4 *sarray, gfc_charlen_type name_len)
256 GFC_INTEGER_4 val;
257 stat_i4_sub (name, sarray, &val, name_len);
258 return val;
261 extern GFC_INTEGER_8 stat_i8 (char *, gfc_array_i8 *, gfc_charlen_type);
262 export_proto(stat_i8);
264 GFC_INTEGER_8
265 stat_i8 (char *name, gfc_array_i8 *sarray, gfc_charlen_type name_len)
267 GFC_INTEGER_8 val;
268 stat_i8_sub (name, sarray, &val, name_len);
269 return val;
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)
279 INTEGER FSTAT
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);
286 void
287 fstat_i4_sub (GFC_INTEGER_4 *unit, gfc_array_i4 *sarray, GFC_INTEGER_4 *status)
289 int val;
290 struct stat sb;
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);
305 if (val >= 0)
306 val = fstat(val, &sb);
308 if (val == 0)
310 /* Device ID */
311 sarray->data[0 * sarray->dim[0].stride] = sb.st_dev;
313 /* Inode number */
314 sarray->data[1 * sarray->dim[0].stride] = sb.st_ino;
316 /* File mode */
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;
322 /* Owner's uid */
323 sarray->data[4 * sarray->dim[0].stride] = sb.st_uid;
325 /* Owner's gid */
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;
331 #else
332 sarray->data[6 * sarray->dim[0].stride] = 0;
333 #endif
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;
350 #else
351 sarray->data[11 * sarray->dim[0].stride] = -1;
352 #endif
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;
357 #else
358 sarray->data[12 * sarray->dim[0].stride] = -1;
359 #endif
362 if (status != NULL)
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);
370 void
371 fstat_i8_sub (GFC_INTEGER_8 *unit, gfc_array_i8 *sarray, GFC_INTEGER_8 *status)
373 int val;
374 struct stat sb;
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);
389 if (val >= 0)
390 val = fstat(val, &sb);
392 if (val == 0)
394 /* Device ID */
395 sarray->data[0] = sb.st_dev;
397 /* Inode number */
398 sarray->data[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_i8_sub);
451 extern GFC_INTEGER_4 fstat_i4 (GFC_INTEGER_4 *, gfc_array_i4 *);
452 export_proto(fstat_i4);
454 GFC_INTEGER_4
455 fstat_i4 (GFC_INTEGER_4 *unit, gfc_array_i4 *sarray)
457 GFC_INTEGER_4 val;
458 fstat_i4_sub (unit, sarray, &val);
459 return val;
462 extern GFC_INTEGER_8 fstat_i8 (GFC_INTEGER_8 *, gfc_array_i8 *);
463 export_proto(fstat_i8);
465 GFC_INTEGER_8
466 fstat_i8 (GFC_INTEGER_8 *unit, gfc_array_i8 *sarray)
468 GFC_INTEGER_8 val;
469 fstat_i8_sub (unit, sarray, &val);
470 return val;