X-Git-Url: https://git.wh0rd.org/?p=home.git;a=blobdiff_plain;f=ddnuke.c;h=1712d16b6f0e0c4ab49a3cf59aef9d9d0f12d1b2;hp=03ac78958fdac7daa83bd1645c172b9dc2ffbc53;hb=64b0a6dd26e21c1c3eac846738470edbe0350251;hpb=d8ceb2d61d353617ea72a576566bb969b60bf0fb diff --git a/ddnuke.c b/ddnuke.c index 03ac789..1712d16 100644 --- a/ddnuke.c +++ b/ddnuke.c @@ -11,6 +11,7 @@ #define _LARGEFILE64_SOURCE #define _GNU_SOURCE +#include #include #include #include @@ -26,12 +27,6 @@ #include #include -#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; @@ -46,7 +41,7 @@ static off_t get_blk_size(int fd) { uint64_t size; if (ioctl(fd, BLKGETSIZE64, &size)) - errp("ioctl(BLKGETSIZE64) failed"); + err(1, "ioctl(BLKGETSIZE64) failed"); return size; } @@ -55,7 +50,7 @@ static off_t get_size(int fd) 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; @@ -63,7 +58,7 @@ static off_t get_size(int fd) 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) @@ -73,6 +68,9 @@ 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); @@ -86,15 +84,16 @@ static void nuke(int fd, off_t offset, off_t max_size, unsigned char pattern) 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); @@ -111,9 +110,9 @@ static void usage(int status) status ? stderr : stdout, "Usage: ddnuke [options] \n" "\n" - "Options:" + "Options:\n" " -o 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); } @@ -135,8 +134,12 @@ int main(int argc, char *argv[]) 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) @@ -145,13 +148,10 @@ int main(int argc, char *argv[]) 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);