diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-09-16 09:10:04 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-09-16 09:10:04 +0000 |
commit | 18b76e6f8047edf2e9e3d8fcc962a2e40ed8ffbd (patch) | |
tree | a43358d389b2e594341e993c7a86d44369244423 | |
parent | 8fede28c7408b2231a50c88da22c2e07f26f6d56 (diff) | |
download | busybox-w32-18b76e6f8047edf2e9e3d8fcc962a2e40ed8ffbd.tar.gz busybox-w32-18b76e6f8047edf2e9e3d8fcc962a2e40ed8ffbd.tar.bz2 busybox-w32-18b76e6f8047edf2e9e3d8fcc962a2e40ed8ffbd.zip |
Watch applet by Michael Habermann
-rw-r--r-- | coreutils/watch.c | 104 | ||||
-rw-r--r-- | include/applets.h | 3 | ||||
-rw-r--r-- | include/usage.h | 12 |
3 files changed, 119 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 | |||
32 | extern 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 | } | ||
diff --git a/include/applets.h b/include/applets.h index d5c257052..de07cc960 100644 --- a/include/applets.h +++ b/include/applets.h | |||
@@ -532,6 +532,9 @@ | |||
532 | #ifdef CONFIG_VLOCK | 532 | #ifdef CONFIG_VLOCK |
533 | APPLET(vlock, vlock_main, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS) | 533 | APPLET(vlock, vlock_main, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS) |
534 | #endif | 534 | #endif |
535 | #ifdef CONFIG_WATCH | ||
536 | APPLET(watch, watch_main, _BB_DIR_BIN, _BB_SUID_NEVER) | ||
537 | #endif | ||
535 | #ifdef CONFIG_WATCHDOG | 538 | #ifdef CONFIG_WATCHDOG |
536 | APPLET(watchdog, watchdog_main, _BB_DIR_SBIN, _BB_SUID_NEVER) | 539 | APPLET(watchdog, watchdog_main, _BB_DIR_SBIN, _BB_SUID_NEVER) |
537 | #endif | 540 | #endif |
diff --git a/include/usage.h b/include/usage.h index 968823e54..1cdaa6681 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -2076,6 +2076,18 @@ | |||
2076 | "Options:\n" \ | 2076 | "Options:\n" \ |
2077 | "\t-a\tLock all VTs" | 2077 | "\t-a\tLock all VTs" |
2078 | 2078 | ||
2079 | #define watch_trivial_usage \ | ||
2080 | "[-n <seconds>] COMMAND..." | ||
2081 | #define watch_full_usage \ | ||
2082 | "Executes a program periodically.\n" \ | ||
2083 | "Options:\n" \ | ||
2084 | "\t-n\tLoop period in seconds - default is 2." | ||
2085 | #define watch_example_usage \ | ||
2086 | "$ watch date\n" \ | ||
2087 | "Mon Dec 17 10:31:40 GMT 2000\n" \ | ||
2088 | "Mon Dec 17 10:31:42 GMT 2000\n" \ | ||
2089 | "Mon Dec 17 10:31:44 GMT 2000" | ||
2090 | |||
2079 | #define watchdog_trivial_usage \ | 2091 | #define watchdog_trivial_usage \ |
2080 | "DEV" | 2092 | "DEV" |
2081 | #define watchdog_full_usage \ | 2093 | #define watchdog_full_usage \ |