From 1eb2f48c275a7e5f572b86c615dffb64bdfab1e8 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 16 Sep 2021 18:52:54 +0200 Subject: Add makefile and gitignore + update copyright year + m style fix + fix warnings + fix limit which had stopped working MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 7 +++++++ LICENSE | 2 +- Makefile | 24 ++++++++++++++++++++++++ limit.c | 42 ++++++++++++++++++++++++++++-------------- measure.c | 2 -- yes.c | 13 ++++++------- 6 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea677b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*\#* +*~ +*.o +*.su +/yes +/limit +/measure diff --git a/LICENSE b/LICENSE index 2553a69..d2bde98 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ ISC License -© 2017 Mattias Andrée +© 2017, 2021 Mattias Andrée Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1841767 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +.POSIX: + +CC = cc + +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE +CFLAGS = -std=c99 -Wall -O2 +LDFLAGS = -s + +BIN = yes limit measure + + +all: $(BIN) + +.o: + $(CC) -o $@ $< $(LDFLAGS) + +.c.o: + $(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS) + +clean: + rm -f -- *.o *.su $(BIN) + +.SUFFIXES: +.SUFFIXES: .c .o diff --git a/limit.c b/limit.c index 7aea33f..e7a2cb7 100644 --- a/limit.c +++ b/limit.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ - -#define _GNU_SOURCE +#include #include #include #include @@ -15,38 +14,53 @@ int -main(int argc, char *argv[]) +main(int argc, const char *argv[]) { static char buf[1 << 16]; - int rw1[2], rw2[2], n = 0, m; + int rw1[2], rw2[2], n = 0; ssize_t r, p, w; size_t sum = 0, totsum = 0; struct timespec start, end; double rate; + int capacity, flags; if (!argc) *argv = "limit"; - if (pipe(rw1) || fcntl(*rw1, F_SETPIPE_SZ, CAPACITY) != CAPACITY) + dprintf(STDERR_FILENO, "Requested capacity: %i\n", CAPACITY); + if (pipe(rw1) || pipe(rw2)) goto fail; - if (pipe(rw2) || fcntl(*rw2, F_SETPIPE_SZ, CAPACITY) != CAPACITY) + capacity = fcntl(*rw1, F_SETPIPE_SZ, CAPACITY); + if (capacity < 0 || fcntl(*rw2, F_SETPIPE_SZ, capacity) != capacity) goto fail; + dprintf(STDERR_FILENO, "Configured capacity: %i\n", capacity); - for (n = CAPACITY; n; n -= (int)r) - if ((r = write(rw1[1], buf, n)) < 0) - goto fail; + flags = fcntl(rw1[1], F_GETFL); + if (flags < 0 || fcntl(rw1[1], F_SETFL, flags |= O_NONBLOCK) < 0) + goto fail; + for (n = capacity, capacity = 0; n; n -= (int)r, capacity += (int)r) { + if ((r = write(rw1[1], buf, (size_t)n)) < 0) { + if (errno != EAGAIN) + goto fail; + n -= 1; + r = 0; + } + } + dprintf(STDERR_FILENO, "Supported capacity: %i\n", capacity); + if (fcntl(rw1[1], F_SETFL, flags ^= O_NONBLOCK) < 0) + goto fail; if (clock_gettime(CLOCK_MONOTONIC, &start)) goto fail; for (;;) { - r = splice(rw1[0], NULL, rw2[1], NULL, CAPACITY, 0); + r = splice(rw1[0], NULL, rw2[1], NULL, (size_t)capacity, 0); if (r <= 0) { if (r < 0) goto fail; goto done; } for (p = 0; p < r; p += w) { - w = splice(rw2[0], NULL, rw1[1], NULL, r - p, 0); + w = splice(rw2[0], NULL, rw1[1], NULL, (size_t)(r - p), 0); if (w <= 0) { if (w < 0) goto fail; @@ -61,10 +75,10 @@ main(int argc, char *argv[]) start.tv_sec = end.tv_sec - start.tv_sec; start.tv_nsec = end.tv_nsec - start.tv_nsec; rate = (double)start.tv_nsec; - rate /= 1000000000.; - rate += start.tv_sec; + rate /= (double)1000000000.f; + rate += (double)start.tv_sec; rate = (double)sum / rate; - rate /= 1000000000.; + rate /= (double)1000000000.f; totsum += sum; sum = 0; dprintf(STDERR_FILENO, " %lf GB/s\033[K\n\033[A", rate); diff --git a/measure.c b/measure.c index ef06c5b..767735b 100644 --- a/measure.c +++ b/measure.c @@ -1,6 +1,4 @@ /* See LICENSE file for copyright and license details. */ - -#define _GNU_SOURCE #include #include #include diff --git a/yes.c b/yes.c index 2da0ec5..cfde0ec 100644 --- a/yes.c +++ b/yes.c @@ -1,6 +1,4 @@ /* See LICENSE file for copyright and license details. */ - -#define _GNU_SOURCE #include #include #include @@ -17,7 +15,7 @@ #endif -static char *argv0; +static const char *argv0; static void (*writeall)(char *, size_t); @@ -55,8 +53,9 @@ writeall_vmsplice(char *buf, size_t n) #endif int -main(int argc, char *argv[]) +main(int argc, const char **argv) { + const char *argvbuf[3] = {NULL, "y", NULL}; size_t n, len, m = 0, p, cap = 0; char *buf, *b; int i, sz, fds[2], flags; @@ -67,7 +66,8 @@ main(int argc, char *argv[]) argv0 = argv[0] ? argv[0] : "yes"; if (argc == 1) { argc = 2; - argv = (char *[]){argv0, "y"}; + argvbuf[0] = argv0; + argv = argvbuf; } n = (size_t)(argc - 1); @@ -141,10 +141,9 @@ main(int argc, char *argv[]) } fallback: - for (p = 0;; p = (p + (size_t)r) % len) { + for (p = 0;; p = (p + (size_t)r) % len) if ((r = write(STDOUT_FILENO, buf + p, len - p)) < 0) goto fail; - } return 0; -- cgit v1.2.3-70-g09d2