Update contrib.
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // connectors for re-entrant system calls
23 #include <fcntl.h> // for open()
24 #include <sys/ioctl.h>
26 #include <sys/errno.h>
27 #include <stdio_r.h> // for popen3
28 #include <stdlib_r.h> // for system
36 Opens the file which name is stored in the file string.
38 @return On Success, a non-negative integer representing the lowest numbered unused file descriptor.
39 On Failure, returns -1, errno may be set.
41 EXPORT_C int open (const char *file, int flags, ...)
46 struct _reent *r = _REENT2;
48 return -1; // Memory for library globals is not allocated (errno not set).
51 ret = _open_r (r, file, flags, va_arg (ap, int));
57 A wide_character version of a open().
59 EXPORT_C int wopen (const wchar_t *file, int flags, ...)
64 struct _reent *r = _REENT2;
66 return -1; // Memory for library globals is not allocated (errno not set).
69 ret = _wopen_r (r, file, flags, va_arg (ap, int));
74 /** A reentrant version of open().
76 EXPORT_C int _open_r (struct _reent *r, const char *name, int mode, int perms)
78 wchar_t _widename[KMaxFileName+1];
80 if (-1 != mbstowcs(_widename, name, KMaxFileName))
82 MSystemInterface& sysIf=Interface(r);
83 return sysIf.open(_widename,mode,perms,r->_errno);
86 MapError(EILSEQ, r->_errno);
87 return 0; //null file pointer
91 /** A reentrant version of wopen().
93 EXPORT_C int _wopen_r (struct _reent *r, const wchar_t *name, int mode, int perms)
95 MSystemInterface& sysIf=Interface(r);
96 return sysIf.open(name,mode,perms,r->_errno);
100 Reads a block of data of the length specified by cnt.
102 @return On Success, return a non-negative integer indicating the number of bytes actually read.
103 On Failure, returns -1, errno may be set.
105 EXPORT_C int read (int fd, char *buf, size_t cnt)
107 struct _reent *r = _REENT2;
109 return -1; // Memory for library globals is not allocated (errno not set).
110 return _read_r (r, fd, buf, cnt);
113 /** A reentrant version of read().
115 EXPORT_C int _read_r (struct _reent *r, int fd, char *buf, size_t nbyte)
117 MSystemInterface& sysIf=Interface(r);
118 return sysIf.read(fd,buf,nbyte,r->_errno);
122 Writes a block of data of the length specified by cnt.
124 @return On Success, returns the number of bytes written to the file. The number
125 shall never be greater than cnt.
126 On Failure, returns -1, errno may be set.
128 EXPORT_C int write (int fd, const char *buf, size_t cnt)
130 struct _reent *r = _REENT2;
132 return -1; // Memory for library globals is not allocated (errno not set).
133 return _write_r (r, fd, buf, cnt);
136 /** A reentrant version of write().
138 EXPORT_C int _write_r (struct _reent *r, int fd, const char *buf, size_t nbyte)
140 MSystemInterface& sysIf=Interface(r);
141 return sysIf.write(fd,buf,nbyte,r->_errno);
147 @return On Success, returns 0.
148 On Failure, returns -1, errno may be set.
150 EXPORT_C int close (int fd)
152 struct _reent *r = _REENT2;
154 return -1; // Memory for library globals is not allocated (errno not set).
155 return _close_r (r, fd);
159 /** A reentrant version of close().
161 EXPORT_C int _close_r (struct _reent *r, int fd)
163 MSystemInterface& sysIf=Interface(r);
164 return sysIf.close(fd,r->_errno);
168 Synchronizes a file's in-memory state with that on the physical medium.
170 @param fd Is the file descriptor for the file to be synchronized.
172 @return On Success, returns 0.
173 On Failure, returns -1, errno may be set.
175 EXPORT_C int fsync (int fd)
177 struct _reent *r = _REENT2;
179 return -1; // Memory for library globals is not allocated (errno not set).
180 return _fsync_r (r, fd);
183 /** A reentrant version of fsync().
185 EXPORT_C int _fsync_r (struct _reent *r, int fd)
187 MSystemInterface& sysIf=Interface(r);
188 return sysIf.fsync(fd,r->_errno);
192 Repositions the read/write file offset.
193 @return a nonnegative integer that indicates the file pointer value.
194 @param fd Is the file descriptor of an open file.
195 @param pos Specifies the number of bytes to offset the file pointer
196 from a specified file origin.
197 @param whence Specifies the location from which to start seeking.
199 EXPORT_C off_t lseek (int fd, off_t pos, int whence)
201 return _lseek_r (_REENT, fd, pos, whence);
204 /** A reentrant version of fseek().
206 EXPORT_C off_t _lseek_r (struct _reent *r, int fd, off_t pos, int whence)
208 MSystemInterface& sysIf=Interface(r);
209 return sysIf.lseek(fd,pos,whence,r->_errno);
213 Gets information about the named file and writes it to the area that buf points to.
214 The system must be able to search all directories leading to the file;
215 however, read, write, or execute permission of the file is not required.
217 @param fd Is a file descriptor referring to a file for which status is returned.
218 @param st Points to a stat structure where status information about the file is to be placed.
220 @return On Success, returns 0.
221 On Failure, returns -1, errno may be set.
223 EXPORT_C int fstat (int fd, struct stat *st)
225 struct _reent *r = _REENT2;
227 return -1; // Memory for library globals is not allocated (errno not set).
228 return _fstat_r (r, fd, st);
231 /** A reentrant version of fstat().
233 EXPORT_C int _fstat_r (struct _reent *r, int fd, struct stat *st)
235 MSystemInterface& sysIf=Interface(r);
236 return sysIf.fstat(fd,st,r->_errno);
240 Gets the size of a file.
242 @return On Success, returns 0.
243 On Failure, returns -1, errno may be set.
245 EXPORT_C int stat (const char *name, struct stat *st)
247 struct _reent *r = _REENT2;
249 return -1; // Memory for library globals is not allocated (errno not set).
250 return _stat_r (r, name, st);
253 /** A reentrant version of stat().
255 EXPORT_C int _stat_r (struct _reent *r, const char *name, struct stat *st)
257 wchar_t tmpbuf[KMaxFullName+1];
258 if (-1 != mbstowcs(tmpbuf, name, KMaxFullName))
260 MSystemInterface& sysIf=Interface(r);
261 return sysIf.stat(tmpbuf, st, r->_errno);
263 MapError(EILSEQ, r->_errno);
268 A wide_character version of a stat().
270 EXPORT_C int wstat (const wchar_t *name, struct stat *st)
272 struct _reent *r = _REENT2;
274 return -1; // Memory for library globals is not allocated (errno not set).
275 return _wstat_r (r, name, st);
278 /** A reentrant version of wstat().
280 EXPORT_C int _wstat_r (struct _reent *r, const wchar_t *name, struct stat *st)
282 MSystemInterface& sysIf=Interface(r);
283 return sysIf.stat(name,st,r->_errno);
287 Duplicates an open file descriptor.
289 @param aFid Is the file descriptor to duplicate.
291 @return On Success, returns a non-negative integer, namely the duplicated file descriptor, which
292 is the lowest available descriptor.
293 On Failure, returns -1, errno may be set.
295 EXPORT_C int dup (int aFid)
297 struct _reent *r = _REENT2;
299 return -1; // Memory for library globals is not allocated (errno not set).
300 return _dup_r(r, aFid);
303 /** A reentrant version of dup().
305 EXPORT_C int _dup_r (struct _reent *r, int aFid)
307 MSystemInterface& sysIf=Interface(r);
308 return sysIf.dup(aFid,r->_errno);
312 Function duplicates an open file descriptor.
314 @param aFid1 Is the file descriptor to duplicate.
315 @param aFid2 Is the file descriptor that filedes is duplicated onto.
317 @return On Success, returns a non-negative integer, namely the duplicated file descriptor, which
318 is the lowest available descriptor.
319 On Failure, returns -1, errno may be set.
321 EXPORT_C int dup2 (int aFid1, int aFid2)
323 struct _reent *r = _REENT2;
325 return -1; // Memory for library globals is not allocated (errno not set).
326 return _dup2_r(r, aFid1, aFid2);
329 /** A reentrant version of dup2().
331 EXPORT_C int _dup2_r (struct _reent *r, int aFid1, int aFid2)
333 MSystemInterface& sysIf=Interface(r);
334 return sysIf.dup2(aFid1,aFid2,r->_errno);
338 Performs a variety of device-specific control functions on device special files.
340 @return On Success, returns a value other than -1 that depends upon the STREAMS device control function.
341 On Failure, return -1, errno may be set.
343 EXPORT_C int ioctl (int aFid, int aCmd, void* aParam)
345 struct _reent *r = _REENT2;
347 return -1; // Memory for library globals is not allocated (errno not set).
348 return _ioctl_r(r, aFid, aCmd, aParam);
351 /** A reentrant version of ioctl().
353 EXPORT_C int _ioctl_r (struct _reent *r, int aFid, int aCmd, void* aParam)
355 MSystemInterface& sysIf=Interface(r);
356 return sysIf.ioctl(aFid,aCmd,aParam,r->_errno);
360 Gets the path name of the current working directory.
361 If a buffer is specified, the path name is placed in that buffer,
362 and the address of the buffer is returned.
363 @return If successful returns buf, if a non-null pointer was specified,
364 or the address of the allocated memory otherwise.
365 @param _buf Points to the buffer to copy the current working directory to,
366 or NULL if getcwd() should allocate the buffer.
367 @param _size Is the size, in bytes, of the array of characters that buf points to.
369 EXPORT_C char* getcwd (char *_buf, size_t _size)
371 return _getcwd_r(_REENT,_buf,_size);
375 A wide_character version of a getcwd().
377 EXPORT_C wchar_t* wgetcwd (wchar_t *_buf, size_t _size)
379 return _wgetcwd_r(_REENT,_buf,_size);
382 /** A reentrant version of getcwd().
384 EXPORT_C char* _getcwd_r (struct _reent *r, char *_buf, size_t _size)
386 char * _ourbuf = _buf;
389 _ourbuf=(char*)User::Alloc(_size);
397 //we are dealing with wide characters from here so we need a temporary buffer
398 wchar_t tmpbuf[KMaxFullName];
400 MSystemInterface& sysIf=Interface(r);
401 wchar_t * rval = sysIf.getcwd((wchar_t*)tmpbuf, _size, r->_errno);
403 if (rval) //we have a path
406 size_t x = wcstombs(_ourbuf, tmpbuf, _size); //convert the buffer
409 //deal with the fact we may have allocated our own buffer
410 if (_buf != _ourbuf) //we allocated it.
415 /** A wide-character version of reentrant of getcwd().
417 EXPORT_C wchar_t * _wgetcwd_r (struct _reent *r, wchar_t *_buf, size_t _size)
421 _buf=(wchar_t *)User::Alloc(_size*sizeof(wchar_t));
428 MSystemInterface& sysIf=Interface(r);
429 return sysIf.getcwd(_buf,_size,r->_errno);
433 Changes the current working directory to be pathname. The current directory is the
434 beginning point for file searches when path names are not absolute.
435 If the chdir() function fails, the current working directory remains unchanged.
437 @param _path Is the path name of a directory.
439 @return On Success, returns 0.
440 On Failure, returns -1, errno may be set.
442 EXPORT_C int chdir (const char *_path)
444 struct _reent *r = _REENT2;
446 return -1; // Memory for library globals is not allocated (errno not set).
447 return _chdir_r(r, _path);
450 /** A reentrant version of chdir().
452 EXPORT_C int _chdir_r (struct _reent *r, const char *_path)
454 //we need to use a wide buffer and convert
455 wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible
456 if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
458 MSystemInterface& sysIf=Interface(r);
459 return sysIf.chdir(tmpbuf, r->_errno);
461 MapError(EILSEQ, r->_errno);
465 /** A wide-character version of chdir().
467 EXPORT_C int wchdir (const wchar_t *_path)
469 struct _reent *r = _REENT2;
471 return -1; // Memory for library globals is not allocated (errno not set).
472 return _wchdir_r(r, _path);
475 /** A reentrant version of wchdir().
477 EXPORT_C int _wchdir_r (struct _reent *r, const wchar_t *_path)
479 MSystemInterface& sysIf=Interface(r);
480 return sysIf.chdir(_path,r->_errno);
484 Removes an empty directory whose name is given by pathname.
485 The directory must not have any entries other than dot (.) and dot-dot (..).
487 @param _path Points to the directory that the rmdir() function removes.
489 @return On Success, returns 0.
490 On Failure, returns -1, errno may be set.
492 EXPORT_C int rmdir (const char *_path)
494 struct _reent *r = _REENT2;
496 return -1; // Memory for library globals is not allocated (errno not set).
497 return _rmdir_r(r, _path);
500 /** A reentrant version of rmdir().
502 EXPORT_C int _rmdir_r (struct _reent *r, const char *_path)
504 wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible
505 if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
507 MSystemInterface& sysIf=Interface(r);
508 return sysIf.rmdir(tmpbuf, r->_errno);
510 MapError(EILSEQ, r->_errno);
514 /** A wide-character version of rmdir().
516 EXPORT_C int wrmdir (const wchar_t *_path)
518 struct _reent *r = _REENT2;
520 return -1; // Memory for library globals is not allocated (errno not set).
521 return _wrmdir_r(r,_path);
524 /** A reentrant version of wrmdir().
526 EXPORT_C int _wrmdir_r (struct _reent *r, const wchar_t *_path)
528 MSystemInterface& sysIf=Interface(r);
529 return sysIf.rmdir(_path,r->_errno);
533 Creates a new directory with the specified path name.
534 The file permissions of the new directory are initialized from the specified mode.
536 @param _path Specifies the name of the new directory. The path name can be absolute or relative.
537 If the specified path name is relative, the directory is created based upon your current
539 @param _mode Is a bitwise-OR field that specifies what permissions the directory has when it is created.
541 @return On Success, returns 0.
542 On Failure, returns -1, errno may be set. Does not create a directory.
544 EXPORT_C int mkdir (const char *_path, mode_t _mode)
546 struct _reent *r = _REENT2;
548 return -1; // Memory for library globals is not allocated (errno not set).
549 return _mkdir_r(r,_path,_mode);
552 /** A reentrant version of mkdir().
554 EXPORT_C int _mkdir_r (struct _reent *r, const char *_path, mode_t _mode)
556 //we need to use a wide buffer and convert
557 wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible
558 if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
560 MSystemInterface& sysIf=Interface(r);
561 return sysIf.mkdir(tmpbuf, _mode, r->_errno);
563 MapError(EILSEQ, r->_errno);
567 /** A wide-character version of mkdir().
569 EXPORT_C int wmkdir (const wchar_t *_path, mode_t _mode)
571 struct _reent *r = _REENT2;
573 return -1; // Memory for library globals is not allocated (errno not set).
574 return _wmkdir_r(r, _path, _mode);
577 /** A reentrant version of wmkdir().
579 EXPORT_C int _wmkdir_r (struct _reent *r, const wchar_t *_path, mode_t _mode)
581 MSystemInterface& sysIf=Interface(r);
582 return sysIf.mkdir(_path,_mode,r->_errno);
586 Sets the access permissions for the file whose name is given by pathname to the bit
587 pattern contained in mode. For this call to succeed, the effective user ID of the
588 process must match the owner of the file, or the process must have appropriate privileges.
589 The owner of the file pathname always has privileges to change permission modes and file attributes.
591 @param _path Points to the name of the file.
592 @param _mode Is a bitwise-or field that specifies the new permission modes for path name.
594 @return On Success, returns 0.
595 On Failure, returns -1, errno may be set.
597 EXPORT_C int chmod (const char *_path, mode_t _mode)
599 struct _reent *r = _REENT2;
601 return -1; // Memory for library globals is not allocated (errno not set).
602 return _chmod_r(r, _path, _mode);
605 /** A reentrant version of chmod().
607 EXPORT_C int _chmod_r (struct _reent *r, const char *_path, mode_t _mode)
609 wchar_t tmpbuf[KMaxFullName+1];
610 if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
612 MSystemInterface& sysIf=Interface(r);
613 return sysIf.chmod(tmpbuf,_mode,r->_errno);
615 MapError(EILSEQ, r->_errno);
619 /** A wide-character version of chmod().
621 EXPORT_C int wchmod (const wchar_t *_path, mode_t _mode)
623 struct _reent *r = _REENT2;
625 return -1; // Memory for library globals is not allocated (errno not set).
626 return _wchmod_r(r, _path, _mode);
629 /** A reentrant version of wchmod().
631 EXPORT_C int _wchmod_r (struct _reent *r, const wchar_t *_path, mode_t _mode)
633 MSystemInterface& sysIf=Interface(r);
634 return sysIf.chmod(_path,_mode,r->_errno);
638 Removes a link to a file, and decrements the link count of the referenced file by one. When
639 the file's link count becomes 0 and no process has the file open, the space occupied by the
640 file is freed, and the file is no longer accessible. If one or more processes have the file
641 open when the last link is removed, the link is removed before unlink() returns, but the
642 removal of the file contents is postponed until all references to the file are closed.
644 @param _path Points to the path name that names the file to be unlinked.
646 @return On Success, returns 0.
647 On Failure, returns -1, errno may be set.
649 EXPORT_C int unlink (const char *_path)
651 struct _reent *r = _REENT2;
653 return -1; // Memory for library globals is not allocated (errno not set).
654 return _unlink_r(r, _path);
657 /** A reentrant version of unlink().
659 EXPORT_C int _unlink_r (struct _reent *r, const char *_path)
661 wchar_t tmpbuf[KMaxFullName+1];
662 if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
664 MSystemInterface& sysIf=Interface(r);
665 return sysIf.unlink(tmpbuf, r->_errno);
667 MapError(EILSEQ, r->_errno);
671 /** A wide-character version of unlink().
673 EXPORT_C int wunlink (const wchar_t *_path)
675 struct _reent *r = _REENT2;
677 return -1; // Memory for library globals is not allocated (errno not set).
678 return _wunlink_r(r, _path);
681 /** A wide-character version of reentrant of unlink().
683 EXPORT_C int _wunlink_r (struct _reent *r, const wchar_t *_path)
685 MSystemInterface& sysIf=Interface(r);
686 return sysIf.unlink(_path,r->_errno);
692 @param oldpath Points to the path name of the file to be renamed. The path name can be
693 absolute or relative. If a relative path name is given, the file is searched from
694 the current working directory.
695 @param newpath Points to the path name of the file. The path name can be absolute or relative.
696 If a relative path name is given, the file is searched from the current working directory.
698 @return On Success, returns 0.
699 On Failure, returns -1, errno may be set. Does not change either the file named
700 by old or the file named by new (if either exists).
702 EXPORT_C int rename (const char *oldpath, const char *newpath)
704 struct _reent *r = _REENT2;
706 return -1; // Memory for library globals is not allocated (errno not set).
707 return _rename_r (r, oldpath, newpath);
710 /** A reentrant version of rename().
712 EXPORT_C int _rename_r (struct _reent *r, const char *oldpath, const char *newpath)
714 wchar_t _old[KMaxFullName+1];
715 wchar_t _new[KMaxFullName+1];
716 if (-1 != mbstowcs(_old, oldpath, KMaxFullName))
718 if (-1 != mbstowcs(_new, newpath, KMaxFullName))
720 MSystemInterface& sysIf=Interface(r);
721 return sysIf.rename(_old, _new, r->_errno);
724 MapError(EILSEQ, r->_errno);
728 /** A wide-character version of rename().
730 EXPORT_C int wrename (const wchar_t *oldpath, const wchar_t *newpath)
732 struct _reent *r = _REENT2;
734 return -1; // Memory for library globals is not allocated (errno not set).
735 return _wrename_r (r, oldpath, newpath);
738 /** A wide-character version of reentrant of rename().
740 EXPORT_C int _wrename_r (struct _reent *r, const wchar_t *oldpath, const wchar_t *newpath)
742 MSystemInterface& sysIf=Interface(r);
743 return sysIf.rename(oldpath,newpath,r->_errno);
747 Takes a specified path name, pathname and resolves all symbolic links,
748 extra slashes (/), and references to /./ and /../.
749 The resulting absolute path name is placed in the memory location
750 pointed to by the resolved_path argument.
751 @return resolved_path.
752 When an error occurs,returns a null pointer, setsresolved_path
753 to the path name that caused the error.
754 @param path Points to the path name that you want resolved to an absolute form.
755 This may be either a relative or absolute path name.
756 All but the final component of this path name must exist when you call realpath().
757 @param resolved Points to the location where the canonical version
758 of pathname is to be placed.
760 EXPORT_C char* realpath (const char* path, char* resolved)
762 return _realpath_r(_REENT, path, resolved);
765 /** A wide-character version of realpath().
767 EXPORT_C wchar_t* wrealpath (const wchar_t* path, wchar_t* resolved)
769 return _wrealpath_r(_REENT, path, resolved);
772 /** A wide-character version of reentrant of realpath().
774 EXPORT_C wchar_t * _wrealpath_r (struct _reent *r, const wchar_t *relpath, wchar_t *resolved)
777 TPtr16 name((TText16*)resolved,MAXPATHLEN);
779 MSystemInterface& sysIf=Interface(r);
780 TInt err = sysIf.ResolvePath(path, relpath, &name);
783 err = path.SetNoWild(path.DriveAndPath(),NULL,&name);
786 name = path.FullName();
787 name.ZeroTerminate();
791 MapError(err,r->_errno);
795 /** A reentrant version of realpath().
797 EXPORT_C char* _realpath_r (struct _reent *r, const char *relpath, char *resolved)
804 MSystemInterface& sysIf=Interface(r);
806 wchar_t _wrelpath[KMaxFileName];
808 if (-1 != mbstowcs(_wrelpath, relpath , KMaxFileName))
810 err = sysIf.ResolvePath(path, _wrelpath, &name);
813 err = path.SetNoWild(path.DriveAndPath(),NULL,&name);
816 name = path.FullName();
818 if (-1 != wcstombs(resolved, (wchar_t*)name.PtrZ(), KMaxFileName))
832 MapError(err,r->_errno);
837 Gives access to the client's stdin.
839 @return On Success, returns a pointer to an open stream, used to read or write to the pipe.
840 On Failure, return a null pointer.
842 EXPORT_C int popen3 (const char* cmd, const char* mode, char** env, int fids[3])
844 struct _reent *r = _REENT2;
846 return NULL; // Memory for library globals is not allocated (errno not set).
847 return _popen3_r (r,cmd,mode,env,fids);
850 /** A wide-character version of popen3().
852 EXPORT_C int wpopen3 (const wchar_t* cmd, const wchar_t* mode, wchar_t** env, int fids[3])
854 struct _reent *r = _REENT2;
856 return NULL; // Memory for library globals is not allocated (errno not set).
857 return _wpopen3_r (r,cmd,mode,env,fids);
860 /** A reentrant version of a popen3().
862 EXPORT_C int _popen3_r (struct _reent *r, const char* cmd, const char* mode, char** env, int fids[3])
865 wchar_t wcmd[MAXPATHLEN+1];
866 wchar_t wmode[MAXPATHLEN+1];
868 wchar_t ** wenv = NULL;
869 wchar_t * buf = NULL;
873 if ((-1 != mbstowcs(wcmd, cmd, MAXPATHLEN)) &&
874 (-1 != mbstowcs(wmode, mode, MAXPATHLEN)))
876 //OK, we've widened the first 2 args
877 //now for the environment
879 //env is basically an array of char pointers with a NULL as the last one
882 //OK we have a ptr to something
883 //count the number of entries and get their lengths so we can work out how much space
884 //is needed for the new one
888 while (env[count] != NULL)
890 total+= strlen(env[count])+1;
893 //total has number of bytes in the strings
894 //max number of unicode chars is with a 1 to 1 mapping.
895 wenv = (wchar_t**)malloc(1 + count*sizeof(wchar_t*));
896 buf = (wchar_t*)malloc(2*total);
898 if (!(wenv && buf)) //we've had a malloc failure
907 for (TInt x = 0; x < count; x++)
910 ret = mbstowcs(p, env[count], MAXPATHLEN);
913 p += ret; //step to next bit of space
925 ret = _wpopen3_r(r, wcmd, wmode, wenv, fids);
932 //don't lose the memory
940 /** A wide-character version of reentrant of popen3().
942 EXPORT_C int _wpopen3_r (struct _reent *r, const wchar_t* cmd, const wchar_t* mode, wchar_t** env, int fids[3])
944 // Find the full path of the thing we are executing...
945 const wchar_t* cp=cmd;
947 ++cp; // skip leading spaces
948 wchar_t file[MAXPATHLEN+1];
951 for (i=0; i<MAXPATHLEN; i++, cp++)
955 if (c==L' ' || c==L'\t' || c==L'\0') // stop at first space, tab or \0
959 wchar_t resolved[MAXPATHLEN+1];
960 if(_wrealpath_r(r, file, resolved)==0)
961 return -1; // no such file
963 // Strip leading whitespace from the rest of the commandline
964 for (; i<MAXPATHLEN;i++,cp++)
969 if ((c!=' ') && (c!='\t'))
976 const wchar_t* mp=mode;
988 MSystemInterface& sysIf=Interface(r);
989 return sysIf.popen3(resolved,cp,mode,env,fids,r->_errno);
993 Lets the calling process obtain status information about one of its child processes.
994 If status information is available for two or more child processes, the order in
995 which their status is reported is unspecified.
997 @param pid Specifies a set of child processes for which the status is requested
998 @param status Specifies the location to which the child process' exit status is stored.
999 @param options Is the bitwise inclusive-OR of zero or more of the following flags.
1001 @return On Success, returns a value equal to the process ID of the child process.
1002 On Failure, returns -1 and errno may be set OR returns 0 if the status is not available
1003 for the specified process and it's set not to hang in the options.
1005 EXPORT_C int waitpid (int pid, int* status, int options)
1007 struct _reent *r = _REENT2;
1009 return -1; // Memory for library globals is not allocated (errno not set).
1010 return _waitpid_r (r, pid, status, options);
1013 /** A reentrant version of waitpid().
1015 EXPORT_C int _waitpid_r (struct _reent *r, int pid, int* status, int options)
1017 MSystemInterface& sysIf=Interface(r);
1018 return sysIf.waitpid(pid,status,options,r->_errno);
1022 Calls reentrant version of waitpid().
1024 EXPORT_C int wait (int* status)
1026 struct _reent *r = _REENT2;
1028 return -1; // Memory for library globals is not allocated (errno not set).
1029 return _waitpid_r (r, -1, status, 0);
1032 /** A reentrant version of wait().
1034 EXPORT_C int _wait_r (struct _reent *r, int* status)
1036 return _waitpid_r (r,-1,status,0);
1042 @param cmd Null-terminated string containing the system command to be executed.
1044 @return On Success, the command interpreter returns an adequate value; generally 0
1045 indicates that the action performed by the command interpreter terminated
1047 On Failure, return -1.
1049 EXPORT_C int system (const char* cmd)
1051 struct _reent *r = _REENT2;
1053 return -1; // Memory for library globals is not allocated (errno not set).
1054 return _system_r (r, cmd);
1057 /** A reentrant version of system().
1059 EXPORT_C int _system_r (struct _reent *r, const char* cmd)
1062 return 1; // special case, says that we do support system().
1064 int pid=_popen3_r(r, cmd, "", 0, fids);
1068 pid=_waitpid_r (r,pid,&status,0);
1074 /** A wide-character version of a system().
1076 EXPORT_C int wsystem (const wchar_t* cmd)
1078 struct _reent *r = _REENT2;
1080 return -1; // Memory for library globals is not allocated (errno not set).
1081 return _wsystem_r (r, cmd);
1084 /** A wide-character version of reentrant of system().
1086 EXPORT_C int _wsystem_r (struct _reent *r, const wchar_t* cmd)
1089 return 1; // special case, says that we do support system().
1091 int pid=_wpopen3_r(r, cmd, (wchar_t*)L"", 0, fids);
1095 pid=_waitpid_r (r,pid,&status,0);
1105 /** Dubious asynchronous interface to ioctl, must be called from C++
1107 @return On Success, returns a value other than -1.
1108 On Failure, returns -1 and errno may be set.
1110 EXPORT_C int ioctl (int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
1112 struct _reent *r = _REENT2;
1114 return -1; // Memory for library globals is not allocated (errno not set).
1115 return _ioctl_r(r, aFid, aCmd, aParam, aStatus);
1118 /** A reentrant version of a ioctl().
1120 EXPORT_C int _ioctl_r (struct _reent *r, int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
1122 MSystemInterface& sysIf=Interface(r);
1123 return sysIf.ioctl(aFid,aCmd,aParam,aStatus,r->_errno);
1126 EXPORT_C int ioctl_complete (int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
1128 struct _reent *r = _REENT2;
1130 return -1; // Memory for library globals is not allocated (errno not set).
1131 return _ioctl_complete_r(r, aFid, aCmd, aParam, aStatus);
1134 /** A reentrant version of a ioctl_complete().
1136 EXPORT_C int _ioctl_complete_r (struct _reent *r, int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
1138 MSystemInterface& sysIf=Interface(r);
1139 return sysIf.ioctl_complete(aFid,aCmd,aParam,aStatus,r->_errno);
1142 EXPORT_C int ioctl_cancel (int aFid)
1144 struct _reent *r = _REENT2;
1146 return -1; // Memory for library globals is not allocated (errno not set).
1147 return _ioctl_cancel_r(r, aFid);
1150 /** A reentrant version of a ioctl_cancel().
1152 EXPORT_C int _ioctl_cancel_r (struct _reent *r, int aFid)
1154 MSystemInterface& sysIf=Interface(r);
1155 return sysIf.ioctl_cancel(aFid,r->_errno);