aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2002-09-16 09:10:04 +0000
committerbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2002-09-16 09:10:04 +0000
commit54cf79dd4a5fb8091181129e2b13b382e49744a3 (patch)
treea43358d389b2e594341e993c7a86d44369244423 /coreutils
parent153698e364ef609d40053a7465b0485428e4ff71 (diff)
downloadbusybox-w32-54cf79dd4a5fb8091181129e2b13b382e49744a3.tar.gz
busybox-w32-54cf79dd4a5fb8091181129e2b13b382e49744a3.tar.bz2
busybox-w32-54cf79dd4a5fb8091181129e2b13b382e49744a3.zip
Watch applet by Michael Habermann
git-svn-id: svn://busybox.net/trunk/busybox@5513 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/watch.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/coreutils/watch.c b/coreutils/watch.c
new file mode 100644
index 000000000..78ede4c1c
--- /dev/null
+++ b/coreutils/watch.c
@@ -0,0 +1,104 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini watch implementation for busybox
4 *
5 * Copyright (C) 2001 by Michael Habermann <mhabermann@gmx.de>
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22
23/* getopt not needed */
24
25#include <stdio.h>
26#include <errno.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <string.h>
30#include "busybox.h"
31
32extern int watch_main(int argc, char **argv)
33{
34 const char date_argv[2][10] = { "date", "" };
35 const char clrscr[] =
36 "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
37 const int header_len = 40;
38 char header[header_len + 1];
39 int period = 2;
40 char **cargv;
41 int cargc;
42 pid_t pid;
43 int old_stdout;
44 int i;
45
46 if (argc < 2) {
47 show_usage();
48 } else {
49 cargv = argv + 1;
50 cargc = argc - 1;
51
52 /* don't use getopt, because it permutes the arguments */
53 if (argc >= 3 && !strcmp(argv[1], "-n")) {
54 period = strtol(argv[2], NULL, 10);
55 if (period < 1)
56 show_usage();
57 cargv += 2;
58 cargc -= 2;
59 }
60 }
61
62
63 /* create header */
64 snprintf(header, header_len, "Every %ds: ", period);
65 for (i = 0; i < cargc && (strlen(header) + strlen(cargv[i]) < header_len);
66 i++) {
67 strcat(header, cargv[i]);
68 strcat(header, " ");
69 }
70
71 /* fill with blanks */
72 for (i = strlen(header); i < header_len; i++)
73 header[i] = ' ';
74
75 header[header_len - 1] = '\0';
76
77
78 /* thanks to lye, who showed me how to redirect stdin/stdout */
79 old_stdout = dup(1);
80
81 while (1) {
82 printf("%s%s", clrscr, header);
83 date_main(1, (char **) date_argv);
84 printf("\n");
85
86 pid = vfork(); /* vfork, because of ucLinux */
87 if (pid > 0) {
88 //parent
89 wait(0);
90 sleep(period);
91 } else if (0 == pid) {
92 //child
93 close(1);
94 dup(old_stdout);
95 if (execvp(*cargv, cargv))
96 error_msg_and_die("Couldn't run command\n");
97 } else {
98 error_msg_and_die("Couldn't vfork\n");
99 }
100 }
101
102
103 return EXIT_SUCCESS;
104}