| 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); |
| 37 | #endif |
| Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 38 | if (argc == 1) { |
| Theodore Ts'o | df9c01b | 2008-07-17 10:50:26 -0400 | [diff] [blame] | 39 | fprintf(stderr, _("Usage: %s device...\n\nPrints out the " |
| Theodore Ts'o | fe365fd | 2008-07-23 16:50:04 -0400 | [diff] [blame] | 40 | "partition information for each given device.\n" |
| 41 | "For example: %s /dev/hda\n\n"), argv[0], argv[0]); |
| Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 42 | exit(1); |
| 43 | } |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 44 | |
| Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 45 | for (i=1; i < argc; i++) { |
| 46 | fd = open(argv[i], O_RDONLY); |
| 47 | |
| 48 | if (fd < 0) { |
| Benno Schulenberg | cca95a8 | 2008-07-17 23:46:49 +0200 | [diff] [blame] | 49 | fprintf(stderr, _("Cannot open %s: %s"), |
| 50 | argv[i], strerror(errno)); |
| Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 51 | continue; |
| 52 | } |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 53 | |
| Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 54 | if (ioctl(fd, HDIO_GETGEO, &loc) < 0) { |
| Benno Schulenberg | cca95a8 | 2008-07-17 23:46:49 +0200 | [diff] [blame] | 55 | fprintf(stderr, _("Cannot get geometry of %s: %s"), |
| 56 | argv[i], strerror(errno)); |
| Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 57 | close(fd); |
| 58 | continue; |
| 59 | } |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 60 | |
| 61 | |
| Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 62 | if (ioctl(fd, BLKGETSIZE, &size) < 0) { |
| Benno Schulenberg | cca95a8 | 2008-07-17 23:46:49 +0200 | [diff] [blame] | 63 | fprintf(stderr, _("Cannot get size of %s: %s"), |
| 64 | argv[i], strerror(errno)); |
| Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 65 | close(fd); |
| 66 | continue; |
| 67 | } |
| Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 68 | |
| Benno Schulenberg | cca95a8 | 2008-07-17 23:46:49 +0200 | [diff] [blame] | 69 | 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] | 70 | argv[i], |
| Theodore Ts'o | 134ea28 | 1997-11-28 14:45:09 +0000 | [diff] [blame] | 71 | 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 | } |