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#if defined(MSDOS) || defined(WIN32) 25 26#if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME) 27# include <libgen.h> 28#endif 29 30#ifdef WIN32 31# include <curl/curl.h> 32# include "tool_cfgable.h" 33# include "tool_libinfo.h" 34#endif 35 36#include "tool_bname.h" 37#include "tool_doswin.h" 38 39#include "memdebug.h" /* keep this as LAST include */ 40 41/* 42 * Macros ALWAYS_TRUE and ALWAYS_FALSE are used to avoid compiler warnings. 43 */ 44 45#define ALWAYS_TRUE (1) 46#define ALWAYS_FALSE (0) 47 48#if defined(_MSC_VER) && !defined(__POCC__) 49# undef ALWAYS_TRUE 50# undef ALWAYS_FALSE 51# if (_MSC_VER < 1500) 52# define ALWAYS_TRUE (0, 1) 53# define ALWAYS_FALSE (1, 0) 54# else 55# define ALWAYS_TRUE \ 56__pragma(warning(push)) \ 57__pragma(warning(disable:4127)) \ 58(1) \ 59__pragma(warning(pop)) 60# define ALWAYS_FALSE \ 61__pragma(warning(push)) \ 62__pragma(warning(disable:4127)) \ 63(0) \ 64__pragma(warning(pop)) 65# endif 66#endif 67 68#ifdef WIN32 69# undef PATH_MAX 70# define PATH_MAX MAX_PATH 71#endif 72 73#ifndef S_ISCHR 74# ifdef S_IFCHR 75# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) 76# else 77# define S_ISCHR(m) (0) /* cannot tell if file is a device */ 78# endif 79#endif 80 81#ifdef WIN32 82# define _use_lfn(f) ALWAYS_TRUE /* long file names always available */ 83#elif !defined(__DJGPP__) || (__DJGPP__ < 2) /* DJGPP 2.0 has _use_lfn() */ 84# define _use_lfn(f) ALWAYS_FALSE /* long file names never available */ 85#endif 86 87static const char *msdosify (const char *file_name); 88static char *rename_if_dos_device_name (char *file_name); 89 90/* 91 * sanitize_dos_name: returns a newly allocated string holding a 92 * valid file name which will be a transformation of given argument 93 * in case this wasn't already a valid file name. 94 * 95 * This function takes ownership of given argument, free'ing it before 96 * returning. Caller is responsible of free'ing returned string. Upon 97 * out of memory condition function returns NULL. 98 */ 99 100char *sanitize_dos_name(char *file_name) 101{ 102 char new_name[PATH_MAX]; 103 104 if(!file_name) 105 return NULL; 106 107 if(strlen(file_name) >= PATH_MAX) 108 file_name[PATH_MAX-1] = '\0'; /* truncate it */ 109 110 strcpy(new_name, msdosify(file_name)); 111 112 Curl_safefree(file_name); 113 114 return strdup(rename_if_dos_device_name(new_name)); 115} 116 117/* The following functions are taken with modification from the DJGPP 118 * port of tar 1.12. They use algorithms originally from DJTAR. */ 119 120static const char *msdosify (const char *file_name) 121{ 122 static char dos_name[PATH_MAX]; 123 static const char illegal_chars_dos[] = ".+, ;=[]" /* illegal in DOS */ 124 "|<>\\\":?*"; /* illegal in DOS & W95 */ 125 static const char *illegal_chars_w95 = &illegal_chars_dos[8]; 126 int idx, dot_idx; 127 const char *s = file_name; 128 char *d = dos_name; 129 const char *const dlimit = dos_name + sizeof(dos_name) - 1; 130 const char *illegal_aliens = illegal_chars_dos; 131 size_t len = sizeof(illegal_chars_dos) - 1; 132 133 /* Support for Windows 9X VFAT systems, when available. */ 134 if(_use_lfn(file_name)) { 135 illegal_aliens = illegal_chars_w95; 136 len -= (illegal_chars_w95 - illegal_chars_dos); 137 } 138 139 /* Get past the drive letter, if any. */ 140 if(s[0] >= 'A' && s[0] <= 'z' && s[1] == ':') { 141 *d++ = *s++; 142 *d++ = *s++; 143 } 144 145 for(idx = 0, dot_idx = -1; *s && d < dlimit; s++, d++) { 146 if(memchr(illegal_aliens, *s, len)) { 147 /* Dots are special: DOS doesn't allow them as the leading character, 148 and a file name cannot have more than a single dot. We leave the 149 first non-leading dot alone, unless it comes too close to the 150 beginning of the name: we want sh.lex.c to become sh_lex.c, not 151 sh.lex-c. */ 152 if(*s == '.') { 153 if(idx == 0 && (s[1] == '/' || (s[1] == '.' && s[2] == '/'))) { 154 /* Copy "./" and "../" verbatim. */ 155 *d++ = *s++; 156 if(*s == '.') 157 *d++ = *s++; 158 *d = *s; 159 } 160 else if(idx == 0) 161 *d = '_'; 162 else if(dot_idx >= 0) { 163 if(dot_idx < 5) { /* 5 is a heuristic ad-hoc'ery */ 164 d[dot_idx - idx] = '_'; /* replace previous dot */ 165 *d = '.'; 166 } 167 else 168 *d = '-'; 169 } 170 else 171 *d = '.'; 172 173 if(*s == '.') 174 dot_idx = idx; 175 } 176 else if(*s == '+' && s[1] == '+') { 177 if(idx - 2 == dot_idx) { /* .c++, .h++ etc. */ 178 *d++ = 'x'; 179 *d = 'x'; 180 } 181 else { 182 /* libg++ etc. */ 183 memcpy (d, "plus", 4); 184 d += 3; 185 } 186 s++; 187 idx++; 188 } 189 else 190 *d = '_'; 191 } 192 else 193 *d = *s; 194 if(*s == '/') { 195 idx = 0; 196 dot_idx = -1; 197 } 198 else 199 idx++; 200 } 201 202 *d = '\0'; 203 return dos_name; 204} 205 206static char *rename_if_dos_device_name (char *file_name) 207{ 208 /* We could have a file whose name is a device on MS-DOS. Trying to 209 * retrieve such a file would fail at best and wedge us at worst. We need 210 * to rename such files. */ 211 char *base; 212 struct_stat st_buf; 213 char fname[PATH_MAX]; 214 215 strncpy(fname, file_name, PATH_MAX-1); 216 fname[PATH_MAX-1] = '\0'; 217 base = basename(fname); 218 if(((stat(base, &st_buf)) == 0) && (S_ISCHR(st_buf.st_mode))) { 219 size_t blen = strlen(base); 220 221 if(strlen(fname) >= PATH_MAX-1) { 222 /* Make room for the '_' */ 223 blen--; 224 base[blen] = '\0'; 225 } 226 /* Prepend a '_'. */ 227 memmove(base + 1, base, blen + 1); 228 base[0] = '_'; 229 strcpy(file_name, fname); 230 } 231 return file_name; 232} 233 234#if defined(MSDOS) && (defined(__DJGPP__) || defined(__GO32__)) 235 236/* 237 * Disable program default argument globbing. We do it on our own. 238 */ 239char **__crt0_glob_function(char *arg) 240{ 241 (void)arg; 242 return (char**)0; 243} 244 245#endif /* MSDOS && (__DJGPP__ || __GO32__) */ 246 247#ifdef WIN32 248 249/* 250 * Function to find CACert bundle on a Win32 platform using SearchPath. 251 * (SearchPath is already declared via inclusions done in setup header file) 252 * (Use the ASCII version instead of the unicode one!) 253 * The order of the directories it searches is: 254 * 1. application's directory 255 * 2. current working directory 256 * 3. Windows System directory (e.g. C:\windows\system32) 257 * 4. Windows Directory (e.g. C:\windows) 258 * 5. all directories along %PATH% 259 * 260 * For WinXP and later search order actually depends on registry value: 261 * HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\SafeProcessSearchMode 262 */ 263 264CURLcode FindWin32CACert(struct Configurable *config, const char *bundle_file) 265{ 266 CURLcode result = CURLE_OK; 267 268 /* search and set cert file only if libcurl supports SSL */ 269 if(curlinfo->features & CURL_VERSION_SSL) { 270 271 DWORD res_len; 272 DWORD buf_tchar_size = PATH_MAX + 1; 273 DWORD buf_bytes_size = sizeof(TCHAR) * buf_tchar_size; 274 char *ptr = NULL; 275 276 char *buf = malloc(buf_bytes_size); 277 if(!buf) 278 return CURLE_OUT_OF_MEMORY; 279 buf[0] = '\0'; 280 281 res_len = SearchPathA(NULL, bundle_file, NULL, buf_tchar_size, buf, &ptr); 282 if(res_len > 0) { 283 Curl_safefree(config->cacert); 284 config->cacert = strdup(buf); 285 if(!config->cacert) 286 result = CURLE_OUT_OF_MEMORY; 287 } 288 289 Curl_safefree(buf); 290 } 291 292 return result; 293} 294 295#endif /* WIN32 */ 296 297#endif /* MSDOS || WIN32 */ 298 299