progress.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Progress bar code.
  4. */
  5. /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
  6. * much of which was blatantly stolen from openssh.
  7. */
  8. /*-
  9. * Copyright (c) 1992, 1993
  10. * The Regents of the University of California. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * 3. BSD Advertising Clause omitted per the July 22, 1999 licensing change
  22. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
  23. *
  24. * 4. Neither the name of the University nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. * SUCH DAMAGE.
  39. */
  40. #include "libbb.h"
  41. #include "unicode.h"
  42. enum {
  43. /* Seconds when xfer considered "stalled" */
  44. STALLTIME = 5
  45. };
  46. static unsigned int get_tty2_width(void)
  47. {
  48. unsigned width;
  49. get_terminal_width_height(2, &width, NULL);
  50. return width;
  51. }
  52. void FAST_FUNC bb_progress_init(bb_progress_t *p)
  53. {
  54. p->start_sec = monotonic_sec();
  55. p->lastupdate_sec = p->start_sec;
  56. p->lastsize = 0;
  57. p->inited = 1;
  58. }
  59. void FAST_FUNC bb_progress_update(bb_progress_t *p,
  60. const char *curfile,
  61. off_t beg_range,
  62. off_t transferred,
  63. off_t totalsize)
  64. {
  65. uoff_t beg_and_transferred;
  66. unsigned since_last_update, elapsed;
  67. unsigned ratio;
  68. int barlength, i;
  69. /* totalsize == 0 if it is unknown */
  70. elapsed = monotonic_sec();
  71. since_last_update = elapsed - p->lastupdate_sec;
  72. /* Do not update on every call
  73. * (we can be called on every network read!) */
  74. if (since_last_update == 0 && !totalsize)
  75. return;
  76. beg_and_transferred = beg_range + transferred;
  77. ratio = 100;
  78. if (beg_and_transferred < totalsize) {
  79. /* Do not update on every call
  80. * (we can be called on every network read!) */
  81. if (since_last_update == 0)
  82. return;
  83. /* long long helps to have it working even if !LFS */
  84. ratio = 100ULL * beg_and_transferred / (uoff_t)totalsize;
  85. }
  86. #if ENABLE_UNICODE_SUPPORT
  87. init_unicode();
  88. /* libbb candidate? */
  89. {
  90. wchar_t wbuf21[21];
  91. char *buf = xstrdup(curfile);
  92. unsigned len;
  93. /* trim to 20 wide chars max (sets wbuf21[20] to 0)
  94. * also, in case mbstowcs fails, we at least
  95. * dont get garbage */
  96. memset(wbuf21, 0, sizeof(wbuf21));
  97. /* convert to wide chars, no more than 20 */
  98. len = mbstowcs(wbuf21, curfile, 20); /* NB: may return -1 */
  99. /* back to multibyte; cant overflow */
  100. wcstombs(buf, wbuf21, INT_MAX);
  101. len = (len > 20) ? 0 : 20 - len;
  102. fprintf(stderr, "\r%s%*s%4u%% ", buf, len, "", ratio);
  103. free(buf);
  104. }
  105. #else
  106. fprintf(stderr, "\r%-20.20s%4u%% ", curfile, ratio);
  107. #endif
  108. barlength = get_tty2_width() - 49;
  109. if (barlength > 0) {
  110. /* god bless gcc for variable arrays :) */
  111. char buf[barlength + 1];
  112. unsigned stars = (unsigned)barlength * ratio / (unsigned)100;
  113. memset(buf, ' ', barlength);
  114. buf[barlength] = '\0';
  115. memset(buf, '*', stars);
  116. fprintf(stderr, "|%s|", buf);
  117. }
  118. i = 0;
  119. while (beg_and_transferred >= 100000) {
  120. i++;
  121. beg_and_transferred >>= 10;
  122. }
  123. /* see http://en.wikipedia.org/wiki/Tera */
  124. fprintf(stderr, "%6u%c ", (unsigned)beg_and_transferred, " kMGTPEZY"[i]);
  125. #define beg_and_transferred dont_use_beg_and_transferred_below
  126. if (transferred > p->lastsize) {
  127. p->lastupdate_sec = elapsed;
  128. p->lastsize = transferred;
  129. if (since_last_update >= STALLTIME) {
  130. /* We "cut off" these seconds from elapsed time
  131. * by adjusting start time */
  132. p->start_sec += since_last_update;
  133. }
  134. since_last_update = 0; /* we are un-stalled now */
  135. }
  136. elapsed -= p->start_sec; /* now it's "elapsed since start" */
  137. if (since_last_update >= STALLTIME) {
  138. fprintf(stderr, " - stalled -");
  139. } else {
  140. off_t to_download = totalsize - beg_range;
  141. if (!totalsize || transferred <= 0 || (int)elapsed <= 0 || transferred > to_download) {
  142. fprintf(stderr, "--:--:-- ETA");
  143. } else {
  144. /* to_download / (transferred/elapsed) - elapsed: */
  145. /* (long long helps to have working ETA even if !LFS) */
  146. unsigned eta = (unsigned long long)to_download*elapsed/(uoff_t)transferred - elapsed;
  147. unsigned secs = eta % 3600;
  148. fprintf(stderr, "%02u:%02u:%02u ETA", eta / 3600, secs / 60, secs % 60);
  149. }
  150. }
  151. }