comparison rogue4/mdport.c @ 23:107a467612fb

rogue4: prevent segfault on getpwuid failure
author edwarj4
date Tue, 24 Nov 2009 12:53:15 +0000
parents 9535a08ddc39
children 3c5682bbf164
comparison
equal deleted inserted replaced
22:63dcd855bc8e 23:107a467612fb
51 char *strdup(const char *s); 51 char *strdup(const char *s);
52 #endif 52 #endif
53 53
54 #include <stdlib.h> 54 #include <stdlib.h>
55 #include <string.h> 55 #include <string.h>
56 #include <errno.h>
56 57
57 #if defined(_WIN32) && !defined(__MINGW32__) 58 #if defined(_WIN32) && !defined(__MINGW32__)
58 #define PATH_MAX MAX_PATH 59 #define PATH_MAX MAX_PATH
59 #endif 60 #endif
60 61
263 #endif 264 #endif
264 #if !defined(_WIN32) && !defined(DJGPP) 265 #if !defined(_WIN32) && !defined(DJGPP)
265 struct passwd *pw; 266 struct passwd *pw;
266 267
267 pw = getpwuid(getuid()); 268 pw = getpwuid(getuid());
268 269 if (pw != NULL)
269 l = pw->pw_name; 270 l = pw->pw_name;
271 else
272 l = NULL;
270 #endif 273 #endif
271 274
272 if ((l == NULL) || (*l == '\0')) 275 if ((l == NULL) || (*l == '\0'))
273 if ( (l = getenv("USERNAME")) == NULL ) 276 if ( (l = getenv("USERNAME")) == NULL )
274 if ( (l = getenv("USER")) == NULL ) 277 if ( (l = getenv("USER")) == NULL )
293 char slash = '\\'; 296 char slash = '\\';
294 #else 297 #else
295 char slash = '/'; 298 char slash = '/';
296 struct passwd *pw; 299 struct passwd *pw;
297 pw = getpwuid(getuid()); 300 pw = getpwuid(getuid());
298 301 if (pw != NULL)
299 h = pw->pw_dir; 302 h = pw->pw_dir;
303 else
304 h = "";
300 305
301 if (strcmp(h,"/") == 0) 306 if (strcmp(h,"/") == 0)
302 h = NULL; 307 h = NULL;
303 #endif 308 #endif
304 homedir[0] = 0; 309 homedir[0] = 0;
341 char *def = "C:\\COMMAND.COM"; 346 char *def = "C:\\COMMAND.COM";
342 #else 347 #else
343 char *def = "/bin/sh"; 348 char *def = "/bin/sh";
344 struct passwd *pw; 349 struct passwd *pw;
345 pw = getpwuid(getuid()); 350 pw = getpwuid(getuid());
346 s = pw->pw_shell; 351 if (pw != NULL)
352 s = pw->pw_shell;
353 else
354 s = NULL;
347 #endif 355 #endif
348 if ((s == NULL) || (*s == '\0')) 356 if ((s == NULL) || (*s == '\0'))
349 if ( (s = getenv("COMSPEC")) == NULL) 357 if ( (s = getenv("COMSPEC")) == NULL)
350 if ( (s = getenv("SHELL")) == NULL) 358 if ( (s = getenv("SHELL")) == NULL)
351 if ( (s = getenv("SystemRoot")) == NULL) 359 if ( (s = getenv("SystemRoot")) == NULL)