#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE
+#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <linux/fs.h>
-#define errp(msg, args...) \
- do { \
- printf("%s: " msg ": %m\n", program_invocation_short_name, ## args); \
- exit(1); \
- } while (0)
-
static unsigned long long mbps(struct timespec *stime, struct timespec *etime, off_t len)
{
uint64_t dtime;
{
uint64_t size;
if (ioctl(fd, BLKGETSIZE64, &size))
- errp("ioctl(BLKGETSIZE64) failed");
+ err(1, "ioctl(BLKGETSIZE64) failed");
return size;
}
struct stat st;
if (fstat(fd, &st))
- errp("could not stat %i", fd);
+ err(1, "could not stat %i", fd);
if (S_ISREG(st.st_mode))
return st.st_size;
return get_blk_size(fd);
errno = EINVAL;
- errp("unknown type of file");
+ err(1, "unknown type of file");
}
static void nuke(int fd, off_t offset, off_t max_size, unsigned char pattern)
if (pattern)
printf("Writing 0x%X to the output\n", pattern);
+ if (lseek(fd, offset, SEEK_SET) != offset)
+ err(1, "lseek(%"PRIu64"u) failed", offset);
+
unsigned long long speed = 0;
struct timespec stime, etime, itime;
clock_gettime(CLOCK_MONOTONIC, &stime);
ret = write(fd, mem, sizeof(mem));
pos += ret;
if (ret != sizeof(mem)) {
- printf("\nread() returned %zi (wanted %zu)\n%'llu MB/s over entire run\n",
- ret, sizeof(mem), mbps(&itime, &etime, pos));
+ if (pos != max_size)
+ printf("\nread() returned %zi (wanted %zu)\n%'llu MB/s over entire run\n",
+ ret, sizeof(mem), mbps(&itime, &etime, pos));
return;
}
- printf("%'llu bytes %u%% (%'llu MB/s)%20s\r", (unsigned long long)pos,
- (unsigned)((pos * 100) / max_size), speed, "");
+ printf("\r\e[2K%'llu bytes %u%% (%'llu MB/s)", (unsigned long long)pos,
+ (unsigned)((pos * 100) / max_size), speed);
- if ((++fsync_pos % 16) == 0) {
+ if ((++fsync_pos % 32) == 0) {
speed = mbps(&stime, &etime, pos - last_pos);
last_pos = pos;
fflush(stdout);
status ? stderr : stdout,
"Usage: ddnuke [options] <dev>\n"
"\n"
- "Options:"
+ "Options:\n"
" -o <offset> Position to start writing\n"
- " -r Write random garbage to the device\n"
+ " -r Write three times: 0x00, then 0xaa, then 0x55\n"
);
exit(status);
}
case 'r':
random = true;
break;
- case 'h': usage(EX_OK);
- default: usage(EX_USAGE);
+ case 'h':
+ usage(EX_OK);
+ break;
+ default:
+ usage(EX_USAGE);
+ break;
}
}
if (argc != optind + 1)
int fd = open(file, O_WRONLY|O_CLOEXEC);
if (fd == -1)
- errp("open(%s) failed", file);
+ err(1, "open(%s) failed", file);
off_t max_size = get_size(fd);
- if (lseek(fd, offset, SEEK_SET) != offset)
- errp("lseek(%"PRIu64"u) failed", offset);
-
nuke(fd, offset, max_size, 0x00);
if (random) {
nuke(fd, offset, max_size, 0xaa);