curl tool: reviewed code moved to tool_*.[ch] files
[platform/upstream/curl.git] / src / tool_writeenv.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 #ifdef USE_ENVIRONMENT
25
26 #ifdef __riscos__
27 #  include <kernel.h>
28 #endif
29
30 #include <curl/curl.h>
31
32 #define _MPRINTF_REPLACE /* use our functions only */
33 #include <curl/mprintf.h>
34
35 #include "tool_writeenv.h"
36
37 #include "memdebug.h" /* keep this as LAST include */
38
39 static const struct
40 {
41   const char * name;
42   CURLINFO id;
43   enum {
44     writeenv_NONE,
45     writeenv_DOUBLE,
46     writeenv_LONG,
47     writeenv_STRING
48   } type;
49 } variables[14] =
50 {
51   {"curl_url_effective", CURLINFO_EFFECTIVE_URL, writeenv_STRING},
52   {"curl_http_code", CURLINFO_RESPONSE_CODE, writeenv_LONG},
53   {"curl_time_total", CURLINFO_TOTAL_TIME, writeenv_DOUBLE},
54   {"curl_time_namelookup", CURLINFO_NAMELOOKUP_TIME, writeenv_DOUBLE},
55   {"curl_time_connect", CURLINFO_CONNECT_TIME, writeenv_DOUBLE},
56   {"curl_time_pretransfer", CURLINFO_PRETRANSFER_TIME, writeenv_DOUBLE},
57   {"curl_time_starttransfer", CURLINFO_STARTTRANSFER_TIME, writeenv_DOUBLE},
58   {"curl_size_header", CURLINFO_HEADER_SIZE, writeenv_LONG},
59   {"curl_size_request", CURLINFO_REQUEST_SIZE, writeenv_LONG},
60   {"curl_size_download", CURLINFO_SIZE_DOWNLOAD, writeenv_DOUBLE},
61   {"curl_size_upload", CURLINFO_SIZE_UPLOAD, writeenv_DOUBLE},
62   {"curl_speed_download", CURLINFO_SPEED_DOWNLOAD, writeenv_DOUBLE},
63   {"curl_speed_upload", CURLINFO_SPEED_UPLOAD, writeenv_DOUBLE},
64   {NULL, 0, writeenv_NONE}
65  };
66
67 static void internalSetEnv(const char * name, char * value)
68 {
69   /* Add your OS-specific code here. */
70 #ifdef __riscos__
71   _kernel_setenv(name, value);
72 #elif defined (CURLDEBUG)
73   curl_memlog("ENV %s = %s\n", name, value);
74 #endif
75   return;
76 }
77
78 void ourWriteEnv(CURL *curl)
79 {
80   unsigned int i;
81   char *string, numtext[10];
82   long longinfo;
83   double doubleinfo;
84
85   for(i=0; variables[i].name; i++) {
86     switch (variables[i].type) {
87     case writeenv_STRING:
88       if(curl_easy_getinfo(curl, variables[i].id, &string) == CURLE_OK)
89         internalSetEnv(variables[i].name, string);
90       else
91         internalSetEnv(variables[i].name, NULL);
92       break;
93
94     case writeenv_LONG:
95       if(curl_easy_getinfo(curl, variables[i].id, &longinfo) == CURLE_OK) {
96         curl_msprintf(numtext, "%5ld", longinfo);
97         internalSetEnv(variables[i].name, numtext);
98       }
99       else
100         internalSetEnv(variables[i].name, NULL);
101       break;
102     case writeenv_DOUBLE:
103       if(curl_easy_getinfo(curl, variables[i].id, &doubleinfo) == CURLE_OK) {
104         curl_msprintf(numtext, "%6.2f", doubleinfo);
105         internalSetEnv(variables[i].name, numtext);
106       }
107       else
108         internalSetEnv(variables[i].name, NULL);
109       break;
110     default:
111       break;
112     }
113   }
114
115   return;
116 }
117
118 #endif