1 /*
2  * partinfo.c
3  *
4  * Originally written by Alain Knaff, <alknaff@innet.lu>.
5  *
6  * Cleaned up by Theodore Ts'o, <tytso@mit.edu>.
7  *
8  */
9 
10 #include "config.h"
11 #include <sys/types.h>
12 #include <fcntl.h>
13 #ifdef HAVE_SYS_IOCTL_H
14 #include <sys/ioctl.h>
15 #endif
16 #include <stdio.h>
17 #include <linux/hdreg.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <string.h>
22 #include "support/nls-enable.h"
23 #include "et/com_err.h"
24 
25 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
26 #define BLKGETSIZE _IO(0x12,96)	/* return device size */
27 #endif
28 
main(int argc,char ** argv)29 int main(int argc, char **argv)
30 {
31 	struct hd_geometry loc;
32 	int fd, i;
33 	unsigned long size;
34 
35 #ifdef ENABLE_NLS
36 	setlocale(LC_MESSAGES, "");
37 	setlocale(LC_CTYPE, "");
38 	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
39 	textdomain(NLS_CAT_NAME);
40 	set_com_err_gettext(gettext);
41 #endif
42 	if (argc == 1) {
43 		fprintf(stderr, _("Usage:  %s device...\n\nPrints out the "
44 			"partition information for each given device.\n"
45 			"For example: %s /dev/hda\n\n"), argv[0], argv[0]);
46 		exit(1);
47 	}
48 
49 	for (i=1; i < argc; i++) {
50 		fd = open(argv[i], O_RDONLY);
51 
52 		if (fd < 0) {
53 			fprintf(stderr, _("Cannot open %s: %s"),
54 				argv[i], strerror(errno));
55 			continue;
56 		}
57 
58 		if (ioctl(fd, HDIO_GETGEO, &loc) < 0) {
59 			fprintf(stderr, _("Cannot get geometry of %s: %s"),
60 				argv[i], strerror(errno));
61 			close(fd);
62 			continue;
63 		}
64 
65 
66 		if (ioctl(fd, BLKGETSIZE, &size) < 0) {
67 			fprintf(stderr, _("Cannot get size of %s: %s"),
68 				argv[i], strerror(errno));
69 			close(fd);
70 			continue;
71 		}
72 
73 		printf(_("%s: h=%3d s=%3d c=%4d   start=%8d size=%8lu end=%8d\n"),
74 		       argv[i],
75 		       loc.heads, (int)loc.sectors, loc.cylinders,
76 		       (int)loc.start, size, (int) loc.start + size -1);
77 		close(fd);
78 	}
79 	exit(0);
80 }
81