1 /****************************************************************************
4 ** Implementation of QDirclass
8 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
10 ** This file is part of the tools module of the Qt GUI Toolkit.
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech AS of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses for Unix/X11 or for Qt/Embedded may use this file in accordance
23 ** with the Qt Commercial License Agreement provided with the Software.
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 ** information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
33 ** Contact info@trolltech.com if any conditions of this licensing are
36 **********************************************************************/
43 #include "qfileinfo.h"
44 #include "qfiledefs_p.h"
46 #include "qstringlist.h"
50 extern QStringList qt_makeFilterList( const QString &filter );
52 extern int qt_cmp_si_sortSpec;
54 #if defined(Q_C_CALLBACKS)
58 extern int qt_cmp_si( const void *, const void * );
60 #if defined(Q_C_CALLBACKS)
65 void QDir::slashify( QString& )
69 QString QDir::homeDirPath()
72 d = QFile::decodeName(getenv("HOME"));
79 QString QDir::canonicalPath() const
85 (void)GETCWD( cur, PATH_MAX );
86 if ( CHDIR(QFile::encodeName(dPath)) >= 0 ) {
87 (void)GETCWD( tmp, PATH_MAX );
88 r = QFile::decodeName(tmp);
96 bool QDir::mkdir( const QString &dirName, bool acceptAbsPath ) const
98 return MKDIR( QFile::encodeName(filePath(dirName,acceptAbsPath)), 0777 )
102 bool QDir::rmdir( const QString &dirName, bool acceptAbsPath ) const
104 return RMDIR( QFile::encodeName(filePath(dirName,acceptAbsPath)) ) == 0;
107 bool QDir::isReadable() const
109 return ACCESS( QFile::encodeName(dPath), R_OK | X_OK ) == 0;
112 bool QDir::isRoot() const
114 return dPath == QString::fromLatin1("/");
117 bool QDir::rename( const QString &name, const QString &newName,
118 bool acceptAbsPaths )
120 if ( name.isEmpty() || newName.isEmpty() ) {
121 #if defined(CHECK_NULL)
122 qWarning( "QDir::rename: Empty or null file name(s)" );
126 QString fn1 = filePath( name, acceptAbsPaths );
127 QString fn2 = filePath( newName, acceptAbsPaths );
128 return ::rename( QFile::encodeName(fn1),
129 QFile::encodeName(fn2) ) == 0;
132 bool QDir::setCurrent( const QString &path )
135 r = CHDIR( QFile::encodeName(path) );
139 QString QDir::currentDirPath()
144 if ( STAT( ".", &st ) == 0 ) {
145 char currentName[PATH_MAX];
146 if ( GETCWD( currentName, PATH_MAX ) != 0 )
147 result = QFile::decodeName(currentName);
149 if ( result.isNull() )
150 qWarning( "QDir::currentDirPath: getcwd() failed" );
154 qWarning( "QDir::currentDirPath: stat(\".\") failed" );
161 QString QDir::rootDirPath()
163 QString d = QString::fromLatin1( "/" );
167 bool QDir::isRelativePath( const QString &path )
169 int len = path.length();
172 return path[0] != '/';
175 bool QDir::readDirEntries( const QString &nameFilter,
176 int filterSpec, int sortSpec )
180 fList = new QStringList;
182 fiList = new QFileInfoList;
184 fiList->setAutoDelete( TRUE );
190 QStringList filters = qt_makeFilterList( nameFilter );
192 bool doDirs = (filterSpec & Dirs) != 0;
193 bool doFiles = (filterSpec & Files) != 0;
194 bool noSymLinks = (filterSpec & NoSymLinks) != 0;
195 bool doReadable = (filterSpec & Readable) != 0;
196 bool doWritable = (filterSpec & Writable) != 0;
197 bool doExecable = (filterSpec & Executable) != 0;
198 bool doHidden = (filterSpec & Hidden) != 0;
200 #if defined(_OS_OS2EMX_)
201 //QRegExp wc( nameFilter, FALSE, TRUE ); // wild card, case insensitive
203 //QRegExp wc( nameFilter, TRUE, TRUE ); // wild card, case sensitive
209 dir = opendir( QFile::encodeName(dPath) );
211 #if defined(CHECK_NULL)
212 qWarning( "QDir::readDirEntries: Cannot read the directory: %s",
213 QFile::encodeName(dPath).data() );
218 while ( (file = readdir(dir)) ) {
219 QString fn = QFile::decodeName(file->d_name);
220 fi.setFile( *this, fn );
221 if ( !match( filters, fn ) && !(allDirs && fi.isDir()) )
223 if ( (doDirs && fi.isDir()) || (doFiles && fi.isFile()) ) {
224 if ( noSymLinks && fi.isSymLink() )
226 if ( (filterSpec & RWEMask) != 0 )
227 if ( (doReadable && !fi.isReadable()) ||
228 (doWritable && !fi.isWritable()) ||
229 (doExecable && !fi.isExecutable()) )
231 if ( !doHidden && fn[0] == '.' &&
232 fn != QString::fromLatin1(".")
233 && fn != QString::fromLatin1("..") )
235 fiList->append( new QFileInfo( fi ) );
238 if ( closedir(dir) != 0 ) {
239 #if defined(CHECK_NULL)
240 qWarning( "QDir::readDirEntries: Cannot close the directory: %s",
241 dPath.local8Bit().data() );
246 if(fiList->count()) {
247 QDirSortItem* si= new QDirSortItem[fiList->count()];
250 for (itm = fiList->first(); itm; itm = fiList->next())
252 qt_cmp_si_sortSpec = sortSpec;
253 qsort( si, i, sizeof(si[0]), qt_cmp_si );
254 // put them back in the list
255 fiList->setAutoDelete( FALSE );
258 for ( j=0; j<i; j++ ) {
259 fiList->append( si[j].item );
260 fList->append( si[j].item->fileName() );
263 fiList->setAutoDelete( TRUE );
266 if ( filterSpec == (FilterSpec)filtS && sortSpec == (SortSpec)sortS &&
267 nameFilter == nameFilt )
274 const QFileInfoList * QDir::drives()
276 // at most one instance of QFileInfoList is leaked, and this variable
277 // points to that list
278 static QFileInfoList * knownMemoryLeak = 0;
280 if ( !knownMemoryLeak ) {
281 knownMemoryLeak = new QFileInfoList;
282 // non-win32 versions both use just one root directory
283 knownMemoryLeak->append( new QFileInfo( rootDirPath() ) );
286 return knownMemoryLeak;