Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 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 | * |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 N/A */ |
| 11 | /* BB_AUDIT GNU defects -- only option -n is supported. */ |
| 12 | |
| 13 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 14 | * |
| 15 | * Removed dependency on date_main(), added proper error checking, and |
| 16 | * reduced size. |
| 17 | */ |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 18 | |
| 19 | #include <stdio.h> |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 22 | #include <limits.h> |
| 23 | #include <time.h> |
| 24 | #include <assert.h> |
| 25 | #include <unistd.h> |
Eric Andersen | 29f9b2f | 2002-12-11 04:25:02 +0000 | [diff] [blame] | 26 | #include <sys/wait.h> |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 27 | #include "busybox.h" |
| 28 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 29 | int watch_main(int argc, char **argv) |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 30 | { |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 31 | int width, len; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | unsigned period = 2; |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 33 | char **watched_argv, *header; |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 34 | |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 35 | if (argc < 2) bb_show_usage(); |
| 36 | |
| 37 | get_terminal_width_height(1, &width, 0); |
| 38 | header = xzalloc(width--); |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 39 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | /* don't use getopt, because it permutes the arguments */ |
| 41 | ++argv; |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 42 | if ((argc > 3) && !strcmp(*argv, "-n")) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | period = bb_xgetularg10_bnd(argv[1], 1, UINT_MAX); |
| 44 | argv += 2; |
| 45 | } |
| 46 | watched_argv = argv; |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 47 | |
| 48 | /* create header */ |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 49 | |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 50 | len = snprintf(header, width, "Every %ds:", period); |
| 51 | while (*argv && len<width) |
| 52 | snprintf(header+len, width-len, " %s", *(argv++)); |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 53 | |
| 54 | while (1) { |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 55 | char *thyme; |
| 56 | time_t t; |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 57 | |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 58 | time(&t); |
| 59 | thyme = ctime(&t); |
| 60 | len = strlen(thyme); |
| 61 | if (len < width) header[width-len] = 0; |
| 62 | |
| 63 | printf("\033[H\033[J%s %s\n", header, thyme); |
| 64 | |
| 65 | waitpid(bb_xspawn(watched_argv),0,0); |
| 66 | sleep(period); |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 67 | } |
Glenn L McGrath | 18b76e6 | 2002-09-16 09:10:04 +0000 | [diff] [blame] | 68 | } |