1 /* vi: set sw=4 ts=4: */
3 * Mini watch implementation for busybox
5 * Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* BB_AUDIT SUSv3 N/A */
24 /* BB_AUDIT GNU defects -- only option -n is supported. */
26 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
28 * Removed dependency on date_main(), added proper error checking, and
42 int watch_main(int argc, char **argv)
44 const int header_len = 40;
51 char header[header_len + 1];
57 /* don't use getopt, because it permutes the arguments */
59 if ((argc > 3) && !strcmp(*argv, "-n")
61 period = bb_xgetularg10_bnd(argv[1], 1, UINT_MAX);
68 len = snprintf(header, header_len, "Every %ds:", period);
69 /* Don't bother checking for len < 0, as it should never happen.
70 * But, just to be prepared... */
74 if (len + len2 >= header_len-1) {
78 memcpy(header+len+1, *argv, len2);
84 /* thanks to lye, who showed me how to redirect stdin/stdout */
85 old_stdout = dup(STDOUT_FILENO);
89 /* Use dprintf to avoid fflush()ing stdout. */
90 if (dprintf(1, "\033[H\033[J%-*s%s\n", header_len, header, ctime(&t)) < 0) {
91 bb_perror_msg_and_die("printf");
94 pid = vfork(); /* vfork, because of ucLinux */
99 } else if (0 == pid) {
101 dup2(old_stdout, STDOUT_FILENO);
102 execvp(*watched_argv, watched_argv);
103 bb_perror_msg_and_die("%s", *watched_argv);
105 bb_perror_msg_and_die("vfork");