Hi PFA, my attempt at cleanup of watch.c in pending. Thanks Haroon
From cf2626c0cc19cf0bcc831c289c6fb85ffd54382b Mon Sep 17 00:00:00 2001 From: "maqsood3...@live.com" <maqsood3...@live.com> Date: Fri, 17 Aug 2018 02:25:37 +0100 Subject: [PATCH] watch cleanup
--- toys/pending/watch.c | 77 +++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/toys/pending/watch.c b/toys/pending/watch.c index 3c0a085..92b9f42 100644 --- a/toys/pending/watch.c +++ b/toys/pending/watch.c @@ -3,7 +3,7 @@ * Copyright 2013 Sandeep Sharma <sandeep.jack2...@gmail.com> * Copyright 2013 Kyungwan Han <asura...@gmail.com> * -USE_WATCH(NEWTOY(watch, "^<1n#<0=2teb", TOYFLAG_USR|TOYFLAG_BIN)) +USE_WATCH(NEWTOY(watch, "^<1n#<1=2teb", TOYFLAG_USR|TOYFLAG_BIN)) config WATCH bool "watch" @@ -27,47 +27,50 @@ GLOBALS( void watch_main(void) { - int i = 0, hlen; - time_t t; - unsigned width = 80, len = sizeof("Www Mmm dd hh:mm:ss yyyy") - 1 ; - char *header, *cmd = *toys.optargs; - int retval; - - while (toys.optargs[++i]) - { - char * oldcmd = cmd; - cmd = xmprintf("%s %s", oldcmd, toys.optargs[i]); - if (CFG_TOYBOX_FREE) free(oldcmd); - } - header = xmprintf("Every %us: %s", TT.interval, cmd); - hlen = strlen(header); - - while(1) { - xprintf("\033[H\033[J"); + char *cmd = "", *tstr, *hdr; + time_t t = {0}; + tstr = hdr = NULL; + while (1) { + if (toys.optargs && *toys.optargs) { + if (strlen(cmd) > 0) + cmd = xmprintf ("%s %s", cmd, *toys.optargs); + else + cmd = xmprintf("%s", *toys.optargs); + ++toys.optargs; + continue; + } + if (!(toys.optflags & FLAG_t)) { - terminal_size(&width, NULL); - if (!width) width = 80; //on serial it may return 0. - time(&t); - if (width > (hlen + len)) xprintf("%s", header); - if (width >= len) - xprintf("%*s\n",width + ((width > (hlen + len))?-hlen:0) + 1, ctime(&t)); - else - xprintf("\n\n"); + xflush(); //flush streams + xprintf("\033[H\033[J"); //clear screen + hdr = xmprintf("Every %us: %s", TT.interval, cmd); + time(&t); + tstr = ctime(&t); + unsigned int width = 80; + terminal_size(&width, NULL); //on serial it may return 0. + width -= strlen(hdr); + width = width <=0 ? 80 : width; + hdr = xmprintf("%s %*s", hdr, width, tstr); + xprintf("%s\n", hdr); } - fflush(NULL); //making sure the screen is clear - retval = system(cmd); - if ((toys.optflags & FLAG_b) && retval) - xprintf("\007"); - if ((toys.optflags & FLAG_e) && retval) { + + if (!system(cmd)) { + sleep(TT.interval); + continue; + } + + if (toys.optflags & FLAG_b) + xprintf("\007"); + + if (toys.optflags & FLAG_e) { xprintf("command exit with non-zero status, press enter to exit\n"); getchar(); break; - } - sleep(TT.interval); + } } - - if (CFG_TOYBOX_FREE){ - free(header); - if (cmd != *toys.optargs) free(cmd); + if (CFG_TOYBOX_FREE) { + free(cmd); + free(tstr); + free(hdr); } } -- 2.17.1
_______________________________________________ Toybox mailing list Toybox@lists.landley.net http://lists.landley.net/listinfo.cgi/toybox-landley.net