diff -r f47e9457a7b8 src/sbin/fdisk/fdisk.c --- a/src/sbin/fdisk/fdisk.c Sun Apr 27 22:16:23 2008 +0200 +++ b/src/sbin/fdisk/fdisk.c Thu May 29 12:31:23 2008 +0200 @@ -244,7 +244,6 @@ int main(int argc, char *argv[]) { - struct stat sb; int c, i; int partition = -1; struct dos_partition *partp; @@ -301,20 +300,12 @@ argv += optind; if (argc == 0) { + printf("Using get_rootdisk!\n"); disk = get_rootdisk(); } else { - if (stat(argv[0], &sb) == 0) { - /* OK, full pathname given */ - disk = argv[0]; - } else if (errno == ENOENT && argv[0][0] != '/') { - /* Try prepending "/dev" */ - asprintf(&disk, "%s%s", _PATH_DEV, argv[0]); - if (disk == NULL) - errx(1, "out of memory"); - } else { - /* other stat error, let it fail below */ - disk = argv[0]; - } + disk = g_device_path(argv[0]); + if (disk == NULL) + err(1, "unable to get correct path for %s\n", argv[0]); } if (open_disk(u_flag) < 0) err(1, "cannot open disk %s", disk); @@ -720,21 +711,14 @@ static int open_disk(int flag) { - struct stat st; int rwmode; - if (stat(disk, &st) == -1) { - if (errno == ENOENT) - return -2; - warnx("can't get file status of %s", disk); - return -1; - } - if ( !(st.st_mode & S_IFCHR) ) - warnx("device %s is not character special", disk); - rwmode = a_flag || I_flag || B_flag || flag ? O_RDWR : O_RDONLY; - fd = open(disk, rwmode); - if (fd == -1 && errno == EPERM && rwmode == O_RDWR) - fd = open(disk, O_RDONLY); + /* Write mode if one of these flags are set. */ + rwmode = (a_flag || I_flag || B_flag || flag); + fd = g_open(disk, rwmode); + /* If the mode fails, try read-only if we didn't. */ + if (fd == -1 && errno == EPERM && rwmode) + fd = g_open(disk, 0); if (fd == -1 && errno == ENXIO) return -2; if (fd == -1) { @@ -836,21 +820,21 @@ dos_cylsecs = cylsecs = heads * sectors; disksecs = cyls * heads * sectors; - error = ioctl(fd, DIOCGSECTORSIZE, &u); - if (error != 0 || u == 0) + u = g_sectorsize(fd); + if (u <= 0) u = 512; else secsize = u; - error = ioctl(fd, DIOCGMEDIASIZE, &o); - if (error == 0) { + o = g_mediasize(fd); + if (o > -1) { + disksecs = o / u; cyls = dos_cyls = o / (u * dos_heads * dos_sectors); } return (disksecs); } - static int read_s0()