MemoryTracking: adjust initialization calling - followup
[platform/upstream/curl.git] / tests / libtest / first.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 "test.h"
23
24 #ifdef HAVE_LOCALE_H
25 #include <locale.h> /* for setlocale() */
26 #endif
27
28 #ifdef CURLDEBUG
29 #  define MEMDEBUG_NODEFINES
30 #  include "memdebug.h"
31 #endif
32
33 int select_test (int num_fds, fd_set *rd, fd_set *wr, fd_set *exc,
34                  struct timeval *tv)
35 {
36 #ifdef USE_WINSOCK
37   /* Winsock doesn't like no socket set in 'rd', 'wr' or 'exc'. This is
38    * case when 'num_fds <= 0. So sleep.
39    */
40   if (num_fds <= 0) {
41     Sleep(1000*tv->tv_sec + tv->tv_usec/1000);
42     return 0;
43   }
44 #endif
45   return select(num_fds, rd, wr, exc, tv);
46 }
47
48 char *libtest_arg2=NULL;
49 char *libtest_arg3=NULL;
50 int test_argc;
51 char **test_argv;
52
53 #ifdef UNITTESTS
54 int unitfail; /* for unittests */
55 #endif
56
57 #ifdef CURLDEBUG
58 static void memory_tracking_init(void)
59 {
60   char *env;
61   /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
62   env = curl_getenv("CURL_MEMDEBUG");
63   if(env) {
64     /* use the value as file name */
65     char fname[CURL_MT_LOGFNAME_BUFSIZE];
66     if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
67       env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
68     strcpy(fname, env);
69     curl_free(env);
70     curl_memdebug(fname);
71     /* this weird stuff here is to make curl_free() get called
72        before curl_memdebug() as otherwise memory tracking will
73        log a free() without an alloc! */
74   }
75   /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
76   env = curl_getenv("CURL_MEMLIMIT");
77   if(env) {
78     char *endptr;
79     long num = strtol(env, &endptr, 10);
80     if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
81       curl_memlimit(num);
82     curl_free(env);
83   }
84 }
85 #else
86 #  define memory_tracking_init()
87 #endif
88
89 int main(int argc, char **argv)
90 {
91   char *URL;
92
93   memory_tracking_init();
94
95   /*
96    * Setup proper locale from environment. This is needed to enable locale-
97    * specific behaviour by the C library in order to test for undesired side
98    * effects that could cause in libcurl.
99    */
100 #ifdef HAVE_SETLOCALE
101   setlocale(LC_ALL, "");
102 #endif
103
104   if(argc< 2 ) {
105     fprintf(stderr, "Pass URL as argument please\n");
106     return 1;
107   }
108
109   test_argc = argc;
110   test_argv = argv;
111
112   if(argc>2)
113     libtest_arg2=argv[2];
114
115   if(argc>3)
116     libtest_arg3=argv[3];
117
118   URL = argv[1]; /* provide this to the rest */
119
120   fprintf(stderr, "URL: %s\n", URL);
121
122   return test(URL);
123 }