blob: f1ec8794545867abfca4dcd1c963091194829f3b [file] [log] [blame]
Theodore Ts'o134ea281997-11-28 14:45:09 +00001/*
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>
Theodore Ts'obb145b02005-06-20 08:35:27 -040012#ifdef HAVE_SYS_IOCTL_H
13#include <sys/ioctl.h>
14#endif
Theodore Ts'o134ea281997-11-28 14:45:09 +000015#include <stdio.h>
Theodore Ts'o134ea281997-11-28 14:45:09 +000016#include <linux/hdreg.h>
17#include <unistd.h>
18#include <stdlib.h>
Theodore Ts'o27401561999-09-14 20:11:19 +000019#include <errno.h>
Theodore Ts'od9c56d32000-02-08 00:47:55 +000020#include "nls-enable.h"
Theodore Ts'o134ea281997-11-28 14:45:09 +000021
Theodore Ts'obb145b02005-06-20 08:35:27 -040022#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
23#define BLKGETSIZE _IO(0x12,96) /* return device size */
24#endif
25
Theodore Ts'o134ea281997-11-28 14:45:09 +000026void print_error(char *operation, int error, char *device)
27{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000028 fprintf(stderr, _("%s failed for %s: %s\n"), operation, device,
Theodore Ts'o134ea281997-11-28 14:45:09 +000029 strerror(error));
30}
31
32int main(int argc, char **argv)
33{
34 struct hd_geometry loc;
Theodore Ts'oe9271681998-03-09 03:23:51 +000035 int fd, i;
Theodore Ts'oa9bc79a2001-05-23 22:29:22 +000036 unsigned long size;
Theodore Ts'o134ea281997-11-28 14:45:09 +000037
Theodore Ts'od9c56d32000-02-08 00:47:55 +000038#ifdef ENABLE_NLS
39 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -050040 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +000041 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
42 textdomain(NLS_CAT_NAME);
43#endif
Theodore Ts'o134ea281997-11-28 14:45:09 +000044 if (argc == 1) {
Theodore Ts'obb145b02005-06-20 08:35:27 -040045 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]);
Theodore Ts'o134ea281997-11-28 14:45:09 +000048 exit(1);
49 }
50
51 for (i=1; i < argc; i++) {
52 fd = open(argv[i], O_RDONLY);
53
54 if (fd < 0) {
Theodore Ts'obb145b02005-06-20 08:35:27 -040055 print_error(_("open"), errno, argv[i]);
Theodore Ts'o134ea281997-11-28 14:45:09 +000056 continue;
57 }
58
59 if (ioctl(fd, HDIO_GETGEO, &loc) < 0) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +000060 print_error(_("HDIO_GETGEO ioctl"), errno, argv[i]);
Theodore Ts'o134ea281997-11-28 14:45:09 +000061 close(fd);
62 continue;
63 }
64
65
66 if (ioctl(fd, BLKGETSIZE, &size) < 0) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +000067 print_error(_("BLKGETSIZE ioctl"), errno, argv[i]);
Theodore Ts'o134ea281997-11-28 14:45:09 +000068 close(fd);
69 continue;
70 }
71
Theodore Ts'oa9bc79a2001-05-23 22:29:22 +000072 printf("%s: h=%3d s=%3d c=%4d start=%8d size=%8lu end=%8d\n",
Theodore Ts'o134ea281997-11-28 14:45:09 +000073 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}