From: Mike Frysinger Date: Mon, 10 Dec 2018 00:21:21 +0000 (-0500) Subject: ddnuke: switch errp to err.h X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=d3bb7a4d45156ac48c01ea78f910e547552c21cd;p=home.git ddnuke: switch errp to err.h --- diff --git a/ddnuke.c b/ddnuke.c index 8e0abdb..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) @@ -74,7 +69,7 @@ static void nuke(int fd, off_t offset, off_t max_size, unsigned char pattern) printf("Writing 0x%X to the output\n", pattern); if (lseek(fd, offset, SEEK_SET) != offset) - errp("lseek(%"PRIu64"u) failed", offset); + err(1, "lseek(%"PRIu64"u) failed", offset); unsigned long long speed = 0; struct timespec stime, etime, itime; @@ -95,8 +90,8 @@ static void nuke(int fd, off_t offset, off_t max_size, unsigned char pattern) 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 % 32) == 0) { speed = mbps(&stime, &etime, pos - last_pos); @@ -139,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) @@ -149,7 +148,7 @@ 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);