Added test cases for bug 588363 (no_r=me).
[mozilla-central.git] / build / wince / shunt / shunt.cpp
blob99d9d92eb133f2e75dad89c6b3efe02fcbc908a0
1 /* -*- Mode: C; c-basic-offset: 2; tab-width: 4; indent-tabs-mode: nil; -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is MOZCE Lib.
17 * The Initial Developer of the Original Code is Doug Turner <dougt@meer.net>.
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * John Wolfe <wolfe@lobo.us>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 #include "include/mozce_shunt.h"
41 #include "time_conversions.h"
42 #include <stdlib.h>
43 #include "Windows.h"
45 // from environment.cpp
46 void putenv_copy(const char *k, const char *v);
48 ////////////////////////////////////////////////////////
49 // special folder stuff
50 ////////////////////////////////////////////////////////
52 typedef struct MOZCE_SHUNT_SPECIAL_FOLDER_INFO
54 int nFolder;
55 char *folderEnvName;
56 } MozceShuntSpecialFolderInfo;
58 // TAKEN DIRECTLY FROM MICROSOFT SHELLAPI.H HEADER FILE
59 // supported SHGetSpecialFolderPath nFolder ids
60 #define CSIDL_DESKTOP 0x0000
61 #define CSIDL_PROGRAMS 0x0002 // \Windows\Start Menu\Programs
62 #define CSIDL_PERSONAL 0x0005
63 #define CSIDL_WINDOWS 0x0024 // \Windows
64 #define CSIDL_PROGRAM_FILES 0x0026 // \Program Files
66 #define CSIDL_APPDATA 0x001A // NOT IN SHELLAPI.H header file
67 #define CSIDL_PROFILE 0x0028 // NOT IN SHELLAPI.H header file
69 MozceShuntSpecialFolderInfo mozceSpecialFoldersToEnvVars[] = {
70 { CSIDL_APPDATA, "APPDATA" },
71 { CSIDL_PROGRAM_FILES, "ProgramFiles" },
72 { CSIDL_WINDOWS, "windir" },
74 // { CSIDL_PROFILE, "HOMEPATH" }, // No return on WinMobile 6 Pro
75 // { CSIDL_PROFILE, "USERPROFILE" }, // No return on WinMobile 6 Pro
76 // { int, "ALLUSERSPROFILE" }, // Only one profile on WinCE
77 // { int, "CommonProgramFiles" },
78 // { int, "COMPUTERNAME" },
79 // { int, "HOMEDRIVE" },
80 // { int, "SystemDrive" },
81 // { int, "SystemRoot" },
82 // { int, "TEMP" },
84 { NULL, NULL }
87 static void InitializeSpecialFolderEnvVars()
89 MozceShuntSpecialFolderInfo *p = mozceSpecialFoldersToEnvVars;
90 while ( p && p->nFolder && p->folderEnvName ) {
91 WCHAR wPath[MAX_PATH];
92 char cPath[MAX_PATH];
93 if ( SHGetSpecialFolderPath(NULL, wPath, p->nFolder, FALSE) )
94 if ( 0 != WideCharToMultiByte(CP_ACP, 0, wPath, -1, cPath, MAX_PATH, 0, 0) )
95 putenv_copy(p->folderEnvName, cPath);
96 p++;
100 ////////////////////////////////////////////////////////
101 // errno
102 ////////////////////////////////////////////////////////
104 char* strerror(int inErrno)
106 return "Unknown Error";
109 int errno = 0;
112 ////////////////////////////////////////////////////////
113 // File System Stuff
114 ////////////////////////////////////////////////////////
116 wchar_t * _wgetcwd(wchar_t * dir, unsigned long size)
118 wchar_t tmp[MAX_PATH] = {0};
119 GetEnvironmentVariableW(L"CWD", tmp, size);
120 if (tmp && tmp[0]) {
121 if (wcslen(tmp) > size)
122 return 0;
123 if (!dir) {
124 dir = (wchar_t*)malloc(sizeof(wchar_t) * (wcslen(tmp) + 2));
125 if (!dir)
126 return 0;
128 wcscpy(dir, tmp);
129 } else {
130 unsigned long i;
131 if (!dir) {
132 dir = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH + 1));
133 if (!dir)
134 return 0;
136 if (!GetModuleFileNameW(GetModuleHandle (NULL), dir, MAX_PATH))
137 return 0;
138 for (i = wcslen(dir); i && dir[i] != '\\'; i--) {}
139 dir[i + 1] = '\0';
140 SetEnvironmentVariableW(L"CWD", dir);
142 size_t len = wcslen(dir);
143 if (dir[len - 1] != '\\' && (len + 2) < size) {
144 dir[len] = '\\';
145 dir[len + 1] = '\0';
147 return dir;
150 wchar_t *_wfullpath( wchar_t *absPath, const wchar_t *relPath, unsigned long maxLength )
152 if(absPath == NULL){
153 absPath = (wchar_t *)malloc(maxLength*sizeof(wchar_t));
155 wchar_t cwd[MAX_PATH];
156 if (NULL == _wgetcwd( cwd, MAX_PATH))
157 return NULL;
159 unsigned long len = wcslen(cwd);
160 if(!(cwd[len-1] == TCHAR('/') || cwd[len-1] == TCHAR('\\'))&& len< maxLength){
161 cwd[len] = TCHAR('\\');
162 cwd[++len] = TCHAR('\0');
164 if(len+wcslen(relPath) < maxLength){
165 #if (_WIN32_WCE > 300)
166 if ( 0 < CeGetCanonicalPathName(relPath[0] == L'\\'? relPath :
167 wcscat(cwd,relPath),
168 absPath, maxLength, 0))
169 return absPath;
170 #else
171 #error Need CeGetCanonicalPathName to build.
172 // NO ACTUAL CeGetCanonicalPathName function in earlier versions of WinCE
173 #endif
175 return NULL;
178 int _wchdir(const WCHAR* path) {
179 return SetEnvironmentVariableW(L"CWD", path);
182 int _unlink(const char *filename)
184 wchar_t wname[MAX_PATH];
186 MultiByteToWideChar(CP_ACP,
188 filename,
189 strlen(filename)+1,
190 wname,
191 MAX_PATH );
192 return DeleteFileW(wname)?0:-1;
195 void abort(void)
197 #if defined(DEBUG)
198 DebugBreak();
199 #endif
200 TerminateProcess((HANDLE) GetCurrentProcessId(), 3);
203 ////////////////////////////////////////////////////////
204 // Locale Stuff
205 ////////////////////////////////////////////////////////
207 #define localeconv __not_supported_on_device_localeconv
208 #include <locale.h>
209 #undef localeconv
211 extern "C" {
212 struct lconv * localeconv(void);
215 static struct lconv s_locale_conv =
217 ".", /* decimal_point */
218 ",", /* thousands_sep */
219 "333", /* grouping */
220 "$", /* int_curr_symbol */
221 "$", /* currency_symbol */
222 "", /* mon_decimal_point */
223 "", /* mon_thousands_sep */
224 "", /* mon_grouping */
225 "+", /* positive_sign */
226 "-", /* negative_sign */
227 '2', /* int_frac_digits */
228 '2', /* frac_digits */
229 1, /* p_cs_precedes */
230 1, /* p_sep_by_space */
231 1, /* n_cs_precedes */
232 1, /* n_sep_by_space */
233 1, /* p_sign_posn */
234 1, /* n_sign_posn */
237 struct lconv * localeconv(void)
239 return &s_locale_conv;
243 ////////////////////////////////////////////////////////
244 // DllMain
245 ////////////////////////////////////////////////////////
247 BOOL WINAPI DllMain(HANDLE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
249 // Perform actions based on the reason for calling.
250 switch( fdwReason )
252 case DLL_PROCESS_ATTACH:
253 // Initialize once for each new process.
254 // Return FALSE to fail DLL load.
255 InitializeSpecialFolderEnvVars();
256 break;
258 case DLL_THREAD_ATTACH:
259 // Do thread-specific initialization.
260 break;
262 case DLL_THREAD_DETACH:
263 // Do thread-specific cleanup.
264 break;
266 case DLL_PROCESS_DETACH:
267 // Perform any necessary cleanup.
268 break;
270 return TRUE; // Successful DLL_PROCESS_ATTACH.