it is always safe to truncate the file on the first write */
if (dtp->u.p.mode == WRITING
&& dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
- && dtp->u.p.current_unit->last_record == 0 && !is_preconnected(dtp->u.p.current_unit->s))
+ && dtp->u.p.current_unit->last_record == 0
+ && !is_preconnected(dtp->u.p.current_unit->s))
struncate(dtp->u.p.current_unit->s);
/* Bugware for badly written mixed C-Fortran I/O. */
free_mem (dtp->u.p.scratch);
if (dtp->u.p.current_unit != NULL)
unlock_unit (dtp->u.p.current_unit);
+ if (is_internal_unit (dtp) && dtp->u.p.current_unit != NULL)
+ free_mem (dtp->u.p.current_unit);
library_end ();
}
case NO_ENDFILE:
/* Get rid of whatever is after this record. */
- flush (dtp->u.p.current_unit->s);
- if (struncate (dtp->u.p.current_unit->s) == FAILURE)
- generate_error (&dtp->common, ERROR_OS, NULL);
-
+ if (!is_internal_unit (dtp))
+ {
+ flush (dtp->u.p.current_unit->s);
+ if (struncate (dtp->u.p.current_unit->s) == FAILURE)
+ generate_error (&dtp->common, ERROR_OS, NULL);
+ }
dtp->u.p.current_unit->endfile = AT_ENDFILE;
break;
}
free_mem (dtp->u.p.scratch);
if (dtp->u.p.current_unit != NULL)
unlock_unit (dtp->u.p.current_unit);
+ if (is_internal_unit (dtp) && dtp->u.p.current_unit != NULL)
+ free_mem (dtp->u.p.current_unit);
library_end ();
}
#define CACHE_SIZE 3
-static gfc_unit internal_unit, *unit_cache[CACHE_SIZE];
+static gfc_unit *unit_cache[CACHE_SIZE];
gfc_offset max_offset;
gfc_unit *unit_root;
#ifdef __GTHREAD_MUTEX_INIT
}
-/* find_unit()-- Given an integer, return a pointer to the unit
+/* get_external_unit()-- Given an integer, return a pointer to the unit
* structure. Returns NULL if the unit does not exist,
* otherwise returns a locked unit. */
static gfc_unit *
-find_unit_1 (int n, int do_create)
+get_external_unit (int n, int do_create)
{
gfc_unit *p;
int c, created = 0;
return p;
}
+
gfc_unit *
find_unit (int n)
{
- return find_unit_1 (n, 0);
+ return get_external_unit (n, 0);
}
+
gfc_unit *
find_or_create_unit (int n)
{
- return find_unit_1 (n, 1);
+ return get_external_unit (n, 1);
}
-/* get_unit()-- Returns the unit structure associated with the integer
- * unit or the internal file. */
gfc_unit *
-get_unit (st_parameter_dt *dtp, int do_create)
+get_internal_unit (st_parameter_dt *dtp)
{
- if ((dtp->common.flags & IOPARM_DT_HAS_INTERNAL_UNIT) != 0)
+ gfc_unit * iunit;
+
+ /* Allocate memory for a unit structure. */
+
+ iunit = get_mem (sizeof (gfc_unit));
+ if (iunit == NULL)
{
- __gthread_mutex_lock (&internal_unit.lock);
- internal_unit.recl = dtp->internal_unit_len;
- if (is_array_io (dtp))
- {
- internal_unit.rank = GFC_DESCRIPTOR_RANK (dtp->internal_unit_desc);
- internal_unit.ls = (array_loop_spec *)
- get_mem (internal_unit.rank * sizeof (array_loop_spec));
- dtp->internal_unit_len *=
- init_loop_spec (dtp->internal_unit_desc, internal_unit.ls);
- }
+ generate_error (&dtp->common, ERROR_INTERNAL_UNIT, NULL);
+ return NULL;
+ }
- internal_unit.s =
- open_internal (dtp->internal_unit, dtp->internal_unit_len);
- internal_unit.bytes_left = internal_unit.recl;
- internal_unit.last_record=0;
- internal_unit.maxrec=0;
- internal_unit.current_record=0;
+ memset (iunit, '\0', sizeof (gfc_unit));
- /* Set flags for the internal unit */
+ iunit->recl = dtp->internal_unit_len;
- internal_unit.flags.access = ACCESS_SEQUENTIAL;
- internal_unit.flags.action = ACTION_READWRITE;
- internal_unit.flags.form = FORM_FORMATTED;
- internal_unit.flags.delim = DELIM_NONE;
- internal_unit.flags.pad = PAD_YES;
+ /* Set up the looping specification from the array descriptor, if any. */
- return &internal_unit;
+ if (is_array_io (dtp))
+ {
+ iunit->rank = GFC_DESCRIPTOR_RANK (dtp->internal_unit_desc);
+ iunit->ls = (array_loop_spec *)
+ get_mem (iunit->rank * sizeof (array_loop_spec));
+ dtp->internal_unit_len *=
+ init_loop_spec (dtp->internal_unit_desc, iunit->ls);
}
+ /* Set initial values for unit parameters. */
+
+ iunit->s = open_internal (dtp->internal_unit, dtp->internal_unit_len);
+ iunit->bytes_left = iunit->recl;
+ iunit->last_record=0;
+ iunit->maxrec=0;
+ iunit->current_record=0;
+ iunit->read_bad = 0;
+
+ /* Set flags for the internal unit. */
+
+ iunit->flags.access = ACCESS_SEQUENTIAL;
+ iunit->flags.action = ACTION_READWRITE;
+ iunit->flags.form = FORM_FORMATTED;
+ iunit->flags.pad = PAD_YES;
+ iunit->flags.status = STATUS_UNSPECIFIED;
+
+ /* Initialize the data transfer parameters. */
+
+ dtp->u.p.advance_status = ADVANCE_YES;
+ dtp->u.p.blank_status = BLANK_UNSPECIFIED;
+ dtp->u.p.seen_dollar = 0;
+ dtp->u.p.skips = 0;
+ dtp->u.p.pending_spaces = 0;
+ dtp->u.p.max_pos = 0;
+
+ /* This flag tells us the unit is assigned to internal I/O. */
+
+ dtp->u.p.unit_is_internal = 1;
+
+ return iunit;
+}
+
+
+/* get_unit()-- Returns the unit structure associated with the integer
+ * unit or the internal file. */
+
+gfc_unit *
+get_unit (st_parameter_dt *dtp, int do_create)
+{
+
+ if ((dtp->common.flags & IOPARM_DT_HAS_INTERNAL_UNIT) != 0)
+ return get_internal_unit(dtp);
+
/* Has to be an external unit */
- return find_unit_1 (dtp->common.unit, do_create);
+ dtp->u.p.unit_is_internal = 0;
+
+ return get_external_unit (dtp->common.unit, do_create);
}
int
is_internal_unit (st_parameter_dt *dtp)
{
- return dtp->u.p.current_unit == &internal_unit;
+ return dtp->u.p.unit_is_internal;
}
__GTHREAD_MUTEX_INIT_FUNCTION (&unit_lock);
#endif
-#ifdef __GTHREAD_MUTEX_INIT
- {
- __gthread_mutex_t tmp = __GTHREAD_MUTEX_INIT;
- internal_unit.lock = tmp;
- }
-#else
- __GTHREAD_MUTEX_INIT_FUNCTION (&internal_unit.lock);
-#endif
-
if (options.stdin_unit >= 0)
{ /* STDIN */
u = insert_unit (options.stdin_unit);