Skip to content

FileSeek

Description

Adjust the file offset relative to whence.

Success

Returns the new absolute file offset (>= 0).

Failure

Returns -1 on error (typically ESPIPE for a pipe/tty).

Usage example (Cross-references)

Usage examples (Cross-references)
        // channel is positionable; if it returns -1 (ESPIPE / Windows
        // equivalent) treat the input as a stream and pull bytes until EOF.
        i64 probe = FileSeek(file, 0, FILE_SEEK_CUR);
        if (probe < 0 || fd == 0 || fd == 1 || fd == 2) {
            LOG_INFO("Reading from non-seekable channel.");
            // actually consumed".
            i64 cur_pos = probe;
            i64 end_pos = FileSeek(file, 0, FILE_SEEK_END);
            if (end_pos < 0) {
                LOG_ERROR("FileSeek(END) failed during f_read_fmt");
            i64 end_pos = FileSeek(file, 0, FILE_SEEK_END);
            if (end_pos < 0) {
                LOG_ERROR("FileSeek(END) failed during f_read_fmt");
                StrDeinit(&buffer);
                DefaultAllocatorDeinit(&scratch);
            }
            i64 file_len = end_pos - cur_pos;
            (void)FileSeek(file, cur_pos, FILE_SEEK_SET);
    
            if (file_len > 0) {
                if (!new_pos) {
                    LOG_ERROR("Parse failed, rolling back...");
                    (void)FileSeek(file, cur_pos, FILE_SEEK_SET);
                } else {
                    // Advance the channel position past the bytes we consumed.
                    // Advance the channel position past the bytes we consumed.
                    i64 consumed = (i64)(new_pos - buffer.data);
                    (void)FileSeek(file, cur_pos + consumed, FILE_SEEK_SET);
                }
            }
    }
    
    i64 FileSeek(File *f, i64 offset, FileWhence whence) {
        if (!FileIsValid(f)) {
            return -1;
    
    i64 FileTell(File *f) {
        return FileSeek(f, 0, FILE_SEEK_CUR);
    }
    
        // technically padding here but we can skip it
        FileSeek(&elf, 7, FILE_SEEK_CUR);
    
        // XXX: For now we only support x86_64 binaries
Last updated on