| t@@ -0,0 +1,15 @@
+ISC License
+
+Copyright (c) 2020 Anders Damsgaard
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| t@@ -0,0 +1,105 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+void
+usage(char *argv0)
+{
+ errx(1, "usage: %s [-p prefix] [-P postfix] [-i interval] [-x]", argv0);
+}
+
+void
+format_time(char *out, size_t outsiz, time_t t_elapsed, char *prefix, char *postfix)
+{
+ time_t h = 0, m = 0, s = 0;
+
+ h = t_elapsed / 3600;
+ m = (t_elapsed % 3600) / 60;
+ s = t_elapsed % 60;
+
+ if (h > 0)
+ snprintf(out, outsiz, "%s%lld:%02lld:%02lld%s",
+ prefix, h, m, s, postfix);
+ else if (m > 0)
+ snprintf(out, outsiz, "%s%lld:%02lld%s", prefix, m, s, postfix);
+ else
+ snprintf(out, outsiz, "%s%lld s%s", prefix, s, postfix);
+}
+
+void
+print_loop(unsigned int interval, char *prefix, char *postfix)
+{
+ char buf[LINE_MAX];
+ time_t t_start = time(NULL);
+
+ while (1) {
+ format_time(buf, sizeof(buf), time(NULL) - t_start, prefix, postfix);
+ printf("\r%s\n", buf);
+ fflush(stdout);
+ sleep(interval);
+ }
+}
+
+void
+xroot_loop(unsigned int interval, char *prefix, char *postfix)
+{
+ Display *dpy;
+ char buf[LINE_MAX];
+ time_t t_start = time(NULL);
+
+ dpy = XOpenDisplay(NULL);
+ if (dpy == NULL)
+ errx(1, "cannot open display");
+ while (1) {
+ format_time(buf, sizeof(buf), time(NULL) - t_start, prefix, postfix);
+ XStoreName(dpy, DefaultRootWindow(dpy), buf);
+ XSync(dpy, False);
+ sleep(interval);
+ }
+}
+
+int
+main(int argc, char *argv[])
+{
+ int ch, xflag = 0;
+ unsigned int interval = 1;
+ char prefix[LINE_MAX] = "", postfix[LINE_MAX] = "";
+ const char *errstr;
+
+ while ((ch = getopt(argc, argv, "p:P:i:x")) != -1) {
+ switch (ch) {
+ case 'p':
+ strlcpy(prefix, optarg, sizeof(prefix));
+ break;
+ case 'P':
+ strlcpy(postfix, optarg, sizeof(postfix));
+ break;
+ case 'i':
+ interval = strtonum(optarg, 1, UINT_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "interval is %s: %s", errstr, optarg);
+ break;
+ case 'x':
+ xflag = 1;
+ break;
+ default:
+ usage(argv[0]);
+ }
+ }
+ argc -= optind;
+ argv += optind;
+ if (argc > 0)
+ usage(argv[0]);
+
+ if (xflag)
+ xroot_loop(interval, prefix, postfix);
+ else
+ print_loop(interval, prefix, postfix);
+
+ return 0;
+} |