blob: 59616549ae4c9108697f3a071fb73cd3ea81d510 [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>.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04007 *
Theodore Ts'o134ea281997-11-28 14:45:09 +00008 */
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 +000026int main(int argc, char **argv)
27{
28 struct hd_geometry loc;
Theodore Ts'oe9271681998-03-09 03:23:51 +000029 int fd, i;
Theodore Ts'oa9bc79a2001-05-23 22:29:22 +000030 unsigned long size;
Theodore Ts'o134ea281997-11-28 14:45:09 +000031
Theodore Ts'od9c56d32000-02-08 00:47:55 +000032#ifdef ENABLE_NLS
33 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -050034 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +000035 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
36 textdomain(NLS_CAT_NAME);
JP Abgralle0ed7402014-03-19 19:08:39 -070037 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +000038#endif
Theodore Ts'o134ea281997-11-28 14:45:09 +000039 if (argc == 1) {
Theodore Ts'odf9c01b2008-07-17 10:50:26 -040040 fprintf(stderr, _("Usage: %s device...\n\nPrints out the "
Theodore Ts'ofe365fd2008-07-23 16:50:04 -040041 "partition information for each given device.\n"
42 "For example: %s /dev/hda\n\n"), argv[0], argv[0]);
Theodore Ts'o134ea281997-11-28 14:45:09 +000043 exit(1);
44 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040045
Theodore Ts'o134ea281997-11-28 14:45:09 +000046 for (i=1; i < argc; i++) {
47 fd = open(argv[i], O_RDONLY);
48
49 if (fd < 0) {
Benno Schulenbergcca95a82008-07-17 23:46:49 +020050 fprintf(stderr, _("Cannot open %s: %s"),
51 argv[i], strerror(errno));
Theodore Ts'o134ea281997-11-28 14:45:09 +000052 continue;
53 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040054
Theodore Ts'o134ea281997-11-28 14:45:09 +000055 if (ioctl(fd, HDIO_GETGEO, &loc) < 0) {
Benno Schulenbergcca95a82008-07-17 23:46:49 +020056 fprintf(stderr, _("Cannot get geometry of %s: %s"),
57 argv[i], strerror(errno));
Theodore Ts'o134ea281997-11-28 14:45:09 +000058 close(fd);
59 continue;
60 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040061
62
Theodore Ts'o134ea281997-11-28 14:45:09 +000063 if (ioctl(fd, BLKGETSIZE, &size) < 0) {
Benno Schulenbergcca95a82008-07-17 23:46:49 +020064 fprintf(stderr, _("Cannot get size of %s: %s"),
65 argv[i], strerror(errno));
Theodore Ts'o134ea281997-11-28 14:45:09 +000066 close(fd);
67 continue;
68 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040069
Benno Schulenbergcca95a82008-07-17 23:46:49 +020070 printf(_("%s: h=%3d s=%3d c=%4d start=%8d size=%8lu end=%8d\n"),
Theodore Ts'oefc6f622008-08-27 23:07:54 -040071 argv[i],
Theodore Ts'o134ea281997-11-28 14:45:09 +000072 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}