if (!S_ISBLK(statbuf.st_mode))
fatal(_("%s is not a block special device"), device);
- fd = open(device, O_RDWR);
+ fd = open(device, O_RDWR | O_EXCL);
if (fd == -1) {
perror(device);
fatal(_("cannot open %s"), device);
tmp += dirsize;
*(short *)tmp = 2;
strcpy(tmp+2,".badblocks");
- DEV = open(device_name,O_RDWR );
+ if (stat(device_name, &statbuf) < 0)
+ die(_("unable to stat %s"));
+ if (S_ISBLK(statbuf.st_mode))
+ DEV = open(device_name,O_RDWR | O_EXCL);
+ else
+ DEV = open(device_name,O_RDWR);
if (DEV<0)
die(_("unable to open %s"));
- if (fstat(DEV,&statbuf)<0)
- die(_("unable to stat %s"));
if (!S_ISBLK(statbuf.st_mode))
check=0;
else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
usage();
}
- DEV = open(device_name,O_RDWR);
- if (DEV < 0 || fstat(DEV, &statbuf) < 0) {
+ if (stat(device_name, &statbuf) < 0) {
+ perror(device_name);
+ exit(EXIT_FAILURE);
+ }
+ if (S_ISBLK(statbuf.st_mode))
+ DEV = open(device_name, O_RDWR | O_EXCL);
+ else
+ DEV = open(device_name, O_RDWR);
+
+ if (DEV < 0) {
perror(device_name);
exit(1);
}