tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Issues with lseek(2) on a block device



Hello, for most operating systems determining the size of a block
device can be done with:

lseek(fd, 0, SEEK_END);

However, on NetBSD this does not seem to work.

#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>

int main(void)
{
        int   fd;
        off_t offset;

        fd = open("/dev/sd0", O_RDONLY);
        assert(fd >= 0);

        offset = lseek(fd, 4096, SEEK_SET);
        assert(offset >= 0);
        printf("lseek(fd, 4096, SEEK_SET) = %ju bytes\n", (uintmax_t)offset);

        offset = lseek(fd, 0, SEEK_END);
        assert(offset >= 0);
        printf("lseek(fd, 0, SEEK_END)    = %ju bytes\n", (uintmax_t)offset);
}

Device /dev/sd0 is capable of seeking and SEEK_SET works as expected.
However, SEEK_END always returns 0.

# gcc -Wall -Wpedantic lseek_test.c && ./a.out
lseek(fd, 4096, SEEK_SET) = 4096 bytes
lseek(fd, 0, SEEK_END)    = 0 bytes

Is this a NetBSD feature or a bug or me doing something wrong?

Thanks.


Home | Main Index | Thread Index | Old Index