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>. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <sys/types.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <stdio.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/hdreg.h> |
| 15 | #include <unistd.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | void print_error(char *operation, int error, char *device) |
| 19 | { |
| 20 | fprintf(stderr, "%s failed for %s: %s\n", operation, device, |
| 21 | strerror(error)); |
| 22 | } |
| 23 | |
| 24 | int main(int argc, char **argv) |
| 25 | { |
| 26 | struct hd_geometry loc; |
| 27 | int fd, size, i; |
| 28 | |
| 29 | if (argc == 1) { |
| 30 | fprintf(stderr, "Usage: %s <dev1> <dev2> <dev3>\n\n" |
| 31 | "This program prints out the partition information " |
| 32 | "for a set of devices\n" |
| 33 | "A common way to use this progrma is:\n\n\t" |
| 34 | "%s /dev/hda?\n\n", argv[0], argv[0]); |
| 35 | exit(1); |
| 36 | } |
| 37 | |
| 38 | for (i=1; i < argc; i++) { |
| 39 | fd = open(argv[i], O_RDONLY); |
| 40 | |
| 41 | if (fd < 0) { |
| 42 | print_error("open", errno, argv[1]); |
| 43 | continue; |
| 44 | } |
| 45 | |
| 46 | if (ioctl(fd, HDIO_GETGEO, &loc) < 0) { |
| 47 | print_error("HDIO_GETGEO ioctl", errno, argv[1]); |
| 48 | close(fd); |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | if (ioctl(fd, BLKGETSIZE, &size) < 0) { |
| 54 | print_error("BLKGETSIZE ioctl", errno, argv[1]); |
| 55 | close(fd); |
| 56 | continue; |
| 57 | } |
| 58 | |
| 59 | printf("%s: h=%3d s=%3d c=%4d start=%8d size=%8d end=%8d\n", |
| 60 | argv[i], |
| 61 | loc.heads, (int)loc.sectors, loc.cylinders, |
| 62 | (int)loc.start, size, (int) loc.start + size -1); |
| 63 | close(fd); |
| 64 | } |
| 65 | exit(0); |
| 66 | } |