sl@0: // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // connectors for re-entrant system calls sl@0: // sl@0: // sl@0: sl@0: #include "SYSIF.H" sl@0: #include "LPOSIX.H" sl@0: sl@0: #include sl@0: #include sl@0: #include // for open() sl@0: #include sl@0: #include sl@0: #include sl@0: #include // for popen3 sl@0: #include // for system sl@0: #include sl@0: #include sl@0: sl@0: sl@0: extern "C" { sl@0: sl@0: /** sl@0: Opens the file which name is stored in the file string. sl@0: sl@0: @return On Success, a non-negative integer representing the lowest numbered unused file descriptor. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int open (const char *file, int flags, ...) sl@0: { sl@0: va_list ap; sl@0: int ret; sl@0: sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: sl@0: va_start (ap, flags); sl@0: ret = _open_r (r, file, flags, va_arg (ap, int)); sl@0: va_end (ap); sl@0: return ret; sl@0: } sl@0: sl@0: /** sl@0: A wide_character version of a open(). sl@0: */ sl@0: EXPORT_C int wopen (const wchar_t *file, int flags, ...) sl@0: { sl@0: va_list ap; sl@0: int ret; sl@0: sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: sl@0: va_start (ap, flags); sl@0: ret = _wopen_r (r, file, flags, va_arg (ap, int)); sl@0: va_end (ap); sl@0: return ret; sl@0: } sl@0: sl@0: /** A reentrant version of open(). sl@0: */ sl@0: EXPORT_C int _open_r (struct _reent *r, const char *name, int mode, int perms) sl@0: { sl@0: wchar_t _widename[KMaxFileName+1]; sl@0: sl@0: if (-1 != mbstowcs(_widename, name, KMaxFileName)) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.open(_widename,mode,perms,r->_errno); sl@0: } sl@0: sl@0: MapError(EILSEQ, r->_errno); sl@0: return 0; //null file pointer sl@0: sl@0: } sl@0: sl@0: /** A reentrant version of wopen(). sl@0: */ sl@0: EXPORT_C int _wopen_r (struct _reent *r, const wchar_t *name, int mode, int perms) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.open(name,mode,perms,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Reads a block of data of the length specified by cnt. sl@0: sl@0: @return On Success, return a non-negative integer indicating the number of bytes actually read. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int read (int fd, char *buf, size_t cnt) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _read_r (r, fd, buf, cnt); sl@0: } sl@0: sl@0: /** A reentrant version of read(). sl@0: */ sl@0: EXPORT_C int _read_r (struct _reent *r, int fd, char *buf, size_t nbyte) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.read(fd,buf,nbyte,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Writes a block of data of the length specified by cnt. sl@0: sl@0: @return On Success, returns the number of bytes written to the file. The number sl@0: shall never be greater than cnt. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int write (int fd, const char *buf, size_t cnt) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _write_r (r, fd, buf, cnt); sl@0: } sl@0: sl@0: /** A reentrant version of write(). sl@0: */ sl@0: EXPORT_C int _write_r (struct _reent *r, int fd, const char *buf, size_t nbyte) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.write(fd,buf,nbyte,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Close a file. sl@0: sl@0: @return On Success, returns 0. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int close (int fd) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _close_r (r, fd); sl@0: } sl@0: sl@0: sl@0: /** A reentrant version of close(). sl@0: */ sl@0: EXPORT_C int _close_r (struct _reent *r, int fd) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.close(fd,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Synchronizes a file's in-memory state with that on the physical medium. sl@0: sl@0: @param fd Is the file descriptor for the file to be synchronized. sl@0: sl@0: @return On Success, returns 0. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int fsync (int fd) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _fsync_r (r, fd); sl@0: } sl@0: sl@0: /** A reentrant version of fsync(). sl@0: */ sl@0: EXPORT_C int _fsync_r (struct _reent *r, int fd) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.fsync(fd,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Repositions the read/write file offset. sl@0: @return a nonnegative integer that indicates the file pointer value. sl@0: @param fd Is the file descriptor of an open file. sl@0: @param pos Specifies the number of bytes to offset the file pointer sl@0: from a specified file origin. sl@0: @param whence Specifies the location from which to start seeking. sl@0: */ sl@0: EXPORT_C off_t lseek (int fd, off_t pos, int whence) sl@0: { sl@0: return _lseek_r (_REENT, fd, pos, whence); sl@0: } sl@0: sl@0: /** A reentrant version of fseek(). sl@0: */ sl@0: EXPORT_C off_t _lseek_r (struct _reent *r, int fd, off_t pos, int whence) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.lseek(fd,pos,whence,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Gets information about the named file and writes it to the area that buf points to. sl@0: The system must be able to search all directories leading to the file; sl@0: however, read, write, or execute permission of the file is not required. sl@0: sl@0: @param fd Is a file descriptor referring to a file for which status is returned. sl@0: @param st Points to a stat structure where status information about the file is to be placed. sl@0: sl@0: @return On Success, returns 0. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int fstat (int fd, struct stat *st) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _fstat_r (r, fd, st); sl@0: } sl@0: sl@0: /** A reentrant version of fstat(). sl@0: */ sl@0: EXPORT_C int _fstat_r (struct _reent *r, int fd, struct stat *st) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.fstat(fd,st,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Gets the size of a file. sl@0: sl@0: @return On Success, returns 0. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int stat (const char *name, struct stat *st) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _stat_r (r, name, st); sl@0: } sl@0: sl@0: /** A reentrant version of stat(). sl@0: */ sl@0: EXPORT_C int _stat_r (struct _reent *r, const char *name, struct stat *st) sl@0: { sl@0: wchar_t tmpbuf[KMaxFullName+1]; sl@0: if (-1 != mbstowcs(tmpbuf, name, KMaxFullName)) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.stat(tmpbuf, st, r->_errno); sl@0: } sl@0: MapError(EILSEQ, r->_errno); sl@0: return -1; sl@0: } sl@0: sl@0: /** sl@0: A wide_character version of a stat(). sl@0: */ sl@0: EXPORT_C int wstat (const wchar_t *name, struct stat *st) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _wstat_r (r, name, st); sl@0: } sl@0: sl@0: /** A reentrant version of wstat(). sl@0: */ sl@0: EXPORT_C int _wstat_r (struct _reent *r, const wchar_t *name, struct stat *st) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.stat(name,st,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Duplicates an open file descriptor. sl@0: sl@0: @param aFid Is the file descriptor to duplicate. sl@0: sl@0: @return On Success, returns a non-negative integer, namely the duplicated file descriptor, which sl@0: is the lowest available descriptor. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int dup (int aFid) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _dup_r(r, aFid); sl@0: } sl@0: sl@0: /** A reentrant version of dup(). sl@0: */ sl@0: EXPORT_C int _dup_r (struct _reent *r, int aFid) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.dup(aFid,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Function duplicates an open file descriptor. sl@0: sl@0: @param aFid1 Is the file descriptor to duplicate. sl@0: @param aFid2 Is the file descriptor that filedes is duplicated onto. sl@0: sl@0: @return On Success, returns a non-negative integer, namely the duplicated file descriptor, which sl@0: is the lowest available descriptor. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int dup2 (int aFid1, int aFid2) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _dup2_r(r, aFid1, aFid2); sl@0: } sl@0: sl@0: /** A reentrant version of dup2(). sl@0: */ sl@0: EXPORT_C int _dup2_r (struct _reent *r, int aFid1, int aFid2) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.dup2(aFid1,aFid2,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Performs a variety of device-specific control functions on device special files. sl@0: sl@0: @return On Success, returns a value other than -1 that depends upon the STREAMS device control function. sl@0: On Failure, return -1, errno may be set. sl@0: */ sl@0: EXPORT_C int ioctl (int aFid, int aCmd, void* aParam) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _ioctl_r(r, aFid, aCmd, aParam); sl@0: } sl@0: sl@0: /** A reentrant version of ioctl(). sl@0: */ sl@0: EXPORT_C int _ioctl_r (struct _reent *r, int aFid, int aCmd, void* aParam) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.ioctl(aFid,aCmd,aParam,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Gets the path name of the current working directory. sl@0: If a buffer is specified, the path name is placed in that buffer, sl@0: and the address of the buffer is returned. sl@0: @return If successful returns buf, if a non-null pointer was specified, sl@0: or the address of the allocated memory otherwise. sl@0: @param _buf Points to the buffer to copy the current working directory to, sl@0: or NULL if getcwd() should allocate the buffer. sl@0: @param _size Is the size, in bytes, of the array of characters that buf points to. sl@0: */ sl@0: EXPORT_C char* getcwd (char *_buf, size_t _size) sl@0: { sl@0: return _getcwd_r(_REENT,_buf,_size); sl@0: } sl@0: sl@0: /** sl@0: A wide_character version of a getcwd(). sl@0: */ sl@0: EXPORT_C wchar_t* wgetcwd (wchar_t *_buf, size_t _size) sl@0: { sl@0: return _wgetcwd_r(_REENT,_buf,_size); sl@0: } sl@0: sl@0: /** A reentrant version of getcwd(). sl@0: */ sl@0: EXPORT_C char* _getcwd_r (struct _reent *r, char *_buf, size_t _size) sl@0: { sl@0: char * _ourbuf = _buf; sl@0: if (_buf==0) sl@0: { sl@0: _ourbuf=(char*)User::Alloc(_size); sl@0: if (_ourbuf==0) sl@0: { sl@0: r->_errno=ENOMEM; sl@0: return _buf; sl@0: } sl@0: } sl@0: sl@0: //we are dealing with wide characters from here so we need a temporary buffer sl@0: wchar_t tmpbuf[KMaxFullName]; sl@0: sl@0: MSystemInterface& sysIf=Interface(r); sl@0: wchar_t * rval = sysIf.getcwd((wchar_t*)tmpbuf, _size, r->_errno); sl@0: sl@0: if (rval) //we have a path sl@0: { sl@0: //convert it to UTF8 sl@0: size_t x = wcstombs(_ourbuf, tmpbuf, _size); //convert the buffer sl@0: return _ourbuf; sl@0: } sl@0: //deal with the fact we may have allocated our own buffer sl@0: if (_buf != _ourbuf) //we allocated it. sl@0: User::Free(_ourbuf); sl@0: return NULL; sl@0: } sl@0: sl@0: /** A wide-character version of reentrant of getcwd(). sl@0: */ sl@0: EXPORT_C wchar_t * _wgetcwd_r (struct _reent *r, wchar_t *_buf, size_t _size) sl@0: { sl@0: if (_buf==0) sl@0: { sl@0: _buf=(wchar_t *)User::Alloc(_size*sizeof(wchar_t)); sl@0: if (_buf==0) sl@0: { sl@0: r->_errno=ENOMEM; sl@0: return _buf; sl@0: } sl@0: } sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.getcwd(_buf,_size,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Changes the current working directory to be pathname. The current directory is the sl@0: beginning point for file searches when path names are not absolute. sl@0: If the chdir() function fails, the current working directory remains unchanged. sl@0: sl@0: @param _path Is the path name of a directory. sl@0: sl@0: @return On Success, returns 0. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int chdir (const char *_path) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _chdir_r(r, _path); sl@0: } sl@0: sl@0: /** A reentrant version of chdir(). sl@0: */ sl@0: EXPORT_C int _chdir_r (struct _reent *r, const char *_path) sl@0: { sl@0: //we need to use a wide buffer and convert sl@0: wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible sl@0: if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName)) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.chdir(tmpbuf, r->_errno); sl@0: } sl@0: MapError(EILSEQ, r->_errno); sl@0: return -1; sl@0: } sl@0: sl@0: /** A wide-character version of chdir(). sl@0: */ sl@0: EXPORT_C int wchdir (const wchar_t *_path) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _wchdir_r(r, _path); sl@0: } sl@0: sl@0: /** A reentrant version of wchdir(). sl@0: */ sl@0: EXPORT_C int _wchdir_r (struct _reent *r, const wchar_t *_path) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.chdir(_path,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Removes an empty directory whose name is given by pathname. sl@0: The directory must not have any entries other than dot (.) and dot-dot (..). sl@0: sl@0: @param _path Points to the directory that the rmdir() function removes. sl@0: sl@0: @return On Success, returns 0. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int rmdir (const char *_path) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _rmdir_r(r, _path); sl@0: } sl@0: sl@0: /** A reentrant version of rmdir(). sl@0: */ sl@0: EXPORT_C int _rmdir_r (struct _reent *r, const char *_path) sl@0: { sl@0: wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible sl@0: if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName)) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.rmdir(tmpbuf, r->_errno); sl@0: } sl@0: MapError(EILSEQ, r->_errno); sl@0: return -1; sl@0: } sl@0: sl@0: /** A wide-character version of rmdir(). sl@0: */ sl@0: EXPORT_C int wrmdir (const wchar_t *_path) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _wrmdir_r(r,_path); sl@0: } sl@0: sl@0: /** A reentrant version of wrmdir(). sl@0: */ sl@0: EXPORT_C int _wrmdir_r (struct _reent *r, const wchar_t *_path) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.rmdir(_path,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Creates a new directory with the specified path name. sl@0: The file permissions of the new directory are initialized from the specified mode. sl@0: sl@0: @param _path Specifies the name of the new directory. The path name can be absolute or relative. sl@0: If the specified path name is relative, the directory is created based upon your current sl@0: working directory. sl@0: @param _mode Is a bitwise-OR field that specifies what permissions the directory has when it is created. sl@0: sl@0: @return On Success, returns 0. sl@0: On Failure, returns -1, errno may be set. Does not create a directory. sl@0: */ sl@0: EXPORT_C int mkdir (const char *_path, mode_t _mode) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _mkdir_r(r,_path,_mode); sl@0: } sl@0: sl@0: /** A reentrant version of mkdir(). sl@0: */ sl@0: EXPORT_C int _mkdir_r (struct _reent *r, const char *_path, mode_t _mode) sl@0: { sl@0: //we need to use a wide buffer and convert sl@0: wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible sl@0: if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName)) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.mkdir(tmpbuf, _mode, r->_errno); sl@0: } sl@0: MapError(EILSEQ, r->_errno); sl@0: return -1; sl@0: } sl@0: sl@0: /** A wide-character version of mkdir(). sl@0: */ sl@0: EXPORT_C int wmkdir (const wchar_t *_path, mode_t _mode) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _wmkdir_r(r, _path, _mode); sl@0: } sl@0: sl@0: /** A reentrant version of wmkdir(). sl@0: */ sl@0: EXPORT_C int _wmkdir_r (struct _reent *r, const wchar_t *_path, mode_t _mode) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.mkdir(_path,_mode,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Sets the access permissions for the file whose name is given by pathname to the bit sl@0: pattern contained in mode. For this call to succeed, the effective user ID of the sl@0: process must match the owner of the file, or the process must have appropriate privileges. sl@0: The owner of the file pathname always has privileges to change permission modes and file attributes. sl@0: sl@0: @param _path Points to the name of the file. sl@0: @param _mode Is a bitwise-or field that specifies the new permission modes for path name. sl@0: sl@0: @return On Success, returns 0. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int chmod (const char *_path, mode_t _mode) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _chmod_r(r, _path, _mode); sl@0: } sl@0: sl@0: /** A reentrant version of chmod(). sl@0: */ sl@0: EXPORT_C int _chmod_r (struct _reent *r, const char *_path, mode_t _mode) sl@0: { sl@0: wchar_t tmpbuf[KMaxFullName+1]; sl@0: if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName)) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.chmod(tmpbuf,_mode,r->_errno); sl@0: } sl@0: MapError(EILSEQ, r->_errno); sl@0: return -1; sl@0: } sl@0: sl@0: /** A wide-character version of chmod(). sl@0: */ sl@0: EXPORT_C int wchmod (const wchar_t *_path, mode_t _mode) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _wchmod_r(r, _path, _mode); sl@0: } sl@0: sl@0: /** A reentrant version of wchmod(). sl@0: */ sl@0: EXPORT_C int _wchmod_r (struct _reent *r, const wchar_t *_path, mode_t _mode) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.chmod(_path,_mode,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Removes a link to a file, and decrements the link count of the referenced file by one. When sl@0: the file's link count becomes 0 and no process has the file open, the space occupied by the sl@0: file is freed, and the file is no longer accessible. If one or more processes have the file sl@0: open when the last link is removed, the link is removed before unlink() returns, but the sl@0: removal of the file contents is postponed until all references to the file are closed. sl@0: sl@0: @param _path Points to the path name that names the file to be unlinked. sl@0: sl@0: @return On Success, returns 0. sl@0: On Failure, returns -1, errno may be set. sl@0: */ sl@0: EXPORT_C int unlink (const char *_path) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _unlink_r(r, _path); sl@0: } sl@0: sl@0: /** A reentrant version of unlink(). sl@0: */ sl@0: EXPORT_C int _unlink_r (struct _reent *r, const char *_path) sl@0: { sl@0: wchar_t tmpbuf[KMaxFullName+1]; sl@0: if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName)) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.unlink(tmpbuf, r->_errno); sl@0: } sl@0: MapError(EILSEQ, r->_errno); sl@0: return -1; sl@0: } sl@0: sl@0: /** A wide-character version of unlink(). sl@0: */ sl@0: EXPORT_C int wunlink (const wchar_t *_path) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _wunlink_r(r, _path); sl@0: } sl@0: sl@0: /** A wide-character version of reentrant of unlink(). sl@0: */ sl@0: EXPORT_C int _wunlink_r (struct _reent *r, const wchar_t *_path) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.unlink(_path,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Renames a file. sl@0: sl@0: @param oldpath Points to the path name of the file to be renamed. The path name can be sl@0: absolute or relative. If a relative path name is given, the file is searched from sl@0: the current working directory. sl@0: @param newpath Points to the path name of the file. The path name can be absolute or relative. sl@0: If a relative path name is given, the file is searched from the current working directory. sl@0: sl@0: @return On Success, returns 0. sl@0: On Failure, returns -1, errno may be set. Does not change either the file named sl@0: by old or the file named by new (if either exists). sl@0: */ sl@0: EXPORT_C int rename (const char *oldpath, const char *newpath) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _rename_r (r, oldpath, newpath); sl@0: } sl@0: sl@0: /** A reentrant version of rename(). sl@0: */ sl@0: EXPORT_C int _rename_r (struct _reent *r, const char *oldpath, const char *newpath) sl@0: { sl@0: wchar_t _old[KMaxFullName+1]; sl@0: wchar_t _new[KMaxFullName+1]; sl@0: if (-1 != mbstowcs(_old, oldpath, KMaxFullName)) sl@0: { sl@0: if (-1 != mbstowcs(_new, newpath, KMaxFullName)) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.rename(_old, _new, r->_errno); sl@0: } sl@0: } sl@0: MapError(EILSEQ, r->_errno); sl@0: return -1; sl@0: } sl@0: sl@0: /** A wide-character version of rename(). sl@0: */ sl@0: EXPORT_C int wrename (const wchar_t *oldpath, const wchar_t *newpath) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _wrename_r (r, oldpath, newpath); sl@0: } sl@0: sl@0: /** A wide-character version of reentrant of rename(). sl@0: */ sl@0: EXPORT_C int _wrename_r (struct _reent *r, const wchar_t *oldpath, const wchar_t *newpath) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.rename(oldpath,newpath,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Takes a specified path name, pathname and resolves all symbolic links, sl@0: extra slashes (/), and references to /./ and /../. sl@0: The resulting absolute path name is placed in the memory location sl@0: pointed to by the resolved_path argument. sl@0: @return resolved_path. sl@0: When an error occurs,returns a null pointer, setsresolved_path sl@0: to the path name that caused the error. sl@0: @param path Points to the path name that you want resolved to an absolute form. sl@0: This may be either a relative or absolute path name. sl@0: All but the final component of this path name must exist when you call realpath(). sl@0: @param resolved Points to the location where the canonical version sl@0: of pathname is to be placed. sl@0: */ sl@0: EXPORT_C char* realpath (const char* path, char* resolved) sl@0: { sl@0: return _realpath_r(_REENT, path, resolved); sl@0: } sl@0: sl@0: /** A wide-character version of realpath(). sl@0: */ sl@0: EXPORT_C wchar_t* wrealpath (const wchar_t* path, wchar_t* resolved) sl@0: { sl@0: return _wrealpath_r(_REENT, path, resolved); sl@0: } sl@0: sl@0: /** A wide-character version of reentrant of realpath(). sl@0: */ sl@0: EXPORT_C wchar_t * _wrealpath_r (struct _reent *r, const wchar_t *relpath, wchar_t *resolved) sl@0: { sl@0: sl@0: TPtr16 name((TText16*)resolved,MAXPATHLEN); sl@0: TParse path; sl@0: MSystemInterface& sysIf=Interface(r); sl@0: TInt err = sysIf.ResolvePath(path, relpath, &name); sl@0: if (!err) sl@0: { sl@0: err = path.SetNoWild(path.DriveAndPath(),NULL,&name); sl@0: if (!err) sl@0: { sl@0: name = path.FullName(); sl@0: name.ZeroTerminate(); sl@0: return resolved; sl@0: } sl@0: } sl@0: MapError(err,r->_errno); sl@0: return 0; sl@0: } sl@0: sl@0: /** A reentrant version of realpath(). sl@0: */ sl@0: EXPORT_C char* _realpath_r (struct _reent *r, const char *relpath, char *resolved) sl@0: { sl@0: sl@0: TFileName name; sl@0: TInt err; sl@0: sl@0: TParse path; sl@0: MSystemInterface& sysIf=Interface(r); sl@0: sl@0: wchar_t _wrelpath[KMaxFileName]; sl@0: sl@0: if (-1 != mbstowcs(_wrelpath, relpath , KMaxFileName)) sl@0: { sl@0: err = sysIf.ResolvePath(path, _wrelpath, &name); sl@0: if (!err) sl@0: { sl@0: err = path.SetNoWild(path.DriveAndPath(),NULL,&name); sl@0: if (!err) sl@0: { sl@0: name = path.FullName(); sl@0: sl@0: if (-1 != wcstombs(resolved, (wchar_t*)name.PtrZ(), KMaxFileName)) sl@0: return resolved; sl@0: else sl@0: { sl@0: err = EILSEQ; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: else sl@0: { sl@0: err = EILSEQ; sl@0: } sl@0: sl@0: MapError(err,r->_errno); sl@0: return 0; sl@0: } sl@0: sl@0: /** sl@0: Gives access to the client's stdin. sl@0: sl@0: @return On Success, returns a pointer to an open stream, used to read or write to the pipe. sl@0: On Failure, return a null pointer. sl@0: */ sl@0: EXPORT_C int popen3 (const char* cmd, const char* mode, char** env, int fids[3]) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return NULL; // Memory for library globals is not allocated (errno not set). sl@0: return _popen3_r (r,cmd,mode,env,fids); sl@0: } sl@0: sl@0: /** A wide-character version of popen3(). sl@0: */ sl@0: EXPORT_C int wpopen3 (const wchar_t* cmd, const wchar_t* mode, wchar_t** env, int fids[3]) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return NULL; // Memory for library globals is not allocated (errno not set). sl@0: return _wpopen3_r (r,cmd,mode,env,fids); sl@0: } sl@0: sl@0: /** A reentrant version of a popen3(). sl@0: */ sl@0: EXPORT_C int _popen3_r (struct _reent *r, const char* cmd, const char* mode, char** env, int fids[3]) sl@0: { sl@0: sl@0: wchar_t wcmd[MAXPATHLEN+1]; sl@0: wchar_t wmode[MAXPATHLEN+1]; sl@0: sl@0: wchar_t ** wenv = NULL; sl@0: wchar_t * buf = NULL; sl@0: sl@0: TInt ret = 0; sl@0: sl@0: if ((-1 != mbstowcs(wcmd, cmd, MAXPATHLEN)) && sl@0: (-1 != mbstowcs(wmode, mode, MAXPATHLEN))) sl@0: { sl@0: //OK, we've widened the first 2 args sl@0: //now for the environment sl@0: sl@0: //env is basically an array of char pointers with a NULL as the last one sl@0: if (env) sl@0: { sl@0: //OK we have a ptr to something sl@0: //count the number of entries and get their lengths so we can work out how much space sl@0: //is needed for the new one sl@0: sl@0: TInt count = 0; sl@0: TInt total = 0; sl@0: while (env[count] != NULL) sl@0: { sl@0: total+= strlen(env[count])+1; sl@0: count++; sl@0: } sl@0: //total has number of bytes in the strings sl@0: //max number of unicode chars is with a 1 to 1 mapping. sl@0: wenv = (wchar_t**)malloc(1 + count*sizeof(wchar_t*)); sl@0: buf = (wchar_t*)malloc(2*total); sl@0: sl@0: if (!(wenv && buf)) //we've had a malloc failure sl@0: { sl@0: r->_errno = ENOMEM; sl@0: goto bailout; sl@0: } sl@0: sl@0: wchar_t* p = buf; sl@0: sl@0: TInt ret; sl@0: for (TInt x = 0; x < count; x++) sl@0: { sl@0: wenv[count] = p; sl@0: ret = mbstowcs(p, env[count], MAXPATHLEN); sl@0: if (ret >= 0) sl@0: { sl@0: p += ret; //step to next bit of space sl@0: } sl@0: else sl@0: { sl@0: r->_errno = EILSEQ; sl@0: goto bailout; sl@0: } sl@0: sl@0: } sl@0: } sl@0: sl@0: sl@0: ret = _wpopen3_r(r, wcmd, wmode, wenv, fids); sl@0: } sl@0: else sl@0: { sl@0: r->_errno = EILSEQ; sl@0: } sl@0: sl@0: //don't lose the memory sl@0: bailout: sl@0: free(wenv); sl@0: free(buf); sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: /** A wide-character version of reentrant of popen3(). sl@0: */ sl@0: EXPORT_C int _wpopen3_r (struct _reent *r, const wchar_t* cmd, const wchar_t* mode, wchar_t** env, int fids[3]) sl@0: { sl@0: // Find the full path of the thing we are executing... sl@0: const wchar_t* cp=cmd; sl@0: while (*cp==L' ') sl@0: ++cp; // skip leading spaces sl@0: wchar_t file[MAXPATHLEN+1]; sl@0: TInt i=0; sl@0: wchar_t c=0; sl@0: for (i=0; i_errno); sl@0: } sl@0: sl@0: /** sl@0: Lets the calling process obtain status information about one of its child processes. sl@0: If status information is available for two or more child processes, the order in sl@0: which their status is reported is unspecified. sl@0: sl@0: @param pid Specifies a set of child processes for which the status is requested sl@0: @param status Specifies the location to which the child process' exit status is stored. sl@0: @param options Is the bitwise inclusive-OR of zero or more of the following flags. sl@0: sl@0: @return On Success, returns a value equal to the process ID of the child process. sl@0: On Failure, returns -1 and errno may be set OR returns 0 if the status is not available sl@0: for the specified process and it's set not to hang in the options. sl@0: */ sl@0: EXPORT_C int waitpid (int pid, int* status, int options) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _waitpid_r (r, pid, status, options); sl@0: } sl@0: sl@0: /** A reentrant version of waitpid(). sl@0: */ sl@0: EXPORT_C int _waitpid_r (struct _reent *r, int pid, int* status, int options) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.waitpid(pid,status,options,r->_errno); sl@0: } sl@0: sl@0: /** sl@0: Calls reentrant version of waitpid(). sl@0: */ sl@0: EXPORT_C int wait (int* status) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _waitpid_r (r, -1, status, 0); sl@0: } sl@0: sl@0: /** A reentrant version of wait(). sl@0: */ sl@0: EXPORT_C int _wait_r (struct _reent *r, int* status) sl@0: { sl@0: return _waitpid_r (r,-1,status,0); sl@0: } sl@0: sl@0: /** sl@0: Execute command. sl@0: sl@0: @param cmd Null-terminated string containing the system command to be executed. sl@0: sl@0: @return On Success, the command interpreter returns an adequate value; generally 0 sl@0: indicates that the action performed by the command interpreter terminated sl@0: with no errors. sl@0: On Failure, return -1. sl@0: */ sl@0: EXPORT_C int system (const char* cmd) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _system_r (r, cmd); sl@0: } sl@0: sl@0: /** A reentrant version of system(). sl@0: */ sl@0: EXPORT_C int _system_r (struct _reent *r, const char* cmd) sl@0: { sl@0: if (cmd==0) sl@0: return 1; // special case, says that we do support system(). sl@0: int fids[3]; sl@0: int pid=_popen3_r(r, cmd, "", 0, fids); sl@0: if (pid<0) sl@0: return -1; sl@0: int status=0; sl@0: pid=_waitpid_r (r,pid,&status,0); sl@0: if (pid<0) sl@0: return -1; sl@0: return status; sl@0: } sl@0: sl@0: /** A wide-character version of a system(). sl@0: */ sl@0: EXPORT_C int wsystem (const wchar_t* cmd) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _wsystem_r (r, cmd); sl@0: } sl@0: sl@0: /** A wide-character version of reentrant of system(). sl@0: */ sl@0: EXPORT_C int _wsystem_r (struct _reent *r, const wchar_t* cmd) sl@0: { sl@0: if (cmd==0) sl@0: return 1; // special case, says that we do support system(). sl@0: int fids[3]; sl@0: int pid=_wpopen3_r(r, cmd, (wchar_t*)L"", 0, fids); sl@0: if (pid<0) sl@0: return -1; sl@0: int status=0; sl@0: pid=_waitpid_r (r,pid,&status,0); sl@0: if (pid<0) sl@0: return -1; sl@0: return status; sl@0: } sl@0: sl@0: } // extern "C" sl@0: sl@0: #include sl@0: sl@0: /** Dubious asynchronous interface to ioctl, must be called from C++ sl@0: sl@0: @return On Success, returns a value other than -1. sl@0: On Failure, returns -1 and errno may be set. sl@0: */ sl@0: EXPORT_C int ioctl (int aFid, int aCmd, void* aParam, TRequestStatus& aStatus) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _ioctl_r(r, aFid, aCmd, aParam, aStatus); sl@0: } sl@0: sl@0: /** A reentrant version of a ioctl(). sl@0: */ sl@0: EXPORT_C int _ioctl_r (struct _reent *r, int aFid, int aCmd, void* aParam, TRequestStatus& aStatus) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.ioctl(aFid,aCmd,aParam,aStatus,r->_errno); sl@0: } sl@0: sl@0: EXPORT_C int ioctl_complete (int aFid, int aCmd, void* aParam, TRequestStatus& aStatus) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _ioctl_complete_r(r, aFid, aCmd, aParam, aStatus); sl@0: } sl@0: sl@0: /** A reentrant version of a ioctl_complete(). sl@0: */ sl@0: EXPORT_C int _ioctl_complete_r (struct _reent *r, int aFid, int aCmd, void* aParam, TRequestStatus& aStatus) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.ioctl_complete(aFid,aCmd,aParam,aStatus,r->_errno); sl@0: } sl@0: sl@0: EXPORT_C int ioctl_cancel (int aFid) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _ioctl_cancel_r(r, aFid); sl@0: } sl@0: sl@0: /** A reentrant version of a ioctl_cancel(). sl@0: */ sl@0: EXPORT_C int _ioctl_cancel_r (struct _reent *r, int aFid) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.ioctl_cancel(aFid,r->_errno); sl@0: } sl@0: