~ubuntu-branches/ubuntu/lucid/curl/lucid-security

« back to all changes in this revision

Viewing changes to lib/progress.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2009-04-29 11:10:29 UTC
  • mfrom: (3.2.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090429111029-2j5eiyokfw2bw049
Tags: 7.19.4-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Drop build dependencies: stunnel, libdb4.6-dev, libssh2-1-dev
  - Add build-dependency on openssh-server
  - Drop libssh2-1-dev from libcurl4-openssl-dev's Depends.
  - Call automake-1.9 with --add-missing --copy --force
* drop debian/patches/security_CVE-2009-0037.patch 
  - this patch is part of 7.19.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: progress.c,v 1.85 2007-11-24 23:16:55 bagder Exp $
 
21
 * $Id: progress.c,v 1.92 2008-10-11 01:56:04 yangtse Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
25
25
 
26
 
#include <string.h>
27
 
#include <time.h>
28
 
 
29
 
#if defined(__EMX__)
30
 
#include <stdlib.h>
31
 
#endif
32
 
 
33
 
#include <curl/curl.h>
34
26
#include "urldata.h"
35
27
#include "sendf.h"
36
28
#include "progress.h"
40
32
 
41
33
/* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
42
34
   byte) */
43
 
static void time2str(char *r, long t)
 
35
static void time2str(char *r, curl_off_t seconds)
44
36
{
45
 
  long h;
46
 
  if(!t) {
 
37
  curl_off_t d, h, m, s;
 
38
  if(seconds <= 0) {
47
39
    strcpy(r, "--:--:--");
48
40
    return;
49
41
  }
50
 
  h = (t/3600);
51
 
  if(h <= 99) {
52
 
    long m = (t-(h*3600))/60;
53
 
    long s = (t-(h*3600)-(m*60));
54
 
    snprintf(r, 9, "%2ld:%02ld:%02ld",h,m,s);
 
42
  h = seconds / CURL_OFF_T_C(3600);
 
43
  if(h <= CURL_OFF_T_C(99)) {
 
44
    m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
 
45
    s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
 
46
    snprintf(r, 9, "%2" FORMAT_OFF_T ":%02" FORMAT_OFF_T ":%02" FORMAT_OFF_T,
 
47
             h, m, s);
55
48
  }
56
49
  else {
57
50
    /* this equals to more than 99 hours, switch to a more suitable output
58
51
       format to fit within the limits. */
59
 
    if(h/24 <= 999)
60
 
      snprintf(r, 9, "%3ldd %02ldh", h/24, h-(h/24)*24);
 
52
    d = seconds / CURL_OFF_T_C(86400);
 
53
    h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
 
54
    if(d <= CURL_OFF_T_C(999))
 
55
      snprintf(r, 9, "%3" FORMAT_OFF_T "d %02" FORMAT_OFF_T "h", d, h);
61
56
    else
62
 
      snprintf(r, 9, "%7ldd", h/24);
 
57
      snprintf(r, 9, "%7" FORMAT_OFF_T "d", d);
63
58
  }
64
59
}
65
60
 
68
63
   Add suffix k, M, G when suitable... */
69
64
static char *max5data(curl_off_t bytes, char *max5)
70
65
{
71
 
#define ONE_KILOBYTE 1024
72
 
#define ONE_MEGABYTE (1024* ONE_KILOBYTE)
73
 
#define ONE_GIGABYTE (1024* ONE_MEGABYTE)
74
 
#define ONE_TERABYTE ((curl_off_t)1024* ONE_GIGABYTE)
75
 
#define ONE_PETABYTE ((curl_off_t)1024* ONE_TERABYTE)
 
66
#define ONE_KILOBYTE  CURL_OFF_T_C(1024)
 
67
#define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
 
68
#define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
 
69
#define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
 
70
#define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
76
71
 
77
 
  if(bytes < 100000) {
 
72
  if(bytes < CURL_OFF_T_C(100000))
78
73
    snprintf(max5, 6, "%5" FORMAT_OFF_T, bytes);
79
 
  }
80
 
  else if(bytes < (10000*ONE_KILOBYTE)) {
81
 
    snprintf(max5, 6, "%4" FORMAT_OFF_T "k", (curl_off_t)(bytes/ONE_KILOBYTE));
82
 
  }
83
 
  else if(bytes < (100*ONE_MEGABYTE)) {
 
74
 
 
75
  else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
 
76
    snprintf(max5, 6, "%4" FORMAT_OFF_T "k", bytes/ONE_KILOBYTE);
 
77
 
 
78
  else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
84
79
    /* 'XX.XM' is good as long as we're less than 100 megs */
85
 
    snprintf(max5, 6, "%2d.%0dM",
86
 
             (int)(bytes/ONE_MEGABYTE),
87
 
             (int)(bytes%ONE_MEGABYTE)/(ONE_MEGABYTE/10) );
88
 
  }
89
 
#if SIZEOF_CURL_OFF_T > 4
90
 
  else if(bytes < ( (curl_off_t)10000*ONE_MEGABYTE))
 
80
    snprintf(max5, 6, "%2" FORMAT_OFF_T ".%0" FORMAT_OFF_T "M",
 
81
              bytes/ONE_MEGABYTE,
 
82
             (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
 
83
 
 
84
#if (CURL_SIZEOF_CURL_OFF_T > 4)
 
85
 
 
86
  else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
91
87
    /* 'XXXXM' is good until we're at 10000MB or above */
92
 
    snprintf(max5, 6, "%4" FORMAT_OFF_T "M", (curl_off_t)(bytes/ONE_MEGABYTE));
 
88
    snprintf(max5, 6, "%4" FORMAT_OFF_T "M", bytes/ONE_MEGABYTE);
93
89
 
94
 
  else if(bytes < (curl_off_t)100*ONE_GIGABYTE)
 
90
  else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
95
91
    /* 10000 MB - 100 GB, we show it as XX.XG */
96
 
    snprintf(max5, 6, "%2d.%0dG",
97
 
             (int)(bytes/ONE_GIGABYTE),
98
 
             (int)(bytes%ONE_GIGABYTE)/(ONE_GIGABYTE/10) );
 
92
    snprintf(max5, 6, "%2" FORMAT_OFF_T ".%0" FORMAT_OFF_T "G",
 
93
              bytes/ONE_GIGABYTE,
 
94
             (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
99
95
 
100
 
  else if(bytes < (curl_off_t)10000 * ONE_GIGABYTE)
 
96
  else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
101
97
    /* up to 10000GB, display without decimal: XXXXG */
102
 
    snprintf(max5, 6, "%4dG", (int)(bytes/ONE_GIGABYTE));
 
98
    snprintf(max5, 6, "%4" FORMAT_OFF_T "G", bytes/ONE_GIGABYTE);
103
99
 
104
 
  else if(bytes < (curl_off_t)10000 * ONE_TERABYTE)
 
100
  else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
105
101
    /* up to 10000TB, display without decimal: XXXXT */
106
 
    snprintf(max5, 6, "%4dT", (int)(bytes/ONE_TERABYTE));
107
 
  else {
 
102
    snprintf(max5, 6, "%4" FORMAT_OFF_T "T", bytes/ONE_TERABYTE);
 
103
 
 
104
  else
108
105
    /* up to 10000PB, display without decimal: XXXXP */
109
 
    snprintf(max5, 6, "%4dP", (int)(bytes/ONE_PETABYTE));
 
106
    snprintf(max5, 6, "%4" FORMAT_OFF_T "P", bytes/ONE_PETABYTE);
110
107
 
111
 
    /* 16384 petabytes (16 exabytes) is maximum a 64 bit number can hold,
112
 
       but this type is signed so 8192PB will be max.*/
113
 
  }
 
108
    /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
 
109
       can hold, but our data type is signed so 8192PB will be the maximum. */
114
110
 
115
111
#else
 
112
 
116
113
  else
117
 
    snprintf(max5, 6, "%4" FORMAT_OFF_T "M", (curl_off_t)(bytes/ONE_MEGABYTE));
 
114
    snprintf(max5, 6, "%4" FORMAT_OFF_T "M", bytes/ONE_MEGABYTE);
 
115
 
118
116
#endif
119
117
 
120
118
  return max5;
172
170
    data->progress.t_connect =
173
171
      Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
174
172
    break;
 
173
  case TIMER_APPCONNECT:
 
174
    data->progress.t_appconnect =
 
175
      Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
 
176
    break;
175
177
  case TIMER_PRETRANSFER:
176
178
    data->progress.t_pretransfer =
177
179
      Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
229
231
  struct timeval now;
230
232
  int result;
231
233
  char max5[6][10];
232
 
  int dlpercen=0;
233
 
  int ulpercen=0;
234
 
  int total_percen=0;
 
234
  curl_off_t dlpercen=0;
 
235
  curl_off_t ulpercen=0;
 
236
  curl_off_t total_percen=0;
235
237
  curl_off_t total_transfer;
236
238
  curl_off_t total_expected_transfer;
237
 
  long timespent;
 
239
  curl_off_t timespent;
238
240
  struct SessionHandle *data = conn->data;
239
241
  int nowindex = data->progress.speeder_c% CURR_TIME;
240
242
  int checkindex;
242
244
  char time_left[10];
243
245
  char time_total[10];
244
246
  char time_spent[10];
245
 
  long ulestimate=0;
246
 
  long dlestimate=0;
247
 
  long total_estimate;
 
247
  curl_off_t ulestimate=0;
 
248
  curl_off_t dlestimate=0;
 
249
  curl_off_t total_estimate;
248
250
  bool shownow=FALSE;
249
251
 
250
252
  now = Curl_tvnow(); /* what time is it */
253
255
  data->progress.timespent =
254
256
    (double)(now.tv_sec - data->progress.start.tv_sec) +
255
257
    (double)(now.tv_usec - data->progress.start.tv_usec)/1000000.0;
256
 
  timespent = (long)data->progress.timespent;
 
258
  timespent = (curl_off_t)data->progress.timespent;
257
259
 
258
260
  /* The average download speed this far */
259
261
  data->progress.dlspeed = (curl_off_t)
312
314
        curl_off_t amount = data->progress.speeder[nowindex]-
313
315
          data->progress.speeder[checkindex];
314
316
 
315
 
        if(amount > 4294967 /* 0xffffffff/1000 */)
 
317
        if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
316
318
          /* the 'amount' value is bigger than would fit in 32 bits if
317
319
             multiplied with 1000, so we use the double math for this */
318
320
          data->progress.current_speed = (curl_off_t)
320
322
        else
321
323
          /* the 'amount' value is small enough to fit within 32 bits even
322
324
             when multiplied with 1000 */
323
 
          data->progress.current_speed = amount*1000/span_ms;
 
325
          data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
324
326
      }
325
327
    }
326
328
    else
369
371
 
370
372
    /* Figure out the estimated time of arrival for the upload */
371
373
    if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
372
 
       (data->progress.ulspeed>0) &&
373
 
       (data->progress.size_ul > 100) ) {
374
 
      ulestimate = (long)(data->progress.size_ul / data->progress.ulspeed);
375
 
      ulpercen = (int)(100*(data->progress.uploaded/100) /
376
 
                        (data->progress.size_ul/100) );
 
374
       (data->progress.ulspeed > CURL_OFF_T_C(0)) &&
 
375
       (data->progress.size_ul > CURL_OFF_T_C(100)) ) {
 
376
      ulestimate = data->progress.size_ul / data->progress.ulspeed;
 
377
      ulpercen = data->progress.uploaded /
 
378
                (data->progress.size_ul/CURL_OFF_T_C(100));
377
379
    }
378
380
 
379
381
    /* ... and the download */
380
382
    if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
381
 
       (data->progress.dlspeed>0) &&
382
 
       (data->progress.size_dl>100)) {
383
 
      dlestimate = (long)(data->progress.size_dl / data->progress.dlspeed);
384
 
      dlpercen = (int)(100*(data->progress.downloaded/100) /
385
 
                        (data->progress.size_dl/100));
 
383
       (data->progress.dlspeed > CURL_OFF_T_C(0)) &&
 
384
       (data->progress.size_dl > CURL_OFF_T_C(100))) {
 
385
      dlestimate = data->progress.size_dl / data->progress.dlspeed;
 
386
      dlpercen = data->progress.downloaded /
 
387
                (data->progress.size_dl/CURL_OFF_T_C(100));
386
388
    }
387
389
 
388
390
    /* Now figure out which of them is slower and use that one for the
405
407
    total_transfer = data->progress.downloaded + data->progress.uploaded;
406
408
 
407
409
    /* Get the percentage of data transfered so far */
408
 
    if(total_expected_transfer > 100)
409
 
      total_percen=(int)(100*(total_transfer/100) /
410
 
                         (total_expected_transfer/100) );
 
410
    if(total_expected_transfer > CURL_OFF_T_C(100))
 
411
      total_percen = total_transfer /
 
412
                    (total_expected_transfer/CURL_OFF_T_C(100));
411
413
 
412
414
    fprintf(data->set.err,
413
 
            "\r%3d %s  %3d %s  %3d %s  %s  %s %s %s %s %s",
 
415
            "\r"
 
416
            "%3" FORMAT_OFF_T " %s  "
 
417
            "%3" FORMAT_OFF_T " %s  "
 
418
            "%3" FORMAT_OFF_T " %s  %s  %s %s %s %s %s",
414
419
            total_percen,  /* 3 letters */                /* total % */
415
420
            max5data(total_expected_transfer, max5[2]),   /* total size */
416
421
            dlpercen,      /* 3 letters */                /* rcvd % */