Added header to be included by dist script.
[platform/upstream/curl.git] / src / tool_progress.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, 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 "setup.h"
23
24 #include <curl/curl.h>
25
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
28 #include "curlx.h"
29
30 #include "tool_cfgable.h"
31 #include "tool_progress.h"
32
33 #include "memdebug.h" /* keep this as LAST include */
34
35 int my_progress(void *clientp,
36                 double dltotal, double dlnow,
37                 double ultotal, double ulnow)
38 {
39   /* The original progress-bar source code was written for curl by Lars Aas,
40      and this new edition inherits some of his concepts. */
41
42   char line[256];
43   char outline[256];
44   char format[40];
45   double frac;
46   double percent;
47   int barwidth;
48   int num;
49   int i;
50
51   struct ProgressData *bar = (struct ProgressData *)clientp;
52
53   /* expected transfer size */
54   curl_off_t total = (curl_off_t)dltotal + (curl_off_t)ultotal +
55     bar->initial_size;
56
57   /* we've come this far */
58   curl_off_t point = (curl_off_t)dlnow + (curl_off_t)ulnow +
59     bar->initial_size;
60
61   if(point > total)
62     /* we have got more than the expected total! */
63     total = point;
64
65   /* simply count invokes */
66   bar->calls++;
67
68   if(total < 1) {
69     curl_off_t prevblock = bar->prev / 1024;
70     curl_off_t thisblock = point / 1024;
71     while(thisblock > prevblock) {
72       fprintf(bar->out, "#");
73       prevblock++;
74     }
75   }
76   else {
77     frac = (double)point / (double)total;
78     percent = frac * 100.0f;
79     barwidth = bar->width - 7;
80     num = (int) (((double)barwidth) * frac);
81     for(i = 0; i < num; i++)
82       line[i] = '#';
83     line[i] = '\0';
84     snprintf(format, sizeof(format), "%%-%ds %%5.1f%%%%", barwidth);
85     snprintf(outline, sizeof(outline), format, line, percent);
86     fprintf(bar->out, "\r%s", outline);
87   }
88   fflush(bar->out);
89   bar->prev = point;
90
91   return 0;
92 }
93
94 void progressbarinit(struct ProgressData *bar,
95                      struct Configurable *config)
96 {
97 #ifdef __EMX__
98   /* 20000318 mgs */
99   int scr_size[2];
100 #endif
101   char *colp;
102
103   memset(bar, 0, sizeof(struct ProgressData));
104
105   /* pass this through to progress function so
106    * it can display progress towards total file
107    * not just the part that's left. (21-may-03, dbyron) */
108   if(config->use_resume)
109     bar->initial_size = config->resume_from;
110
111 /* TODO: get terminal width through ansi escapes or something similar.
112    try to update width when xterm is resized... - 19990617 larsa */
113 #ifndef __EMX__
114   /* 20000318 mgs
115    * OS/2 users most likely won't have this env var set, and besides that
116    * we're using our own way to determine screen width */
117   colp = curlx_getenv("COLUMNS");
118   if(colp) {
119     char *endptr;
120     long num = strtol(colp, &endptr, 10);
121     if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 0))
122       bar->width = (int)num;
123     else
124       bar->width = 79;
125     curl_free(colp);
126   }
127   else
128     bar->width = 79;
129 #else
130   /* 20000318 mgs
131    * We use this emx library call to get the screen width, and subtract
132    * one from what we got in order to avoid a problem with the cursor
133    * advancing to the next line if we print a string that is as long as
134    * the screen is wide. */
135
136   _scrsize(scr_size);
137   bar->width = scr_size[0] - 1;
138 #endif
139
140   bar->out = config->errors;
141 }
142