#if wxUSE_UNICODE
#define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
#define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
+
+ // these functions should be used when the conversions really, really have
+ // to succeed (usually because we pass their results to a standard C
+ // function which would crash if we passed NULL to it), so these functions
+ // always return a valid pointer if their argument is non-NULL
+
+ // this function safety is achieved by trying wxConvLibc first, wxConvUTF8
+ // next if it fails and, finally, wxConvISO8859_1 which always succeeds
+ extern WXDLLIMPEXP_BASE wxWCharBuffer wxSafeConvertMB2WX(const char *s);
+
+ // this function uses wxConvLibc and wxConvUTF8(MAP_INVALID_UTF8_TO_OCTAL)
+ // if it fails
+ extern WXDLLIMPEXP_BASE wxCharBuffer wxSafeConvertWX2MB(const wchar_t *ws);
#else // ANSI
// no conversions to do
#define wxConvertWX2MB(s) (s)
#define wxConvertMB2WX(s) (s)
+ #define wxSafeConvertMB2WX(s) (s)
+ #define wxSafeConvertWX2MB(s) (s)
#endif // Unicode/ANSI
#endif // _WX_STRCONV_H_
while (argv[mb_argc])
{
- wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]);
+ wxWX2MBbuf mb_arg = wxSafeConvertWX2MB(argv[mb_argc]);
mb_argv[mb_argc] = strdup(mb_arg);
mb_argc++;
}
}
if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL)
{
- who = getpwnam(wxConvertWX2MB(ptr));
+ who = getpwnam(wxSafeConvertWX2MB(ptr));
}
// We now make sure the the user exists!
who = getpwnam (user.mb_str());
}
- return wxConvertMB2WX(who ? who->pw_dir : 0);
+ return wxSafeConvertMB2WX(who ? who->pw_dir : 0);
}
// ----------------------------------------------------------------------------
bool ok = uname(&uts) != -1;
if ( ok )
{
- wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1);
+ wxStrncpy(buf, wxSafeConvertMB2WX(uts.nodename), sz - 1);
buf[sz] = wxT('\0');
}
#elif defined(HAVE_GETHOSTNAME)
bool ok = gethostname(cbuf, sz) != -1;
if ( ok )
{
- wxStrncpy(buf, wxConvertMB2WX(cbuf), sz - 1);
+ wxStrncpy(buf, wxSafeConvertMB2WX(cbuf), sz - 1);
buf[sz] = wxT('\0');
}
#else // no uname, no gethostname
{
if ( !wxStrchr(buf, wxT('.')) )
{
- struct hostent *host = gethostbyname(wxConvertWX2MB(buf));
+ struct hostent *host = gethostbyname(wxSafeConvertWX2MB(buf));
if ( !host )
{
wxLogSysError(_("Cannot get the official hostname"));
else
{
// the canonical name
- wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz);
+ wxStrncpy(buf, wxSafeConvertMB2WX(host->h_name), sz);
}
}
//else: it's already a FQDN (BSD behaves this way)
*buf = wxT('\0');
if ((who = getpwuid(getuid ())) != NULL)
{
- wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1);
+ wxStrncpy (buf, wxSafeConvertMB2WX(who->pw_name), sz - 1);
return true;
}
bool wxGetUserName(wxChar *buf, int sz)
{
+#ifdef HAVE_PW_GECOS
struct passwd *who;
*buf = wxT('\0');
if ((who = getpwuid (getuid ())) != NULL)
{
- // pw_gecos field in struct passwd is not standard
-#ifdef HAVE_PW_GECOS
char *comma = strchr(who->pw_gecos, ',');
if (comma)
*comma = '\0'; // cut off non-name comment fields
- wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1);
-#else // !HAVE_PW_GECOS
- wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1);
-#endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
+ wxStrncpy (buf, wxSafeConvertMB2WX(who->pw_gecos), sz - 1);
return true;
}
return false;
+#else // !HAVE_PW_GECOS
+ return wxGetUserId(buf, sz);
+#endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
}
bool wxIsPlatform64Bit()