Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 1 May 2004 16:01:36 +0200
From:      Dimitry Andric <dimitry@andric.com>
To:        Jens Schweikhardt <schweikh@schweikhardt.net>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: lseek to EOF always 0 for devices
Message-ID:  <1434443012.20040501160136@andric.com>
In-Reply-To: <20040501113704.GA5730@schweikhardt.net>
References:  <20040501113704.GA5730@schweikhardt.net>

next in thread | previous in thread | raw e-mail | index | archive | help
------------9C16C19E114218
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

On 2004-05-01 at 13:37:04 Jens Schweikhardt wrote:

> I'm not sure if I'm trying something impossible here. I want to find the
> size in bytes of a disk special device, eg /dev/da0, /dev/da0s1,
> /dev/da0s1a, /dev/vinum/usr and so on.

I have no idea why lseek'ing in the devices doesn't work, but try
using ioctl(2) with DIOCGMEDIASIZE (from <sys/disk.h>), this works
fine for me:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/disk.h>

int main (int argc, char **argv)
{
    int fd;
    off_t len;

    if (argc != 2)
        return 1;

    if ((fd = open (argv[1], O_RDONLY)) < 0) {
        perror (argv[1]);
        return 1;
    }

    if (ioctl (fd, DIOCGMEDIASIZE, &len) == -1) {
        perror ("DIOCGMEDIASIZE");
        return 1;
    }

    printf ("%lld\n", (long long)len);

    close (fd);
    return 0;
}

------------9C16C19E114218
Content-Type: application/pgp-signature

-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAk63AsF6jCi4glqMRAiAUAJoDaKao+Uk92h943NrH6l8feHhBNgCgqFJi
UZn7ZyVMNuQft1KM0WgW5jY=
=xO8W
-----END PGP MESSAGE-----

------------9C16C19E114218--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1434443012.20040501160136>