------- Original Message -------
On Friday, September 8th, 2023 at 2:38 AM, Rob Landley <r...@landley.net> wrote:
> On 9/7/23 23:51, Oliver Webb via Toybox wrote:
>
> > I wrote a implementation of the command 'ts' for toybox
>
To clarify my earlier emails, I normally wouldn't submit a command this obscure,
if it wasn't mentioned (Very briefly, but mentioned) in the roadmap page of the
project.
I have improved the source code of ts to fix a memory leak, and remove the
malloc by just doing what 'date' does and writing to 'toybuf'. I also added
another reference page that <scsi...@lamiaworks.com> linked
Since submitting a patch to patch a currently pending patch seems wrong, I just
made a new standalone one.
From a9e4444157cfa9292fdd9474d455de9347a18f9d Mon Sep 17 00:00:00 2001
From: Oliver Webb <aquahobby...@proton.me>
Date: Sat, 9 Sep 2023 11:11:51 -0500
Subject: [PATCH] A improved implementation of the ts command
---
toys/pending/ts.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)
create mode 100644 toys/pending/ts.c
diff --git a/toys/pending/ts.c b/toys/pending/ts.c
new file mode 100644
index 00000000..8b7c5816
--- /dev/null
+++ b/toys/pending/ts.c
@@ -0,0 +1,48 @@
+/* ts.c - timestamp input lines
+ *
+ * Copyright 2023 Oliver Webb <aquahobby...@proton.me>
+ *
+ * See https://linux.die.net/man/1/ts
+ * Or https://linuxcommandlibrary.com/man/ts
+
+USE_TS(NEWTOY(ts, "i", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_MAYFORK))
+
+config TS
+ bool "ts"
+ default n
+ help
+ usage: ts [-i] [FORMAT]
+
+ timestamp input using strftime(3)
+ The default timestamp formatting is "%b %d %H:%M:%S"
+ If -i is specified, the formatting is Changed to "%H:%M:%S"
+ -i Timestamps Incrementally Starting at 0
+*/
+
+#define FOR_ts
+#include "toys.h"
+
+static time_t starttime;
+static char *format;
+
+static char *getftime(void)
+{
+ time_t curtime = time(NULL);
+ struct tm *submtime;
+ if (FLAG(i)) {
+ curtime -= starttime;
+ submtime = gmtime(&curtime);
+ } else submtime = localtime(&curtime);
+ if(!strftime(toybuf,sizeof(toybuf)-12,format,submtime))
+ perror_exit("bad format '%s'", format);
+ return toybuf;
+}
+
+void ts_main(void)
+{
+ starttime = time(NULL);
+ if (FLAG(i)) format = "%T"; else format = "%b %d %T";
+ if (toys.optargs[0]) format = toys.optargs[0];
+ for (char *line;(line = xgetline(stdin));free(line))
+ xprintf("%s %s\n",getftime(),line);
+}
--
2.34.1
_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net