summaryrefslogtreecommitdiffstats
path: root/src/widgets/dialogs
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2023-05-29 12:52:35 +0300
committerAhmad Samir <a.samirh78@gmail.com>2023-06-05 23:35:20 +0300
commit79710abf950e47971836a787f24e7c14aeccc183 (patch)
tree3ff1e5cb6150dcb7656df0851bd3c7474598e3ec /src/widgets/dialogs
parent8d77ee0f2b042af7aec43e1e83eb26d92c2f8234 (diff)
QFileDialog: split a static helper
Split the getpwnam* related code to a separate helper; this makes the code more readable. Change-Id: Ia7c6b6f7801d88b6b3a8809e973ede6c58e89923 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets/dialogs')
-rw-r--r--src/widgets/dialogs/qfiledialog.cpp45
1 files changed, 25 insertions, 20 deletions
diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp
index 1f88608fe9..7461a643ad 100644
--- a/src/widgets/dialogs/qfiledialog.cpp
+++ b/src/widgets/dialogs/qfiledialog.cpp
@@ -1104,6 +1104,30 @@ void QFileDialog::selectUrl(const QUrl &url)
}
#ifdef Q_OS_UNIX
+static QString homeDirFromPasswdEntry(const QString &path, const QByteArray &userName)
+{
+#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) && !defined(Q_OS_WASM)
+ passwd pw;
+ passwd *tmpPw;
+ char buf[200];
+ const int bufSize = sizeof(buf);
+ int err = 0;
+# if defined(Q_OS_SOLARIS) && (_POSIX_C_SOURCE - 0 < 199506L)
+ tmpPw = getpwnam_r(userName.constData(), &pw, buf, bufSize);
+# else
+ err = getpwnam_r(userName.constData(), &pw, buf, bufSize, &tmpPw);
+# endif
+ if (err || !tmpPw)
+ return path;
+ return QFile::decodeName(pw.pw_dir);
+#else
+ passwd *pw = getpwnam(userName.constData());
+ if (!pw)
+ return path;
+ return QFile::decodeName(pw->pw_dir);
+#endif // defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) && !defined(Q_OS_WASM)
+}
+
Q_AUTOTEST_EXPORT QString qt_tildeExpansion(const QString &path)
{
if (!path.startsWith(u'~'))
@@ -1118,26 +1142,7 @@ Q_AUTOTEST_EXPORT QString qt_tildeExpansion(const QString &path)
const QString homePath = QDir::homePath();
#else
const QByteArray userName = QStringView{path}.mid(1, separatorPosition - 1).toLocal8Bit();
-# if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) && !defined(Q_OS_WASM)
- passwd pw;
- passwd *tmpPw;
- char buf[200];
- const int bufSize = sizeof(buf);
- int err = 0;
-# if defined(Q_OS_SOLARIS) && (_POSIX_C_SOURCE - 0 < 199506L)
- tmpPw = getpwnam_r(userName.constData(), &pw, buf, bufSize);
-# else
- err = getpwnam_r(userName.constData(), &pw, buf, bufSize, &tmpPw);
-# endif
- if (err || !tmpPw)
- return path;
- const QString homePath = QString::fromLocal8Bit(pw.pw_dir);
-# else
- passwd *pw = getpwnam(userName.constData());
- if (!pw)
- return path;
- const QString homePath = QString::fromLocal8Bit(pw->pw_dir);
-# endif
+ QString homePath = homeDirFromPasswdEntry(path, userName);
#endif
return homePath + QStringView{path}.mid(separatorPosition);
}