Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 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>. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 7 | * |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <sys/types.h> |
| 11 | #include <fcntl.h> |
Theodore Ts'o | bb145b0 | 2005-06-20 08:35:27 -0400 | [diff] [blame] | 12 | #ifdef HAVE_SYS_IOCTL_H |
| 13 | #include <sys/ioctl.h> |
| 14 | #endif |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 16 | #include <linux/hdreg.h> |
| 17 | #include <unistd.h> |
| 18 | #include <stdlib.h> |
Theodore Ts'o | 2740156 | 1999-09-14 20:11:19 +0000 | [diff] [blame] | 19 | #include <errno.h> |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 20 | #include "nls-enable.h" |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 21 | |
Theodore Ts'o | bb145b0 | 2005-06-20 08:35:27 -0400 | [diff] [blame] | 22 | #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE) |
| 23 | #define BLKGETSIZE _IO(0x12,96) /* return device size */ |
| 24 | #endif |
| 25 | |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 26 | int main(int argc, char **argv) |
| 27 | { |
| 28 | struct hd_geometry loc; |
Theodore Ts'o | e927168 | 1998-03-09 03:23:51 +0000 | [diff] [blame] | 29 | int fd, i; |
Theodore Ts'o | a9bc79a | 2001-05-23 22:29:22 +0000 | [diff] [blame] | 30 | unsigned long size; |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 31 | |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 32 | #ifdef ENABLE_NLS |
| 33 | setlocale(LC_MESSAGES, ""); |
Theodore Ts'o | 14308a5 | 2002-03-05 03:26:52 -0500 | [diff] [blame] | 34 | setlocale(LC_CTYPE, ""); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 35 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 36 | textdomain(NLS_CAT_NAME); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 37 | set_com_err_gettext(gettext); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 38 | #endif |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 39 | if (argc == 1) { |
Theodore Ts'o | df9c01b | 2008-07-17 10:50:26 -0400 | [diff] [blame] | 40 | fprintf(stderr, _("Usage: %s device...\n\nPrints out the " |
Theodore Ts'o | fe365fd | 2008-07-23 16:50:04 -0400 | [diff] [blame] | 41 | "partition information for each given device.\n" |
| 42 | "For example: %s /dev/hda\n\n"), argv[0], argv[0]); |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 43 | exit(1); |
| 44 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 45 | |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 46 | for (i=1; i < argc; i++) { |
| 47 | fd = open(argv[i], O_RDONLY); |
| 48 | |
| 49 | if (fd < 0) { |
Benno Schulenberg | cca95a8 | 2008-07-17 23:46:49 +0200 | [diff] [blame] | 50 | fprintf(stderr, _("Cannot open %s: %s"), |
| 51 | argv[i], strerror(errno)); |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 52 | continue; |
| 53 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 54 | |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 55 | if (ioctl(fd, HDIO_GETGEO, &loc) < 0) { |
Benno Schulenberg | cca95a8 | 2008-07-17 23:46:49 +0200 | [diff] [blame] | 56 | fprintf(stderr, _("Cannot get geometry of %s: %s"), |
| 57 | argv[i], strerror(errno)); |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 58 | close(fd); |
| 59 | continue; |
| 60 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 61 | |
| 62 | |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 63 | if (ioctl(fd, BLKGETSIZE, &size) < 0) { |
Benno Schulenberg | cca95a8 | 2008-07-17 23:46:49 +0200 | [diff] [blame] | 64 | fprintf(stderr, _("Cannot get size of %s: %s"), |
| 65 | argv[i], strerror(errno)); |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 66 | close(fd); |
| 67 | continue; |
| 68 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 69 | |
Benno Schulenberg | cca95a8 | 2008-07-17 23:46:49 +0200 | [diff] [blame] | 70 | printf(_("%s: h=%3d s=%3d c=%4d start=%8d size=%8lu end=%8d\n"), |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 71 | argv[i], |
Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 72 | 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 | } |