comparison src/main/win32/SDL_win32_main.c @ 1379:c0a74f199ecf

Use only safe string functions
author Sam Lantinga <slouken@libsdl.org>
date Sun, 19 Feb 2006 23:46:34 +0000
parents 19418e4422cb
children 4ed717f9e509
comparison
equal deleted inserted replaced
1378:dc0e13e7e1ae 1379:c0a74f199ecf
16 # define setbuf(f,b) 16 # define setbuf(f,b)
17 # define setvbuf(w,x,y,z) 17 # define setvbuf(w,x,y,z)
18 # define fopen _wfopen 18 # define fopen _wfopen
19 # define freopen _wfreopen 19 # define freopen _wfreopen
20 # define remove(x) DeleteFile(x) 20 # define remove(x) DeleteFile(x)
21 # define strcat wcscat
22 #else 21 #else
23 # define DIR_SEPERATOR TEXT("/") 22 # define DIR_SEPERATOR TEXT("/")
24 # include <direct.h> 23 # include <direct.h>
25 #endif 24 #endif
26 25
206 205
207 bufp = (char *)alloca(n+1); 206 bufp = (char *)alloca(n+1);
208 if ( bufp == NULL ) { 207 if ( bufp == NULL ) {
209 return OutOfMemory(); 208 return OutOfMemory();
210 } 209 }
211 SDL_strncpy(bufp, appname, n); 210 SDL_strlcpy(bufp, appname, n);
212 bufp[n] = '\0';
213 appname = bufp; 211 appname = bufp;
214 212
215 /* Load SDL dynamic link library */ 213 /* Load SDL dynamic link library */
216 if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) { 214 if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
217 ShowError("WinMain() error", SDL_GetError()); 215 ShowError("WinMain() error", SDL_GetError());
257 #ifdef _WIN32_WCE 255 #ifdef _WIN32_WCE
258 wchar_t *bufp; 256 wchar_t *bufp;
259 int nLen; 257 int nLen;
260 #else 258 #else
261 char *bufp; 259 char *bufp;
260 size_t nLen;
262 #endif 261 #endif
263 #ifndef NO_STDIO_REDIRECT 262 #ifndef NO_STDIO_REDIRECT
264 FILE *newfp; 263 FILE *newfp;
265 #endif 264 #endif
265 int retval;
266 266
267 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't 267 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
268 keep them open. This is a hack.. hopefully it will be fixed 268 keep them open. This is a hack.. hopefully it will be fixed
269 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded. 269 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
270 */ 270 */
272 if ( handle != NULL ) { 272 if ( handle != NULL ) {
273 FreeLibrary(handle); 273 FreeLibrary(handle);
274 } 274 }
275 275
276 #ifndef NO_STDIO_REDIRECT 276 #ifndef NO_STDIO_REDIRECT
277 pathlen = GetModuleFileName(NULL, path, SDL_TABLESIZE(path)); 277 pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
278 while ( pathlen > 0 && path[pathlen] != '\\' ) { 278 while ( pathlen > 0 && path[pathlen] != '\\' ) {
279 --pathlen; 279 --pathlen;
280 } 280 }
281 path[pathlen] = '\0'; 281 path[pathlen] = '\0';
282 282
283 SDL_strcpy( stdoutPath, path ); 283 SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
284 SDL_strcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE ); 284 SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
285 285
286 /* Redirect standard input and standard output */ 286 /* Redirect standard input and standard output */
287 newfp = freopen(stdoutPath, TEXT("w"), stdout); 287 newfp = freopen(stdoutPath, TEXT("w"), stdout);
288 288
289 #ifndef _WIN32_WCE 289 #ifndef _WIN32_WCE
297 } 297 }
298 #endif 298 #endif
299 } 299 }
300 #endif /* _WIN32_WCE */ 300 #endif /* _WIN32_WCE */
301 301
302 SDL_strcpy( stderrPath, path ); 302 SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
303 SDL_strcat( stderrPath, DIR_SEPERATOR STDERR_FILE ); 303 SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
304 304
305 newfp = freopen(stderrPath, TEXT("w"), stderr); 305 newfp = freopen(stderrPath, TEXT("w"), stderr);
306 #ifndef _WIN32_WCE 306 #ifndef _WIN32_WCE
307 if ( newfp == NULL ) { /* This happens on NT */ 307 if ( newfp == NULL ) { /* This happens on NT */
308 #if !defined(stderr) 308 #if !defined(stderr)
326 wcscpy (bufp, TEXT("\"")); 326 wcscpy (bufp, TEXT("\""));
327 GetModuleFileName(NULL, bufp+1, 128-3); 327 GetModuleFileName(NULL, bufp+1, 128-3);
328 wcscpy (bufp+wcslen(bufp), TEXT("\" ")); 328 wcscpy (bufp+wcslen(bufp), TEXT("\" "));
329 wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp)); 329 wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
330 nLen = wcslen(bufp)+1; 330 nLen = wcslen(bufp)+1;
331 cmdline = (char *)alloca(nLen); 331 cmdline = SDL_stack_alloc(wchar_t, nLen);
332 if ( cmdline == NULL ) { 332 if ( cmdline == NULL ) {
333 return OutOfMemory(); 333 return OutOfMemory();
334 } 334 }
335 WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL); 335 WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
336 #else 336 #else
337 /* Grab the command line (use alloca() on Windows) */ 337 /* Grab the command line (use alloca() on Windows) */
338 bufp = GetCommandLine(); 338 bufp = GetCommandLine();
339 cmdline = (char *)alloca(SDL_strlen(bufp)+1); 339 nLen = SDL_strlen(bufp)+1;
340 cmdline = SDL_stack_alloc(char, nLen);
340 if ( cmdline == NULL ) { 341 if ( cmdline == NULL ) {
341 return OutOfMemory(); 342 return OutOfMemory();
342 } 343 }
343 SDL_strcpy(cmdline, bufp); 344 SDL_strlcpy(cmdline, bufp, nLen);
344 #endif 345 #endif
345 346
346 /* Parse it into argv and argc */ 347 /* Parse it into argv and argc */
347 argc = ParseCommandLine(cmdline, NULL); 348 argc = ParseCommandLine(cmdline, NULL);
348 argv = (char **)alloca((argc+1)*(sizeof *argv)); 349 argv = SDL_stack_alloc(char*, argc+1);
349 if ( argv == NULL ) { 350 if ( argv == NULL ) {
350 return OutOfMemory(); 351 return OutOfMemory();
351 } 352 }
352 ParseCommandLine(cmdline, argv); 353 ParseCommandLine(cmdline, argv);
353 354
354 /* Run the main program (after a little SDL initialization) */ 355 /* Run the main program (after a little SDL initialization) */
355 return(console_main(argc, argv)); 356 retval = console_main(argc, argv);
356 } 357
358 SDL_stack_free(cmdline);
359 SDL_stack_free(argv);
360 return retval;
361 }