autodriver/autodriver.c
static int dump_file(int fd, size_t bytes, off_t offset) {
    static unsigned currentStamp = 0;
    size_t read_rem = bytes;
    size_t nextOffset = offset;

    if (offset) {  // second part of output file, after truncating in the middle
      // insert a message, indicating file truncation
      char *msg = "\n...[excess bytes elided by autodriver]...\n";
      if (writeBuffer(msg, strlen(msg))) {return -1;}
    }

    // Flush stdout so our writes here don't race with buffer flushes
    if (fflush(stdout) != 0) {
        ERROR_ERRNO("Flushing standard out");
        return -1;
    }

    if (lseek(fd, offset, SEEK_SET) < 0) {
        ERROR_ERRNO("Seeking in output file");
        return -1;
    }

    while (read_rem > 0) {
      char buffer[BUFSIZE + 1];  // keep the last byte as string terminator
      ssize_t nread;

      memset(buffer, 0, BUFSIZE + 1);
      if ((nread = read(fd, buffer, min(read_rem, BUFSIZE))) < 0) {
        ERROR_ERRNO("Reading from output file");
        return -1;
      }
      read_rem -= nread;
      char *scanCursor = buffer;

      if (timestampCount) {  // If inserting timestamp
        insertTimestamp(buffer, nextOffset, nread, &scanCursor, &currentStamp);
      }

      if (writeBuffer(scanCursor, nread - (scanCursor - buffer))) {
        ERROR("Write failed: buffer %p cursor %p nread %lu", buffer, scanCursor, nread);
        return -1;
      }

      nextOffset += nread;  // offset of next read buffer in the file
    }  // while loop finish reading

    return 0;
}
/**
 * @brief Dumps a specified number of bytes from a file to standard out
 *
 * @arg fd File to read from
 * @arg bytes Number of bytes to read
 * @arg offset Offset to start from
 *
 * @return 0 on success, -1 on failure
 */
@brief Dumps a specified number of bytes from a file to standard out
@arg fd File to read from
@arg bytes Number of bytes to read
@arg offset Offset to start from

@return 0 on success, -1 on failure
@brief Dumps a specified number of bytes from a file to standard out @arg fd File to read from @arg bytes Number of bytes to read @arg offset Offset to start from @return 0 on success - 1 on failure
static int dump_file(int fd, size_t bytes, off_t offset) {
    static unsigned currentStamp = 0;
    size_t read_rem = bytes;
    size_t nextOffset = offset;
    if (offset) {  
      char *msg = "\n...[excess bytes elided by autodriver]...\n";
      if (writeBuffer(msg, strlen(msg))) {return -1;}
    }
    if (fflush(stdout) != 0) {
        ERROR_ERRNO("Flushing standard out");
        return -1;
    }
    if (lseek(fd, offset, SEEK_SET) < 0) {
        ERROR_ERRNO("Seeking in output file");
        return -1;
    }
    while (read_rem > 0) {
      char buffer[BUFSIZE + 1];  
      ssize_t nread;
      memset(buffer, 0, BUFSIZE + 1);
      if ((nread = read(fd, buffer, min(read_rem, BUFSIZE))) < 0) {
        ERROR_ERRNO("Reading from output file");
        return -1;
      }
      read_rem -= nread;
      char *scanCursor = buffer;
      if (timestampCount) {  
        insertTimestamp(buffer, nextOffset, nread, &scanCursor, &currentStamp);
      }
      if (writeBuffer(scanCursor, nread - (scanCursor - buffer))) {
        ERROR("Write failed: buffer %p cursor %p nread %lu", buffer, scanCursor, nread);
        return -1;
      }
      nextOffset += nread;  
    }  
    return 0;
}
static int dump_file ( int fd , size_t bytes , off_t offset ) { static unsigned currentStamp = 0 ; size_t read_rem = bytes ; size_t nextOffset = offset ; if ( offset ) { char * msg = " \n \n " ; if ( writeBuffer ( msg , strlen ( msg ) ) ) { return -1 ; } } if ( fflush ( stdout ) != 0 ) { ERROR_ERRNO ( " " ) ; return -1 ; } if ( lseek ( fd , offset , SEEK_SET ) < 0 ) { ERROR_ERRNO ( " " ) ; return -1 ; } while ( read_rem > 0 ) { char buffer [ BUFSIZE + 1 ] ; ssize_t nread ; memset ( buffer , 0 , BUFSIZE + 1 ) ; if ( ( nread = read ( fd , buffer , min ( read_rem , BUFSIZE ) ) ) < 0 ) { ERROR_ERRNO ( " " ) ; return -1 ; } read_rem -= nread ; char * scanCursor = buffer ; if ( timestampCount ) { insertTimestamp ( buffer , nextOffset , nread , & scanCursor , & currentStamp ) ; } if ( writeBuffer ( scanCursor , nread - ( scanCursor - buffer ) ) ) { ERROR ( " " , buffer , scanCursor , nread ) ; return -1 ; } nextOffset += nread ; } return 0 ; }
@brief Dumps a specified number of bytes from a file to standard out
 @arg fd File to read from
 @arg bytes Number of bytes to read
 @arg offset Offset to start from
@brief Dumps a specified number of bytes from a file to standard out @arg fd File to read from @arg bytes Number of bytes to read @arg offset Offset to start from
// second part of output file, after truncating in the middle // insert a message, indicating file truncation // Flush stdout so our writes here don't race with buffer flushes // keep the last byte as string terminator // If inserting timestamp // offset of next read buffer in the file // while loop finish reading