xref: /minix3/minix/commands/devsize/devsize.c (revision d0055759dd8892194db7fce6acc5085d5c9aeaee)
1433d6423SLionel Sambuc /* Ben Gras
2433d6423SLionel Sambuc  *
3433d6423SLionel Sambuc  * Based on sizeup() in mkfs.c.
4433d6423SLionel Sambuc  */
5433d6423SLionel Sambuc 
6433d6423SLionel Sambuc #include <sys/types.h>
7433d6423SLionel Sambuc #include <sys/stat.h>
8433d6423SLionel Sambuc #include <machine/partition.h>
9433d6423SLionel Sambuc #include <minix/partition.h>
10*d0055759SDavid van Moolenbroek #include <sys/ioctl.h>
11433d6423SLionel Sambuc #include <stdio.h>
12433d6423SLionel Sambuc #include <errno.h>
13433d6423SLionel Sambuc #include <fcntl.h>
14433d6423SLionel Sambuc #include <limits.h>
15433d6423SLionel Sambuc #include <stdlib.h>
16433d6423SLionel Sambuc #include <string.h>
17433d6423SLionel Sambuc #include <time.h>
18433d6423SLionel Sambuc #include <unistd.h>
19433d6423SLionel Sambuc 
20433d6423SLionel Sambuc unsigned long sizeup(char *);
21433d6423SLionel Sambuc 
main(int argc,char * argv[])22433d6423SLionel Sambuc int main(int argc, char *argv[])
23433d6423SLionel Sambuc {
24433d6423SLionel Sambuc   if(argc != 2) {
25433d6423SLionel Sambuc 	fprintf(stderr, "Usage: %s <device>\n", argv[0]);
26433d6423SLionel Sambuc 	return 1;
27433d6423SLionel Sambuc   }
28433d6423SLionel Sambuc 
29433d6423SLionel Sambuc   printf("%lu\n", sizeup(argv[1]));
30433d6423SLionel Sambuc   return 0;
31433d6423SLionel Sambuc }
32433d6423SLionel Sambuc 
33433d6423SLionel Sambuc 
sizeup(char * device)34433d6423SLionel Sambuc unsigned long sizeup(char *device)
35433d6423SLionel Sambuc {
36433d6423SLionel Sambuc   int fd;
37433d6423SLionel Sambuc   struct part_geom entry;
38433d6423SLionel Sambuc   unsigned long d;
39433d6423SLionel Sambuc 
40433d6423SLionel Sambuc   if ((fd = open(device, O_RDONLY)) == -1) {
41433d6423SLionel Sambuc   	perror("sizeup open");
42433d6423SLionel Sambuc   	exit(1);
43433d6423SLionel Sambuc   }
44433d6423SLionel Sambuc   if (ioctl(fd, DIOCGETP, &entry) == -1) {
45433d6423SLionel Sambuc   	perror("sizeup ioctl");
46433d6423SLionel Sambuc   	exit(1);
47433d6423SLionel Sambuc   }
48433d6423SLionel Sambuc   close(fd);
49433d6423SLionel Sambuc   d = entry.size / 512;
50433d6423SLionel Sambuc   return d;
51433d6423SLionel Sambuc }
52