fseek64, fs_total64, ftell64, fread, fwrite

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
BB Hood
DC Developer
DC Developer
Posts: 189
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Fri Mar 30, 2007 12:09 am
Has thanked: 41 times
Been thanked: 10 times

fseek64, fs_total64, ftell64, fread, fwrite

Post by BB Hood »

I was trying to get TinyXML2 running and the example included with the library failed on me. I was using libfatfs and thought id try and use the above functions but they don't compile(undefined reference).

fs.h says:

/* 64-bit file access functions. Generally, you should only define one of
the 64-bit or 32-bit versions of these functions.*/

so lets say I ONLY have the 64 bit file access functions defined, will fs_total() call the 64 bit version since the 32-bit version isn't defined?

I tried to compile ext2 example by calling fs_total64() and I get the same compile error.

How do we call the 64-bit versions?
Last edited by BB Hood on Sun Oct 13, 2013 3:04 pm, edited 1 time in total.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5666
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: fseek64, fs_total64, ftell64

Post by BlueCrab »

BB Hood wrote:I was trying to get TinyXML2 running and the example included with the library failed on me. I was using libfatfs and thought id try and use the above functions but they don't compile(undefined reference).
Can you actually give the full error? The fs_seek64, fs_total64, and fs_tell64 functions should all be defined. fseek64 and ftell64 are not part of KOS (they'd be in newlib, if anywhere).
fs.h says:

/* 64-bit file access functions. Generally, you should only define one of
the 64-bit or 32-bit versions of these functions.*/

so lets say I ONLY have the 64 bit file access functions defined, will fs_total() call the 64 bit version since the 32-bit version isn't defined?
Correct. fs_total will prefer the 32-bit version, but fall-back to the 64-bit version if needed. fs_total64 does the opposite (prefers 64-bit, falls back to 32-bit). See the following code snippet from fs.c:

Code: Select all

size_t fs_total(file_t fd) {
    fs_hnd_t *h = fs_map_hnd(fd);

    if(h == NULL) return -1;

    if(h->handler == NULL) {
        errno = EINVAL;
        return -1;
    }

    /* Prefer the 32-bit version, but fall back if needed to the 64-bit one. */
    if(h->handler->total)
        return h->handler->total(h->hnd);
    else if(h->handler->total64)
        return (size_t)h->handler->total64(h->hnd);

    errno = EINVAL;
    return -1;
}

uint64 fs_total64(file_t fd) {
    fs_hnd_t *h = fs_map_hnd(fd);

    if(h == NULL) return -1;

    if(h->handler == NULL) {
        errno = EINVAL;
        return -1;
    }

    /* Prefer the 64-bit version, but fall back if needed to the 32-bit one. */
    if(h->handler->total64)
        return h->handler->total64(h->hnd);
    else if(h->handler->total)
        return (uint64)h->handler->total(h->hnd);

    errno = EINVAL;
    return -1;
}
I tried to compile ext2 example by calling fs_total64() and I get the same compile error.

How do we call the 64-bit versions?
fs_total64 shouldn't give you any problems. The other two functions in the topic title aren't in KOS -- they're newlib's responsibility. I dunno if newlib even provides them (it probably doesn't, as they aren't standard functions, IIRC).
User avatar
BB Hood
DC Developer
DC Developer
Posts: 189
Joined: Fri Mar 30, 2007 12:09 am
Has thanked: 41 times
Been thanked: 10 times

Re: fseek64, fs_total64, ftell64

Post by BB Hood »

ugh looks like KallistiOS just needed a good cleaning and recompile. As for fseek64 and ftell64. There are actually supposed to be fseeko and ftello.

More about those functions are explained here: http://man7.org/linux/man-pages/man3/fseeko.3.html

Thanks again BlueCrab.

EDIT: So it seems fs_total()/fs_total64() only works for file descriptors and not file pointers?

Also fread() and (most likely; didnt test yet) fwrite() work differently than read() and write() for some reason.

When I use read(file descriptor) it reads the amount of bytes I tell it to in one go. When I use fread(file pointer) it reads in multiplies of 1024 bytes(repeat calls). For example:

Code: Select all

fread(buf, 1, 10, fp );
the debug output I put into fs_read() says that I want to read 1024 bytes instead of 10. Another example:

Code: Select all

fread( buf, 1, 1025, fp );
the debug output I put into fs_read() says that I want to read 1024 bytes instead of 1025. However, fs_read() is (automatically) called again reading another 1024 bytes starting from ptr = 1024.

read(file descriptor) doesn't do this. It reads it in one go (the right amount of bytes asked for) instead of being called again.
Post Reply