blob: 48b395f6ec9bae6de96fe2124b09f94920fb9766 [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);
37#endif
Theodore Ts'o134ea281997-11-28 14:45:09 +000038 if (argc == 1) {
Theodore Ts'odf9c01b2008-07-17 10:50:26 -040039 fprintf(stderr, _("Usage: %s device...\n\nPrints out the "
Theodore Ts'ofe365fd2008-07-23 16:50:04 -040040 "partition information for each given device.\n"
41 "For example: %s /dev/hda\n\n"), argv[0], argv[0]);
Theodore Ts'o134ea281997-11-28 14:45:09 +000042 exit(1);
43 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040044
Theodore Ts'o134ea281997-11-28 14:45:09 +000045 for (i=1; i < argc; i++) {
46 fd = open(argv[i], O_RDONLY);
47
48 if (fd < 0) {
Benno Schulenbergcca95a82008-07-17 23:46:49 +020049 fprintf(stderr, _("Cannot open %s: %s"),
50 argv[i], strerror(errno));
Theodore Ts'o134ea281997-11-28 14:45:09 +000051 continue;
52 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040053
Theodore Ts'o134ea281997-11-28 14:45:09 +000054 if (ioctl(fd, HDIO_GETGEO, &loc) < 0) {
Benno Schulenbergcca95a82008-07-17 23:46:49 +020055 fprintf(stderr, _("Cannot get geometry of %s: %s"),
56 argv[i], strerror(errno));
Theodore Ts'o134ea281997-11-28 14:45:09 +000057 close(fd);
58 continue;
59 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040060
61
Theodore Ts'o134ea281997-11-28 14:45:09 +000062 if (ioctl(fd, BLKGETSIZE, &size) < 0) {
Benno Schulenbergcca95a82008-07-17 23:46:49 +020063 fprintf(stderr, _("Cannot get size of %s: %s"),
64 argv[i], strerror(errno));
Theodore Ts'o134ea281997-11-28 14:45:09 +000065 close(fd);
66 continue;
67 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040068
Benno Schulenbergcca95a82008-07-17 23:46:49 +020069 printf(_("%s: h=%3d s=%3d c=%4d start=%8d size=%8lu end=%8d\n"),
Theodore Ts'oefc6f622008-08-27 23:07:54 -040070 argv[i],
Theodore Ts'o134ea281997-11-28 14:45:09 +000071 loc.heads, (int)loc.sectors, loc.cylinders,
72 (int)loc.start, size, (int) loc.start + size -1);
73 close(fd);
74 }
75 exit(0);
76}