[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-core-devel
Subject: [PATCH] kurlcompletion.cpp version 2
From: Stefan Teleman <steleman () nyc ! rr ! com>
Date: 2004-10-28 3:39:42
Message-ID: 200410272339.42563.steleman () nyc ! rr ! com
[Download RAW message or body]
Cleaned up, mem*() instead of b*(), Scott Wheeler's 200ms favorite
timeout is back, removed an unnecessary copy by value, mutexes.
--Stefan
--
Stefan Teleman 'Nobody Expects the Spanish Inquisition'
steleman@nyc.rr.com -Monty Python
["kurlcompletion.cpp.diff" (text/x-diff)]
--- kurlcompletion.cpp.kde.orig Tue Jun 22 13:36:41 2004
+++ kurlcompletion.cpp Wed Oct 27 22:31:20 2004
@@ -36,6 +36,7 @@
#include <qtextstream.h>
#include <qdeepcopy.h>
#include <qthread.h>
+#include <qmutex.h>
#include <kapplication.h>
#include <kdebug.h>
@@ -52,6 +53,7 @@
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
+#include <sys/param.h>
#include <pwd.h>
#include <time.h>
@@ -95,6 +97,7 @@
{
protected:
CompletionThread( KURLCompletion *receiver ) :
+ m_mutex(TRUE),
QThread(),
m_receiver( receiver ),
m_terminationRequested( false )
@@ -102,10 +105,21 @@
public:
void requestTermination() { m_terminationRequested = true; }
- QDeepCopy<QStringList> matches() const { return m_matches; }
+ QDeepCopy<QStringList> matches() const
+ {
+ QMutexLocker mutexLock (&m_mutex);
+ return m_matches;
+ }
protected:
- void addMatch( const QString &match ) { m_matches.append( match ); }
+ mutable QMutex m_mutex;
+
+protected:
+ void addMatch( const QString &match ) {
+ m_mutex.lock();
+ m_matches.append( match );
+ m_mutex.unlock();
+ }
bool terminationRequested() const { return m_terminationRequested; }
void done()
{
@@ -138,9 +152,14 @@
{
static const QChar tilde = '~';
- struct passwd *pw;
- while ( ( pw = ::getpwent() ) && !terminationRequested() )
- addMatch( tilde + QString::fromLocal8Bit( pw->pw_name ) );
+ struct passwd pw;
+ struct passwd *pwptr;
+ char buf[1024];
+ int buflen = 1024;
+ (void) memset (&pw, '\0', sizeof (struct passwd));
+ while ( ( pwptr = ::getpwent_r(&pw, buf, buflen) ) && !terminationRequested() )
+ if (pwptr)
+ addMatch( tilde + QString::fromLocal8Bit( pw.pw_name ) );
::endpwent();
@@ -219,10 +238,11 @@
// Loop through all directory entries
- struct dirent dirPosition;
+ size_t direntSize = sizeof (struct dirent) + size_t(MAXPATHLEN) + sizeof(char);
+ struct dirent* dirPosition = (struct dirent *) malloc (direntSize);
struct dirent *dirEntry = 0;
while ( !terminationRequested() &&
- ::readdir_r( dir, &dirPosition, &dirEntry ) == 0 && dirEntry )
+ ::readdir_r( dir, dirPosition, &dirEntry ) == 0 && dirEntry )
{
// Skip hidden files if m_noHidden is true
@@ -269,11 +289,9 @@
continue;
}
}
-
addMatch( file );
}
}
-
// chdir to the original directory
QDir::setCurrent( path );
@@ -280,6 +298,7 @@
::closedir( dir );
dir = 0;
+ free (dirPosition);
}
done();
@@ -726,8 +745,7 @@
// are added to the first matching case.
d->userListThread->wait( 200 );
- QStringList l = d->userListThread->matches();
- addMatches( l );
+ addMatches( d->userListThread->matches() );
}
}
*match = finished();
@@ -1450,10 +1468,16 @@
// ~user is replaced with the dir from passwd
//
else {
- struct passwd *pw = ::getpwnam( user.local8Bit() );
+ struct passwd pw;
+ struct passwd* pwptr;
+ char buf[1024];
+ int buflen = 1024;
+ (void) memset (&pw, '\0', sizeof (struct passwd));
+ // POSIX !!!
+ int ret = ::getpwnam_r( user.local8Bit(), &pw, buf, buflen, &pwptr );
- if ( pw )
- dir = QFile::decodeName( pw->pw_dir );
+ if ( 0 == ret )
+ dir = QFile::decodeName( pw.pw_dir );
::endpwent();
}
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic