8f2fbb77b2d5f9b608e7ef485b0fa6f4558d8406
[platform/upstream/curl.git] / src / tool_main.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 #include <sys/stat.h>
25
26 #ifdef HAVE_SIGNAL_H
27 #include <signal.h>
28 #endif
29
30 #define ENABLE_CURLX_PRINTF
31 /* use our own printf() functions */
32 #include "curlx.h"
33
34 #include "tool_cfgable.h"
35 #include "tool_convert.h"
36 #include "tool_operate.h"
37 #include "tool_panykey.h"
38 #include "tool_vms.h"
39 #include "tool_main.h"
40
41 /*
42  * This is low-level hard-hacking memory leak tracking and similar. Using
43  * the library level code from this client-side is ugly, but we do this
44  * anyway for convenience.
45  */
46 #include "memdebug.h" /* keep this as LAST include */
47
48 #ifdef __VMS
49 /*
50  * vms_show is a global variable, used in main() as parameter for
51  * function vms_special_exit() to allow proper curl tool exiting.
52  * Its value may be set in other tool_*.c source files thanks to
53  * forward declaration present in tool_vms.h
54  */
55 int vms_show = 0;
56 #endif
57
58 /* if we build a static library for unit tests, there is no main() function */
59 #ifndef UNITTESTS
60
61 /*
62  * Ensure that file descriptors 0, 1 and 2 (stdin, stdout, stderr) are
63  * open before starting to run.  Otherwise, the first three network
64  * sockets opened by curl could be used for input sources, downloaded data
65  * or error logs as they will effectively be stdin, stdout and/or stderr.
66  */
67 static void main_checkfds(void)
68 {
69 #ifdef HAVE_PIPE
70   int fd[2] = { STDIN_FILENO, STDIN_FILENO };
71   while(fd[0] == STDIN_FILENO ||
72         fd[0] == STDOUT_FILENO ||
73         fd[0] == STDERR_FILENO ||
74         fd[1] == STDIN_FILENO ||
75         fd[1] == STDOUT_FILENO ||
76         fd[1] == STDERR_FILENO)
77     if(pipe(fd) < 0)
78       return;   /* Out of handles. This isn't really a big problem now, but
79                    will be when we try to create a socket later. */
80   close(fd[0]);
81   close(fd[1]);
82 #endif
83 }
84
85 /*
86 ** curl tool main function.
87 */
88 int main(int argc, char *argv[])
89 {
90   int res;
91   struct Configurable config;
92
93   memset(&config, 0, sizeof(struct Configurable));
94
95   config.errors = stderr; /* default errors to stderr */
96
97   main_checkfds();
98
99 #if defined(HAVE_SIGNAL) && defined(SIGPIPE)
100   (void)signal(SIGPIPE, SIG_IGN);
101 #endif
102
103   res = operate(&config, argc, argv);
104
105 #ifdef __SYMBIAN32__
106   if(config.showerror)
107     tool_pressanykey();
108 #endif
109
110   free_config_fields(&config);
111
112 #ifdef __NOVELL_LIBC__
113   if(getenv("_IN_NETWARE_BASH_") == NULL)
114     tool_pressanykey();
115 #endif
116
117 #ifdef __VMS
118   vms_special_exit(res, vms_show);
119 #else
120   return res;
121 #endif
122 }
123
124 #endif /* ndef UNITTESTS */