• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
print_error(char * operation,int error,char * device)26 void print_error(char *operation, int error, char *device)
27 {
28 	fprintf(stderr, _("%s failed for %s: %s\n"), operation, device,
29 		strerror(error));
30 }
31 
main(int argc,char ** argv)32 int main(int argc, char **argv)
33 {
34 	struct hd_geometry loc;
35 	int fd, i;
36 	unsigned long size;
37 
38 #ifdef ENABLE_NLS
39 	setlocale(LC_MESSAGES, "");
40 	setlocale(LC_CTYPE, "");
41 	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
42 	textdomain(NLS_CAT_NAME);
43 #endif
44 	if (argc == 1) {
45 		fprintf(stderr, _("Usage:  %s device...\n\nPrints out the"
46 			"partition information for each given device.\n"),
47 			"For example: %s /dev/hda\n\n", argv[0], argv[0]);
48 		exit(1);
49 	}
50 
51 	for (i=1; i < argc; i++) {
52 		fd = open(argv[i], O_RDONLY);
53 
54 		if (fd < 0) {
55 			print_error(_("open"), errno, argv[i]);
56 			continue;
57 		}
58 
59 		if (ioctl(fd, HDIO_GETGEO, &loc) < 0) {
60 			print_error(_("HDIO_GETGEO ioctl"), errno, argv[i]);
61 			close(fd);
62 			continue;
63 		}
64 
65 
66 		if (ioctl(fd, BLKGETSIZE, &size) < 0) {
67 			print_error(_("BLKGETSIZE ioctl"), errno, argv[i]);
68 			close(fd);
69 			continue;
70 		}
71 
72 		printf("%s: h=%3d s=%3d c=%4d   start=%8d size=%8lu end=%8d\n",
73 		       argv[i],
74 		       loc.heads, (int)loc.sectors, loc.cylinders,
75 		       (int)loc.start, size, (int) loc.start + size -1);
76 		close(fd);
77 	}
78 	exit(0);
79 }
80