]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/io/unix.c
2005-11-26 Eric Christopher <echristo@apple.com>
[thirdparty/gcc.git] / libgfortran / io / unix.c
CommitLineData
a0007dfa 1/* Copyright (C) 2002, 2003, 2004, 2005
2 Free Software Foundation, Inc.
4ee9c684 3 Contributed by Andy Vaught
4
5This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
7Libgfortran is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
b417ea8c 12In addition to the permissions in the GNU General Public License, the
13Free Software Foundation gives you unlimited permission to link the
14compiled version of this file into combinations with other programs,
15and to distribute those combinations without any restriction coming
16from the use of this file. (The General Public License restrictions
17do apply in other respects; for example, they cover modification of
18the file, and distribution when not linked into a combine
19executable.)
20
4ee9c684 21Libgfortran is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24GNU General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with Libgfortran; see the file COPYING. If not, write to
5ac2525b 28the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29Boston, MA 02110-1301, USA. */
4ee9c684 30
31/* Unix stream I/O module */
32
33#include "config.h"
34#include <stdlib.h>
35#include <limits.h>
36
37#include <unistd.h>
91e6f54a 38#include <stdio.h>
4ee9c684 39#include <sys/stat.h>
40#include <fcntl.h>
2639e4cd 41#include <assert.h>
4ee9c684 42
4ee9c684 43#include <string.h>
44#include <errno.h>
45
46#include "libgfortran.h"
47#include "io.h"
60c514ba 48#include "unix.h"
4ee9c684 49
8df8ec57 50#ifndef SSIZE_MAX
51#define SSIZE_MAX SHRT_MAX
52#endif
53
4ee9c684 54#ifndef PATH_MAX
55#define PATH_MAX 1024
56#endif
57
d2455565 58#ifndef PROT_READ
59#define PROT_READ 1
60#endif
61
62#ifndef PROT_WRITE
63#define PROT_WRITE 2
64#endif
65
7dfba97b 66/* These flags aren't defined on all targets (mingw32), so provide them
67 here. */
68#ifndef S_IRGRP
69#define S_IRGRP 0
70#endif
71
72#ifndef S_IWGRP
73#define S_IWGRP 0
74#endif
75
76#ifndef S_IROTH
77#define S_IROTH 0
78#endif
79
80#ifndef S_IWOTH
81#define S_IWOTH 0
82#endif
83
4ee9c684 84/* This implementation of stream I/O is based on the paper:
85 *
86 * "Exploiting the advantages of mapped files for stream I/O",
87 * O. Krieger, M. Stumm and R. Umrau, "Proceedings of the 1992 Winter
88 * USENIX conference", p. 27-42.
89 *
90 * It differs in a number of ways from the version described in the
91 * paper. First of all, threads are not an issue during I/O and we
92 * also don't have to worry about having multiple regions, since
93 * fortran's I/O model only allows you to be one place at a time.
94 *
95 * On the other hand, we have to be able to writing at the end of a
96 * stream, read from the start of a stream or read and write blocks of
97 * bytes from an arbitrary position. After opening a file, a pointer
98 * to a stream structure is returned, which is used to handle file
99 * accesses until the file is closed.
100 *
101 * salloc_at_r(stream, len, where)-- Given a stream pointer, return a
102 * pointer to a block of memory that mirror the file at position
103 * 'where' that is 'len' bytes long. The len integer is updated to
104 * reflect how many bytes were actually read. The only reason for a
105 * short read is end of file. The file pointer is updated. The
106 * pointer is valid until the next call to salloc_*.
107 *
108 * salloc_at_w(stream, len, where)-- Given the stream pointer, returns
109 * a pointer to a block of memory that is updated to reflect the state
110 * of the file. The length of the buffer is always equal to that
111 * requested. The buffer must be completely set by the caller. When
112 * data has been written, the sfree() function must be called to
113 * indicate that the caller is done writing data to the buffer. This
114 * may or may not cause a physical write.
115 *
116 * Short forms of these are salloc_r() and salloc_w() which drop the
117 * 'where' parameter and use the current file pointer. */
118
119
4ee9c684 120/*move_pos_offset()-- Move the record pointer right or left
121 *relative to current position */
122
123int
124move_pos_offset (stream* st, int pos_off)
125{
126 unix_stream * str = (unix_stream*)st;
127 if (pos_off < 0)
128 {
363dcb81 129 str->logical_offset += pos_off;
4ee9c684 130
363dcb81 131 if (str->dirty_offset + str->ndirty > str->logical_offset)
4ee9c684 132 {
363dcb81 133 if (str->ndirty + pos_off > 0)
134 str->ndirty += pos_off;
4ee9c684 135 else
136 {
137 str->dirty_offset += pos_off + pos_off;
363dcb81 138 str->ndirty = 0;
4ee9c684 139 }
140 }
141
363dcb81 142 return pos_off;
4ee9c684 143 }
363dcb81 144 return 0;
4ee9c684 145}
146
147
148/* fix_fd()-- Given a file descriptor, make sure it is not one of the
149 * standard descriptors, returning a non-standard descriptor. If the
150 * user specifies that system errors should go to standard output,
151 * then closes standard output, we don't want the system errors to a
152 * file that has been given file descriptor 1 or 0. We want to send
153 * the error to the invalid descriptor. */
154
155static int
156fix_fd (int fd)
157{
158 int input, output, error;
159
160 input = output = error = 0;
161
7145fd06 162 /* Unix allocates the lowest descriptors first, so a loop is not
163 required, but this order is. */
4ee9c684 164
165 if (fd == STDIN_FILENO)
166 {
167 fd = dup (fd);
168 input = 1;
169 }
170 if (fd == STDOUT_FILENO)
171 {
172 fd = dup (fd);
173 output = 1;
174 }
175 if (fd == STDERR_FILENO)
176 {
177 fd = dup (fd);
178 error = 1;
179 }
180
181 if (input)
182 close (STDIN_FILENO);
183 if (output)
184 close (STDOUT_FILENO);
185 if (error)
186 close (STDERR_FILENO);
187
188 return fd;
189}
190
353c8a95 191int
192is_preconnected (stream * s)
193{
194 int fd;
195
196 fd = ((unix_stream *) s)->fd;
197 if (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO)
198 return 1;
199 else
200 return 0;
201}
4ee9c684 202
2488b3b6 203/* If the stream corresponds to a preconnected unit, we flush the
204 corresponding C stream. This is bugware for mixed C-Fortran codes
205 where the C code doesn't flush I/O before returning. */
206void
207flush_if_preconnected (stream * s)
208{
209 int fd;
210
211 fd = ((unix_stream *) s)->fd;
212 if (fd == STDIN_FILENO)
213 fflush (stdin);
214 else if (fd == STDOUT_FILENO)
215 fflush (stdout);
216 else if (fd == STDERR_FILENO)
217 fflush (stderr);
218}
219
4ee9c684 220
b2a112ca 221/* Reset a stream after reading/writing. Assumes that the buffers have
222 been flushed. */
223
224inline static void
225reset_stream (unix_stream * s, size_t bytes_rw)
4ee9c684 226{
b2a112ca 227 s->physical_offset += bytes_rw;
228 s->logical_offset = s->physical_offset;
229 if (s->file_length != -1 && s->physical_offset > s->file_length)
230 s->file_length = s->physical_offset;
231}
4ee9c684 232
4ee9c684 233
b2a112ca 234/* Read bytes into a buffer, allowing for short reads. If the nbytes
235 * argument is less on return than on entry, it is because we've hit
236 * the end of file. */
4ee9c684 237
b2a112ca 238static int
239do_read (unix_stream * s, void * buf, size_t * nbytes)
240{
241 ssize_t trans;
242 size_t bytes_left;
243 char *buf_st;
244 int status;
245
246 status = 0;
247 bytes_left = *nbytes;
248 buf_st = (char *) buf;
249
250 /* We must read in a loop since some systems don't restart system
251 calls in case of a signal. */
252 while (bytes_left > 0)
253 {
254 /* Requests between SSIZE_MAX and SIZE_MAX are undefined by SUSv3,
255 so we must read in chunks smaller than SSIZE_MAX. */
256 trans = (bytes_left < SSIZE_MAX) ? bytes_left : SSIZE_MAX;
257 trans = read (s->fd, buf_st, trans);
258 if (trans < 0)
259 {
260 if (errno == EINTR)
261 continue;
262 else
263 {
264 status = errno;
265 break;
266 }
267 }
268 else if (trans == 0) /* We hit EOF. */
269 break;
270 buf_st += trans;
271 bytes_left -= trans;
4ee9c684 272 }
273
b2a112ca 274 *nbytes -= bytes_left;
275 return status;
4ee9c684 276}
277
278
b2a112ca 279/* Write a buffer to a stream, allowing for short writes. */
4ee9c684 280
281static int
b2a112ca 282do_write (unix_stream * s, const void * buf, size_t * nbytes)
4ee9c684 283{
b2a112ca 284 ssize_t trans;
285 size_t bytes_left;
286 char *buf_st;
287 int status;
288
289 status = 0;
290 bytes_left = *nbytes;
291 buf_st = (char *) buf;
292
293 /* We must write in a loop since some systems don't restart system
294 calls in case of a signal. */
295 while (bytes_left > 0)
4ee9c684 296 {
b2a112ca 297 /* Requests between SSIZE_MAX and SIZE_MAX are undefined by SUSv3,
298 so we must write in chunks smaller than SSIZE_MAX. */
299 trans = (bytes_left < SSIZE_MAX) ? bytes_left : SSIZE_MAX;
300 trans = write (s->fd, buf_st, trans);
301 if (trans < 0)
302 {
303 if (errno == EINTR)
304 continue;
305 else
306 {
307 status = errno;
308 break;
309 }
310 }
311 buf_st += trans;
312 bytes_left -= trans;
4ee9c684 313 }
314
b2a112ca 315 *nbytes -= bytes_left;
316 return status;
4ee9c684 317}
4ee9c684 318
319
320/* get_oserror()-- Get the most recent operating system error. For
321 * unix, this is errno. */
322
323const char *
324get_oserror (void)
325{
4ee9c684 326 return strerror (errno);
327}
328
329
330/* sys_exit()-- Terminate the program with an exit code */
331
332void
333sys_exit (int code)
334{
4ee9c684 335 exit (code);
336}
337
338
4ee9c684 339/*********************************************************************
340 File descriptor stream functions
341*********************************************************************/
342
b2a112ca 343
4ee9c684 344/* fd_flush()-- Write bytes that need to be written */
345
346static try
347fd_flush (unix_stream * s)
348{
b2a112ca 349 size_t writelen;
350
4ee9c684 351 if (s->ndirty == 0)
352 return SUCCESS;;
353
354 if (s->physical_offset != s->dirty_offset &&
355 lseek (s->fd, s->dirty_offset, SEEK_SET) < 0)
356 return FAILURE;
357
b2a112ca 358 writelen = s->ndirty;
359 if (do_write (s, s->buffer + (s->dirty_offset - s->buffer_offset),
360 &writelen) != 0)
4ee9c684 361 return FAILURE;
362
b2a112ca 363 s->physical_offset = s->dirty_offset + writelen;
5a78b88f 364
365 /* don't increment file_length if the file is non-seekable */
366 if (s->file_length != -1 && s->physical_offset > s->file_length)
b2a112ca 367 s->file_length = s->physical_offset;
368
369 s->ndirty -= writelen;
370 if (s->ndirty != 0)
371 return FAILURE;
4ee9c684 372
373 return SUCCESS;
374}
375
376
377/* fd_alloc()-- Arrange a buffer such that the salloc() request can be
378 * satisfied. This subroutine gets the buffer ready for whatever is
379 * to come next. */
380
381static void
a0007dfa 382fd_alloc (unix_stream * s, gfc_offset where,
383 int *len __attribute__ ((unused)))
4ee9c684 384{
385 char *new_buffer;
386 int n, read_len;
387
388 if (*len <= BUFFER_SIZE)
389 {
390 new_buffer = s->small_buffer;
391 read_len = BUFFER_SIZE;
392 }
393 else
394 {
395 new_buffer = get_mem (*len);
396 read_len = *len;
397 }
398
399 /* Salvage bytes currently within the buffer. This is important for
400 * devices that cannot seek. */
401
402 if (s->buffer != NULL && s->buffer_offset <= where &&
403 where <= s->buffer_offset + s->active)
404 {
405
406 n = s->active - (where - s->buffer_offset);
407 memmove (new_buffer, s->buffer + (where - s->buffer_offset), n);
408
409 s->active = n;
410 }
411 else
412 { /* new buffer starts off empty */
413 s->active = 0;
414 }
415
416 s->buffer_offset = where;
417
418 /* free the old buffer if necessary */
419
420 if (s->buffer != NULL && s->buffer != s->small_buffer)
421 free_mem (s->buffer);
422
423 s->buffer = new_buffer;
424 s->len = read_len;
4ee9c684 425}
426
427
428/* fd_alloc_r_at()-- Allocate a stream buffer for reading. Either
429 * we've already buffered the data or we need to load it. Returns
430 * NULL on I/O error. */
431
432static char *
b093181d 433fd_alloc_r_at (unix_stream * s, int *len, gfc_offset where)
4ee9c684 434{
b093181d 435 gfc_offset m;
4ee9c684 436
437 if (where == -1)
438 where = s->logical_offset;
439
440 if (s->buffer != NULL && s->buffer_offset <= where &&
441 where + *len <= s->buffer_offset + s->active)
442 {
443
444 /* Return a position within the current buffer */
445
446 s->logical_offset = where + *len;
447 return s->buffer + where - s->buffer_offset;
448 }
449
450 fd_alloc (s, where, len);
451
452 m = where + s->active;
453
454 if (s->physical_offset != m && lseek (s->fd, m, SEEK_SET) < 0)
455 return NULL;
456
fef3501c 457 /* do_read() hangs on read from terminals for *BSD-systems. Only
458 use read() in that case. */
459
460 if (s->special_file)
461 {
462 ssize_t n;
463
464 n = read (s->fd, s->buffer + s->active, s->len - s->active);
465 if (n < 0)
466 return NULL;
467
468 s->physical_offset = where + n;
469 s->active += n;
470 }
471 else
472 {
473 size_t n;
4ee9c684 474
fef3501c 475 n = s->len - s->active;
476 if (do_read (s, s->buffer + s->active, &n) != 0)
477 return NULL;
478
479 s->physical_offset = where + n;
480 s->active += n;
481 }
4ee9c684 482
4ee9c684 483 if (s->active < *len)
484 *len = s->active; /* Bytes actually available */
485
486 s->logical_offset = where + *len;
487
488 return s->buffer;
489}
490
491
492/* fd_alloc_w_at()-- Allocate a stream buffer for writing. Either
493 * we've already buffered the data or we need to load it. */
494
495static char *
b093181d 496fd_alloc_w_at (unix_stream * s, int *len, gfc_offset where)
4ee9c684 497{
b093181d 498 gfc_offset n;
4ee9c684 499
500 if (where == -1)
501 where = s->logical_offset;
502
503 if (s->buffer == NULL || s->buffer_offset > where ||
504 where + *len > s->buffer_offset + s->len)
505 {
506
507 if (fd_flush (s) == FAILURE)
508 return NULL;
509 fd_alloc (s, where, len);
510 }
511
512 /* Return a position within the current buffer */
5a78b88f 513 if (s->ndirty == 0
514 || where > s->dirty_offset + s->ndirty
515 || s->dirty_offset > where + *len)
516 { /* Discontiguous blocks, start with a clean buffer. */
517 /* Flush the buffer. */
518 if (s->ndirty != 0)
519 fd_flush (s);
520 s->dirty_offset = where;
521 s->ndirty = *len;
4ee9c684 522 }
523 else
5a78b88f 524 {
525 gfc_offset start; /* Merge with the existing data. */
526 if (where < s->dirty_offset)
527 start = where;
528 else
529 start = s->dirty_offset;
530 if (where + *len > s->dirty_offset + s->ndirty)
531 s->ndirty = where + *len - start;
532 else
533 s->ndirty = s->dirty_offset + s->ndirty - start;
534 s->dirty_offset = start;
4ee9c684 535 }
536
537 s->logical_offset = where + *len;
538
f82543e7 539 if (where + *len > s->file_length)
540 s->file_length = where + *len;
541
4ee9c684 542 n = s->logical_offset - s->buffer_offset;
543 if (n > s->active)
544 s->active = n;
545
546 return s->buffer + where - s->buffer_offset;
547}
548
549
550static try
551fd_sfree (unix_stream * s)
552{
4ee9c684 553 if (s->ndirty != 0 &&
554 (s->buffer != s->small_buffer || options.all_unbuffered ||
555 s->unbuffered))
556 return fd_flush (s);
557
558 return SUCCESS;
559}
560
561
b2a112ca 562static try
b093181d 563fd_seek (unix_stream * s, gfc_offset offset)
4ee9c684 564{
b2a112ca 565 if (s->physical_offset == offset) /* Are we lucky and avoid syscall? */
566 {
567 s->logical_offset = offset;
568 return SUCCESS;
569 }
570
4ee9c684 571 s->physical_offset = s->logical_offset = offset;
572
573 return (lseek (s->fd, offset, SEEK_SET) < 0) ? FAILURE : SUCCESS;
574}
575
576
577/* truncate_file()-- Given a unit, truncate the file at the current
578 * position. Sets the physical location to the new end of the file.
579 * Returns nonzero on error. */
580
581static try
582fd_truncate (unix_stream * s)
583{
5a78b88f 584 if (lseek (s->fd, s->logical_offset, SEEK_SET) == -1)
4ee9c684 585 return FAILURE;
586
5a78b88f 587 /* non-seekable files, like terminals and fifo's fail the lseek.
227e9423 588 Using ftruncate on a seekable special file (like /dev/null)
589 is undefined, so we treat it as if the ftruncate failed.
590 */
a54b1ce7 591#ifdef HAVE_FTRUNCATE
227e9423 592 if (s->special_file || ftruncate (s->fd, s->logical_offset))
a54b1ce7 593#else
594#ifdef HAVE_CHSIZE
227e9423 595 if (s->special_file || chsize (s->fd, s->logical_offset))
a54b1ce7 596#endif
597#endif
72909c79 598 {
599 s->physical_offset = s->file_length = 0;
600 return FAILURE;
601 }
5a78b88f 602
603 s->physical_offset = s->file_length = s->logical_offset;
4ee9c684 604
605 return SUCCESS;
606}
607
608
b2a112ca 609
610
611/* Stream read function. Avoids using a buffer for big reads. The
612 interface is like POSIX read(), but the nbytes argument is a
613 pointer; on return it contains the number of bytes written. The
614 function return value is the status indicator (0 for success). */
615
616static int
617fd_read (unix_stream * s, void * buf, size_t * nbytes)
618{
619 void *p;
620 int tmp, status;
621
622 if (*nbytes < BUFFER_SIZE && !s->unbuffered)
623 {
624 tmp = *nbytes;
625 p = fd_alloc_r_at (s, &tmp, -1);
626 if (p)
627 {
628 *nbytes = tmp;
629 memcpy (buf, p, *nbytes);
630 return 0;
631 }
632 else
633 {
634 *nbytes = 0;
635 return errno;
636 }
637 }
638
639 /* If the request is bigger than BUFFER_SIZE we flush the buffers
640 and read directly. */
641 if (fd_flush (s) == FAILURE)
642 {
643 *nbytes = 0;
644 return errno;
645 }
646
647 if (is_seekable ((stream *) s) && fd_seek (s, s->logical_offset) == FAILURE)
648 {
649 *nbytes = 0;
650 return errno;
651 }
652
653 status = do_read (s, buf, nbytes);
654 reset_stream (s, *nbytes);
655 return status;
656}
657
658
659/* Stream write function. Avoids using a buffer for big writes. The
660 interface is like POSIX write(), but the nbytes argument is a
661 pointer; on return it contains the number of bytes written. The
662 function return value is the status indicator (0 for success). */
663
664static int
665fd_write (unix_stream * s, const void * buf, size_t * nbytes)
666{
667 void *p;
668 int tmp, status;
669
670 if (*nbytes < BUFFER_SIZE && !s->unbuffered)
671 {
672 tmp = *nbytes;
673 p = fd_alloc_w_at (s, &tmp, -1);
674 if (p)
675 {
676 *nbytes = tmp;
677 memcpy (p, buf, *nbytes);
678 return 0;
679 }
680 else
681 {
682 *nbytes = 0;
683 return errno;
684 }
685 }
686
687 /* If the request is bigger than BUFFER_SIZE we flush the buffers
688 and write directly. */
689 if (fd_flush (s) == FAILURE)
690 {
691 *nbytes = 0;
692 return errno;
693 }
694
695 if (is_seekable ((stream *) s) && fd_seek (s, s->logical_offset) == FAILURE)
696 {
697 *nbytes = 0;
698 return errno;
699 }
700
701 status = do_write (s, buf, nbytes);
702 reset_stream (s, *nbytes);
703 return status;
704}
705
706
4ee9c684 707static try
708fd_close (unix_stream * s)
709{
4ee9c684 710 if (fd_flush (s) == FAILURE)
711 return FAILURE;
712
713 if (s->buffer != NULL && s->buffer != s->small_buffer)
714 free_mem (s->buffer);
715
f8f6940b 716 if (s->fd != STDOUT_FILENO && s->fd != STDERR_FILENO)
717 {
718 if (close (s->fd) < 0)
719 return FAILURE;
720 }
4ee9c684 721
722 free_mem (s);
723
724 return SUCCESS;
725}
726
727
728static void
729fd_open (unix_stream * s)
730{
4ee9c684 731 if (isatty (s->fd))
732 s->unbuffered = 1;
733
734 s->st.alloc_r_at = (void *) fd_alloc_r_at;
735 s->st.alloc_w_at = (void *) fd_alloc_w_at;
736 s->st.sfree = (void *) fd_sfree;
737 s->st.close = (void *) fd_close;
738 s->st.seek = (void *) fd_seek;
739 s->st.truncate = (void *) fd_truncate;
b2a112ca 740 s->st.read = (void *) fd_read;
741 s->st.write = (void *) fd_write;
4ee9c684 742
743 s->buffer = NULL;
744}
745
746
4ee9c684 747
b2a112ca 748
4ee9c684 749/*********************************************************************
750 memory stream functions - These are used for internal files
751
752 The idea here is that a single stream structure is created and all
753 requests must be satisfied from it. The location and size of the
754 buffer is the character variable supplied to the READ or WRITE
755 statement.
756
757*********************************************************************/
758
759
760static char *
b093181d 761mem_alloc_r_at (unix_stream * s, int *len, gfc_offset where)
4ee9c684 762{
b093181d 763 gfc_offset n;
4ee9c684 764
765 if (where == -1)
766 where = s->logical_offset;
767
768 if (where < s->buffer_offset || where > s->buffer_offset + s->active)
769 return NULL;
770
4ee9c684 771 s->logical_offset = where + *len;
772
11de4bf9 773 n = s->buffer_offset + s->active - where;
4ee9c684 774 if (*len > n)
775 *len = n;
776
777 return s->buffer + (where - s->buffer_offset);
778}
779
780
781static char *
b093181d 782mem_alloc_w_at (unix_stream * s, int *len, gfc_offset where)
4ee9c684 783{
b093181d 784 gfc_offset m;
4ee9c684 785
2639e4cd 786 assert (*len >= 0); /* Negative values not allowed. */
787
4ee9c684 788 if (where == -1)
789 where = s->logical_offset;
790
791 m = where + *len;
792
2639e4cd 793 if (where < s->buffer_offset)
4ee9c684 794 return NULL;
795
2639e4cd 796 if (m > s->file_length)
72231bd6 797 return NULL;
2639e4cd 798
4ee9c684 799 s->logical_offset = m;
800
801 return s->buffer + (where - s->buffer_offset);
802}
803
804
b2a112ca 805/* Stream read function for internal units. This is not actually used
806 at the moment, as all internal IO is formatted and the formatted IO
807 routines use mem_alloc_r_at. */
808
809static int
810mem_read (unix_stream * s, void * buf, size_t * nbytes)
811{
812 void *p;
813 int tmp;
814
815 tmp = *nbytes;
816 p = mem_alloc_r_at (s, &tmp, -1);
817 if (p)
818 {
819 *nbytes = tmp;
820 memcpy (buf, p, *nbytes);
821 return 0;
822 }
823 else
824 {
825 *nbytes = 0;
826 return errno;
827 }
828}
829
830
831/* Stream write function for internal units. This is not actually used
832 at the moment, as all internal IO is formatted and the formatted IO
833 routines use mem_alloc_w_at. */
834
835static int
836mem_write (unix_stream * s, const void * buf, size_t * nbytes)
837{
838 void *p;
839 int tmp;
840
841 errno = 0;
842
843 tmp = *nbytes;
844 p = mem_alloc_w_at (s, &tmp, -1);
845 if (p)
846 {
847 *nbytes = tmp;
848 memcpy (p, buf, *nbytes);
849 return 0;
850 }
851 else
852 {
853 *nbytes = 0;
854 return errno;
855 }
856}
857
858
4ee9c684 859static int
b093181d 860mem_seek (unix_stream * s, gfc_offset offset)
4ee9c684 861{
4ee9c684 862 if (offset > s->file_length)
863 {
864 errno = ESPIPE;
865 return FAILURE;
866 }
867
868 s->logical_offset = offset;
869 return SUCCESS;
870}
871
872
873static int
a0007dfa 874mem_truncate (unix_stream * s __attribute__ ((unused)))
4ee9c684 875{
4ee9c684 876 return SUCCESS;
877}
878
879
880static try
881mem_close (unix_stream * s)
882{
15fee5a5 883 free_mem (s);
4ee9c684 884
885 return SUCCESS;
886}
887
888
889static try
a0007dfa 890mem_sfree (unix_stream * s __attribute__ ((unused)))
4ee9c684 891{
4ee9c684 892 return SUCCESS;
893}
894
895
896
897/*********************************************************************
898 Public functions -- A reimplementation of this module needs to
899 define functional equivalents of the following.
900*********************************************************************/
901
902/* empty_internal_buffer()-- Zero the buffer of Internal file */
903
904void
905empty_internal_buffer(stream *strm)
906{
7145fd06 907 unix_stream * s = (unix_stream *) strm;
908 memset(s->buffer, ' ', s->file_length);
4ee9c684 909}
910
911/* open_internal()-- Returns a stream structure from an internal file */
912
913stream *
914open_internal (char *base, int length)
915{
916 unix_stream *s;
917
918 s = get_mem (sizeof (unix_stream));
8f8ad899 919 memset (s, '\0', sizeof (unix_stream));
4ee9c684 920
921 s->buffer = base;
922 s->buffer_offset = 0;
923
924 s->logical_offset = 0;
925 s->active = s->file_length = length;
926
927 s->st.alloc_r_at = (void *) mem_alloc_r_at;
928 s->st.alloc_w_at = (void *) mem_alloc_w_at;
929 s->st.sfree = (void *) mem_sfree;
930 s->st.close = (void *) mem_close;
931 s->st.seek = (void *) mem_seek;
932 s->st.truncate = (void *) mem_truncate;
b2a112ca 933 s->st.read = (void *) mem_read;
934 s->st.write = (void *) mem_write;
4ee9c684 935
936 return (stream *) s;
937}
938
939
940/* fd_to_stream()-- Given an open file descriptor, build a stream
941 * around it. */
942
943static stream *
f0b5d33f 944fd_to_stream (int fd, int prot)
4ee9c684 945{
946 struct stat statbuf;
947 unix_stream *s;
948
949 s = get_mem (sizeof (unix_stream));
8f8ad899 950 memset (s, '\0', sizeof (unix_stream));
4ee9c684 951
952 s->fd = fd;
953 s->buffer_offset = 0;
954 s->physical_offset = 0;
955 s->logical_offset = 0;
956 s->prot = prot;
957
958 /* Get the current length of the file. */
959
960 fstat (fd, &statbuf);
961 s->file_length = S_ISREG (statbuf.st_mode) ? statbuf.st_size : -1;
227e9423 962 s->special_file = !S_ISREG (statbuf.st_mode);
4ee9c684 963
4ee9c684 964 fd_open (s);
4ee9c684 965
966 return (stream *) s;
967}
968
969
771c1b50 970/* Given the Fortran unit number, convert it to a C file descriptor. */
971
972int
60c514ba 973unit_to_fd (int unit)
771c1b50 974{
771c1b50 975 gfc_unit *us;
60c514ba 976 int fd;
771c1b50 977
60c514ba 978 us = find_unit (unit);
771c1b50 979 if (us == NULL)
980 return -1;
981
60c514ba 982 fd = ((unix_stream *) us->s)->fd;
983 unlock_unit (us);
984 return fd;
771c1b50 985}
986
987
4ee9c684 988/* unpack_filename()-- Given a fortran string and a pointer to a
989 * buffer that is PATH_MAX characters, convert the fortran string to a
990 * C string in the buffer. Returns nonzero if this is not possible. */
991
1dc95e51 992int
4ee9c684 993unpack_filename (char *cstring, const char *fstring, int len)
994{
4ee9c684 995 len = fstrlen (fstring, len);
996 if (len >= PATH_MAX)
997 return 1;
998
999 memmove (cstring, fstring, len);
1000 cstring[len] = '\0';
1001
1002 return 0;
1003}
1004
1005
1006/* tempfile()-- Generate a temporary filename for a scratch file and
1007 * open it. mkstemp() opens the file for reading and writing, but the
1008 * library mode prevents anything that is not allowed. The descriptor
7dfba97b 1009 * is returned, which is -1 on error. The template is pointed to by
60c514ba 1010 * opp->file, which is copied into the unit structure
4ee9c684 1011 * and freed later. */
1012
1013static int
60c514ba 1014tempfile (st_parameter_open *opp)
4ee9c684 1015{
1016 const char *tempdir;
1017 char *template;
1018 int fd;
1019
1020 tempdir = getenv ("GFORTRAN_TMPDIR");
1021 if (tempdir == NULL)
1022 tempdir = getenv ("TMP");
ac09d5cc 1023 if (tempdir == NULL)
1024 tempdir = getenv ("TEMP");
4ee9c684 1025 if (tempdir == NULL)
1026 tempdir = DEFAULT_TEMPDIR;
1027
1028 template = get_mem (strlen (tempdir) + 20);
1029
7dfba97b 1030 st_sprintf (template, "%s/gfortrantmpXXXXXX", tempdir);
1031
1032#ifdef HAVE_MKSTEMP
4ee9c684 1033
1034 fd = mkstemp (template);
1035
7dfba97b 1036#else /* HAVE_MKSTEMP */
1037
1038 if (mktemp (template))
1039 do
cf55c3cf 1040#ifdef HAVE_CRLF
1041 fd = open (template, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
1042 S_IREAD | S_IWRITE);
1043#else
ac09d5cc 1044 fd = open (template, O_RDWR | O_CREAT | O_EXCL, S_IREAD | S_IWRITE);
cf55c3cf 1045#endif
7dfba97b 1046 while (!(fd == -1 && errno == EEXIST) && mktemp (template));
1047 else
1048 fd = -1;
1049
1050#endif /* HAVE_MKSTEMP */
1051
4ee9c684 1052 if (fd < 0)
1053 free_mem (template);
1054 else
1055 {
60c514ba 1056 opp->file = template;
1057 opp->file_len = strlen (template); /* Don't include trailing nul */
4ee9c684 1058 }
1059
1060 return fd;
1061}
1062
1063
6d12c489 1064/* regular_file()-- Open a regular file.
2d6ba0f9 1065 * Change flags->action if it is ACTION_UNSPECIFIED on entry,
1066 * unless an error occurs.
6d12c489 1067 * Returns the descriptor, which is less than zero on error. */
4ee9c684 1068
1069static int
60c514ba 1070regular_file (st_parameter_open *opp, unit_flags *flags)
4ee9c684 1071{
1072 char path[PATH_MAX + 1];
4ee9c684 1073 int mode;
6d12c489 1074 int rwflag;
2d6ba0f9 1075 int crflag;
6d12c489 1076 int fd;
4ee9c684 1077
60c514ba 1078 if (unpack_filename (path, opp->file, opp->file_len))
4ee9c684 1079 {
1080 errno = ENOENT; /* Fake an OS error */
1081 return -1;
1082 }
1083
6d12c489 1084 rwflag = 0;
4ee9c684 1085
6d12c489 1086 switch (flags->action)
4ee9c684 1087 {
1088 case ACTION_READ:
6d12c489 1089 rwflag = O_RDONLY;
4ee9c684 1090 break;
1091
1092 case ACTION_WRITE:
6d12c489 1093 rwflag = O_WRONLY;
4ee9c684 1094 break;
1095
1096 case ACTION_READWRITE:
6d12c489 1097 case ACTION_UNSPECIFIED:
1098 rwflag = O_RDWR;
4ee9c684 1099 break;
1100
1101 default:
60c514ba 1102 internal_error (&opp->common, "regular_file(): Bad action");
4ee9c684 1103 }
1104
6d12c489 1105 switch (flags->status)
4ee9c684 1106 {
1107 case STATUS_NEW:
2d6ba0f9 1108 crflag = O_CREAT | O_EXCL;
4ee9c684 1109 break;
1110
2d6ba0f9 1111 case STATUS_OLD: /* open will fail if the file does not exist*/
1112 crflag = 0;
4ee9c684 1113 break;
1114
1115 case STATUS_UNKNOWN:
1116 case STATUS_SCRATCH:
2d6ba0f9 1117 crflag = O_CREAT;
4ee9c684 1118 break;
1119
1120 case STATUS_REPLACE:
2d6ba0f9 1121 crflag = O_CREAT | O_TRUNC;
4ee9c684 1122 break;
1123
1124 default:
60c514ba 1125 internal_error (&opp->common, "regular_file(): Bad status");
4ee9c684 1126 }
1127
6d12c489 1128 /* rwflag |= O_LARGEFILE; */
4ee9c684 1129
cf55c3cf 1130#ifdef HAVE_CRLF
1131 crflag |= O_BINARY;
1132#endif
1133
6d12c489 1134 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2d6ba0f9 1135 fd = open (path, rwflag | crflag, mode);
1136 if (flags->action != ACTION_UNSPECIFIED)
1137 return fd;
1138
1139 if (fd >= 0)
6d12c489 1140 {
2d6ba0f9 1141 flags->action = ACTION_READWRITE;
1142 return fd;
6d12c489 1143 }
2d6ba0f9 1144 if (errno != EACCES)
1145 return fd;
1146
1147 /* retry for read-only access */
1148 rwflag = O_RDONLY;
1149 fd = open (path, rwflag | crflag, mode);
1150 if (fd >=0)
1151 {
1152 flags->action = ACTION_READ;
1153 return fd; /* success */
1154 }
1155
1156 if (errno != EACCES)
1157 return fd; /* failure */
1158
1159 /* retry for write-only access */
1160 rwflag = O_WRONLY;
1161 fd = open (path, rwflag | crflag, mode);
1162 if (fd >=0)
1163 {
1164 flags->action = ACTION_WRITE;
1165 return fd; /* success */
1166 }
1167 return fd; /* failure */
4ee9c684 1168}
1169
1170
1171/* open_external()-- Open an external file, unix specific version.
6d12c489 1172 * Change flags->action if it is ACTION_UNSPECIFIED on entry.
4ee9c684 1173 * Returns NULL on operating system error. */
1174
1175stream *
60c514ba 1176open_external (st_parameter_open *opp, unit_flags *flags)
4ee9c684 1177{
1178 int fd, prot;
1179
6d12c489 1180 if (flags->status == STATUS_SCRATCH)
1181 {
60c514ba 1182 fd = tempfile (opp);
6d12c489 1183 if (flags->action == ACTION_UNSPECIFIED)
1184 flags->action = ACTION_READWRITE;
1dc95e51 1185
1186#if HAVE_UNLINK_OPEN_FILE
6d12c489 1187 /* We can unlink scratch files now and it will go away when closed. */
60c514ba 1188 if (fd >= 0)
1189 unlink (opp->file);
1dc95e51 1190#endif
6d12c489 1191 }
1192 else
1193 {
2d6ba0f9 1194 /* regular_file resets flags->action if it is ACTION_UNSPECIFIED and
1195 * if it succeeds */
60c514ba 1196 fd = regular_file (opp, flags);
6d12c489 1197 }
4ee9c684 1198
1199 if (fd < 0)
1200 return NULL;
1201 fd = fix_fd (fd);
1202
6d12c489 1203 switch (flags->action)
4ee9c684 1204 {
1205 case ACTION_READ:
1206 prot = PROT_READ;
1207 break;
1208
1209 case ACTION_WRITE:
1210 prot = PROT_WRITE;
1211 break;
1212
1213 case ACTION_READWRITE:
1214 prot = PROT_READ | PROT_WRITE;
1215 break;
1216
1217 default:
60c514ba 1218 internal_error (&opp->common, "open_external(): Bad action");
4ee9c684 1219 }
1220
f0b5d33f 1221 return fd_to_stream (fd, prot);
4ee9c684 1222}
1223
1224
1225/* input_stream()-- Return a stream pointer to the default input stream.
1226 * Called on initialization. */
1227
1228stream *
1229input_stream (void)
1230{
f0b5d33f 1231 return fd_to_stream (STDIN_FILENO, PROT_READ);
4ee9c684 1232}
1233
1234
ff81ee3b 1235/* output_stream()-- Return a stream pointer to the default output stream.
4ee9c684 1236 * Called on initialization. */
1237
1238stream *
1239output_stream (void)
1240{
f0b5d33f 1241 return fd_to_stream (STDOUT_FILENO, PROT_WRITE);
4ee9c684 1242}
1243
1244
ff81ee3b 1245/* error_stream()-- Return a stream pointer to the default error stream.
1246 * Called on initialization. */
1247
1248stream *
1249error_stream (void)
1250{
f0b5d33f 1251 return fd_to_stream (STDERR_FILENO, PROT_WRITE);
ff81ee3b 1252}
1253
4ee9c684 1254/* init_error_stream()-- Return a pointer to the error stream. This
1255 * subroutine is called when the stream is needed, rather than at
1256 * initialization. We want to work even if memory has been seriously
1257 * corrupted. */
1258
1259stream *
60c514ba 1260init_error_stream (unix_stream *error)
4ee9c684 1261{
60c514ba 1262 memset (error, '\0', sizeof (*error));
4ee9c684 1263
60c514ba 1264 error->fd = options.use_stderr ? STDERR_FILENO : STDOUT_FILENO;
4ee9c684 1265
60c514ba 1266 error->st.alloc_w_at = (void *) fd_alloc_w_at;
1267 error->st.sfree = (void *) fd_sfree;
4ee9c684 1268
60c514ba 1269 error->unbuffered = 1;
1270 error->buffer = error->small_buffer;
4ee9c684 1271
60c514ba 1272 return (stream *) error;
4ee9c684 1273}
1274
1275
1276/* compare_file_filename()-- Given an open stream and a fortran string
1277 * that is a filename, figure out if the file is the same as the
1278 * filename. */
1279
1280int
daad4fd5 1281compare_file_filename (gfc_unit *u, const char *name, int len)
4ee9c684 1282{
1283 char path[PATH_MAX + 1];
daad4fd5 1284 struct stat st1;
1285#ifdef HAVE_WORKING_STAT
1286 struct stat st2;
1287#endif
4ee9c684 1288
1289 if (unpack_filename (path, name, len))
1290 return 0; /* Can't be the same */
1291
1292 /* If the filename doesn't exist, then there is no match with the
1293 * existing file. */
1294
1295 if (stat (path, &st1) < 0)
1296 return 0;
1297
daad4fd5 1298#ifdef HAVE_WORKING_STAT
1299 fstat (((unix_stream *) (u->s))->fd, &st2);
4ee9c684 1300 return (st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino);
daad4fd5 1301#else
1302 if (len != u->file_len)
1303 return 0;
1304 return (memcmp(path, u->file, len) == 0);
1305#endif
4ee9c684 1306}
1307
1308
60c514ba 1309#ifdef HAVE_WORKING_STAT
1310# define FIND_FILE0_DECL struct stat *st
1311# define FIND_FILE0_ARGS st
1312#else
1313# define FIND_FILE0_DECL const char *file, gfc_charlen_type file_len
1314# define FIND_FILE0_ARGS file, file_len
1315#endif
1316
4ee9c684 1317/* find_file0()-- Recursive work function for find_file() */
1318
f02dd226 1319static gfc_unit *
60c514ba 1320find_file0 (gfc_unit *u, FIND_FILE0_DECL)
4ee9c684 1321{
f02dd226 1322 gfc_unit *v;
4ee9c684 1323
1324 if (u == NULL)
1325 return NULL;
1326
daad4fd5 1327#ifdef HAVE_WORKING_STAT
60c514ba 1328 if (u->s != NULL
1329 && fstat (((unix_stream *) u->s)->fd, &st[1]) >= 0 &&
1330 st[0].st_dev == st[1].st_dev && st[0].st_ino == st[1].st_ino)
4ee9c684 1331 return u;
daad4fd5 1332#else
60c514ba 1333 if (compare_string (u->file_len, u->file, file_len, file) == 0)
daad4fd5 1334 return u;
1335#endif
4ee9c684 1336
60c514ba 1337 v = find_file0 (u->left, FIND_FILE0_ARGS);
4ee9c684 1338 if (v != NULL)
1339 return v;
1340
60c514ba 1341 v = find_file0 (u->right, FIND_FILE0_ARGS);
4ee9c684 1342 if (v != NULL)
1343 return v;
1344
1345 return NULL;
1346}
1347
1348
1349/* find_file()-- Take the current filename and see if there is a unit
1350 * that has the file already open. Returns a pointer to the unit if so. */
1351
f02dd226 1352gfc_unit *
60c514ba 1353find_file (const char *file, gfc_charlen_type file_len)
4ee9c684 1354{
1355 char path[PATH_MAX + 1];
60c514ba 1356 struct stat st[2];
1357 gfc_unit *u;
4ee9c684 1358
60c514ba 1359 if (unpack_filename (path, file, file_len))
4ee9c684 1360 return NULL;
1361
60c514ba 1362 if (stat (path, &st[0]) < 0)
4ee9c684 1363 return NULL;
1364
60c514ba 1365 __gthread_mutex_lock (&unit_lock);
1366retry:
1367 u = find_file0 (unit_root, FIND_FILE0_ARGS);
1368 if (u != NULL)
1369 {
1370 /* Fast path. */
1371 if (! __gthread_mutex_trylock (&u->lock))
1372 {
1373 /* assert (u->closed == 0); */
1374 __gthread_mutex_unlock (&unit_lock);
1375 return u;
1376 }
1377
1378 inc_waiting_locked (u);
1379 }
1380 __gthread_mutex_unlock (&unit_lock);
1381 if (u != NULL)
1382 {
1383 __gthread_mutex_lock (&u->lock);
1384 if (u->closed)
1385 {
1386 __gthread_mutex_lock (&unit_lock);
1387 __gthread_mutex_unlock (&u->lock);
1388 if (predec_waiting_locked (u) == 0)
1389 free_mem (u);
1390 goto retry;
1391 }
1392
1393 dec_waiting_unlocked (u);
1394 }
1395 return u;
1396}
1397
1398static gfc_unit *
1399flush_all_units_1 (gfc_unit *u, int min_unit)
1400{
1401 while (u != NULL)
1402 {
1403 if (u->unit_number > min_unit)
1404 {
1405 gfc_unit *r = flush_all_units_1 (u->left, min_unit);
1406 if (r != NULL)
1407 return r;
1408 }
1409 if (u->unit_number >= min_unit)
1410 {
1411 if (__gthread_mutex_trylock (&u->lock))
1412 return u;
1413 if (u->s)
1414 flush (u->s);
1415 __gthread_mutex_unlock (&u->lock);
1416 }
1417 u = u->right;
1418 }
1419 return NULL;
1420}
1421
1422void
1423flush_all_units (void)
1424{
1425 gfc_unit *u;
1426 int min_unit = 0;
1427
1428 __gthread_mutex_lock (&unit_lock);
1429 do
1430 {
1431 u = flush_all_units_1 (unit_root, min_unit);
1432 if (u != NULL)
1433 inc_waiting_locked (u);
1434 __gthread_mutex_unlock (&unit_lock);
1435 if (u == NULL)
1436 return;
1437
1438 __gthread_mutex_lock (&u->lock);
1439
1440 min_unit = u->unit_number + 1;
1441
1442 if (u->closed == 0)
1443 {
1444 flush (u->s);
1445 __gthread_mutex_lock (&unit_lock);
1446 __gthread_mutex_unlock (&u->lock);
1447 (void) predec_waiting_locked (u);
1448 }
1449 else
1450 {
1451 __gthread_mutex_lock (&unit_lock);
1452 __gthread_mutex_unlock (&u->lock);
1453 if (predec_waiting_locked (u) == 0)
1454 free_mem (u);
1455 }
1456 }
1457 while (1);
4ee9c684 1458}
1459
1460
1461/* stream_at_bof()-- Returns nonzero if the stream is at the beginning
1462 * of the file. */
1463
1464int
1465stream_at_bof (stream * s)
1466{
1467 unix_stream *us;
1468
5df4b62f 1469 if (!is_seekable (s))
1470 return 0;
4ee9c684 1471
5df4b62f 1472 us = (unix_stream *) s;
4ee9c684 1473
1474 return us->logical_offset == 0;
1475}
1476
1477
1478/* stream_at_eof()-- Returns nonzero if the stream is at the beginning
1479 * of the file. */
1480
1481int
1482stream_at_eof (stream * s)
1483{
1484 unix_stream *us;
1485
5df4b62f 1486 if (!is_seekable (s))
1487 return 0;
4ee9c684 1488
5df4b62f 1489 us = (unix_stream *) s;
4ee9c684 1490
1491 return us->logical_offset == us->dirty_offset;
1492}
1493
1494
1495/* delete_file()-- Given a unit structure, delete the file associated
1496 * with the unit. Returns nonzero if something went wrong. */
1497
1498int
f02dd226 1499delete_file (gfc_unit * u)
4ee9c684 1500{
1501 char path[PATH_MAX + 1];
1502
1503 if (unpack_filename (path, u->file, u->file_len))
1504 { /* Shouldn't be possible */
1505 errno = ENOENT;
1506 return 1;
1507 }
1508
1509 return unlink (path);
1510}
1511
1512
1513/* file_exists()-- Returns nonzero if the current filename exists on
1514 * the system */
1515
1516int
60c514ba 1517file_exists (const char *file, gfc_charlen_type file_len)
4ee9c684 1518{
1519 char path[PATH_MAX + 1];
1520 struct stat statbuf;
1521
60c514ba 1522 if (unpack_filename (path, file, file_len))
4ee9c684 1523 return 0;
1524
1525 if (stat (path, &statbuf) < 0)
1526 return 0;
1527
1528 return 1;
1529}
1530
1531
1532
fb35179a 1533static const char yes[] = "YES", no[] = "NO", unknown[] = "UNKNOWN";
4ee9c684 1534
1535/* inquire_sequential()-- Given a fortran string, determine if the
1536 * file is suitable for sequential access. Returns a C-style
1537 * string. */
1538
1539const char *
1540inquire_sequential (const char *string, int len)
1541{
1542 char path[PATH_MAX + 1];
1543 struct stat statbuf;
1544
1545 if (string == NULL ||
1546 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1547 return unknown;
1548
1549 if (S_ISREG (statbuf.st_mode) ||
1550 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1551 return yes;
1552
1553 if (S_ISDIR (statbuf.st_mode) || S_ISBLK (statbuf.st_mode))
1554 return no;
1555
1556 return unknown;
1557}
1558
1559
1560/* inquire_direct()-- Given a fortran string, determine if the file is
1561 * suitable for direct access. Returns a C-style string. */
1562
1563const char *
1564inquire_direct (const char *string, int len)
1565{
1566 char path[PATH_MAX + 1];
1567 struct stat statbuf;
1568
1569 if (string == NULL ||
1570 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1571 return unknown;
1572
1573 if (S_ISREG (statbuf.st_mode) || S_ISBLK (statbuf.st_mode))
1574 return yes;
1575
1576 if (S_ISDIR (statbuf.st_mode) ||
1577 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1578 return no;
1579
1580 return unknown;
1581}
1582
1583
1584/* inquire_formatted()-- Given a fortran string, determine if the file
1585 * is suitable for formatted form. Returns a C-style string. */
1586
1587const char *
1588inquire_formatted (const char *string, int len)
1589{
1590 char path[PATH_MAX + 1];
1591 struct stat statbuf;
1592
1593 if (string == NULL ||
1594 unpack_filename (path, string, len) || stat (path, &statbuf) < 0)
1595 return unknown;
1596
1597 if (S_ISREG (statbuf.st_mode) ||
1598 S_ISBLK (statbuf.st_mode) ||
1599 S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))
1600 return yes;
1601
1602 if (S_ISDIR (statbuf.st_mode))
1603 return no;
1604
1605 return unknown;
1606}
1607
1608
1609/* inquire_unformatted()-- Given a fortran string, determine if the file
1610 * is suitable for unformatted form. Returns a C-style string. */
1611
1612const char *
1613inquire_unformatted (const char *string, int len)
1614{
4ee9c684 1615 return inquire_formatted (string, len);
1616}
1617
1618
1619/* inquire_access()-- Given a fortran string, determine if the file is
1620 * suitable for access. */
1621
1622static const char *
1623inquire_access (const char *string, int len, int mode)
1624{
1625 char path[PATH_MAX + 1];
1626
1627 if (string == NULL || unpack_filename (path, string, len) ||
1628 access (path, mode) < 0)
1629 return no;
1630
1631 return yes;
1632}
1633
1634
1635/* inquire_read()-- Given a fortran string, determine if the file is
1636 * suitable for READ access. */
1637
1638const char *
1639inquire_read (const char *string, int len)
1640{
4ee9c684 1641 return inquire_access (string, len, R_OK);
1642}
1643
1644
1645/* inquire_write()-- Given a fortran string, determine if the file is
1646 * suitable for READ access. */
1647
1648const char *
1649inquire_write (const char *string, int len)
1650{
4ee9c684 1651 return inquire_access (string, len, W_OK);
1652}
1653
1654
1655/* inquire_readwrite()-- Given a fortran string, determine if the file is
1656 * suitable for read and write access. */
1657
1658const char *
1659inquire_readwrite (const char *string, int len)
1660{
4ee9c684 1661 return inquire_access (string, len, R_OK | W_OK);
1662}
1663
1664
1665/* file_length()-- Return the file length in bytes, -1 if unknown */
1666
b093181d 1667gfc_offset
4ee9c684 1668file_length (stream * s)
1669{
4ee9c684 1670 return ((unix_stream *) s)->file_length;
1671}
1672
1673
1674/* file_position()-- Return the current position of the file */
1675
b093181d 1676gfc_offset
4ee9c684 1677file_position (stream * s)
1678{
4ee9c684 1679 return ((unix_stream *) s)->logical_offset;
1680}
1681
1682
1683/* is_seekable()-- Return nonzero if the stream is seekable, zero if
1684 * it is not */
1685
1686int
1687is_seekable (stream * s)
1688{
b2a112ca 1689 /* By convention, if file_length == -1, the file is not
1690 seekable. */
5a78b88f 1691 return ((unix_stream *) s)->file_length!=-1;
4ee9c684 1692}
1693
b0342e98 1694try
1695flush (stream *s)
1696{
1697 return fd_flush( (unix_stream *) s);
1698}
1699
60d77e0d 1700int
1701stream_isatty (stream *s)
1702{
1703 return isatty (((unix_stream *) s)->fd);
1704}
1705
1706char *
1707stream_ttyname (stream *s)
1708{
f2c0a16d 1709#ifdef HAVE_TTYNAME
60d77e0d 1710 return ttyname (((unix_stream *) s)->fd);
f2c0a16d 1711#else
1712 return NULL;
1713#endif
60d77e0d 1714}
1715
16de8065 1716gfc_offset
1717stream_offset (stream *s)
1718{
1719 return (((unix_stream *) s)->logical_offset);
1720}
1721
4ee9c684 1722
1723/* How files are stored: This is an operating-system specific issue,
1724 and therefore belongs here. There are three cases to consider.
1725
1726 Direct Access:
1727 Records are written as block of bytes corresponding to the record
1728 length of the file. This goes for both formatted and unformatted
1729 records. Positioning is done explicitly for each data transfer,
1730 so positioning is not much of an issue.
1731
1732 Sequential Formatted:
1733 Records are separated by newline characters. The newline character
1734 is prohibited from appearing in a string. If it does, this will be
1735 messed up on the next read. End of file is also the end of a record.
1736
1737 Sequential Unformatted:
1738 In this case, we are merely copying bytes to and from main storage,
1739 yet we need to keep track of varying record lengths. We adopt
1740 the solution used by f2c. Each record contains a pair of length
1741 markers:
1742
1743 Length of record n in bytes
1744 Data of record n
1745 Length of record n in bytes
1746
1747 Length of record n+1 in bytes
1748 Data of record n+1
1749 Length of record n+1 in bytes
1750
1751 The length is stored at the end of a record to allow backspacing to the
1752 previous record. Between data transfer statements, the file pointer
1753 is left pointing to the first length of the current record.
1754
1755 ENDFILE records are never explicitly stored.
1756
1757*/