b049377eaa93eb641b07744b0e29cd400209d8f7
[platform/upstream/curl.git] / src / tool_cb_prg.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 #include "tool_setup.h"
23
24 #define ENABLE_CURLX_PRINTF
25 /* use our own printf() functions */
26 #include "curlx.h"
27
28 #include "tool_cfgable.h"
29 #include "tool_cb_prg.h"
30 #include "tool_util.h"
31
32 #include "memdebug.h" /* keep this as LAST include */
33
34 /*
35 ** callback for CURLOPT_PROGRESSFUNCTION
36 */
37
38 #define MAX_BARLENGTH 256
39
40 int tool_progress_cb(void *clientp,
41                      double dltotal, double dlnow,
42                      double ultotal, double ulnow)
43 {
44   /* The original progress-bar source code was written for curl by Lars Aas,
45      and this new edition inherits some of his concepts. */
46
47   char line[MAX_BARLENGTH+1];
48   char format[40];
49   double frac;
50   double percent;
51   int barwidth;
52   int num;
53   struct timeval now = tvnow();
54   struct ProgressData *bar = (struct ProgressData *)clientp;
55   curl_off_t total;
56   curl_off_t point;
57
58   if(bar->calls && (tvdiff(now, bar->prevtime) < 200L))
59     /* after first call, limit progress-bar updating to 5 Hz */
60     return 0;
61
62   /* expected transfer size */
63   total = (curl_off_t)dltotal + (curl_off_t)ultotal + bar->initial_size;
64
65   /* we've come this far */
66   point = (curl_off_t)dlnow + (curl_off_t)ulnow + bar->initial_size;
67
68   if(point > total)
69     /* we have got more than the expected total! */
70     total = point;
71
72   /* simply count invokes */
73   bar->calls++;
74
75   if(total < 1) {
76     curl_off_t prevblock = bar->prev / 1024;
77     curl_off_t thisblock = point / 1024;
78     while(thisblock > prevblock) {
79       fprintf(bar->out, "#");
80       prevblock++;
81     }
82   }
83   else if(point != bar->prev) {
84     frac = (double)point / (double)total;
85     percent = frac * 100.0f;
86     barwidth = bar->width - 7;
87     num = (int) (((double)barwidth) * frac);
88     if(num > MAX_BARLENGTH)
89       num = MAX_BARLENGTH;
90     memset(line, '#', num);
91     line[num] = '\0';
92     snprintf(format, sizeof(format), "\r%%-%ds %%5.1f%%%%", barwidth);
93     fprintf(bar->out, format, line, percent);
94   }
95   fflush(bar->out);
96   bar->prev = point;
97   bar->prevtime = now;
98
99   return 0;
100 }
101
102 void progressbarinit(struct ProgressData *bar,
103                      struct Configurable *config)
104 {
105 #ifdef __EMX__
106   /* 20000318 mgs */
107   int scr_size[2];
108 #endif
109   char *colp;
110
111   memset(bar, 0, sizeof(struct ProgressData));
112
113   /* pass this through to progress function so
114    * it can display progress towards total file
115    * not just the part that's left. (21-may-03, dbyron) */
116   if(config->use_resume)
117     bar->initial_size = config->resume_from;
118
119 /* TODO: get terminal width through ansi escapes or something similar.
120    try to update width when xterm is resized... - 19990617 larsa */
121 #ifndef __EMX__
122   /* 20000318 mgs
123    * OS/2 users most likely won't have this env var set, and besides that
124    * we're using our own way to determine screen width */
125   colp = curlx_getenv("COLUMNS");
126   if(colp) {
127     char *endptr;
128     long num = strtol(colp, &endptr, 10);
129     if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 0))
130       bar->width = (int)num;
131     else
132       bar->width = 79;
133     curl_free(colp);
134   }
135   else
136     bar->width = 79;
137 #else
138   /* 20000318 mgs
139    * We use this emx library call to get the screen width, and subtract
140    * one from what we got in order to avoid a problem with the cursor
141    * advancing to the next line if we print a string that is as long as
142    * the screen is wide. */
143
144   _scrsize(scr_size);
145   bar->width = scr_size[0] - 1;
146 #endif
147
148   bar->out = config->errors;
149 }
150