📄 osutil.cxx
字号:
/*
* osutil.cxx
*
* Operating System classes implementation
*
* Portable Windows Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Log: osutil.cxx,v $
* Revision 1.1 2006/06/29 04:18:41 joegenbaclor
* *** empty log message ***
*
* Revision 1.85 2005/11/30 12:47:42 csoutheren
* Removed tabs, reformatted some code, and changed tags for Doxygen
*
* Revision 1.84 2005/11/25 02:29:54 csoutheren
* Applied patch #1353487 by Hannes Friederich
* Mac OS X update
*
* Revision 1.83 2004/09/23 05:00:10 csoutheren
* Extra proofing against possible NULL pointers
*
* Revision 1.82 2004/04/03 23:53:10 csoutheren
* Added various changes to improce compatibility with the Sun Forte compiler
* Thanks to Brian Cameron
* Added detection of readdir_r version
*
* Revision 1.81 2004/04/01 12:51:11 csoutheren
* Fixed problem with args to access, thanks to Borko Jandras
*
* Revision 1.80 2004/01/07 21:30:30 dsandras
* Applied patch from Miguel Rodriguez Perez <migrax@terra.es> to remove problematic PAssert failing in cases where it shouldn't.
*
* Revision 1.79 2003/09/17 01:18:04 csoutheren
* Removed recursive include file system and removed all references
* to deprecated coooperative threading support
*
* Revision 1.78 2003/05/01 06:08:36 robertj
* Fixed concurrency problem with some time functions, thanks chad@broadmind.com
*
* Revision 1.77 2003/01/24 10:21:06 robertj
* Fixed issues in RTEMS support, thanks Vladimir Nesic
*
* Revision 1.76 2002/11/22 10:14:07 robertj
* QNX port, thanks Xiaodan Tang
*
* Revision 1.75 2002/11/20 01:55:06 robertj
* Fixed to follow new semantics of GetPath(), first entry is volume which on
* UNix machines is always an empty string. Also collapses consecutive
* slashes as they are meaningless.
*
* Revision 1.74 2002/11/19 11:21:30 robertj
* Changed PFilePath so can be empty string, indicating illegal path.
* Added function to extract a path as an array of directories components.
*
* Revision 1.73 2002/10/22 07:42:52 robertj
* Added extra debugging for file handle and thread leak detection.
*
* Revision 1.72 2002/10/17 13:44:27 robertj
* Port to RTEMS, thanks Vladimir Nesic.
*
* Revision 1.71 2002/10/17 12:57:24 robertj
* Added ability to increase maximum file handles on a process.
*
* Revision 1.70 2002/10/10 04:43:44 robertj
* VxWorks port, thanks Martijn Roest
*
* Revision 1.69 2002/06/06 09:28:42 robertj
* Fixed problems with canonicalising directories now PINDEX is signed.
*
* Revision 1.68 2002/02/11 02:26:54 craigs
* Fixed problem with reading lines of length > 100 chares from text files
* Thanks to Ohnuma Masato
*
* Revision 1.67 2002/01/26 23:58:15 craigs
* Changed for GCC 3.0 compatibility, thanks to manty@manty.net
*
* Revision 1.66 2001/10/11 02:20:54 robertj
* Added IRIX support (no audio/video), thanks Andre Schulze.
*
* Revision 1.65 2001/09/18 05:56:03 robertj
* Fixed numerous problems with thread suspend/resume and signals handling.
*
* Revision 1.64 2001/09/04 04:15:44 robertj
* Fixed PFileInfo (stat) of file name that is dangling symlink.
*
* Revision 1.63 2001/08/11 15:38:43 rogerh
* Add Mac OS Carbon changes from John Woods <jfw@jfwhome.funhouse.com>
*
* Revision 1.62 2001/06/30 06:59:07 yurik
* Jac Goudsmit from Be submit these changes 6/28. Implemented by Yuri Kiryanov
*
* Revision 1.61 2001/05/29 03:35:16 craigs
* Changed to not tempnam to avoid linker warning on new Linux systems
*
* Revision 1.60 2001/03/12 02:35:20 robertj
* Fixed PDirectory::Exists so only returns TRUE if a directory and not file.
*
* Revision 1.59 2001/02/23 07:16:36 rogerh
* Darwin (MACOS X) does not have thread safe localtime_t() and gmtime_r()
* functions. Use the unsafe localtime() and gmtime() calls for now.
*
* Revision 1.58 2001/02/13 06:59:57 robertj
* Fixed problem with operator= in PDirectory class, part of larger change previously made.
*
* Revision 1.57 2001/02/13 05:15:31 robertj
* Fixed problem with operator= in container classes. Some containers will
* break unless the copy is virtual (eg PStringStream's buffer pointers) so
* needed to add a new AssignContents() function to all containers.
*
* Revision 1.56 2000/09/11 22:49:31 robertj
* Fixed bug where last char was always removed in mkdir() instead of only if '/'.
*
* Revision 1.55 2000/06/21 01:01:22 robertj
* AIX port, thanks Wolfgang Platzer (wolfgang.platzer@infonova.at).
*
* Revision 1.54 2000/04/19 00:13:53 robertj
* BeOS port changes.
*
* Revision 1.53 2000/04/09 18:19:23 rogerh
* Add my changes for NetBSD support.
*
* Revision 1.52 2000/04/06 12:11:32 rogerh
* MacOS X support submitted by Kevin Packard
*
* Revision 1.51 2000/04/05 02:55:11 robertj
* Added microseconds to PTime class.
*
* Revision 1.50 2000/03/08 12:17:09 rogerh
* Add OpenBSD support
*
* Revision 1.49 2000/02/24 11:03:49 robertj
* Fixed warning on Linux systems about _REENTRANT
*
* Revision 1.48 1999/08/17 07:37:36 robertj
* Fixed inlines so are inlined for optimised version
*
* Revision 1.47 1999/06/28 09:28:02 robertj
* Portability issues, especially n BeOS (thanks Yuri!)
*
* Revision 1.46 1999/06/26 08:21:12 robertj
* Fixed bug in PFilePath::SetType finding dots outside of file name in path.
*
* Revision 1.45 1999/06/14 08:39:57 robertj
* Added PConsoleChannel class for access to stdin/stdout/stderr
*
* Revision 1.44 1999/06/09 04:08:46 robertj
* Added support for opening stdin/stdout/stderr as PFile objects.
*
* Revision 1.43 1999/02/22 13:26:53 robertj
* BeOS port changes.
*
* Revision 1.42 1998/12/12 01:06:24 robertj
* Fixed off by one error in month on FreeBSD platform
*
* Revision 1.41 1998/11/30 21:51:43 robertj
* New directory structure.
*
* Revision 1.40 1998/11/26 11:54:16 robertj
* Fixed error return on PFile::GetInfo
*
* Revision 1.39 1998/11/24 09:39:09 robertj
* FreeBSD port.
*
* Revision 1.38 1998/11/10 13:00:52 robertj
* Fixed strange problems with readdir_r usage.
*
* Revision 1.37 1998/11/06 04:44:46 robertj
* Solaris compatibility
*
* Revision 1.36 1998/11/05 12:03:13 robertj
* Fixed solaris compatibility and Linux warning on readdir_r function.
*
* Revision 1.35 1998/11/05 09:05:55 craigs
* Changed directory routines to use reenttrant functions, and added PDirectory::GetParent
*
* Revision 1.34 1998/09/24 07:39:49 robertj
* Removed file that only had #pragma implmentation for PTextFile and nothing else.
*
* Revision 1.33 1998/09/24 04:12:12 robertj
* Added open software license.
*
*/
#define _OSUTIL_CXX
#pragma implementation "timer.h"
#pragma implementation "pdirect.h"
#pragma implementation "file.h"
#pragma implementation "textfile.h"
#pragma implementation "conchan.h"
#pragma implementation "ptime.h"
#pragma implementation "timeint.h"
#pragma implementation "filepath.h"
#pragma implementation "lists.h"
#pragma implementation "pstring.h"
#pragma implementation "dict.h"
#pragma implementation "array.h"
#pragma implementation "object.h"
#pragma implementation "contain.h"
#if defined(P_LINUX)
#ifndef _REENTRANT
#define _REENTRANT
#endif
#elif defined(P_SOLARIS)
#define _POSIX_PTHREAD_SEMANTICS
#endif
#include <ptlib.h>
#include <fcntl.h>
#ifdef P_VXWORKS
#include <sys/times.h>
#else
#include <time.h>
#include <sys/time.h>
#endif
#include <ctype.h>
#if defined(P_LINUX)
#include <mntent.h>
#include <sys/vfs.h>
#if (__GNUC_MINOR__ < 7 && __GNUC__ < 3)
#include <localeinfo.h>
#else
#define P_USE_LANGINFO
#endif
#elif defined(P_FREEBSD) || defined(P_OPENBSD) || defined(P_NETBSD) || defined(P_MACOSX) || defined(P_MACOS)
#define P_USE_STRFTIME
#include <sys/param.h>
#include <sys/mount.h>
#elif defined(P_HPUX9)
#define P_USE_LANGINFO
#elif defined(P_AIX)
#define P_USE_STRFTIME
#include <fstab.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#elif defined(P_SOLARIS)
#define P_USE_LANGINFO
#include <sys/timeb.h>
#include <sys/statvfs.h>
#include <sys/mnttab.h>
#elif defined(P_SUN4)
#include <sys/timeb.h>
#elif defined(__BEOS__)
#define P_USE_STRFTIME
#elif defined(P_IRIX)
#define P_USE_LANGINFO
#include <sys/stat.h>
#include <sys/statfs.h>
#include <stdio.h>
#include <mntent.h>
#elif defined(P_VXWORKS)
#define P_USE_STRFTIME
#elif defined(P_RTEMS)
#define P_USE_STRFTIME
#include <time.h>
#include <stdio.h>
#define random() rand()
#define srandom(a) srand(a)
#elif defined(P_QNX)
#include <sys/dcmd_blk.h>
#include <sys/statvfs.h>
#define P_USE_STRFTIME
#endif
#ifdef P_USE_LANGINFO
#include <langinfo.h>
#endif
#define LINE_SIZE_STEP 100
#define DEFAULT_FILE_MODE (S_IRUSR|S_IWUSR|S_IROTH|S_IRGRP)
#if !P_USE_INLINES
#include "ptlib/osutil.inl"
#ifdef _WIN32
#include "ptlib/win32/ptlib/ptlib.inl"
#else
#include "ptlib/unix/ptlib/ptlib.inl"
#endif
#endif
#ifdef P_SUN4
extern "C" {
int on_exit(void (*f)(void), caddr_t);
int atexit(void (*f)(void))
{
return on_exit(f, 0);
}
static char *tzname[2] = { "STD", "DST" };
};
#endif
#define new PNEW
int PX_NewHandle(const char * clsName, int fd)
{
if (fd < 0)
return fd;
static int lowWaterMark = INT_MAX;
static int highWaterMark = 0;
if (fd > highWaterMark) {
highWaterMark = fd;
lowWaterMark = fd;
int maxHandles = PProcess::Current().GetMaxHandles();
if (fd < (maxHandles-maxHandles/20))
PTRACE(4, "PWLib\tFile handle high water mark set: " << fd << ' ' << clsName);
else
PTRACE(1, "PWLib\tFile handle high water mark within 5% of maximum: " << fd << ' ' << clsName);
}
if (fd < lowWaterMark) {
lowWaterMark = fd;
PTRACE(4, "PWLib\tFile handle low water mark set: " << fd << ' ' << clsName);
}
return fd;
}
static PString CanonicaliseDirectory (const PString & path)
{
PString canonical_path;
if (path[0] == '/')
canonical_path = '/';
else {
char *p = getcwd(canonical_path.GetPointer(P_MAX_PATH), P_MAX_PATH);
PAssertOS (p != NULL);
// if the path doesn't end in a slash, add one
if (canonical_path[canonical_path.GetLength()-1] != '/')
canonical_path += '/';
}
const char * ptr = path;
const char * end;
for (;;) {
// ignore slashes
while (*ptr == '/' && *ptr != '\0')
ptr++;
// finished if end of string
if (*ptr == '\0')
break;
// collect non-slash characters
end = ptr;
while (*end != '/' && *end != '\0')
end++;
// make a string out of the element
PString element(ptr, end - ptr);
if (element == "..") {
PINDEX last_char = canonical_path.GetLength()-1;
if (last_char > 0)
canonical_path = canonical_path.Left(canonical_path.FindLast('/', last_char-1)+1);
} else if (element == "." || element == "") {
} else {
canonical_path += element;
canonical_path += '/';
}
ptr = end;
}
return canonical_path;
}
static PString CanonicaliseFilename(const PString & filename)
{
if (filename.IsEmpty())
return filename;
PINDEX p;
PString dirname;
// if there is a slash in the string, extract the dirname
if ((p = filename.FindLast('/')) != P_MAX_INDEX) {
dirname = filename(0,p);
while (filename[p] == '/')
p++;
} else
p = 0;
return CanonicaliseDirectory(dirname) + filename(p, P_MAX_INDEX);
}
PInt64 PString::AsInt64(unsigned base) const
{
char * dummy;
#if defined(P_SOLARIS) || defined(__BEOS__) || defined (P_AIX) || defined(P_IRIX) || defined (P_QNX)
return strtoll(theArray, &dummy, base);
#elif defined(P_VXWORKS) || defined(P_RTEMS)
return strtol(theArray, &dummy, base);
#else
return strtoq(theArray, &dummy, base);
#endif
}
PUInt64 PString::AsUnsigned64(unsigned base) const
{
char * dummy;
#if defined(P_SOLARIS) || defined(__BEOS__) || defined (P_AIX) || defined (P_IRIX) || defined (P_QNX)
return strtoull(theArray, &dummy, base);
#elif defined(P_VXWORKS) || defined(P_RTEMS)
return strtoul(theArray, &dummy, base);
#else
return strtouq(theArray, &dummy, base);
#endif
}
///////////////////////////////////////////////////////////////////////////////
//
// timer
PTimeInterval PTimer::Tick()
{
#ifdef P_VXWORKS
struct timespec ts;
clock_gettime(0,&ts);
return (int)(ts.tv_sec*10000) + ts.tv_nsec/100000L;
#else
struct timeval tv;
::gettimeofday (&tv, NULL);
return (PInt64)(tv.tv_sec) * 1000 + tv.tv_usec/1000L;
#endif // P_VXWORKS
}
///////////////////////////////////////////////////////////////////////////////
//
// PDirectory
//
void PDirectory::CopyContents(const PDirectory & d)
{
if (d.entryInfo == NULL)
entryInfo = NULL;
else {
entryInfo = new PFileInfo;
*entryInfo = *d.entryInfo;
}
directory = NULL;
entryBuffer = NULL;
}
void PDirectory::Close()
{
if (directory != NULL) {
PAssert(closedir(directory) == 0, POperatingSystemError);
directory = NULL;
}
if (entryBuffer != NULL) {
free(entryBuffer);
entryBuffer = NULL;
}
if (entryInfo != NULL) {
delete entryInfo;
entryInfo = NULL;
}
}
void PDirectory::Construct ()
{
directory = NULL;
entryBuffer = NULL;
entryInfo = NULL;
PString::AssignContents(CanonicaliseDirectory(*this));
}
BOOL PDirectory::Open(int ScanMask)
{
if (directory != NULL)
Close();
scanMask = ScanMask;
if ((directory = opendir(theArray)) == NULL)
return FALSE;
entryBuffer = (struct dirent *)malloc(sizeof(struct dirent) + P_MAX_PATH);
entryInfo = new PFileInfo;
if (Next())
return TRUE;
Close();
return FALSE;
}
BOOL PDirectory::Next()
{
if (directory == NULL)
return FALSE;
do {
do {
struct dirent * entryPtr;
entryBuffer->d_name[0] = '\0';
#if P_HAS_POSIX_READDIR_R == 3
if (::readdir_r(directory, entryBuffer, &entryPtr) != 0)
return FALSE;
if (entryPtr != entryBuffer)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -