diff --git a/libgcobol/gfileio.cc b/libgcobol/gfileio.cc index 988ef6ac89ed76d08a9ee7975e7dc7841d4676e8..1c2a4ea4c1280f9312c278e970ce570bd3457e9a 100644 --- a/libgcobol/gfileio.cc +++ b/libgcobol/gfileio.cc @@ -113,7 +113,7 @@ static bool handle_ferror(cblc_file_t *file, const char *function, const char *msg) { // This routine gets called after an operation that might result in a - // failure, ocr in an end-of-file + // failure, or in an end-of-file bool retval = false; // Indicates no error file->errnum = ferror(file->file_pointer); @@ -705,13 +705,14 @@ relative_file_delete(cblc_file_t *file, bool is_random) errno = 0; file->errnum = 0; - if( pread(rfp.fd, &record_marker, 1, rfp.flag_position) == -1 ) + ssize_t presult = pread(rfp.fd, &record_marker, 1, rfp.flag_position); + if( presult < 0 ) { handle_errno(file, __func__, "pread() error"); goto done; } - if( record_marker != internal_newline ) + if( presult == 0 || record_marker != internal_newline ) { // There isn't a record there for us to delete, which is an error file->io_status = FsNotFound; // "23" @@ -1419,9 +1420,15 @@ relative_file_start(cblc_file_t *file, && rfp.record_position+total_record_length <= rfp.file_size ) { char record_marker; - pread(rfp.fd, &record_marker, 1, rfp.flag_position); - if( handle_ferror(file, __func__, "pread() error") ) + ssize_t presult = pread(rfp.fd, &record_marker, 1, rfp.flag_position); + if( presult < 0 ) { + handle_errno(file, __func__, "pread() error"); + goto done; + } + if( presult == 0 ) + { + // end of file goto done; } if( record_marker == internal_newline ) @@ -1874,12 +1881,15 @@ relative_file_rewrite( cblc_file_t *file, size_t length, bool is_random ) { goto done; } - pread(rfp.fd, &record_marker, 1, rfp.flag_position); - if( handle_ferror(file, __func__, "pread() error") ) + + ssize_t presult = pread(rfp.fd, &record_marker, 1, rfp.flag_position); + if( presult < 0 ) { + handle_errno(file, __func__, "pread() error"); goto done; } - if( record_marker != internal_newline ) + + if( presult == 0 || record_marker != internal_newline ) { // The record is not specified: file->io_status = FsNotFound; // "23" @@ -2438,18 +2448,21 @@ relative_file_write(cblc_file_t *file, } // Let's check to make sure the slot for this record is currently available: char record_marker; - pread(rfp.fd, &record_marker, 1, rfp.flag_position); - if( handle_ferror(file, __func__, "pread() error") ) + ssize_t presult = pread(rfp.fd, &record_marker, 1, rfp.flag_position); + if( presult < 0 ) { + handle_errno(file, __func__, "pread() error"); goto done; } - if( record_marker == internal_newline ) + if( presult == 1 && record_marker == internal_newline ) { // The slot has something in it already: file->io_status = FsDupWrite; // "22" goto done; } + // Either the record is available, or else the write is taking place at the + // end of the current file size. // Position the file pointer at the slot: fseek(file->file_pointer, rfp.record_position, SEEK_SET); @@ -3484,8 +3497,7 @@ relative_file_read( cblc_file_t *file, goto done; } char record_marker; - pread(rfp.fd, &record_marker, 1, rfp.flag_position); - if( handle_ferror(file, __func__, "pread() error") ) + if( pread(rfp.fd, &record_marker, 1, rfp.flag_position) <= 0) { goto done; }