2002-09-16 09:10:04 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini watch implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* BB_AUDIT SUSv3 N/A */
|
|
|
|
/* BB_AUDIT GNU defects -- only option -n is supported. */
|
|
|
|
|
|
|
|
/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
|
|
|
|
*
|
|
|
|
* Removed dependency on date_main(), added proper error checking, and
|
|
|
|
* reduced size.
|
|
|
|
*/
|
2002-09-16 09:10:04 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2003-03-19 09:13:01 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
2002-12-11 04:25:02 +00:00
|
|
|
#include <sys/wait.h>
|
2002-09-16 09:10:04 +00:00
|
|
|
#include "busybox.h"
|
|
|
|
|
2006-03-06 20:47:33 +00:00
|
|
|
int watch_main(int argc, char **argv)
|
2002-09-16 09:10:04 +00:00
|
|
|
{
|
|
|
|
const int header_len = 40;
|
2003-03-19 09:13:01 +00:00
|
|
|
time_t t;
|
2002-09-16 09:10:04 +00:00
|
|
|
pid_t pid;
|
2003-03-19 09:13:01 +00:00
|
|
|
unsigned period = 2;
|
2002-09-16 09:10:04 +00:00
|
|
|
int old_stdout;
|
2003-03-19 09:13:01 +00:00
|
|
|
int len, len2;
|
|
|
|
char **watched_argv;
|
|
|
|
char header[header_len + 1];
|
2002-09-16 09:10:04 +00:00
|
|
|
|
|
|
|
if (argc < 2) {
|
2003-03-19 09:13:01 +00:00
|
|
|
bb_show_usage();
|
2002-09-16 09:10:04 +00:00
|
|
|
}
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* don't use getopt, because it permutes the arguments */
|
|
|
|
++argv;
|
|
|
|
if ((argc > 3) && !strcmp(*argv, "-n")
|
|
|
|
) {
|
|
|
|
period = bb_xgetularg10_bnd(argv[1], 1, UINT_MAX);
|
|
|
|
argv += 2;
|
2002-09-16 09:10:04 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
watched_argv = argv;
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* create header */
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
len = snprintf(header, header_len, "Every %ds:", period);
|
|
|
|
/* Don't bother checking for len < 0, as it should never happen.
|
|
|
|
* But, just to be prepared... */
|
|
|
|
assert(len >= 0);
|
|
|
|
do {
|
|
|
|
len2 = strlen(*argv);
|
|
|
|
if (len + len2 >= header_len-1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
header[len] = ' ';
|
|
|
|
memcpy(header+len+1, *argv, len2);
|
|
|
|
len += len2+1;
|
|
|
|
} while (*++argv);
|
2002-09-16 09:10:04 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
header[len] = 0;
|
2002-09-16 09:10:04 +00:00
|
|
|
|
|
|
|
/* thanks to lye, who showed me how to redirect stdin/stdout */
|
2005-06-30 03:43:14 +00:00
|
|
|
old_stdout = dup(STDOUT_FILENO);
|
2002-09-16 09:10:04 +00:00
|
|
|
|
|
|
|
while (1) {
|
2003-03-19 09:13:01 +00:00
|
|
|
time(&t);
|
|
|
|
/* Use dprintf to avoid fflush()ing stdout. */
|
|
|
|
if (dprintf(1, "\033[H\033[J%-*s%s\n", header_len, header, ctime(&t)) < 0) {
|
|
|
|
bb_perror_msg_and_die("printf");
|
|
|
|
}
|
2002-09-16 09:10:04 +00:00
|
|
|
|
|
|
|
pid = vfork(); /* vfork, because of ucLinux */
|
|
|
|
if (pid > 0) {
|
|
|
|
//parent
|
|
|
|
wait(0);
|
|
|
|
sleep(period);
|
|
|
|
} else if (0 == pid) {
|
|
|
|
//child
|
2005-06-30 03:43:14 +00:00
|
|
|
dup2(old_stdout, STDOUT_FILENO);
|
2005-05-28 23:36:38 +00:00
|
|
|
execvp(*watched_argv, watched_argv);
|
2006-03-23 02:07:20 +00:00
|
|
|
bb_perror_msg_and_die("%s", *watched_argv);
|
2002-09-16 09:10:04 +00:00
|
|
|
} else {
|
2005-05-28 23:36:38 +00:00
|
|
|
bb_perror_msg_and_die("vfork");
|
2002-09-16 09:10:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|