1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
30 #define ENABLE_CURLX_PRINTF
31 /* use our own printf() functions */
34 #include "tool_cfgable.h"
35 #include "tool_convert.h"
36 #include "tool_msgs.h"
37 #include "tool_operate.h"
38 #include "tool_panykey.h"
40 #include "tool_main.h"
41 #include "tool_libinfo.h"
44 * This is low-level hard-hacking memory leak tracking and similar. Using
45 * the library level code from this client-side is ugly, but we do this
46 * anyway for convenience.
48 #include "memdebug.h" /* keep this as LAST include */
52 * vms_show is a global variable, used in main() as parameter for
53 * function vms_special_exit() to allow proper curl tool exiting.
54 * Its value may be set in other tool_*.c source files thanks to
55 * forward declaration present in tool_vms.h
60 /* if we build a static library for unit tests, there is no main() function */
64 * Ensure that file descriptors 0, 1 and 2 (stdin, stdout, stderr) are
65 * open before starting to run. Otherwise, the first three network
66 * sockets opened by curl could be used for input sources, downloaded data
67 * or error logs as they will effectively be stdin, stdout and/or stderr.
69 static void main_checkfds(void)
72 int fd[2] = { STDIN_FILENO, STDIN_FILENO };
73 while(fd[0] == STDIN_FILENO ||
74 fd[0] == STDOUT_FILENO ||
75 fd[0] == STDERR_FILENO ||
76 fd[1] == STDIN_FILENO ||
77 fd[1] == STDOUT_FILENO ||
78 fd[1] == STDERR_FILENO)
80 return; /* Out of handles. This isn't really a big problem now, but
81 will be when we try to create a socket later. */
88 static void memory_tracking_init(void)
91 /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
92 env = curlx_getenv("CURL_MEMDEBUG");
94 /* use the value as file name */
95 char fname[CURL_MT_LOGFNAME_BUFSIZE];
96 if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
97 env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
100 curl_memdebug(fname);
101 /* this weird stuff here is to make curl_free() get called
102 before curl_memdebug() as otherwise memory tracking will
103 log a free() without an alloc! */
105 /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
106 env = curlx_getenv("CURL_MEMLIMIT");
109 long num = strtol(env, &endptr, 10);
110 if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
116 # define memory_tracking_init() Curl_nop_stmt
120 * This is the main global constructor for the app. Call this before
121 * _any_ libcurl usage. If this fails, *NO* libcurl functions may be
122 * used, or havoc may be the result.
124 static CURLcode main_init(struct Configurable *config)
126 CURLcode result = CURLE_OK;
128 #if defined(__DJGPP__) || defined(__GO32__)
129 /* stop stat() wasting time */
130 _djstat_flags |= _STAT_INODE | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
133 /* Perform the libcurl initialization */
134 result = curl_global_init(CURL_GLOBAL_DEFAULT);
136 /* Get information about libcurl */
137 result = get_libcurl_info();
140 helpf(config->errors, "error retrieving curl library information\n");
143 helpf(config->errors, "error initializing curl library\n");
149 * This is the main global destructor for the app. Call this after
150 * _all_ libcurl usage is done.
152 static void main_free(void)
154 curl_global_cleanup();
160 ** curl tool main function.
162 int main(int argc, char *argv[])
165 struct Configurable *config;
169 #if defined(HAVE_SIGNAL) && defined(SIGPIPE)
170 (void)signal(SIGPIPE, SIG_IGN);
173 /* Initialize memory tracking */
174 memory_tracking_init();
176 /* Allocate the initial config */
177 config = malloc(sizeof(struct Configurable));
180 /* Initialise the config */
183 /* Initialize the curl library - do not call any libcurl functions before
185 res = main_init(config);
187 /* Start our curl operation */
188 res = operate(config, argc, argv);
190 /* Perform the main cleanup */
195 if(config->showerror)
199 /* Free the config structures */
204 helpf(stderr, "error initializing curl\n");
205 res = CURLE_FAILED_INIT;
208 #ifdef __NOVELL_LIBC__
209 if(getenv("_IN_NETWARE_BASH_") == NULL)
214 vms_special_exit(res, vms_show);
220 #endif /* ndef UNITTESTS */