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