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