summaryrefslogtreecommitdiff
path: root/util/sbase/tail.c
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2025-10-04 09:14:33 +0100
committerLeah Rowe <leah@libreboot.org>2025-10-04 09:20:12 +0100
commite9a910b33c7837b4b868e3abda18eb4810df7f02 (patch)
tree749e1830cb0607952df1a1afc0ae09ec1db54140 /util/sbase/tail.c
parent2cfaba181b3c68761871fa47b32725c934423c14 (diff)
config/git: import suckless sbase
i currently use the output of sha512sum in several places of xbmk, which is a bit unreliable in case output changes. other cases where i use util outputs in variables are probably reliable, because i'm using mostly posix utilities in those. to mitigate this, i now import suckless sbase, which has a reasonable sha512sum implementation. *every* binary it builds is being placed in build.list, because i'll probably start using more of them. for example, i may start modifying the "date" implementation, adding the GNU-specific options that i need as mentioned on init.sh i'm importing it in util/ because the sha512sum util is needed for verifying project sources, so if sbase itself is a "project source", that means we can into a chicken and egg bootstrapping problem. this is sbase at revision: 055cc1ae1b3a13c3d8f25af0a4a3316590efcd48 Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/sbase/tail.c')
-rw-r--r--util/sbase/tail.c229
1 files changed, 229 insertions, 0 deletions
diff --git a/util/sbase/tail.c b/util/sbase/tail.c
new file mode 100644
index 00000000..bbc5ad58
--- /dev/null
+++ b/util/sbase/tail.c
@@ -0,0 +1,229 @@
+/* See LICENSE file for copyright and license details. */
+#include <sys/stat.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "utf.h"
+#include "util.h"
+
+static char mode = 'n';
+
+static int
+dropinit(int fd, const char *fname, size_t count)
+{
+ Rune r;
+ char buf[BUFSIZ], *p;
+ ssize_t n;
+ int nr;
+
+ if (count < 2)
+ goto copy;
+ count--; /* numbering starts at 1 */
+ while (count && (n = read(fd, buf, sizeof(buf))) > 0) {
+ switch (mode) {
+ case 'n': /* lines */
+ for (p = buf; count && n > 0; p++, n--) {
+ if (*p == '\n')
+ count--;
+ }
+ break;
+ case 'c': /* bytes */
+ if (count > n) {
+ count -= n;
+ } else {
+ p = buf + count;
+ n -= count;
+ count = 0;
+ }
+ break;
+ case 'm': /* runes */
+ for (p = buf; count && n > 0; p += nr, n -= nr, count--) {
+ nr = charntorune(&r, p, n);
+ if (!nr) {
+ /* we don't have a full rune, move
+ * remaining data to beginning and read
+ * again */
+ memmove(buf, p, n);
+ break;
+ }
+ }
+ break;
+ }
+ }
+ if (count) {
+ if (n < 0)
+ weprintf("read %s:", fname);
+ if (n <= 0)
+ return n;
+ }
+
+ /* write the rest of the buffer */
+ if (writeall(1, p, n) < 0)
+ eprintf("write:");
+copy:
+ switch (concat(fd, fname, 1, "<stdout>")) {
+ case -1: /* read error */
+ return -1;
+ case -2: /* write error */
+ exit(1);
+ default:
+ return 0;
+ }
+}
+
+static int
+taketail(int fd, const char *fname, size_t count)
+{
+ static char *buf = NULL;
+ static size_t size = 0;
+ char *p;
+ size_t len = 0, left;
+ ssize_t n;
+
+ if (!count)
+ return 0;
+ for (;;) {
+ if (len + BUFSIZ > size) {
+ /* make sure we have at least BUFSIZ to read */
+ size += 2 * BUFSIZ;
+ buf = erealloc(buf, size);
+ }
+ n = read(fd, buf + len, size - len);
+ if (n < 0) {
+ weprintf("read %s:", fname);
+ return -1;
+ }
+ if (n == 0)
+ break;
+ len += n;
+ switch (mode) {
+ case 'n': /* lines */
+ /* ignore the last character; if it is a newline, it
+ * ends the last line */
+ for (p = buf + len - 2, left = count; p >= buf; p--) {
+ if (*p != '\n')
+ continue;
+ left--;
+ if (!left) {
+ p++;
+ break;
+ }
+ }
+ break;
+ case 'c': /* bytes */
+ p = count < len ? buf + len - count : buf;
+ break;
+ case 'm': /* runes */
+ for (p = buf + len - 1, left = count; p >= buf; p--) {
+ /* skip utf-8 continuation bytes */
+ if (UTF8_POINT(*p))
+ continue;
+ left--;
+ if (!left)
+ break;
+ }
+ break;
+ }
+ if (p > buf) {
+ len -= p - buf;
+ memmove(buf, p, len);
+ }
+ }
+ if (writeall(1, buf, len) < 0)
+ eprintf("write:");
+ return 0;
+}
+
+static void
+usage(void)
+{
+ eprintf("usage: %s [-f] [-c num | -m num | -n num | -num] [file ...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct stat st1, st2;
+ int fd;
+ size_t n = 10;
+ int fflag = 0, ret = 0, newline = 0, many = 0;
+ char *numstr;
+ int (*tail)(int, const char *, size_t) = taketail;
+
+ ARGBEGIN {
+ case 'f':
+ fflag = 1;
+ break;
+ case 'c':
+ case 'm':
+ case 'n':
+ mode = ARGC();
+ numstr = EARGF(usage());
+ n = MIN(llabs(estrtonum(numstr, LLONG_MIN + 1,
+ MIN(LLONG_MAX, SIZE_MAX))), SIZE_MAX);
+ if (strchr(numstr, '+'))
+ tail = dropinit;
+ break;
+ ARGNUM:
+ n = ARGNUMF();
+ break;
+ default:
+ usage();
+ } ARGEND
+
+ if (!argc) {
+ if (tail(0, "<stdin>", n) < 0)
+ ret = 1;
+ } else {
+ if ((many = argc > 1) && fflag)
+ usage();
+ for (newline = 0; *argv; argc--, argv++) {
+ if (!strcmp(*argv, "-")) {
+ *argv = "<stdin>";
+ fd = 0;
+ } else if ((fd = open(*argv, O_RDONLY)) < 0) {
+ weprintf("open %s:", *argv);
+ ret = 1;
+ continue;
+ }
+ if (many)
+ printf("%s==> %s <==\n", newline ? "\n" : "", *argv);
+ if (fstat(fd, &st1) < 0)
+ eprintf("fstat %s:", *argv);
+ if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode)))
+ fflag = 0;
+ newline = 1;
+ if (tail(fd, *argv, n) < 0) {
+ ret = 1;
+ fflag = 0;
+ }
+
+ if (!fflag) {
+ if (fd != 0)
+ close(fd);
+ continue;
+ }
+ for (;;) {
+ if (concat(fd, *argv, 1, "<stdout>") < 0)
+ exit(1);
+ if (fstat(fd, &st2) < 0)
+ eprintf("fstat %s:", *argv);
+ if (st2.st_size < st1.st_size) {
+ fprintf(stderr, "%s: file truncated\n", *argv);
+ if (lseek(fd, SEEK_SET, 0) < 0)
+ eprintf("lseek:");
+ }
+ st1 = st2;
+ sleep(1);
+ }
+ }
+ }
+
+ return ret;
+}