watch.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /* BB_AUDIT SUSv3 N/A */
  23. /* BB_AUDIT GNU defects -- only option -n is supported. */
  24. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  25. *
  26. * Removed dependency on date_main(), added proper error checking, and
  27. * reduced size.
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <limits.h>
  33. #include <time.h>
  34. #include <assert.h>
  35. #include <unistd.h>
  36. #include <sys/wait.h>
  37. #include "busybox.h"
  38. extern int watch_main(int argc, char **argv)
  39. {
  40. const int header_len = 40;
  41. time_t t;
  42. pid_t pid;
  43. unsigned period = 2;
  44. int old_stdout;
  45. int len, len2;
  46. char **watched_argv;
  47. char header[header_len + 1];
  48. if (argc < 2) {
  49. bb_show_usage();
  50. }
  51. /* don't use getopt, because it permutes the arguments */
  52. ++argv;
  53. if ((argc > 3) && !strcmp(*argv, "-n")
  54. ) {
  55. period = bb_xgetularg10_bnd(argv[1], 1, UINT_MAX);
  56. argv += 2;
  57. }
  58. watched_argv = argv;
  59. /* create header */
  60. len = snprintf(header, header_len, "Every %ds:", period);
  61. /* Don't bother checking for len < 0, as it should never happen.
  62. * But, just to be prepared... */
  63. assert(len >= 0);
  64. do {
  65. len2 = strlen(*argv);
  66. if (len + len2 >= header_len-1) {
  67. break;
  68. }
  69. header[len] = ' ';
  70. memcpy(header+len+1, *argv, len2);
  71. len += len2+1;
  72. } while (*++argv);
  73. header[len] = 0;
  74. /* thanks to lye, who showed me how to redirect stdin/stdout */
  75. old_stdout = dup(STDOUT_FILENO);
  76. while (1) {
  77. time(&t);
  78. /* Use dprintf to avoid fflush()ing stdout. */
  79. if (dprintf(1, "\033[H\033[J%-*s%s\n", header_len, header, ctime(&t)) < 0) {
  80. bb_perror_msg_and_die("printf");
  81. }
  82. pid = vfork(); /* vfork, because of ucLinux */
  83. if (pid > 0) {
  84. //parent
  85. wait(0);
  86. sleep(period);
  87. } else if (0 == pid) {
  88. //child
  89. dup2(old_stdout, STDOUT_FILENO);
  90. execvp(*watched_argv, watched_argv);
  91. bb_perror_msg_and_die(*watched_argv);
  92. } else {
  93. bb_perror_msg_and_die("vfork");
  94. }
  95. }
  96. }