From 98afe36bb6075027d9ac82a87dfb99cb5a688b55 Mon Sep 17 00:00:00 2001 From: Arthur Bacci Date: Thu, 4 Jan 2024 16:48:35 -0300 Subject: [PATCH 01/15] fix: status bar fix --- watch/watch.c | 137 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 83 insertions(+), 54 deletions(-) diff --git a/watch/watch.c b/watch/watch.c index 7ba8677..d171a92 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -2,7 +2,8 @@ * watch.c - Keep an eye on a command output */ /* - * Copyright (C) 2023: Luiz Antônio Rangel (takusuman) + * Copyright (C) 2023: Luiz Antônio Rangel (takusuman) + * Arthur Bacci (arthurbacci) * * SPDX-Licence-Identifier: Zlib * @@ -28,25 +29,27 @@ #include static char *progname; -int main(int argc, char *argv[]); -void usage(void); struct Flag { int Beep_on_error, No_title; }; static struct Flag flag; +void usage(void) { + pfmt( + stderr, MM_NOSTD, + "usage: %s [-n seconds] [-bt] command [args...]\n", + progname + ); + exit(1); +} + int main(int argc, char *argv[]) { progname = argv[0]; - // Initialize every integer, to avoid warnings when compiling - // with -Wconditional-uninitialized. - // Default interval of 2 seconds, as other major implementations - // usually do. - int option = 0, - interval = 2, - c = 0, - ec = 0, - term_x = 0, - term_y = 0; + int option = 0; + /* Default interval of 2 seconds, as other major implementations + usually do */ + int interval = 2; + int c = 0, ec = 0, term_x = 0, term_y = 0; char **commandv; pid_t exec_pid; @@ -54,76 +57,110 @@ int main(int argc, char *argv[]) { // Defining nodename from now, since it shouldn't change while we're // watching the command. time_t now; + struct tm *timeinfo; struct utsname u; - if ( uname(&u) == -1 ) { + if (uname(&u) == -1) { prerror(errno); exit(-1); } - while ( (option = getopt(argc, argv, "n:hbt")) != -1 ) { - switch (option) { - case 'n': - interval = atoi(optarg); - break; - case 'b': - flag.Beep_on_error = 1; - break; - case 't': - flag.No_title = 1; - break; - case 'h': - default: - usage(); - } + while ((option = getopt(argc, argv, "n:hbt")) != -1) { + switch (option) { + case 'n': + interval = atoi(optarg); + break; + case 'b': + flag.Beep_on_error = 1; + break; + case 't': + flag.No_title = 1; + break; + case 'h': + default: + usage(); + } } + + // FIXME: not good practise argc -= optind; argv += optind; // Missing operand - if ( argc < 1 ){ - usage(); - } + if (argc < 1) usage(); // Now we just have to copy the "rest" of argv[] to a new character // array allocating some space in memory with calloc(3) and then // copying using a for loop. - if ( (commandv = calloc((unsigned long)(argc + 1), sizeof(char *))) == NULL ) { - prerror(errno); + if ((commandv = calloc((unsigned long)(argc + 1), sizeof(char *))) == NULL) { + // Should i use prerror? + perror("couldn't callocate"); exit(-1); } - for ( c = 0; c < argc; c++ ) { - commandv[c] = argv[c]; - } + for (c = 0; c < argc; c++) commandv[c] = argv[c]; // Initialize curses terminal with colours to be used. // Get terminal size too, we're going to need it. newterm(getenv("TERM"), stdout, stdin); start_color(); init_pair(1, COLOR_BLACK, COLOR_WHITE); - getmaxyx(stdscr, term_y, term_x); for (;;) { + // Clear terminal for the next cycle. + clear(); + + getmaxyx(stdscr, term_y, term_x); // Get current time to be passed as a string with ctime(3). time(&now); - if ( ! flag.No_title ){ + timeinfo = localtime(&now); + + if (!flag.No_title){ + char left[256], right[256], time[256]; + int left_len = 0, right_len = 0; + attron(COLOR_PAIR(1) | A_BOLD); - // Use a factor of terminal width (term_x) divided by 5, - // since it seems to work the best to keep the header at - // a "confortable" size. - printw("Every %d second(s): %-*s %s: %s\n", - interval, (term_x/5), argv[0], u.nodename, ctime(&now)); + + left_len = snprintf( + left, 256, "Every %d second%s: %s", + interval, interval == 1 ? "" : "s", argv[0] + ); + + // This is done because ctime returns a string with \n + strftime(time, 256, "%c", timeinfo); + + right_len = snprintf( + right, 256, "%s: %s", + u.nodename, time + ); + + if (left_len <= 0 || right_len <= 0) { + perror("please try to execute without the bar\n"); + exit(EXIT_FAILURE); + } + + if (left_len + right_len >= term_x) { + if (left_len > term_x || right_len > term_x) { + // TODO: + } + printw("%-*s", term_x, left); + printw("%*s", term_x, right); + } else { + printw("%s%*s", left, term_x - left_len, right); + } + attroff(COLOR_PAIR(1) | A_BOLD); } + printw("\n"); + // Not using endwin(3x), since it breaks with multiline // also-curses programs, such as ls(1) with the "-l" option. reset_shell_mode(); refresh(); - if ( (exec_pid = fork()) == 0 ) { - if ( (execvp(commandv[0], commandv)) == -1 ) { + if ((exec_pid = fork()) == 0) { + if ((execvp(commandv[0], commandv)) == -1) { prerror(errno); exit(-1); } @@ -137,14 +174,6 @@ int main(int argc, char *argv[]) { } sleep( (uint)(interval) ); - - // Clear terminal for the next cycle. - clear(); } } -void usage(void) { - pfmt(stderr, MM_NOSTD, - "usage: %s [-n seconds] [-bt] command [args...]\n", progname); - exit(1); -} From 98c8bc394d8cc825009e79ba7c094f3d3c7ea6db Mon Sep 17 00:00:00 2001 From: Arthur Bacci Date: Thu, 4 Jan 2024 17:27:09 -0300 Subject: [PATCH 02/15] fix: line after bar is only printed if the bar is enobaled --- watch/watch.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/watch/watch.c b/watch/watch.c index d171a92..7f29310 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -150,9 +150,10 @@ int main(int argc, char *argv[]) { } attroff(COLOR_PAIR(1) | A_BOLD); + + printw("\n"); } - printw("\n"); // Not using endwin(3x), since it breaks with multiline // also-curses programs, such as ls(1) with the "-l" option. From e3d80f33e5628cb6436da8a9233c73ad39006cb4 Mon Sep 17 00:00:00 2001 From: Arthur Bacci Date: Thu, 4 Jan 2024 18:37:29 -0300 Subject: [PATCH 03/15] fix: now the bar disappears if there isn't enough space for it to be fully displayed --- watch/Makefile | 358 +++++++++++++++++++++++++++++++++++++++++++++++++ watch/watch | Bin 0 -> 17912 bytes watch/watch.c | 24 ++-- watch/watch.o | Bin 0 -> 6120 bytes 4 files changed, 369 insertions(+), 13 deletions(-) create mode 100644 watch/Makefile create mode 100755 watch/watch create mode 100644 watch/watch.o diff --git a/watch/Makefile b/watch/Makefile new file mode 100644 index 0000000..111b92d --- /dev/null +++ b/watch/Makefile @@ -0,0 +1,358 @@ +# +# Don't change this Makefile! It has been generated from Makefile.mk; +# change that file instead and run make at the toplevel. +# +###################################################################### + +# +# This is the shell used for the compilation phase, the execution of most +# installed scripts, and the shell escapes in the traditional command +# versions. It needs not conform to POSIX. The system shell should work +# fine; for maximum compatibility with traditional tools, the Heirloom +# Bourne shell is recommended. It then must obviously be compiled and +# installed first. +# +SHELL = /bin/sh + +# +# Specify the path name for a POSIX-conforming shell here. For example, +# Solaris users should insert /usr/xpg4/bin/sh. This shell is used for +# the shell escape in the POSIX variants of the ed utility. +# +POSIX_SHELL = /bin/sh + +# +# Root directory. Mainly useful for package building; leave empty for +# normal installation. +# +ROOT ?= + +# +# Location for binaries that have no special personality. This location +# may be identical to that of one of the first three personalities below. +# +DEFBIN = /usr/5bin + +# +# Location for SVID3/SVR4-style binaries. +# +SV3BIN = /usr/5bin + +# +# Location for SVID4/SVR4.2-style binaries. +# +S42BIN = /usr/5bin/s42 + +# +# Location for POSIX.2/SUS-style binaries. +# +SUSBIN = /usr/5bin/posix + +# +# Location for POSIX.1-2001/SUSv3-style binaries. +SU3BIN = /usr/5bin/posix2001 + +# +# Location for SVR4 UCB-style binaries. These do not form a complete +# personality, and the binary path must not be identical to the default +# one. +# +UCBBIN = /usr/ucb + +# +# Location for development binaries. The "tsort" utility is +# installed in this directory, and the utilities from the +# "Heirloom Development Tools" package are expected to be +# installed in it. +# +CCSBIN = /usr/ccs/bin + +# +# Location for library files. +# +DEFLIB = /usr/5lib + +# +# Location for superuser-only binaries. May be identical to the +# default binary directory. +# +DEFSBIN = /usr/5bin + +# +# Location for manual pages (with man1, man1b ... man8 below). +# +MANDIR = /usr/share/man/5man + +# +# Location for default files. Make sure that this directory is accessible +# to all users. +# +DFLDIR = /etc/default + +# +# Location for the spell history file (contains misspelled words for +# all users; set to /dev/null to disable). +# +SPELLHIST = /var/adm/spellhist + +# +# Location for the su logfile. +# +SULOG = /var/log/sulog + +# +# Where to store the file command's magic. Note that the existing +# file at this location is replaced unconditionally at installation. +# +MAGIC = $(DEFLIB)/magic + +# +# The group whose members may write utmp or utmpx entries. Appropriate values +# are "utmp" for RedHat Linux or "adm" for Open UNIX. On Solaris, the group +# does not actually matter but should be set to "adm" too. (The '-g' is an +# argument to the install command and should remain as is.) +# +# On HP-UX, AIX, FreeBSD, NetBSD, and OpenBSD, this setting is not used. +# +TTYGRP = -g utmp + +# +# Library path, just change it if you need. This is somewhat self-describing: +# it contains the path for both static or dynamic libraries. Since we're +# building Heirloom statically per default, this only contains the pathes for +# the static libraries on the system. In Copacabana Linux, static libraries stay +# at the /usr/lib directory, while dynamic ones stay at /lib. +# +# Uncomment the definition below and comment the current definition if you are +# on the GNU C Library, since it doesn't seems to like static linking. +# +LIBPATH = -L/lib +#LIBPATH = -L/usr/lib -L/usr/ccs/lib + +# +# Curses library. Change to -lncurses if necessary. Caution: Some gcc +# setups on Solaris are broken so that ncurses headers are used during +# compilation, but the system libcurses is used for linking. This will +# usually lead to segmentation violations. The easiest fix is to write +# something like -L/path/to/ncurses/lib/directory -lncurses here. +# +# The 4.4 BSD curses library, which is still supplied with NetBSD 2.0, +# does not supply our needs. Use -ltermcap instead and add -DUSE_TERMCAP +# to CPPFLAGS. +# +LCURS = -lcurses + +# +# Socket library, necessary on Solaris and Open UNIX. If your system has +# socket support in libc (as glibc on Linux), leave it empty or undefined. +#LSOCKET = -lsocket -lnsl + +# +# Uncomment this on Open UNIX. +# +#LIBGEN = -lgen + +# +# Uncomment this on FreeBSD, NetBSD, and OpenBSD. +# +#LKVM = -lkvm + +# +# zlib (statically linked by default). Set USE_ZLIB to 0 if you don't have +# zlib or don't want to use it; you need it only if you want to use inflate +# compression when creating zip files with cpio. +# +# Uncomment the definition below and comment the current definition if you are +# on the GNU C Library, since it doesn't seems to like static binaries. +# +#LIBZ = -Wl,-Bstatic -lz -Wl,-Bdynamic +LIBZ = -Wl,-Bstatic -lz +USE_ZLIB = 1 + +# +# The name of the bzip2 library, and whether to use it. The library is only +# needed to read and write bzip2 compressed parts of zip files with cpio. +# +# Uncomment the definition below and comment the current definition if you are +# on the GNU C Library, since it doesn't seems to like static binaries. +# +USE_BZLIB = 0 + +# +# Compiler and linker flags. HOSTCC is for cross compiling. +# + +CC ?= cc +HOSTCC = $(CC) +WARN= + +LD = $(CC) +# +# Uncomment the definition below and comment the current definition if you are +# on the GNU C Library, since it doesn't seems to like static binaries. +# +LDFLAGS = $(LIBPATH) +# +# LDFLAGS = -static $(LIBPATH) + +# +# Flags for the C preprocessor. +# On Linux with glibc or uClibc, add -D_GNU_SOURCE. +# On Solaris, -D__EXTENSIONS__ should be added. +# On HP-UX, -D_INCLUDE__STDC_A1_SOURCE must be added. +# On AIX, -D_TPARM_COMPAT must be added. +# On AIX, -D_MTEXTEND_H should be added if mtextend.h is not found. +# On NetBSD, add -DUSE_TERMCAP. +# +CPPFLAGS = -D_GNU_SOURCE + +# +# CFLAGS, CFLAGS2, CFLAGSS, and CFLAGSU make it possible to give special +# compiler flags for objects where speed is critical. There is no other +# purpose with this so setting all to -O will work too. +# +# On 64-bit HP-UX, you should add +DD64 to all CFLAGS to create 64-bit +# executables. The ps command will otherwise not be able to display any +# processes. +# +# On AIX 5.1, the system-supplied major(), minor(), and makedev() macros +# for accessing dev_t values are inappropriate if the compiler is used in +# 64-bit mode (by specifying -q64 or OBJECT_MODE=64 in the environment). +# Moreover, its 64-bit and 32-bit dev_t representations are incompatible. +# As a result, any programs that work with the contents of dev_t values +# will not give correct results in 64-bit mode; this affects at least +# cpio, mknod, pgrep, ps, tar, and whodo. Thus always use the 32-bit +# compiler mode (which is the default) until this issue is properly +# handled by the system vendor. +# +CFLAGS = -O -fomit-frame-pointer $(WARN) +CFLAGS2 = -O2 -fomit-frame-pointer $(WARN) +CFLAGSS = -Os -fomit-frame-pointer $(WARN) +CFLAGSU = -O2 -fomit-frame-pointer -funroll-loops $(WARN) + +# +# SEMANTICS_LIKE_ITS_89 --- the prefixes "C_" and "L_" stands for "C compiler" +# and "Link-editor", respectively --- may be used to fix problems that appear +# when link-editing the final binary, caused in the preprocessing stage because of +# the inclusion of inline functions with GNU89 semantics --- or, as in the +# G.C.C. documentation, "traditional GNU semantics" --- when building C99 code. +# This happens when building cpio, diff and tabs, because, while the main program +# code is C99 because it was almost or partially rewritten, some of the original +# headers pulled from Research UNIX or SunOS/Solaris still using C89 (or GNU89, +# call it what you like) inline function declarations. +# This could be solved with some refactoring? Sure, but the team isn't big enough +# to focus on solving compatibility problems that, for some reason, went ignored +# for decades since we still have bigger problems to deal with for now. +# +# Comment these if you're not using GNU Compiler Collection's cc(1) as the +# compiler and/or you haven't got any funky "multiple definition" errors with it +# disabled. +# Not sure on what is the exact version range, but if it's greater +# than G.C.C. 10.2.1, you will need to use it. Enabling it per default. +# +C_SEMANTICS_LIKE_ITS_89 = -fgnu89-inline +L_SEMANTICS_LIKE_ITS_89 = -Wl,-z,muldefs + +# +# Binaries are stripped with this command after installation. +# +STRIP = strip -s -R .comment -R .note + +# +# The define for large file support in 32-bit environments. +# +# On Linux, Solaris, and Open UNIX, use -D_FILE_OFFSET_BITS=64L +# On HP-UX B.11.11 on PA-RISC in 32-bit (default) mode, this was found +# not to work with fseeko(3), so disable it there unless you intend to +# debug the problem. +# +# On AIX, use -D_LARGE_FILES. +# +# On FreeBSD, NetBSD, and OpenBSD, no such define is necessary. +# +LARGEF = -D_FILE_OFFSET_BITS=64L + +# +# Use this if you prefer symbolic links between installed files. +# +#LNS = ln -s +# +# Use this if you prefer hard links between installed files. +# +LNS = $(SHELL) -c "if cd \`dirname \$$2\`; then ln \$$1 \$$2; fi" dummy + +# +# Yacc implementations known to work with the Heirloom Toolchest are +# the original Unix yacc, Berkeley yacc, and GNU bison -y (v. 1.875). +# +YACC = yacc + +# +# Lex implementations known to work with the Heirloom Toolchest are +# most derivatives of the original Unix lex and flex. +# +LEX = flex + +# +# Whether to use the supplied widechar emulation library. This should +# only be enabled if the system lacks appropriate widechar support. +# It is currently needed on +# - Linux/diet libc +# - FreeBSD 4 +# - NetBSD 1.x, because it lacks wctype_t/wctrans_t etc. in wctype.h. +# - OpenBSD +# +#IWCHAR = -I../libwchar +#LWCHAR = -L../libwchar -lwchar + +# +# Crypt library. -lcrypt is okay for everything except HP-UX, AIX, NetBSD, +# and old versions of diet libc. +# +LCRYPT=-lcrypt + +# +# Run ranlib if it is in the current path. This should take care +# of most situations. On Mac OS X, you need to uncomment "ranlib -c". +# +RANLIB=(hash ranlib) >/dev/null 2>&1 || exit 0; ranlib +#RANLIB=ranlib -c + +# +# Don't change the rest of this file unless you really know what you are +# doing. +# + +######################################################################## +######################################################################## +######################################################################## +######################################################################## +######################################################################## + +UCBINST = $(ROOT)$(UCBBIN)/install + +ICOMMON = -I../libcommon +LCOMMON = -L../libcommon -lcommon + +IUXRE = -I../libuxre -DUXRE +LUXRE = -L../libuxre -luxre + +MANINST = $(SHELL) ../build/maninst +all: watch + +watch: watch.o + $(LD) $(LDFLAGS) watch.o $(LCURS) $(LCOMMON) $(LIBS) -o watch + +watch.o: watch.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(ICOMMON) -c watch.c + +install: all + $(UCBINST) -c watch $(ROOT)$(DEFBIN)/watch + $(STRIP) $(ROOT)$(DEFBIN)/watch + $(MANINST) -c -m 644 watch.1 $(ROOT)$(MANDIR)/man1/watch.1 + +clean: + rm -f watch watch.o core log *~ + +mrproper: clean + rm -f Makefile $(MRPROPER) diff --git a/watch/watch b/watch/watch new file mode 100755 index 0000000000000000000000000000000000000000..d5e582ddb6550f3e5ab3e06bb19cb3a12fbe8db4 GIT binary patch literal 17912 zcmeHPeRNyJl^@A=V)7xGkOT)3@&Yo%fQ6l}Iw>YnB00}RPF%+!VUwW9vTSRSB_lm2 zhC*?xg!1BuOs65U4#QK2X4 z8FC0oQW;N*2_22XUMY2T45vv*#IhN@<4y zGf8EAAB7&peZnYSrp>%sRx4p9_7iie#uAq(d^42%|$L(!U-D$xwW(sd<*6ug)$bY(5U^M)T+FnmzfK=caY9 z3sj}Qdv4mXn|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q4Vth?I;GM1je6-ibYnffOD=Jz)$H7jOIrep%093$AYY0l@uh3<^JQf9&+Uycb_^7W%WV#3 ze)Amz#TvA8p$HM%Ak0koLtOzaAa@|GXklR{{Kw}$A5hQlTh>b2q zMYJ`g2=M!{RmksM?fJ7CgXET%B2{CLkeUB{&rOkh@y4IxBDx5S_SzJ|xPoF=4}wut z!kmeF`?NqOC^iLg+ADL|Y!r(3GiNlWg`7Rn0VnQ#*s7!5TL5(qgs}$|+j9&l_llM} z4fjPZ-W$M%Cui5aQB+}2EpC+Hchv-K@Zn;D@uMsriUdeOpyGReqW3HO)Etxk%lQ^bRUw|AZz)!10q``1A@${a zjHJDyLh_N~EZZ;VMZ&|3S-+fTk^eVB{(lJS|CFSjypHbzqb5pyxqmQy3rQF6*&|-+OZq;rLVY=JlM(vW zCh?Q{R+Ja&=fBUz{F)qRR5vB5XK>)5OqldvaT_JoLVrjK3aXUwQ$_vtCAict9e?C2 zr9vwEo>5|)8%6zXBJlJaqU$XEm+hkAC4DzEjq}5e2BrNXg-O;cvCEMeOMgp~Au+Q= Ip@e1s1NEVa<^TWy literal 0 HcmV?d00001 diff --git a/watch/watch.c b/watch/watch.c index 7f29310..59ace27 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -115,7 +115,7 @@ int main(int argc, char *argv[]) { time(&now); timeinfo = localtime(&now); - if (!flag.No_title){ + if (!flag.No_title) { char left[256], right[256], time[256]; int left_len = 0, right_len = 0; @@ -138,20 +138,18 @@ int main(int argc, char *argv[]) { perror("please try to execute without the bar\n"); exit(EXIT_FAILURE); } - - if (left_len + right_len >= term_x) { - if (left_len > term_x || right_len > term_x) { - // TODO: + + if (left_len <= term_x && right_len <= term_x) { + if (left_len + right_len >= term_x) { + printw("%-*s", term_x, left); + printw("%*s", term_x, right); + } else { + printw("%s%*s", left, term_x - left_len, right); } - printw("%-*s", term_x, left); - printw("%*s", term_x, right); - } else { - printw("%s%*s", left, term_x - left_len, right); + printw("\n"); } attroff(COLOR_PAIR(1) | A_BOLD); - - printw("\n"); } @@ -170,11 +168,11 @@ int main(int argc, char *argv[]) { // execvp'd command hasn't exit with success and we have // flag.Beep_on_error activated. - if ( ec != 0 && flag.Beep_on_error ) { + if (ec != 0 && flag.Beep_on_error) { beep(); } - sleep( (uint)(interval) ); + sleep((uint)(interval)); } } diff --git a/watch/watch.o b/watch/watch.o new file mode 100644 index 0000000000000000000000000000000000000000..819090f8ae9d1c4af8603953b83736062490c7ee GIT binary patch literal 6120 zcmb`LZEPGz8OP`1#5TdbGr!jrBmo&A5hHt3dk}^hT@hZNth-*<=!9z4>Ia*LQLm8ezHS9>n$4HGX%_dnf1p+k00OBRN%H zwRoz&%K72?>IB~>3PjKt6w&21Fp;{n9lXq=E7@;BGv3nm=axhVu=Ce{Ow!->7PAe} zcy`yy`IW@QUx1?djhJ3on{S7ta4_oqPCvhpIQLxuV}+H6njOo!mrHi*g%$n9w_C*E z=#8$}sMpo1*B9FLp{vvH<{BxGLq~*ny(z{~xYNC*ocE^g-4HnuTxtWU=r|eQ`dUm* zh29js*Av;7bT6Ci^iC!_uKf+F`2L9RU@T~hfm6MCA-MoUzU#X8hTi;jasfs}DuX%R z050L1%(Iww%eeZD`wJv^6>stU!gSYY<3P^)Kv0;qs2!;?wj%KL|3@+2$=-YMU% zKFAX1XF!+r{Mn>)s5it2G~mTyDtKGs^6}RCB8*Qp*`v$FqT*5SW#w{qEBG9T<+1r2 z=*{2yBhbhuyK{~99K@Yj??3Cm0O(CHEZ!@*=C6_qEwEm_e^E(Jb)cC`eogp<5gzld zQcd6{3$p&Vy>+BGK4{aVV^PVBKX*DTmp(LDv3 z34Tf|*tMEr6}1zFGwG(&=~LU;RMj+GQ{&DVjoX?zYZj)tsm)Y)*`DSaFPqwg;cREt z{_+H81$(+$wC>?r!Kha4g27ETKJxGwV=fyxZGwwbQ8t-!u@5X&U@BF_!c=iPOYPYW z1^Boa!cYJ3p+oz%p2J^0x=ZUDNcX4vwEo`yf!@L1KE_}tVj4WS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kXnoUboam4gA!Fw#mAvYr2`e*DK>uk zi{h}h3>5E4bnT;1ll8;>Qtyu%w5Q=gpW*sPWc{x~8MSczuR|dY)i4!7x{R9%m}(!_ z51#j`zFI$yF`l(}F9_GiyG3~I0AlTUSpYj18n957;hwr#{U6Bwe@l1? If^hx+0jO}b?f?J) literal 0 HcmV?d00001 From 30a89987bd77be77048e26cb424196b07e9ab90f Mon Sep 17 00:00:00 2001 From: Arthur Bacci Date: Thu, 4 Jan 2024 19:32:42 -0300 Subject: [PATCH 04/15] feat: more precise intervals --- watch/watch.c | 53 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/watch/watch.c b/watch/watch.c index 59ace27..a463bd8 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -46,13 +46,15 @@ void usage(void) { int main(int argc, char *argv[]) { progname = argv[0]; int option = 0; - /* Default interval of 2 seconds, as other major implementations - usually do */ - int interval = 2; + struct timespec interval = {0}; int c = 0, ec = 0, term_x = 0, term_y = 0; char **commandv; pid_t exec_pid; + /* Default interval of 2 seconds, as other major implementations + usually do */ + interval.tv_sec = 2; + // Variables for the information header. // Defining nodename from now, since it shouldn't change while we're // watching the command. @@ -67,7 +69,43 @@ int main(int argc, char *argv[]) { while ((option = getopt(argc, argv, "n:hbt")) != -1) { switch (option) { case 'n': - interval = atoi(optarg); + if (!optarg) break; + + char arg[128]; + char *afterpoint = NULL; + strncpy(arg, optarg, 128); + arg[127] = '\0'; + + size_t point = 0; + for (; arg[point]; point++) { + if (arg[point] == '.' || arg[point] == ',') { + arg[point] = '\0'; + afterpoint = &arg[point + 1]; + break; + } + } + + if (strlen(arg) == 0) interval.tv_sec = 0; + else interval.tv_sec = atoi(arg); + + size_t afterpointlen = afterpoint ? strlen(afterpoint) : 0; + if (afterpointlen > 0) { + long integer = atoi(afterpoint); + + if (afterpointlen > 9) { + afterpoint[9] = '\0'; + afterpointlen = 9; + } + + for (size_t i = 0; i < 9 - afterpointlen; i++) + integer *= 10; + + interval.tv_nsec = integer; + } + + if (interval.tv_sec == 0 && interval.tv_nsec < 100000000) + interval.tv_nsec = 100000000; + break; case 'b': flag.Beep_on_error = 1; @@ -122,8 +160,9 @@ int main(int argc, char *argv[]) { attron(COLOR_PAIR(1) | A_BOLD); left_len = snprintf( - left, 256, "Every %d second%s: %s", - interval, interval == 1 ? "" : "s", argv[0] + left, 256, "Every %d.%d second(s): %s", + (int)interval.tv_sec, (int)interval.tv_nsec, + argv[0] ); // This is done because ctime returns a string with \n @@ -172,7 +211,7 @@ int main(int argc, char *argv[]) { beep(); } - sleep((uint)(interval)); + nanosleep(&interval, NULL); } } From bb082c833a3fef6b26e0e0b29ba063303fef5da0 Mon Sep 17 00:00:00 2001 From: Arthur Bacci Date: Thu, 4 Jan 2024 19:56:19 -0300 Subject: [PATCH 05/15] remove accidental upload --- watch/Makefile | 358 ------------------------------------------------- watch/watch | Bin 17912 -> 0 bytes watch/watch.o | Bin 6120 -> 0 bytes 3 files changed, 358 deletions(-) delete mode 100644 watch/Makefile delete mode 100755 watch/watch delete mode 100644 watch/watch.o diff --git a/watch/Makefile b/watch/Makefile deleted file mode 100644 index 111b92d..0000000 --- a/watch/Makefile +++ /dev/null @@ -1,358 +0,0 @@ -# -# Don't change this Makefile! It has been generated from Makefile.mk; -# change that file instead and run make at the toplevel. -# -###################################################################### - -# -# This is the shell used for the compilation phase, the execution of most -# installed scripts, and the shell escapes in the traditional command -# versions. It needs not conform to POSIX. The system shell should work -# fine; for maximum compatibility with traditional tools, the Heirloom -# Bourne shell is recommended. It then must obviously be compiled and -# installed first. -# -SHELL = /bin/sh - -# -# Specify the path name for a POSIX-conforming shell here. For example, -# Solaris users should insert /usr/xpg4/bin/sh. This shell is used for -# the shell escape in the POSIX variants of the ed utility. -# -POSIX_SHELL = /bin/sh - -# -# Root directory. Mainly useful for package building; leave empty for -# normal installation. -# -ROOT ?= - -# -# Location for binaries that have no special personality. This location -# may be identical to that of one of the first three personalities below. -# -DEFBIN = /usr/5bin - -# -# Location for SVID3/SVR4-style binaries. -# -SV3BIN = /usr/5bin - -# -# Location for SVID4/SVR4.2-style binaries. -# -S42BIN = /usr/5bin/s42 - -# -# Location for POSIX.2/SUS-style binaries. -# -SUSBIN = /usr/5bin/posix - -# -# Location for POSIX.1-2001/SUSv3-style binaries. -SU3BIN = /usr/5bin/posix2001 - -# -# Location for SVR4 UCB-style binaries. These do not form a complete -# personality, and the binary path must not be identical to the default -# one. -# -UCBBIN = /usr/ucb - -# -# Location for development binaries. The "tsort" utility is -# installed in this directory, and the utilities from the -# "Heirloom Development Tools" package are expected to be -# installed in it. -# -CCSBIN = /usr/ccs/bin - -# -# Location for library files. -# -DEFLIB = /usr/5lib - -# -# Location for superuser-only binaries. May be identical to the -# default binary directory. -# -DEFSBIN = /usr/5bin - -# -# Location for manual pages (with man1, man1b ... man8 below). -# -MANDIR = /usr/share/man/5man - -# -# Location for default files. Make sure that this directory is accessible -# to all users. -# -DFLDIR = /etc/default - -# -# Location for the spell history file (contains misspelled words for -# all users; set to /dev/null to disable). -# -SPELLHIST = /var/adm/spellhist - -# -# Location for the su logfile. -# -SULOG = /var/log/sulog - -# -# Where to store the file command's magic. Note that the existing -# file at this location is replaced unconditionally at installation. -# -MAGIC = $(DEFLIB)/magic - -# -# The group whose members may write utmp or utmpx entries. Appropriate values -# are "utmp" for RedHat Linux or "adm" for Open UNIX. On Solaris, the group -# does not actually matter but should be set to "adm" too. (The '-g' is an -# argument to the install command and should remain as is.) -# -# On HP-UX, AIX, FreeBSD, NetBSD, and OpenBSD, this setting is not used. -# -TTYGRP = -g utmp - -# -# Library path, just change it if you need. This is somewhat self-describing: -# it contains the path for both static or dynamic libraries. Since we're -# building Heirloom statically per default, this only contains the pathes for -# the static libraries on the system. In Copacabana Linux, static libraries stay -# at the /usr/lib directory, while dynamic ones stay at /lib. -# -# Uncomment the definition below and comment the current definition if you are -# on the GNU C Library, since it doesn't seems to like static linking. -# -LIBPATH = -L/lib -#LIBPATH = -L/usr/lib -L/usr/ccs/lib - -# -# Curses library. Change to -lncurses if necessary. Caution: Some gcc -# setups on Solaris are broken so that ncurses headers are used during -# compilation, but the system libcurses is used for linking. This will -# usually lead to segmentation violations. The easiest fix is to write -# something like -L/path/to/ncurses/lib/directory -lncurses here. -# -# The 4.4 BSD curses library, which is still supplied with NetBSD 2.0, -# does not supply our needs. Use -ltermcap instead and add -DUSE_TERMCAP -# to CPPFLAGS. -# -LCURS = -lcurses - -# -# Socket library, necessary on Solaris and Open UNIX. If your system has -# socket support in libc (as glibc on Linux), leave it empty or undefined. -#LSOCKET = -lsocket -lnsl - -# -# Uncomment this on Open UNIX. -# -#LIBGEN = -lgen - -# -# Uncomment this on FreeBSD, NetBSD, and OpenBSD. -# -#LKVM = -lkvm - -# -# zlib (statically linked by default). Set USE_ZLIB to 0 if you don't have -# zlib or don't want to use it; you need it only if you want to use inflate -# compression when creating zip files with cpio. -# -# Uncomment the definition below and comment the current definition if you are -# on the GNU C Library, since it doesn't seems to like static binaries. -# -#LIBZ = -Wl,-Bstatic -lz -Wl,-Bdynamic -LIBZ = -Wl,-Bstatic -lz -USE_ZLIB = 1 - -# -# The name of the bzip2 library, and whether to use it. The library is only -# needed to read and write bzip2 compressed parts of zip files with cpio. -# -# Uncomment the definition below and comment the current definition if you are -# on the GNU C Library, since it doesn't seems to like static binaries. -# -USE_BZLIB = 0 - -# -# Compiler and linker flags. HOSTCC is for cross compiling. -# - -CC ?= cc -HOSTCC = $(CC) -WARN= - -LD = $(CC) -# -# Uncomment the definition below and comment the current definition if you are -# on the GNU C Library, since it doesn't seems to like static binaries. -# -LDFLAGS = $(LIBPATH) -# -# LDFLAGS = -static $(LIBPATH) - -# -# Flags for the C preprocessor. -# On Linux with glibc or uClibc, add -D_GNU_SOURCE. -# On Solaris, -D__EXTENSIONS__ should be added. -# On HP-UX, -D_INCLUDE__STDC_A1_SOURCE must be added. -# On AIX, -D_TPARM_COMPAT must be added. -# On AIX, -D_MTEXTEND_H should be added if mtextend.h is not found. -# On NetBSD, add -DUSE_TERMCAP. -# -CPPFLAGS = -D_GNU_SOURCE - -# -# CFLAGS, CFLAGS2, CFLAGSS, and CFLAGSU make it possible to give special -# compiler flags for objects where speed is critical. There is no other -# purpose with this so setting all to -O will work too. -# -# On 64-bit HP-UX, you should add +DD64 to all CFLAGS to create 64-bit -# executables. The ps command will otherwise not be able to display any -# processes. -# -# On AIX 5.1, the system-supplied major(), minor(), and makedev() macros -# for accessing dev_t values are inappropriate if the compiler is used in -# 64-bit mode (by specifying -q64 or OBJECT_MODE=64 in the environment). -# Moreover, its 64-bit and 32-bit dev_t representations are incompatible. -# As a result, any programs that work with the contents of dev_t values -# will not give correct results in 64-bit mode; this affects at least -# cpio, mknod, pgrep, ps, tar, and whodo. Thus always use the 32-bit -# compiler mode (which is the default) until this issue is properly -# handled by the system vendor. -# -CFLAGS = -O -fomit-frame-pointer $(WARN) -CFLAGS2 = -O2 -fomit-frame-pointer $(WARN) -CFLAGSS = -Os -fomit-frame-pointer $(WARN) -CFLAGSU = -O2 -fomit-frame-pointer -funroll-loops $(WARN) - -# -# SEMANTICS_LIKE_ITS_89 --- the prefixes "C_" and "L_" stands for "C compiler" -# and "Link-editor", respectively --- may be used to fix problems that appear -# when link-editing the final binary, caused in the preprocessing stage because of -# the inclusion of inline functions with GNU89 semantics --- or, as in the -# G.C.C. documentation, "traditional GNU semantics" --- when building C99 code. -# This happens when building cpio, diff and tabs, because, while the main program -# code is C99 because it was almost or partially rewritten, some of the original -# headers pulled from Research UNIX or SunOS/Solaris still using C89 (or GNU89, -# call it what you like) inline function declarations. -# This could be solved with some refactoring? Sure, but the team isn't big enough -# to focus on solving compatibility problems that, for some reason, went ignored -# for decades since we still have bigger problems to deal with for now. -# -# Comment these if you're not using GNU Compiler Collection's cc(1) as the -# compiler and/or you haven't got any funky "multiple definition" errors with it -# disabled. -# Not sure on what is the exact version range, but if it's greater -# than G.C.C. 10.2.1, you will need to use it. Enabling it per default. -# -C_SEMANTICS_LIKE_ITS_89 = -fgnu89-inline -L_SEMANTICS_LIKE_ITS_89 = -Wl,-z,muldefs - -# -# Binaries are stripped with this command after installation. -# -STRIP = strip -s -R .comment -R .note - -# -# The define for large file support in 32-bit environments. -# -# On Linux, Solaris, and Open UNIX, use -D_FILE_OFFSET_BITS=64L -# On HP-UX B.11.11 on PA-RISC in 32-bit (default) mode, this was found -# not to work with fseeko(3), so disable it there unless you intend to -# debug the problem. -# -# On AIX, use -D_LARGE_FILES. -# -# On FreeBSD, NetBSD, and OpenBSD, no such define is necessary. -# -LARGEF = -D_FILE_OFFSET_BITS=64L - -# -# Use this if you prefer symbolic links between installed files. -# -#LNS = ln -s -# -# Use this if you prefer hard links between installed files. -# -LNS = $(SHELL) -c "if cd \`dirname \$$2\`; then ln \$$1 \$$2; fi" dummy - -# -# Yacc implementations known to work with the Heirloom Toolchest are -# the original Unix yacc, Berkeley yacc, and GNU bison -y (v. 1.875). -# -YACC = yacc - -# -# Lex implementations known to work with the Heirloom Toolchest are -# most derivatives of the original Unix lex and flex. -# -LEX = flex - -# -# Whether to use the supplied widechar emulation library. This should -# only be enabled if the system lacks appropriate widechar support. -# It is currently needed on -# - Linux/diet libc -# - FreeBSD 4 -# - NetBSD 1.x, because it lacks wctype_t/wctrans_t etc. in wctype.h. -# - OpenBSD -# -#IWCHAR = -I../libwchar -#LWCHAR = -L../libwchar -lwchar - -# -# Crypt library. -lcrypt is okay for everything except HP-UX, AIX, NetBSD, -# and old versions of diet libc. -# -LCRYPT=-lcrypt - -# -# Run ranlib if it is in the current path. This should take care -# of most situations. On Mac OS X, you need to uncomment "ranlib -c". -# -RANLIB=(hash ranlib) >/dev/null 2>&1 || exit 0; ranlib -#RANLIB=ranlib -c - -# -# Don't change the rest of this file unless you really know what you are -# doing. -# - -######################################################################## -######################################################################## -######################################################################## -######################################################################## -######################################################################## - -UCBINST = $(ROOT)$(UCBBIN)/install - -ICOMMON = -I../libcommon -LCOMMON = -L../libcommon -lcommon - -IUXRE = -I../libuxre -DUXRE -LUXRE = -L../libuxre -luxre - -MANINST = $(SHELL) ../build/maninst -all: watch - -watch: watch.o - $(LD) $(LDFLAGS) watch.o $(LCURS) $(LCOMMON) $(LIBS) -o watch - -watch.o: watch.c - $(CC) $(CFLAGS) $(CPPFLAGS) $(ICOMMON) -c watch.c - -install: all - $(UCBINST) -c watch $(ROOT)$(DEFBIN)/watch - $(STRIP) $(ROOT)$(DEFBIN)/watch - $(MANINST) -c -m 644 watch.1 $(ROOT)$(MANDIR)/man1/watch.1 - -clean: - rm -f watch watch.o core log *~ - -mrproper: clean - rm -f Makefile $(MRPROPER) diff --git a/watch/watch b/watch/watch deleted file mode 100755 index d5e582ddb6550f3e5ab3e06bb19cb3a12fbe8db4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17912 zcmeHPeRNyJl^@A=V)7xGkOT)3@&Yo%fQ6l}Iw>YnB00}RPF%+!VUwW9vTSRSB_lm2 zhC*?xg!1BuOs65U4#QK2X4 z8FC0oQW;N*2_22XUMY2T45vv*#IhN@<4y zGf8EAAB7&peZnYSrp>%sRx4p9_7iie#uAq(d^42%|$L(!U-D$xwW(sd<*6ug)$bY(5U^M)T+FnmzfK=caY9 z3sj}Qdv4mXn|93}9R5^p={v)-7aTk{?Ymz)Cq_GjOVX#ok5%YEg=69SP2_)Mg5Pgq zhuWjyC`};%W7T`m1lLXQD^2SCk_p~sf?qJPzup9Y+QiNl6M4Id{2a(DI7-v)Ci1_4 z;8<}RG{L`Of=5m8RVMX*#sqIRu`^&IZ!^Icn&3B?;3*UQX85DvC`}O)`8EiS70+u- zHuZ7GotalD#M7r69?3HgUhj_^Eud*}f2_NkZ3{+1fjHY14|T^wiC)$j3iUHl z9A)8XSo8M>!f}SekmgVHh9VJvU#u&{qM>c5zK76OZFu?UOxYxrY5Ax;LO z0q6n<#CxcU*nq~icJnH`!N((^C`&}heJF&YTUkPsg$6^xt^K5eI-nnl$4S1M`;v$X zsqR?(Z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lJS|CFSjypHbzqb5pyxqmQy3rQF6*&|-+OZq;rLVY=JlM(vW zCh?Q{R+Ja&=fBUz{F)qRR5vB5XK>)5OqldvaT_JoLVrjK3aXUwQ$_vtCAict9e?C2 zr9vwEo>5|)8%6zXBJlJaqU$XEm+hkAC4DzEjq}5e2BrNXg-O;cvCEMeOMgp~Au+Q= Ip@e1s1NEVa<^TWy diff --git a/watch/watch.o b/watch/watch.o deleted file mode 100644 index 819090f8ae9d1c4af8603953b83736062490c7ee..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6120 zcmb`LZEPGz8OP`1#5TdbGr!jrBmo&A5hHt3dk}^hT@hZNth-*<=!9z4>Ia*LQLm8ezHS9>n$4HGX%_dnf1p+k00OBRN%H zwRoz&%K72?>IB~>3PjKt6w&21Fp;{n9lXq=E7@;BGv3nm=axhVu=Ce{Ow!->7PAe} zcy`yy`IW@QUx1?djhJ3on{S7ta4_oqPCvhpIQLxuV}+H6njOo!mrHi*g%$n9w_C*E z=#8$}sMpo1*B9FLp{vvH<{BxGLq~*ny(z{~xYNC*ocE^g-4HnuTxtWU=r|eQ`dUm* zh29js*Av;7bT6Ci^iC!_uKf+F`2L9RU@T~hfm6MCA-MoUzU#X8hTi;jasfs}DuX%R z050L1%(Iww%eeZD`wJv^6>stU!gSYY<3P^)Kv0;qs2!;?wj%KL|3@+2$=-YMU% zKFAX1XF!+r{Mn>)s5it2G~mTyDtKGs^6}RCB8*Qp*`v$FqT*5SW#w{qEBG9T<+1r2 z=*{2yBhbhuyK{~99K@Yj??3Cm0O(CHEZ!@*=C6_qEwEm_e^E(Jb)cC`eogp<5gzld zQcd6{3$p&Vy>+BGK4{aVV^PVBKX*DTmp(LDv3 z34Tf|*tMEr6}1zFGwG(&=~LU;RMj+GQ{&DVjoX?zYZj)tsm)Y)*`DSaFPqwg;cREt z{_+H81$(+$wC>?r!Kha4g27ETKJxGwV=fyxZGwwbQ8t-!u@5X&U@BF_!c=iPOYPYW z1^Bo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`EW?j`7ReST$u zs((xgWvuF}ehdTOtp3ks`zJ&7RXr7dMb^hXkXnoUboam4gA!Fw#mAvYr2`e*DK>uk zi{h}h3>5E4bnT;1ll8;>Qtyu%w5Q=gpW*sPWc{x~8MSczuR|dY)i4!7x{R9%m}(!_ z51#j`zFI$yF`l(}F9_GiyG3~I0AlTUSpYj18n957;hwr#{U6Bwe@l1? If^hx+0jO}b?f?J) From 453dbd1f14cd445a86ceda680ecb1ce666094182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ant=C3=B4nio?= Date: Fri, 5 Jan 2024 02:29:36 -0400 Subject: [PATCH 06/15] chore(watch): Small changes on code formatting because this is an "Old sock" project. I know that it may not look as hip as it looked before, but at least it's more conformant with the decades-old code style that this project made question of perpetuate, it's also more simple for someone studying this code. --- watch/watch.c | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/watch/watch.c b/watch/watch.c index a463bd8..a9102a6 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -29,20 +29,13 @@ #include static char *progname; +int main(int argc, char *argv[]); +void usage(void); struct Flag { int Beep_on_error, No_title; }; static struct Flag flag; -void usage(void) { - pfmt( - stderr, MM_NOSTD, - "usage: %s [-n seconds] [-bt] command [args...]\n", - progname - ); - exit(1); -} - int main(int argc, char *argv[]) { progname = argv[0]; int option = 0; @@ -69,7 +62,9 @@ int main(int argc, char *argv[]) { while ((option = getopt(argc, argv, "n:hbt")) != -1) { switch (option) { case 'n': - if (!optarg) break; + if (!optarg) { + break; + } char arg[128]; char *afterpoint = NULL; @@ -85,8 +80,11 @@ int main(int argc, char *argv[]) { } } - if (strlen(arg) == 0) interval.tv_sec = 0; - else interval.tv_sec = atoi(arg); + if (strlen(arg) == 0) { + interval.tv_sec = 0; + } else { + interval.tv_sec = atoi(arg); + } size_t afterpointlen = afterpoint ? strlen(afterpoint) : 0; if (afterpointlen > 0) { @@ -119,24 +117,28 @@ int main(int argc, char *argv[]) { } } - // FIXME: not good practise + // FIXME: Not a good practice. argc -= optind; argv += optind; // Missing operand - if (argc < 1) usage(); + if (argc < 1) { + usage(); + } // Now we just have to copy the "rest" of argv[] to a new character // array allocating some space in memory with calloc(3) and then // copying using a for loop. if ((commandv = calloc((unsigned long)(argc + 1), sizeof(char *))) == NULL) { - // Should i use prerror? + // Should I use prerror? perror("couldn't callocate"); exit(-1); } - for (c = 0; c < argc; c++) commandv[c] = argv[c]; + for (c = 0; c < argc; c++) { + commandv[c] = argv[c]; + } // Initialize curses terminal with colours to be used. // Get terminal size too, we're going to need it. @@ -215,3 +217,11 @@ int main(int argc, char *argv[]) { } } +void usage(void) { + pfmt( + stderr, MM_NOSTD, + "usage: %s [-n seconds] [-bt] command [args...]\n", + progname + ); + exit(1); +} From 2c38487a967e16a9df7ecfef06788db5b02739d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ant=C3=B4nio?= Date: Fri, 5 Jan 2024 02:52:53 -0400 Subject: [PATCH 07/15] chore(watch): Small changes on code formatting because this is an "Old sock" project. I know that it may not look as hip as it looked before, but at least it's more conformant with the decades-old code style that this project made question of perpetuate, it's also more simple for someone studying this code. Also explained the code a little bit more than the necessary. --- watch/watch.c | 72 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/watch/watch.c b/watch/watch.c index a9102a6..1203d81 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -1,19 +1,19 @@ -/* - * watch.c - Keep an eye on a command output - */ -/* - * Copyright (C) 2023: Luiz Antônio Rangel (takusuman) - * Arthur Bacci (arthurbacci) - * - * SPDX-Licence-Identifier: Zlib - * - * Support for calling commands with arguments without having to escape them - * with double-dash thoroughly based of IIJ's iwatch(1). - * As per the copyright header of IIJ's iwatch.c: - * Copyright (c) 2000, 2001 Internet Initiative Japan Inc. - * - * SPDX-Licence-Identifier: BSD-2-Clause - */ +// +// watch.c - Keep an eye on a command output +// +// +// Copyright (C) 2023: Luiz Antônio Rangel (takusuman) +// Arthur Bacci (arthurbacci) +// +// SPDX-Licence-Identifier: Zlib +// +// Support for calling commands with arguments without having to escape them +// with double-dash thoroughly based of IIJ's iwatch(1). +// As per the copyright header of IIJ's iwatch.c: +// Copyright (c) 2000, 2001 Internet Initiative Japan Inc. +// +// SPDX-Licence-Identifier: BSD-2-Clause +// #include #include @@ -44,13 +44,13 @@ int main(int argc, char *argv[]) { char **commandv; pid_t exec_pid; - /* Default interval of 2 seconds, as other major implementations - usually do */ + // Default interval of 2 seconds, as other major + // implementations usually do. interval.tv_sec = 2; // Variables for the information header. - // Defining nodename from now, since it shouldn't change while we're - // watching the command. + // Defining nodename from now, since it shouldn't change + // while we're watching the command. time_t now; struct tm *timeinfo; struct utsname u; @@ -101,8 +101,9 @@ int main(int argc, char *argv[]) { interval.tv_nsec = integer; } - if (interval.tv_sec == 0 && interval.tv_nsec < 100000000) + if (interval.tv_sec == 0 && interval.tv_nsec < 100000000) { interval.tv_nsec = 100000000; + } break; case 'b': @@ -121,7 +122,7 @@ int main(int argc, char *argv[]) { argc -= optind; argv += optind; - // Missing operand + // Missing operand. if (argc < 1) { usage(); } @@ -129,7 +130,6 @@ int main(int argc, char *argv[]) { // Now we just have to copy the "rest" of argv[] to a new character // array allocating some space in memory with calloc(3) and then // copying using a for loop. - if ((commandv = calloc((unsigned long)(argc + 1), sizeof(char *))) == NULL) { // Should I use prerror? perror("couldn't callocate"); @@ -160,14 +160,22 @@ int main(int argc, char *argv[]) { int left_len = 0, right_len = 0; attron(COLOR_PAIR(1) | A_BOLD); - + + // FIXME: When ones use "-n 0.1", it actually prints + // "0.100000000" instead of 0.1 or even 0.10. + // That could be fixed --- although not being a real + // problem --- aiming for less visual pollution on the + // status bar. + // Maybe this could be fixed by multiplying the interval + // by a nanosecond ratio on nanosleep()? left_len = snprintf( left, 256, "Every %d.%d second(s): %s", (int)interval.tv_sec, (int)interval.tv_nsec, argv[0] ); - // This is done because ctime returns a string with \n + // This is done because ctime returns + // a string with '\n'. strftime(time, 256, "%c", timeinfo); right_len = snprintf( @@ -175,12 +183,24 @@ int main(int argc, char *argv[]) { u.nodename, time ); + // I think that a case involving left_len and right_len, + // which contain the "Every (int).(int) second(s): (string)" + // and "hostname: date". respectively, is improbable, so + // this is just a pure formality in case of one's curses + // implementation having a faulty snprintf(). + // EXIT_FAILURE could be a more "frightening" code that + // gives a clue about the seriousness of this error on + // his/her system curses implementation. if (left_len <= 0 || right_len <= 0) { perror("please try to execute without the bar\n"); exit(EXIT_FAILURE); } - + if (left_len <= term_x && right_len <= term_x) { + // If the sum of the text on left and right + // sections of the bar is larger than the + // terminal x axis, it shall be justified. + // Else, keep it as normal. if (left_len + right_len >= term_x) { printw("%-*s", term_x, left); printw("%*s", term_x, right); From 6555ec3adc768da6cfe73953cda0bde75f1333e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ant=C3=B4nio?= Date: Sun, 7 Jan 2024 00:34:34 -0400 Subject: [PATCH 08/15] chore(watch): Small correction at right_len and left_len explanation and "status bar" naming. --- watch/watch.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/watch/watch.c b/watch/watch.c index 1203d81..09952a7 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -185,15 +185,12 @@ int main(int argc, char *argv[]) { // I think that a case involving left_len and right_len, // which contain the "Every (int).(int) second(s): (string)" - // and "hostname: date". respectively, is improbable, so - // this is just a pure formality in case of one's curses - // implementation having a faulty snprintf(). - // EXIT_FAILURE could be a more "frightening" code that - // gives a clue about the seriousness of this error on - // his/her system curses implementation. + // and "hostname: date", respectively, is improbable, so + // this is just a pure formality in case of one's + // C library implementation having a faulty snprintf(). if (left_len <= 0 || right_len <= 0) { - perror("please try to execute without the bar\n"); - exit(EXIT_FAILURE); + perror("please try to execute it without the information header.\n"); + exit(255); } if (left_len <= term_x && right_len <= term_x) { From 9c1071d974a0a5585760ad31706b66407171ab4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ant=C3=B4nio?= Date: Sun, 7 Jan 2024 00:47:06 -0400 Subject: [PATCH 09/15] chore(watch): Made comments conform to the C standard, not to the C++ comment syntax --- although it's possible to use it since C99. --- watch/watch.c | 126 +++++++++++++++++++++++++++++--------------------- 1 file changed, 73 insertions(+), 53 deletions(-) diff --git a/watch/watch.c b/watch/watch.c index 09952a7..cd047ef 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -1,19 +1,19 @@ -// -// watch.c - Keep an eye on a command output -// -// -// Copyright (C) 2023: Luiz Antônio Rangel (takusuman) -// Arthur Bacci (arthurbacci) -// -// SPDX-Licence-Identifier: Zlib -// -// Support for calling commands with arguments without having to escape them -// with double-dash thoroughly based of IIJ's iwatch(1). -// As per the copyright header of IIJ's iwatch.c: -// Copyright (c) 2000, 2001 Internet Initiative Japan Inc. -// -// SPDX-Licence-Identifier: BSD-2-Clause -// +/* + * watch.c - Keep an eye on a command output + * + */ +/* Copyright (C) 2023: Luiz Antônio Rangel (takusuman) + * Arthur Bacci (arthurbacci) + * + * SPDX-Licence-Identifier: Zlib + * + * Support for calling commands with arguments without having to escape them + * with double-dash thoroughly based of IIJ's iwatch(1). + * As per the copyright header of IIJ's iwatch.c: + * Copyright (c) 2000, 2001 Internet Initiative Japan Inc. + * + * SPDX-Licence-Identifier: BSD-2-Clause +*/ #include #include @@ -44,13 +44,17 @@ int main(int argc, char *argv[]) { char **commandv; pid_t exec_pid; - // Default interval of 2 seconds, as other major - // implementations usually do. + /* + * Default interval of 2 seconds, as other major + * implementations usually do. + */ interval.tv_sec = 2; - // Variables for the information header. - // Defining nodename from now, since it shouldn't change - // while we're watching the command. + /* + * Variables for the information header. + * Defining nodename from now, since it shouldn't change + * while we're watching the command. + */ time_t now; struct tm *timeinfo; struct utsname u; @@ -118,20 +122,22 @@ int main(int argc, char *argv[]) { } } - // FIXME: Not a good practice. + /* FIXME: Not a good practice. */ argc -= optind; argv += optind; - // Missing operand. + /* Missing operand. */ if (argc < 1) { usage(); } - // Now we just have to copy the "rest" of argv[] to a new character - // array allocating some space in memory with calloc(3) and then - // copying using a for loop. + /* + * Now we just have to copy the "rest" of argv[] to a new character + * array allocating some space in memory with calloc(3) and then + * copying using a for loop. + */ if ((commandv = calloc((unsigned long)(argc + 1), sizeof(char *))) == NULL) { - // Should I use prerror? + /* Should I use prerror? */ perror("couldn't callocate"); exit(-1); } @@ -140,18 +146,20 @@ int main(int argc, char *argv[]) { commandv[c] = argv[c]; } - // Initialize curses terminal with colours to be used. - // Get terminal size too, we're going to need it. + /* + * Initialize curses terminal with colours to be used. + * Get terminal size too, we're going to need it. + */ newterm(getenv("TERM"), stdout, stdin); start_color(); init_pair(1, COLOR_BLACK, COLOR_WHITE); for (;;) { - // Clear terminal for the next cycle. + /* Clear terminal for the next cycle. */ clear(); getmaxyx(stdscr, term_y, term_x); - // Get current time to be passed as a string with ctime(3). + /* Get current time to be passed as a string with ctime(3). */ time(&now); timeinfo = localtime(&now); @@ -161,21 +169,25 @@ int main(int argc, char *argv[]) { attron(COLOR_PAIR(1) | A_BOLD); - // FIXME: When ones use "-n 0.1", it actually prints - // "0.100000000" instead of 0.1 or even 0.10. - // That could be fixed --- although not being a real - // problem --- aiming for less visual pollution on the - // status bar. - // Maybe this could be fixed by multiplying the interval - // by a nanosecond ratio on nanosleep()? + /* + * FIXME: When ones use "-n 0.1", it actually prints + * "0.100000000" instead of 0.1 or even 0.10. + * That could be fixed --- although not being a real + * problem --- aiming for less visual pollution on the + * information header. + * Maybe this could be fixed by multiplying the interval + * by a nanosecond ratio on nanosleep()? + */ left_len = snprintf( left, 256, "Every %d.%d second(s): %s", (int)interval.tv_sec, (int)interval.tv_nsec, argv[0] ); - // This is done because ctime returns - // a string with '\n'. + /* + * This is done because ctime returns + * a string with '\n'. + */ strftime(time, 256, "%c", timeinfo); right_len = snprintf( @@ -183,21 +195,25 @@ int main(int argc, char *argv[]) { u.nodename, time ); - // I think that a case involving left_len and right_len, - // which contain the "Every (int).(int) second(s): (string)" - // and "hostname: date", respectively, is improbable, so - // this is just a pure formality in case of one's - // C library implementation having a faulty snprintf(). + /* + * I think that a case involving left_len and right_len, + * which contain the "Every (int).(int) second(s): (string)" + * and "hostname: date", respectively, is improbable, so + * this is just a pure formality in case of one's + * C library implementation having a faulty snprintf(). + */ if (left_len <= 0 || right_len <= 0) { perror("please try to execute it without the information header.\n"); exit(255); } if (left_len <= term_x && right_len <= term_x) { - // If the sum of the text on left and right - // sections of the bar is larger than the - // terminal x axis, it shall be justified. - // Else, keep it as normal. + /* + * If the sum of the text on left and right + * sections of the bar is larger than the + * terminal x axis, it shall be justified. + * Else, keep it as normal. + */ if (left_len + right_len >= term_x) { printw("%-*s", term_x, left); printw("%*s", term_x, right); @@ -211,8 +227,10 @@ int main(int argc, char *argv[]) { } - // Not using endwin(3x), since it breaks with multiline - // also-curses programs, such as ls(1) with the "-l" option. + /* + * Not using endwin(3x), since it breaks with multiline + * also-curses programs, such as ls(1) with the "-l" option. + */ reset_shell_mode(); refresh(); @@ -224,8 +242,10 @@ int main(int argc, char *argv[]) { } waitpid(exec_pid, &ec, 0); - // execvp'd command hasn't exit with success and we have - // flag.Beep_on_error activated. + /* + * execvp'd command hasn't exit with success and we have + * flag.Beep_on_error activated. + */ if (ec != 0 && flag.Beep_on_error) { beep(); } From 0463e8d134b92c55aee67f3c308500da24e10d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ant=C3=B4nio?= Date: Sun, 7 Jan 2024 02:27:42 -0400 Subject: [PATCH 10/15] chore(watch): Grand refactor. --- watch/watch.c | 89 +++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/watch/watch.c b/watch/watch.c index cd047ef..4b25074 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -1,19 +1,20 @@ -/* +/* * watch.c - Keep an eye on a command output * */ -/* Copyright (C) 2023: Luiz Antônio Rangel (takusuman) - * Arthur Bacci (arthurbacci) +/* + * Copyright (C) 2023, 2024: Luiz Antônio Rangel (takusuman) + * Arthur Bacci (arthurbacci) * * SPDX-Licence-Identifier: Zlib * * Support for calling commands with arguments without having to escape them * with double-dash thoroughly based of IIJ's iwatch(1). * As per the copyright header of IIJ's iwatch.c: - * Copyright (c) 2000, 2001 Internet Initiative Japan Inc. + * Copyright (c) 2000, 2001 Internet Initiative Japan Inc. * * SPDX-Licence-Identifier: BSD-2-Clause -*/ + */ #include #include @@ -32,25 +33,24 @@ static char *progname; int main(int argc, char *argv[]); void usage(void); -struct Flag { - int Beep_on_error, No_title; -}; static struct Flag flag; - int main(int argc, char *argv[]) { progname = argv[0]; int option = 0; - struct timespec interval = {0}; - int c = 0, ec = 0, term_x = 0, term_y = 0; + int fBeep_on_error = 0, + fNo_title = 0, + c = 0, ec = 0, + term_x = 0; char **commandv; pid_t exec_pid; + struct timespec interval = {0}; - /* + /* * Default interval of 2 seconds, as other major * implementations usually do. */ interval.tv_sec = 2; - /* + /* * Variables for the information header. * Defining nodename from now, since it shouldn't change * while we're watching the command. @@ -69,7 +69,6 @@ int main(int argc, char *argv[]) { if (!optarg) { break; } - char arg[128]; char *afterpoint = NULL; strncpy(arg, optarg, 128); @@ -111,10 +110,10 @@ int main(int argc, char *argv[]) { break; case 'b': - flag.Beep_on_error = 1; + fBeep_on_error = 1; break; case 't': - flag.No_title = 1; + fNo_title = 1; break; case 'h': default: @@ -131,7 +130,7 @@ int main(int argc, char *argv[]) { usage(); } - /* + /* * Now we just have to copy the "rest" of argv[] to a new character * array allocating some space in memory with calloc(3) and then * copying using a for loop. @@ -146,7 +145,7 @@ int main(int argc, char *argv[]) { commandv[c] = argv[c]; } - /* + /* * Initialize curses terminal with colours to be used. * Get terminal size too, we're going to need it. */ @@ -154,37 +153,43 @@ int main(int argc, char *argv[]) { start_color(); init_pair(1, COLOR_BLACK, COLOR_WHITE); + /* + * Declaring the left side of the information header, which contains + * "Every χ second(s): ...", outside the loop because it's immutable + * after we got the program we're going to run and the amount of time. + */ + static int left_len = 0; + static char left[256]; + if (!fNo_title) { + /* + * FIXME: When one uses "-n 0.1", it actually prints + * "0.100000000" instead of 0.1 or even 0.10. + */ + left_len = snprintf( + left, 256, "Every %d.%d second(s): %s", + (int)interval.tv_sec, (int)interval.tv_nsec, + argv[0] + ); + } + for (;;) { /* Clear terminal for the next cycle. */ clear(); - getmaxyx(stdscr, term_y, term_x); + /* Get the terminal maximum x-axis size. */ + term_x = getmaxx(stdscr); + /* Get current time to be passed as a string with ctime(3). */ time(&now); timeinfo = localtime(&now); - if (!flag.No_title) { - char left[256], right[256], time[256]; - int left_len = 0, right_len = 0; + if (!fNo_title) { + int right_len = 0; /* "left_len = 0" declared above. */ + char right[256], time[256]; /* "left[256]" declared above. */ attron(COLOR_PAIR(1) | A_BOLD); /* - * FIXME: When ones use "-n 0.1", it actually prints - * "0.100000000" instead of 0.1 or even 0.10. - * That could be fixed --- although not being a real - * problem --- aiming for less visual pollution on the - * information header. - * Maybe this could be fixed by multiplying the interval - * by a nanosecond ratio on nanosleep()? - */ - left_len = snprintf( - left, 256, "Every %d.%d second(s): %s", - (int)interval.tv_sec, (int)interval.tv_nsec, - argv[0] - ); - - /* * This is done because ctime returns * a string with '\n'. */ @@ -195,7 +200,7 @@ int main(int argc, char *argv[]) { u.nodename, time ); - /* + /* * I think that a case involving left_len and right_len, * which contain the "Every (int).(int) second(s): (string)" * and "hostname: date", respectively, is improbable, so @@ -208,7 +213,7 @@ int main(int argc, char *argv[]) { } if (left_len <= term_x && right_len <= term_x) { - /* + /* * If the sum of the text on left and right * sections of the bar is larger than the * terminal x axis, it shall be justified. @@ -218,7 +223,7 @@ int main(int argc, char *argv[]) { printw("%-*s", term_x, left); printw("%*s", term_x, right); } else { - printw("%s%*s", left, term_x - left_len, right); + printw("%s%*s", left, (term_x - left_len), right); } printw("\n"); } @@ -244,9 +249,9 @@ int main(int argc, char *argv[]) { /* * execvp'd command hasn't exit with success and we have - * flag.Beep_on_error activated. + * fBeep_on_error activated. */ - if (ec != 0 && flag.Beep_on_error) { + if (ec != 0 && fBeep_on_error) { beep(); } From 4ec155d3ac6df80a5058aee16d33d1517eea6047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ant=C3=B4nio?= Date: Sun, 7 Jan 2024 02:31:52 -0400 Subject: [PATCH 11/15] chore(watch): Flag order at getopt(). --- watch/watch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/watch/watch.c b/watch/watch.c index 4b25074..f511ee5 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -63,7 +63,7 @@ int main(int argc, char *argv[]) { exit(-1); } - while ((option = getopt(argc, argv, "n:hbt")) != -1) { + while ((option = getopt(argc, argv, "n:bth")) != -1) { switch (option) { case 'n': if (!optarg) { From f0c97697d414c0f81b8cea699c103e555d7799d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ant=C3=B4nio?= Date: Sun, 7 Jan 2024 17:44:23 -0300 Subject: [PATCH 12/15] chore(watch): Remove ``static`` modifier from left_len and left declaration. --- watch/watch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/watch/watch.c b/watch/watch.c index f511ee5..d49959e 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -158,8 +158,8 @@ int main(int argc, char *argv[]) { * "Every χ second(s): ...", outside the loop because it's immutable * after we got the program we're going to run and the amount of time. */ - static int left_len = 0; - static char left[256]; + int left_len = 0; + char left[256]; if (!fNo_title) { /* * FIXME: When one uses "-n 0.1", it actually prints From 49780b5991eb38e8f127fcd0565e56827a2baf23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ant=C3=B4nio?= Date: Sun, 7 Jan 2024 17:53:44 -0300 Subject: [PATCH 13/15] chore(watch): Define a value for ``tv_sec`` and ``tv_nsec`` manually, not setting the entire struct as 0 since it implementation can change between UNIX-compatible systems without a lot of interference from POSIX. --- watch/watch.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/watch/watch.c b/watch/watch.c index d49959e..83efb89 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -42,13 +42,14 @@ int main(int argc, char *argv[]) { term_x = 0; char **commandv; pid_t exec_pid; - struct timespec interval = {0}; + struct timespec interval; /* * Default interval of 2 seconds, as other major * implementations usually do. */ interval.tv_sec = 2; + interval.tv_nsec = 0; /* * Variables for the information header. From 8f7b13874364ee1ad90a5ce65005e51ebcd04300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ant=C3=B4nio?= Date: Sun, 7 Jan 2024 18:15:05 -0300 Subject: [PATCH 14/15] chore(watch): Initialize `char left[]` for preventing the compiler considering it an "incomplete type". --- watch/watch.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/watch/watch.c b/watch/watch.c index 83efb89..c22564e 100644 --- a/watch/watch.c +++ b/watch/watch.c @@ -158,9 +158,12 @@ int main(int argc, char *argv[]) { * Declaring the left side of the information header, which contains * "Every χ second(s): ...", outside the loop because it's immutable * after we got the program we're going to run and the amount of time. + * Also initialize 'char left[]' with a null character for preventing + * it being considered an "incomplete type" by the compiler. */ int left_len = 0; char left[256]; + left[0] = '\0'; if (!fNo_title) { /* * FIXME: When one uses "-n 0.1", it actually prints From 03489939998a1e19edf35f15184d1b9fd2b63920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ant=C3=B4nio?= Date: Sun, 7 Jan 2024 18:36:01 -0300 Subject: [PATCH 15/15] docs(watch): Update manual page. --- watch/watch.1 | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/watch/watch.1 b/watch/watch.1 index dd98c37..9a4753e 100644 --- a/watch/watch.1 +++ b/watch/watch.1 @@ -17,34 +17,35 @@ This allows you to watch a program output change over the time. It accepts the following options: .TP .B \-n -Specifies the interval to refresh the output. +Specifies the interval to refresh the output. Per default, the +interval is of 2 seconds. .TP .B \-b Beeps if the program being run returns a non-zero exit status. .TP .B \-t Turns off the information header, letting just the verbatim program -output being refreshed every time on the screen. +output being refreshed every time on the screen \(em equivalent to a \fIksh\fR(1) +\fIfor ((;;))\fR accompanied by \fIsleep\fR(1). .br Not recommended, unless you're using \fItmux\fR(1) or another terminal multiplexer -that shows you information that would originally be shown by this header. +that shows you information that would originally be shown by that header. .SH EXAMPLES It can be very useful when backuping some disc and needing to see how many files were already copied. .br -Let's suppose you're copying a disc from a terminal and, in another, you want to -keep an eye on \fIdu\fR(1) output to see every minute if the file size matches +Lets suppose you're copying a disc from a terminal and, in another, you want to +keep an eye at \fIdu\fR(1) output to see every minute if the file size matches with what you're expecting. .IP \& 2 .BI "% watch -n 60 -b du -sh /dsk/1" .LP .SH NOTES -This implementation does not support floating point numbers as the time -interval, neither it has support to the "\fIWATCH_INTERVAL\fR" environment +This implementation does not support the "\fIWATCH_INTERVAL\fR" environment variable. .SH "SEE ALSO" execvp(3), -sleep(3), +nanosleep(2), curses(3X) .SH HISTORY The @@ -53,16 +54,16 @@ utility has a hazy history. Nobody seems to be actually sure of where it has appeared first. .br -According to procps 010114 watch C source code, where it first +According to procps 010114's watch C source code, where it first appeared in that package, it was written from scratch by Tony Rems in 1991, and later recieved many modifications and corrections by Francois Pinard. .br -However, according to Internet Initiative Japan Inc. iwatch +However, according to Internet Initiative Japan Inc.'s iwatch manual page, the watch utility came first from BSDI Inc. BSD/OS 3.1 and their code is slightly derived from it; he/she \(em who wrote the manual page and comments at the original source code \(em also -speculated that it's existence was earlier than BSD/OS 3.1, from some +speculated that its existence was earlier than BSD/OS 3.1, from some another free distribution, but there's no further information that supports this. .PP