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