1 /* vi: set sw=4 ts=4: */
3 * getsize.c --- get the size of a partition.
5 * Copyright (C) 1995, 1995 Theodore Ts'o.
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
13 /* include this before sys/queues.h! */
22 #ifdef HAVE_SYS_IOCTL_H
23 #include <sys/ioctl.h>
25 #ifdef HAVE_LINUX_FD_H
28 #ifdef HAVE_SYS_DISKLABEL_H
29 #include <sys/disklabel.h>
32 #ifdef HAVE_SYS_DISK_H
33 #ifdef HAVE_SYS_QUEUE_H
34 #include <sys/queue.h> /* for LIST_HEAD */
39 #include <sys/utsname.h>
42 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
43 #define BLKGETSIZE _IO(0x12,96) /* return device size */
46 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
47 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
51 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
52 #endif /* APPLE_DARWIN */
54 static int valid_offset(int fd
, blkid_loff_t offset
)
58 if (blkid_llseek(fd
, offset
, 0) < 0)
60 if (read(fd
, &ch
, 1) < 1)
66 * Returns the number of blocks in a partition
68 blkid_loff_t
blkid_get_dev_size(int fd
)
70 int valid_blkgetsize64
= 1;
74 unsigned long long size64
;
76 blkid_loff_t high
, low
;
78 struct floppy_struct this_floppy
;
80 #ifdef HAVE_SYS_DISKLABEL_H
86 #endif /* HAVE_SYS_DISKLABEL_H */
88 #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
89 if (ioctl(fd
, DKIOCGETBLOCKCOUNT
, &size64
) >= 0) {
90 if ((sizeof(blkid_loff_t
) < sizeof(unsigned long long))
91 && (size64
<< 9 > 0xFFFFFFFF))
93 return (blkid_loff_t
) size64
<< 9;
99 if ((uname(&ut
) == 0) &&
100 ((ut
.release
[0] == '2') && (ut
.release
[1] == '.') &&
101 (ut
.release
[2] < '6') && (ut
.release
[3] == '.')))
102 valid_blkgetsize64
= 0;
104 if (valid_blkgetsize64
&&
105 ioctl(fd
, BLKGETSIZE64
, &size64
) >= 0) {
106 if ((sizeof(blkid_loff_t
) < sizeof(unsigned long long))
107 && ((size64
) > 0xFFFFFFFF))
108 return 0; /* EFBIG */
114 if (ioctl(fd
, BLKGETSIZE
, &size
) >= 0)
115 return (blkid_loff_t
)size
<< 9;
119 if (ioctl(fd
, FDGETPRM
, &this_floppy
) >= 0)
120 return (blkid_loff_t
)this_floppy
.size
<< 9;
122 #ifdef HAVE_SYS_DISKLABEL_H
125 * This should work in theory but I haven't tested it. Anyone
126 * on a BSD system want to test this for me? In the meantime,
127 * binary search mechanism should work just fine.
129 if ((fstat(fd
, &st
) >= 0) && S_ISBLK(st
.st_mode
))
130 part
= st
.st_rdev
& 7;
131 if (part
>= 0 && (ioctl(fd
, DIOCGDINFO
, (char *)&lab
) >= 0)) {
132 pp
= &lab
.d_partitions
[part
];
134 return pp
->p_size
<< 9;
137 #endif /* HAVE_SYS_DISKLABEL_H */
140 * OK, we couldn't figure it out by using a specialized ioctl,
141 * which is generally the best way. So do binary search to
142 * find the size of the partition.
145 for (high
= 1024; valid_offset(fd
, high
); high
*= 2)
147 while (low
< high
- 1)
149 const blkid_loff_t mid
= (low
+ high
) / 2;
151 if (valid_offset(fd
, mid
))
160 int main(int argc
, char **argv
)
166 fprintf(stderr
, "Usage: %s device\n"
167 "Determine the size of a device\n", argv
[0]);
171 if ((fd
= open(argv
[1], O_RDONLY
)) < 0)
174 bytes
= blkid_get_dev_size(fd
);
175 printf("Device %s has %lld 1k blocks.\n", argv
[1], bytes
>> 10);