1 //===-- runtime/unit.cpp --------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "unit.h"
10 #include "io-error.h"
11 #include "lock.h"
12 #include "unit-map.h"
13 #include <cstdio>
14 #include <limits>
15 #include <utility>
16 
17 namespace Fortran::runtime::io {
18 
19 // The per-unit data structures are created on demand so that Fortran I/O
20 // should work without a Fortran main program.
21 static Lock unitMapLock;
22 static UnitMap *unitMap{nullptr};
23 static ExternalFileUnit *defaultInput{nullptr}; // unit 5
24 static ExternalFileUnit *defaultOutput{nullptr}; // unit 6
25 static ExternalFileUnit *errorOutput{nullptr}; // unit 0 extension
26 
27 void FlushOutputOnCrash(const Terminator &terminator) {
28   if (!defaultOutput && !errorOutput) {
29     return;
30   }
31   IoErrorHandler handler{terminator};
32   handler.HasIoStat(); // prevent nested crash if flush has error
33   CriticalSection critical{unitMapLock};
34   if (defaultOutput) {
35     defaultOutput->FlushOutput(handler);
36   }
37   if (errorOutput) {
38     errorOutput->FlushOutput(handler);
39   }
40 }
41 
42 ExternalFileUnit *ExternalFileUnit::LookUp(int unit) {
43   return GetUnitMap().LookUp(unit);
44 }
45 
46 ExternalFileUnit &ExternalFileUnit::LookUpOrCrash(
47     int unit, const Terminator &terminator) {
48   ExternalFileUnit *file{LookUp(unit)};
49   if (!file) {
50     terminator.Crash("%d is not an open I/O unit number", unit);
51   }
52   return *file;
53 }
54 
55 ExternalFileUnit &ExternalFileUnit::LookUpOrCreate(
56     int unit, const Terminator &terminator, bool &wasExtant) {
57   return GetUnitMap().LookUpOrCreate(unit, terminator, wasExtant);
58 }
59 
60 ExternalFileUnit &ExternalFileUnit::LookUpOrCreateAnonymous(int unit,
61     Direction dir, std::optional<bool> isUnformatted,
62     const Terminator &terminator) {
63   bool exists{false};
64   ExternalFileUnit &result{
65       GetUnitMap().LookUpOrCreate(unit, terminator, exists)};
66   if (!exists) {
67     IoErrorHandler handler{terminator};
68     result.OpenAnonymousUnit(
69         dir == Direction::Input ? OpenStatus::Unknown : OpenStatus::Replace,
70         Action::ReadWrite, Position::Rewind, Convert::Native, handler);
71     result.isUnformatted = isUnformatted;
72   }
73   return result;
74 }
75 
76 ExternalFileUnit *ExternalFileUnit::LookUp(const char *path) {
77   return GetUnitMap().LookUp(path);
78 }
79 
80 ExternalFileUnit &ExternalFileUnit::CreateNew(
81     int unit, const Terminator &terminator) {
82   bool wasExtant{false};
83   ExternalFileUnit &result{
84       GetUnitMap().LookUpOrCreate(unit, terminator, wasExtant)};
85   RUNTIME_CHECK(terminator, !wasExtant);
86   return result;
87 }
88 
89 ExternalFileUnit *ExternalFileUnit::LookUpForClose(int unit) {
90   return GetUnitMap().LookUpForClose(unit);
91 }
92 
93 ExternalFileUnit &ExternalFileUnit::NewUnit(
94     const Terminator &terminator, bool forChildIo) {
95   ExternalFileUnit &unit{GetUnitMap().NewUnit(terminator)};
96   unit.createdForInternalChildIo_ = forChildIo;
97   return unit;
98 }
99 
100 void ExternalFileUnit::OpenUnit(std::optional<OpenStatus> status,
101     std::optional<Action> action, Position position, OwningPtr<char> &&newPath,
102     std::size_t newPathLength, Convert convert, IoErrorHandler &handler) {
103   if (executionEnvironment.conversion != Convert::Unknown) {
104     convert = executionEnvironment.conversion;
105   }
106   swapEndianness_ = convert == Convert::Swap ||
107       (convert == Convert::LittleEndian && !isHostLittleEndian) ||
108       (convert == Convert::BigEndian && isHostLittleEndian);
109   if (IsConnected()) {
110     bool isSamePath{newPath.get() && path() && pathLength() == newPathLength &&
111         std::memcmp(path(), newPath.get(), newPathLength) == 0};
112     if (status && *status != OpenStatus::Old && isSamePath) {
113       handler.SignalError("OPEN statement for connected unit may not have "
114                           "explicit STATUS= other than 'OLD'");
115       return;
116     }
117     if (!newPath.get() || isSamePath) {
118       // OPEN of existing unit, STATUS='OLD' or unspecified, not new FILE=
119       newPath.reset();
120       return;
121     }
122     // Otherwise, OPEN on open unit with new FILE= implies CLOSE
123     DoImpliedEndfile(handler);
124     FlushOutput(handler);
125     Close(CloseStatus::Keep, handler);
126   }
127   set_path(std::move(newPath), newPathLength);
128   Open(status.value_or(OpenStatus::Unknown), action, position, handler);
129   auto totalBytes{knownSize()};
130   if (access == Access::Direct) {
131     if (!openRecl) {
132       handler.SignalError(IostatOpenBadRecl,
133           "OPEN(UNIT=%d,ACCESS='DIRECT'): record length is not known",
134           unitNumber());
135     } else if (*openRecl <= 0) {
136       handler.SignalError(IostatOpenBadRecl,
137           "OPEN(UNIT=%d,ACCESS='DIRECT',RECL=%jd): record length is invalid",
138           unitNumber(), static_cast<std::intmax_t>(*openRecl));
139     } else if (totalBytes && (*totalBytes % *openRecl != 0)) {
140       handler.SignalError(IostatOpenBadAppend,
141           "OPEN(UNIT=%d,ACCESS='DIRECT',RECL=%jd): record length is not an "
142           "even divisor of the file size %jd",
143           unitNumber(), static_cast<std::intmax_t>(*openRecl),
144           static_cast<std::intmax_t>(*totalBytes));
145     }
146     recordLength = openRecl;
147   }
148   endfileRecordNumber.reset();
149   currentRecordNumber = 1;
150   if (totalBytes && access == Access::Direct && openRecl.value_or(0) > 0) {
151     endfileRecordNumber = 1 + (*totalBytes / *openRecl);
152   }
153   if (position == Position::Append && access != Access::Stream) {
154     if (!endfileRecordNumber) {
155       // Fake it so that we can backspace relative from the end
156       endfileRecordNumber = std::numeric_limits<std::int64_t>::max() - 2;
157     }
158     currentRecordNumber = *endfileRecordNumber;
159   }
160 }
161 
162 void ExternalFileUnit::OpenAnonymousUnit(std::optional<OpenStatus> status,
163     std::optional<Action> action, Position position, Convert convert,
164     IoErrorHandler &handler) {
165   // I/O to an unconnected unit reads/creates a local file, e.g. fort.7
166   std::size_t pathMaxLen{32};
167   auto path{SizedNew<char>{handler}(pathMaxLen)};
168   std::snprintf(path.get(), pathMaxLen, "fort.%d", unitNumber_);
169   OpenUnit(status, action, position, std::move(path), std::strlen(path.get()),
170       convert, handler);
171 }
172 
173 void ExternalFileUnit::CloseUnit(CloseStatus status, IoErrorHandler &handler) {
174   DoImpliedEndfile(handler);
175   FlushOutput(handler);
176   Close(status, handler);
177 }
178 
179 void ExternalFileUnit::DestroyClosed() {
180   GetUnitMap().DestroyClosed(*this); // destroys *this
181 }
182 
183 Iostat ExternalFileUnit::SetDirection(Direction direction) {
184   if (direction == Direction::Input) {
185     if (mayRead()) {
186       direction_ = Direction::Input;
187       return IostatOk;
188     } else {
189       return IostatReadFromWriteOnly;
190     }
191   } else {
192     if (mayWrite()) {
193       direction_ = Direction::Output;
194       return IostatOk;
195     } else {
196       return IostatWriteToReadOnly;
197     }
198   }
199 }
200 
201 UnitMap &ExternalFileUnit::GetUnitMap() {
202   if (unitMap) {
203     return *unitMap;
204   }
205   CriticalSection critical{unitMapLock};
206   if (unitMap) {
207     return *unitMap;
208   }
209   Terminator terminator{__FILE__, __LINE__};
210   IoErrorHandler handler{terminator};
211   UnitMap *newUnitMap{New<UnitMap>{terminator}().release()};
212 
213   bool wasExtant{false};
214   ExternalFileUnit &out{newUnitMap->LookUpOrCreate(6, terminator, wasExtant)};
215   RUNTIME_CHECK(terminator, !wasExtant);
216   out.Predefine(1);
217   handler.SignalError(out.SetDirection(Direction::Output));
218   out.isUnformatted = false;
219   defaultOutput = &out;
220 
221   ExternalFileUnit &in{newUnitMap->LookUpOrCreate(5, terminator, wasExtant)};
222   RUNTIME_CHECK(terminator, !wasExtant);
223   in.Predefine(0);
224   handler.SignalError(in.SetDirection(Direction::Input));
225   in.isUnformatted = false;
226   defaultInput = &in;
227 
228   ExternalFileUnit &error{newUnitMap->LookUpOrCreate(0, terminator, wasExtant)};
229   RUNTIME_CHECK(terminator, !wasExtant);
230   error.Predefine(2);
231   handler.SignalError(error.SetDirection(Direction::Output));
232   error.isUnformatted = false;
233   errorOutput = &error;
234 
235   unitMap = newUnitMap;
236   return *unitMap;
237 }
238 
239 void ExternalFileUnit::CloseAll(IoErrorHandler &handler) {
240   CriticalSection critical{unitMapLock};
241   if (unitMap) {
242     unitMap->CloseAll(handler);
243     FreeMemoryAndNullify(unitMap);
244   }
245   defaultOutput = nullptr;
246   defaultInput = nullptr;
247   errorOutput = nullptr;
248 }
249 
250 void ExternalFileUnit::FlushAll(IoErrorHandler &handler) {
251   CriticalSection critical{unitMapLock};
252   if (unitMap) {
253     unitMap->FlushAll(handler);
254   }
255 }
256 
257 static void SwapEndianness(
258     char *data, std::size_t bytes, std::size_t elementBytes) {
259   if (elementBytes > 1) {
260     auto half{elementBytes >> 1};
261     for (std::size_t j{0}; j + elementBytes <= bytes; j += elementBytes) {
262       for (std::size_t k{0}; k < half; ++k) {
263         std::swap(data[j + k], data[j + elementBytes - 1 - k]);
264       }
265     }
266   }
267 }
268 
269 bool ExternalFileUnit::Emit(const char *data, std::size_t bytes,
270     std::size_t elementBytes, IoErrorHandler &handler) {
271   auto furthestAfter{std::max(furthestPositionInRecord,
272       positionInRecord + static_cast<std::int64_t>(bytes))};
273   if (openRecl) {
274     // Check for fixed-length record overrun, but allow for
275     // sequential record termination.
276     int extra{0};
277     int header{0};
278     if (access == Access::Sequential) {
279       if (isUnformatted.value_or(false)) {
280         // record header + footer
281         header = static_cast<int>(sizeof(std::uint32_t));
282         extra = 2 * header;
283       } else {
284 #ifdef _WIN32
285         if (!isWindowsTextFile()) {
286           ++extra; // carriage return (CR)
287         }
288 #endif
289         ++extra; // newline (LF)
290       }
291     }
292     if (furthestAfter > extra + *openRecl) {
293       handler.SignalError(IostatRecordWriteOverrun,
294           "Attempt to write %zd bytes to position %jd in a fixed-size record "
295           "of %jd bytes",
296           bytes, static_cast<std::intmax_t>(positionInRecord - header),
297           static_cast<std::intmax_t>(*openRecl));
298       return false;
299     }
300   }
301   if (recordLength) {
302     // It is possible for recordLength to have a value now for a
303     // variable-length output record if the previous operation
304     // was a BACKSPACE or non advancing input statement.
305     recordLength.reset();
306     beganReadingRecord_ = false;
307   }
308   if (IsAfterEndfile()) {
309     handler.SignalError(IostatWriteAfterEndfile);
310     return false;
311   }
312   CheckDirectAccess(handler);
313   WriteFrame(frameOffsetInFile_, recordOffsetInFrame_ + furthestAfter, handler);
314   if (positionInRecord > furthestPositionInRecord) {
315     std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord, ' ',
316         positionInRecord - furthestPositionInRecord);
317   }
318   char *to{Frame() + recordOffsetInFrame_ + positionInRecord};
319   std::memcpy(to, data, bytes);
320   if (swapEndianness_) {
321     SwapEndianness(to, bytes, elementBytes);
322   }
323   positionInRecord += bytes;
324   furthestPositionInRecord = furthestAfter;
325   return true;
326 }
327 
328 bool ExternalFileUnit::Receive(char *data, std::size_t bytes,
329     std::size_t elementBytes, IoErrorHandler &handler) {
330   RUNTIME_CHECK(handler, direction_ == Direction::Input);
331   auto furthestAfter{std::max(furthestPositionInRecord,
332       positionInRecord + static_cast<std::int64_t>(bytes))};
333   if (furthestAfter > recordLength.value_or(furthestAfter)) {
334     handler.SignalError(IostatRecordReadOverrun,
335         "Attempt to read %zd bytes at position %jd in a record of %jd bytes",
336         bytes, static_cast<std::intmax_t>(positionInRecord),
337         static_cast<std::intmax_t>(*recordLength));
338     return false;
339   }
340   auto need{recordOffsetInFrame_ + furthestAfter};
341   auto got{ReadFrame(frameOffsetInFile_, need, handler)};
342   if (got >= need) {
343     std::memcpy(data, Frame() + recordOffsetInFrame_ + positionInRecord, bytes);
344     if (swapEndianness_) {
345       SwapEndianness(data, bytes, elementBytes);
346     }
347     positionInRecord += bytes;
348     furthestPositionInRecord = furthestAfter;
349     return true;
350   } else {
351     handler.SignalEnd();
352     if (access == Access::Sequential) {
353       endfileRecordNumber = currentRecordNumber;
354     }
355     return false;
356   }
357 }
358 
359 std::size_t ExternalFileUnit::GetNextInputBytes(
360     const char *&p, IoErrorHandler &handler) {
361   RUNTIME_CHECK(handler, direction_ == Direction::Input);
362   std::size_t length{1};
363   if (auto recl{EffectiveRecordLength()}) {
364     if (positionInRecord < *recl) {
365       length = *recl - positionInRecord;
366     } else {
367       p = nullptr;
368       return 0;
369     }
370   }
371   p = FrameNextInput(handler, length);
372   return p ? length : 0;
373 }
374 
375 const char *ExternalFileUnit::FrameNextInput(
376     IoErrorHandler &handler, std::size_t bytes) {
377   RUNTIME_CHECK(handler, isUnformatted.has_value() && !*isUnformatted);
378   if (static_cast<std::int64_t>(positionInRecord + bytes) <=
379       recordLength.value_or(positionInRecord + bytes)) {
380     auto at{recordOffsetInFrame_ + positionInRecord};
381     auto need{static_cast<std::size_t>(at + bytes)};
382     auto got{ReadFrame(frameOffsetInFile_, need, handler)};
383     SetVariableFormattedRecordLength();
384     if (got >= need) {
385       return Frame() + at;
386     }
387     handler.SignalEnd();
388     if (access == Access::Sequential) {
389       endfileRecordNumber = currentRecordNumber;
390     }
391   }
392   return nullptr;
393 }
394 
395 bool ExternalFileUnit::SetVariableFormattedRecordLength() {
396   if (recordLength || access == Access::Direct) {
397     return true;
398   } else if (FrameLength() > recordOffsetInFrame_) {
399     const char *record{Frame() + recordOffsetInFrame_};
400     std::size_t bytes{FrameLength() - recordOffsetInFrame_};
401     if (const char *nl{
402             reinterpret_cast<const char *>(std::memchr(record, '\n', bytes))}) {
403       recordLength = nl - record;
404       if (*recordLength > 0 && record[*recordLength - 1] == '\r') {
405         --*recordLength;
406       }
407       return true;
408     }
409   }
410   return false;
411 }
412 
413 bool ExternalFileUnit::BeginReadingRecord(IoErrorHandler &handler) {
414   RUNTIME_CHECK(handler, direction_ == Direction::Input);
415   if (!beganReadingRecord_) {
416     beganReadingRecord_ = true;
417     if (access == Access::Direct) {
418       CheckDirectAccess(handler);
419       auto need{static_cast<std::size_t>(recordOffsetInFrame_ + *openRecl)};
420       auto got{ReadFrame(frameOffsetInFile_, need, handler)};
421       if (got >= need) {
422         recordLength = openRecl;
423       } else {
424         recordLength.reset();
425         handler.SignalEnd();
426       }
427     } else {
428       recordLength.reset();
429       if (IsAtEOF()) {
430         handler.SignalEnd();
431       } else {
432         RUNTIME_CHECK(handler, isUnformatted.has_value());
433         if (*isUnformatted) {
434           if (access == Access::Sequential) {
435             BeginSequentialVariableUnformattedInputRecord(handler);
436           }
437         } else { // formatted sequential or stream
438           BeginVariableFormattedInputRecord(handler);
439         }
440       }
441     }
442   }
443   RUNTIME_CHECK(handler,
444       recordLength.has_value() || !IsRecordFile() || handler.InError());
445   return !handler.InError();
446 }
447 
448 void ExternalFileUnit::FinishReadingRecord(IoErrorHandler &handler) {
449   RUNTIME_CHECK(handler, direction_ == Direction::Input && beganReadingRecord_);
450   beganReadingRecord_ = false;
451   if (handler.InError() && handler.GetIoStat() != IostatEor) {
452     // Avoid bogus crashes in END/ERR circumstances; but
453     // still increment the current record number so that
454     // an attempted read of an endfile record, followed by
455     // a BACKSPACE, will still be at EOF.
456     ++currentRecordNumber;
457   } else if (IsRecordFile()) {
458     RUNTIME_CHECK(handler, recordLength.has_value());
459     recordOffsetInFrame_ += *recordLength;
460     if (access != Access::Direct) {
461       RUNTIME_CHECK(handler, isUnformatted.has_value());
462       recordLength.reset();
463       if (isUnformatted.value_or(false)) {
464         // Retain footer in frame for more efficient BACKSPACE
465         frameOffsetInFile_ += recordOffsetInFrame_;
466         recordOffsetInFrame_ = sizeof(std::uint32_t);
467       } else { // formatted
468         if (FrameLength() > recordOffsetInFrame_ &&
469             Frame()[recordOffsetInFrame_] == '\r') {
470           ++recordOffsetInFrame_;
471         }
472         if (FrameLength() > recordOffsetInFrame_ &&
473             Frame()[recordOffsetInFrame_] == '\n') {
474           ++recordOffsetInFrame_;
475         }
476         if (!pinnedFrame || mayPosition()) {
477           frameOffsetInFile_ += recordOffsetInFrame_;
478           recordOffsetInFrame_ = 0;
479         }
480       }
481     }
482     ++currentRecordNumber;
483   } else { // unformatted stream
484     furthestPositionInRecord =
485         std::max(furthestPositionInRecord, positionInRecord);
486     frameOffsetInFile_ += recordOffsetInFrame_ + furthestPositionInRecord;
487   }
488   BeginRecord();
489 }
490 
491 bool ExternalFileUnit::AdvanceRecord(IoErrorHandler &handler) {
492   if (direction_ == Direction::Input) {
493     FinishReadingRecord(handler);
494     return BeginReadingRecord(handler);
495   } else { // Direction::Output
496     bool ok{true};
497     RUNTIME_CHECK(handler, isUnformatted.has_value());
498     positionInRecord = furthestPositionInRecord;
499     if (access == Access::Direct) {
500       if (furthestPositionInRecord <
501           openRecl.value_or(furthestPositionInRecord)) {
502         // Pad remainder of fixed length record
503         WriteFrame(
504             frameOffsetInFile_, recordOffsetInFrame_ + *openRecl, handler);
505         std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord,
506             isUnformatted.value_or(false) ? 0 : ' ',
507             *openRecl - furthestPositionInRecord);
508         furthestPositionInRecord = *openRecl;
509       }
510     } else if (*isUnformatted) {
511       if (access == Access::Sequential) {
512         // Append the length of a sequential unformatted variable-length record
513         // as its footer, then overwrite the reserved first four bytes of the
514         // record with its length as its header.  These four bytes were skipped
515         // over in BeginUnformattedIO<Output>().
516         // TODO: Break very large records up into subrecords with negative
517         // headers &/or footers
518         std::uint32_t length;
519         length = furthestPositionInRecord - sizeof length;
520         ok = ok &&
521             Emit(reinterpret_cast<const char *>(&length), sizeof length,
522                 sizeof length, handler);
523         positionInRecord = 0;
524         ok = ok &&
525             Emit(reinterpret_cast<const char *>(&length), sizeof length,
526                 sizeof length, handler);
527       } else {
528         // Unformatted stream: nothing to do
529       }
530     } else if (handler.GetIoStat() != IostatOk &&
531         furthestPositionInRecord == 0) {
532       // Error in formatted variable length record, and no output yet; do
533       // nothing, like most other Fortran compilers do.
534       return true;
535     } else {
536       // Terminate formatted variable length record
537       const char *lineEnding{"\n"};
538       std::size_t lineEndingBytes{1};
539 #ifdef _WIN32
540       if (!isWindowsTextFile()) {
541         lineEnding = "\r\n";
542         lineEndingBytes = 2;
543       }
544 #endif
545       ok = ok && Emit(lineEnding, lineEndingBytes, 1, handler);
546     }
547     if (IsAfterEndfile()) {
548       return false;
549     }
550     CommitWrites();
551     ++currentRecordNumber;
552     if (access != Access::Direct) {
553       impliedEndfile_ = IsRecordFile();
554       if (IsAtEOF()) {
555         endfileRecordNumber.reset();
556       }
557     }
558     return ok;
559   }
560 }
561 
562 void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
563   if (access == Access::Direct || !IsRecordFile()) {
564     handler.SignalError(IostatBackspaceNonSequential,
565         "BACKSPACE(UNIT=%d) on direct-access file or unformatted stream",
566         unitNumber());
567   } else {
568     if (IsAfterEndfile()) {
569       // BACKSPACE after explicit ENDFILE
570       currentRecordNumber = *endfileRecordNumber;
571     } else {
572       DoImpliedEndfile(handler);
573       if (frameOffsetInFile_ + recordOffsetInFrame_ > 0) {
574         --currentRecordNumber;
575         if (openRecl && access == Access::Direct) {
576           BackspaceFixedRecord(handler);
577         } else {
578           RUNTIME_CHECK(handler, isUnformatted.has_value());
579           if (isUnformatted.value_or(false)) {
580             BackspaceVariableUnformattedRecord(handler);
581           } else {
582             BackspaceVariableFormattedRecord(handler);
583           }
584         }
585       }
586     }
587     BeginRecord();
588   }
589 }
590 
591 void ExternalFileUnit::FlushOutput(IoErrorHandler &handler) {
592   if (!mayPosition()) {
593     auto frameAt{FrameAt()};
594     if (frameOffsetInFile_ >= frameAt &&
595         frameOffsetInFile_ <
596             static_cast<std::int64_t>(frameAt + FrameLength())) {
597       // A Flush() that's about to happen to a non-positionable file
598       // needs to advance frameOffsetInFile_ to prevent attempts at
599       // impossible seeks
600       CommitWrites();
601     }
602   }
603   Flush(handler);
604 }
605 
606 void ExternalFileUnit::FlushIfTerminal(IoErrorHandler &handler) {
607   if (isTerminal()) {
608     FlushOutput(handler);
609   }
610 }
611 
612 void ExternalFileUnit::Endfile(IoErrorHandler &handler) {
613   if (access == Access::Direct) {
614     handler.SignalError(IostatEndfileDirect,
615         "ENDFILE(UNIT=%d) on direct-access file", unitNumber());
616   } else if (!mayWrite()) {
617     handler.SignalError(IostatEndfileUnwritable,
618         "ENDFILE(UNIT=%d) on read-only file", unitNumber());
619   } else if (IsAfterEndfile()) {
620     // ENDFILE after ENDFILE
621   } else {
622     DoEndfile(handler);
623     if (access == Access::Sequential) {
624       // Explicit ENDFILE leaves position *after* the endfile record
625       RUNTIME_CHECK(handler, endfileRecordNumber.has_value());
626       currentRecordNumber = *endfileRecordNumber + 1;
627     }
628   }
629 }
630 
631 void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
632   if (access == Access::Direct) {
633     handler.SignalError(IostatRewindNonSequential,
634         "REWIND(UNIT=%d) on non-sequential file", unitNumber());
635   } else {
636     SetPosition(0, handler);
637     currentRecordNumber = 1;
638   }
639 }
640 
641 void ExternalFileUnit::SetPosition(std::int64_t pos, IoErrorHandler &handler) {
642   DoImpliedEndfile(handler);
643   frameOffsetInFile_ = pos;
644   recordOffsetInFrame_ = 0;
645   if (access == Access::Direct) {
646     directAccessRecWasSet_ = true;
647   }
648   BeginRecord();
649 }
650 
651 void ExternalFileUnit::EndIoStatement() {
652   io_.reset();
653   u_.emplace<std::monostate>();
654   lock_.Drop();
655 }
656 
657 void ExternalFileUnit::BeginSequentialVariableUnformattedInputRecord(
658     IoErrorHandler &handler) {
659   std::int32_t header{0}, footer{0};
660   std::size_t need{recordOffsetInFrame_ + sizeof header};
661   std::size_t got{ReadFrame(frameOffsetInFile_, need, handler)};
662   // Try to emit informative errors to help debug corrupted files.
663   const char *error{nullptr};
664   if (got < need) {
665     if (got == recordOffsetInFrame_) {
666       handler.SignalEnd();
667     } else {
668       error = "Unformatted variable-length sequential file input failed at "
669               "record #%jd (file offset %jd): truncated record header";
670     }
671   } else {
672     std::memcpy(&header, Frame() + recordOffsetInFrame_, sizeof header);
673     recordLength = sizeof header + header; // does not include footer
674     need = recordOffsetInFrame_ + *recordLength + sizeof footer;
675     got = ReadFrame(frameOffsetInFile_, need, handler);
676     if (got < need) {
677       error = "Unformatted variable-length sequential file input failed at "
678               "record #%jd (file offset %jd): hit EOF reading record with "
679               "length %jd bytes";
680     } else {
681       std::memcpy(&footer, Frame() + recordOffsetInFrame_ + *recordLength,
682           sizeof footer);
683       if (footer != header) {
684         error = "Unformatted variable-length sequential file input failed at "
685                 "record #%jd (file offset %jd): record header has length %jd "
686                 "that does not match record footer (%jd)";
687       }
688     }
689   }
690   if (error) {
691     handler.SignalError(error, static_cast<std::intmax_t>(currentRecordNumber),
692         static_cast<std::intmax_t>(frameOffsetInFile_),
693         static_cast<std::intmax_t>(header), static_cast<std::intmax_t>(footer));
694     // TODO: error recovery
695   }
696   positionInRecord = sizeof header;
697 }
698 
699 void ExternalFileUnit::BeginVariableFormattedInputRecord(
700     IoErrorHandler &handler) {
701   if (this == defaultInput) {
702     if (defaultOutput) {
703       defaultOutput->FlushOutput(handler);
704     }
705     if (errorOutput) {
706       errorOutput->FlushOutput(handler);
707     }
708   }
709   std::size_t length{0};
710   do {
711     std::size_t need{length + 1};
712     length =
713         ReadFrame(frameOffsetInFile_, recordOffsetInFrame_ + need, handler) -
714         recordOffsetInFrame_;
715     if (length < need) {
716       if (length > 0) {
717         // final record w/o \n
718         recordLength = length;
719       } else {
720         handler.SignalEnd();
721       }
722       break;
723     }
724   } while (!SetVariableFormattedRecordLength());
725 }
726 
727 void ExternalFileUnit::BackspaceFixedRecord(IoErrorHandler &handler) {
728   RUNTIME_CHECK(handler, openRecl.has_value());
729   if (frameOffsetInFile_ < *openRecl) {
730     handler.SignalError(IostatBackspaceAtFirstRecord);
731   } else {
732     frameOffsetInFile_ -= *openRecl;
733   }
734 }
735 
736 void ExternalFileUnit::BackspaceVariableUnformattedRecord(
737     IoErrorHandler &handler) {
738   std::int32_t header{0}, footer{0};
739   auto headerBytes{static_cast<std::int64_t>(sizeof header)};
740   frameOffsetInFile_ += recordOffsetInFrame_;
741   recordOffsetInFrame_ = 0;
742   if (frameOffsetInFile_ <= headerBytes) {
743     handler.SignalError(IostatBackspaceAtFirstRecord);
744     return;
745   }
746   // Error conditions here cause crashes, not file format errors, because the
747   // validity of the file structure before the current record will have been
748   // checked informatively in NextSequentialVariableUnformattedInputRecord().
749   std::size_t got{
750       ReadFrame(frameOffsetInFile_ - headerBytes, headerBytes, handler)};
751   if (static_cast<std::int64_t>(got) < headerBytes) {
752     handler.SignalError(IostatShortRead);
753     return;
754   }
755   std::memcpy(&footer, Frame(), sizeof footer);
756   recordLength = footer;
757   if (frameOffsetInFile_ < *recordLength + 2 * headerBytes) {
758     handler.SignalError(IostatBadUnformattedRecord);
759     return;
760   }
761   frameOffsetInFile_ -= *recordLength + 2 * headerBytes;
762   if (frameOffsetInFile_ >= headerBytes) {
763     frameOffsetInFile_ -= headerBytes;
764     recordOffsetInFrame_ = headerBytes;
765   }
766   auto need{static_cast<std::size_t>(
767       recordOffsetInFrame_ + sizeof header + *recordLength)};
768   got = ReadFrame(frameOffsetInFile_, need, handler);
769   if (got < need) {
770     handler.SignalError(IostatShortRead);
771     return;
772   }
773   std::memcpy(&header, Frame() + recordOffsetInFrame_, sizeof header);
774   if (header != *recordLength) {
775     handler.SignalError(IostatBadUnformattedRecord);
776     return;
777   }
778 }
779 
780 // There's no portable memrchr(), unfortunately, and strrchr() would
781 // fail on a record with a NUL, so we have to do it the hard way.
782 static const char *FindLastNewline(const char *str, std::size_t length) {
783   for (const char *p{str + length}; p-- > str;) {
784     if (*p == '\n') {
785       return p;
786     }
787   }
788   return nullptr;
789 }
790 
791 void ExternalFileUnit::BackspaceVariableFormattedRecord(
792     IoErrorHandler &handler) {
793   // File offset of previous record's newline
794   auto prevNL{
795       frameOffsetInFile_ + static_cast<std::int64_t>(recordOffsetInFrame_) - 1};
796   if (prevNL < 0) {
797     handler.SignalError(IostatBackspaceAtFirstRecord);
798     return;
799   }
800   while (true) {
801     if (frameOffsetInFile_ < prevNL) {
802       if (const char *p{
803               FindLastNewline(Frame(), prevNL - 1 - frameOffsetInFile_)}) {
804         recordOffsetInFrame_ = p - Frame() + 1;
805         recordLength = prevNL - (frameOffsetInFile_ + recordOffsetInFrame_);
806         break;
807       }
808     }
809     if (frameOffsetInFile_ == 0) {
810       recordOffsetInFrame_ = 0;
811       recordLength = prevNL;
812       break;
813     }
814     frameOffsetInFile_ -= std::min<std::int64_t>(frameOffsetInFile_, 1024);
815     auto need{static_cast<std::size_t>(prevNL + 1 - frameOffsetInFile_)};
816     auto got{ReadFrame(frameOffsetInFile_, need, handler)};
817     if (got < need) {
818       handler.SignalError(IostatShortRead);
819       return;
820     }
821   }
822   if (Frame()[recordOffsetInFrame_ + *recordLength] != '\n') {
823     handler.SignalError(IostatMissingTerminator);
824     return;
825   }
826   if (*recordLength > 0 &&
827       Frame()[recordOffsetInFrame_ + *recordLength - 1] == '\r') {
828     --*recordLength;
829   }
830 }
831 
832 void ExternalFileUnit::DoImpliedEndfile(IoErrorHandler &handler) {
833   if (impliedEndfile_) {
834     impliedEndfile_ = false;
835     if (access != Access::Direct && IsRecordFile() && mayPosition()) {
836       DoEndfile(handler);
837     }
838   }
839 }
840 
841 void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
842   if (IsRecordFile()) {
843     endfileRecordNumber = currentRecordNumber;
844   }
845   FlushOutput(handler);
846   Truncate(frameOffsetInFile_ + recordOffsetInFrame_, handler);
847   BeginRecord();
848   impliedEndfile_ = false;
849 }
850 
851 void ExternalFileUnit::CommitWrites() {
852   frameOffsetInFile_ +=
853       recordOffsetInFrame_ + recordLength.value_or(furthestPositionInRecord);
854   recordOffsetInFrame_ = 0;
855   BeginRecord();
856 }
857 
858 bool ExternalFileUnit::CheckDirectAccess(IoErrorHandler &handler) {
859   if (access == Access::Direct) {
860     RUNTIME_CHECK(handler, openRecl);
861     if (!directAccessRecWasSet_) {
862       handler.SignalError(
863           "No REC= was specified for a data transfer with ACCESS='DIRECT'");
864       return false;
865     }
866   }
867   return true;
868 }
869 
870 ChildIo &ExternalFileUnit::PushChildIo(IoStatementState &parent) {
871   OwningPtr<ChildIo> current{std::move(child_)};
872   Terminator &terminator{parent.GetIoErrorHandler()};
873   OwningPtr<ChildIo> next{New<ChildIo>{terminator}(parent, std::move(current))};
874   child_.reset(next.release());
875   return *child_;
876 }
877 
878 void ExternalFileUnit::PopChildIo(ChildIo &child) {
879   if (child_.get() != &child) {
880     child.parent().GetIoErrorHandler().Crash(
881         "ChildIo being popped is not top of stack");
882   }
883   child_.reset(child.AcquirePrevious().release()); // deletes top child
884 }
885 
886 void ChildIo::EndIoStatement() {
887   io_.reset();
888   u_.emplace<std::monostate>();
889 }
890 
891 Iostat ChildIo::CheckFormattingAndDirection(
892     bool unformatted, Direction direction) {
893   bool parentIsInput{!parent_.get_if<IoDirectionState<Direction::Output>>()};
894   bool parentIsFormatted{parentIsInput
895           ? parent_.get_if<FormattedIoStatementState<Direction::Input>>() !=
896               nullptr
897           : parent_.get_if<FormattedIoStatementState<Direction::Output>>() !=
898               nullptr};
899   bool parentIsUnformatted{!parentIsFormatted};
900   if (unformatted != parentIsUnformatted) {
901     return unformatted ? IostatUnformattedChildOnFormattedParent
902                        : IostatFormattedChildOnUnformattedParent;
903   } else if (parentIsInput != (direction == Direction::Input)) {
904     return parentIsInput ? IostatChildOutputToInputParent
905                          : IostatChildInputFromOutputParent;
906   } else {
907     return IostatOk;
908   }
909 }
910 
911 } // namespace Fortran::runtime::io
912