Update contrib.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: connectors for re-entrant system calls
18 // connectors for re-entrant system calls
23 #include <fcntl.h> // for open()
24 #include <sys/ioctl.h>
26 #include <sys/errno.h>
31 #include <sys/types.h>
33 #include <sys/aeselect.h>
34 #include "aeselectreent.h"
36 #include "stdio_r.h" // for popen3
37 #include "stdlib_r.h" // for system
39 #if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__)))
40 #include "libc_wsd_defs.h"
42 #define MAXPATHLEN 260 /* E32STD.H: KMaxFullName + 4 to avoid data loss */
47 Opens the file which name is stored in the file string.
49 EXPORT_C int open (const char *file, int flags, ...)
57 return -1 ; //null file pointer
60 va_start (argList, flags);
61 perms = va_arg(argList,int);
65 if( perms > (S_IRWXU | S_IRWXG | S_IRWXO ) )
67 //make it read-write for all
68 perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
70 wchar_t _widename[MAXPATHLEN+1];
72 if ((size_t)-1 != mbstowcs(_widename, file, MAXPATHLEN))
74 return _open_r (&errno, _widename, flags, perms);
79 return -1; // Illegal Sequence of wide characeters
85 A wide_character version of a open().
87 EXPORT_C int wopen (const wchar_t *file, int flags, ...)
100 perms = va_arg(ap,int);
104 if( perms > (S_IRWXU | S_IRWXG | S_IRWXO ) )
106 //make it read-write for all
107 perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
110 ret = _wopen_r (&errno, file, flags, perms);
117 Reads a block of data of the length specified by cnt.
119 EXPORT_C int read (int fd, void *buf, size_t cnt)
136 //If the fd corresponding to STDIN
137 if(fd == STDIN_FILENO && (Backend()->GetDesc(STDIN_FILENO))->Attributes() == KConsoleFd)
144 ret = _read_r(&errno, STDIN_FILENO, &ch, 1);
146 //Copy requested count of data, ignore the rest
147 if(ret > 0 && len < cnt )
149 ((char*)buf)[len] = ch;
169 return _read_r(&errno, fd, (char*)buf, cnt);
175 Writes a block of data of the length specified by cnt.
177 EXPORT_C int write (int fd, const void *buf, size_t cnt)
193 return _write_r(&errno, fd, (char*)buf, cnt);
200 EXPORT_C int close (int fd)
202 return _close_r(&errno, fd);
208 Synchronizes a file's in-memory state with that on the physical medium.
210 EXPORT_C int fsync (int fd)
212 return _fsync_r(&errno, fd);
216 Repositions the read/write file offset.
218 EXPORT_C off_t lseek (int fd, off_t pos, int whence)
220 return _lseek_r(&errno, fd, pos, whence);
225 Gets information about the named file and writes it to the area that buf points to.
226 The system must be able to search all directories leading to the file;
227 however, read, write, or execute permission of the file is not required.
229 EXPORT_C int fstat (int fd, struct stat *st)
236 return _fstat_r(&errno, fd, st);
241 Gets the size of a file.
243 EXPORT_C int stat (const char *name, struct stat *st)
251 wchar_t tmpbuf[MAXPATHLEN+1];
252 if ((size_t)-1 != mbstowcs(tmpbuf, name, MAXPATHLEN))
254 return _stat_r(&errno, tmpbuf, st);
261 EXPORT_C int utime (const char *name, const struct utimbuf *filetimes)
269 wchar_t tmpbuf[MAXPATHLEN+1];
270 if ((size_t)-1 != mbstowcs(tmpbuf, name, MAXPATHLEN))
272 return _utime_r (&errno, tmpbuf, filetimes);
282 A wide_character version of a stat().
284 EXPORT_C int wstat (const wchar_t *name, struct stat *st)
291 return _wstat_r (&errno, name, st);
296 duplicates an open file descriptor.
298 EXPORT_C int dup (int aFid)
300 return _dup_r(&errno, aFid);
305 function duplicates an open file descriptor.
307 EXPORT_C int dup2 (int aFid1, int aFid2)
309 return _dup2_r(&errno, aFid1, aFid2);
314 sorms a variety of device-specific control functions on device special files.
316 EXPORT_C int ioctl (int aFid, unsigned long aCmd, ...)
321 va_start(Vlist , aCmd ) ;
322 aParam = va_arg(Vlist , void *) ;
329 ret = _ioctl_r(&errno, aFid, aCmd, aParam);
338 Gets the path name of the current working directory.
339 If a buffer is specified, the path name is placed in that buffer,
340 and the address of the buffer is returned.
344 A wide_character version of a getcwd().
346 EXPORT_C wchar_t* wgetcwd (wchar_t *_buf, size_t _size)
348 return _wgetcwd_r(&errno, _buf, _size);
353 Changes the current working directory to be pathname.
354 The current directory is the beginning point for file
355 searches when path names are not absolute.
356 If the chdir() function fails, the current working directory remains unchanged.
358 EXPORT_C int chdir (const char *_path)
366 //we need to use a wide buffer and convert
367 wchar_t tmpbuf[MAXPATHLEN+1]; //use the max path length possible
368 if ((size_t)-1 != mbstowcs(tmpbuf, _path, MAXPATHLEN))
370 return _chdir_r(&errno, tmpbuf);
377 /* A wide-character version of chdir().
379 EXPORT_C int wchdir (const wchar_t *_path)
388 return _wchdir_r(&errno, _path);
393 Removes an empty directory whose name is given by pathname.
394 The directory must not have any entries other than dot (.) and dot-dot (..).
396 EXPORT_C int rmdir (const char *_path)
404 if(!strcmp((_path + strlen(_path) -1 ) , ".") )
410 wchar_t tmpbuf[MAXPATHLEN+1]; //use the max path length possible
411 if ((size_t)-1 != mbstowcs(tmpbuf, _path, MAXPATHLEN))
413 return _rmdir_r(&errno, tmpbuf);
421 /* A wide-character version of rmdir().
423 EXPORT_C int wrmdir (const wchar_t *_path)
430 return _wrmdir_r(&errno, _path);
435 Creates a new directory with the specified path name.
436 The file permissions of the new directory are initialized from the specified mode.
438 EXPORT_C int mkdir (const char *_path, mode_t _mode)
446 //we need to use a wide buffer and convert
447 wchar_t tmpbuf[MAXPATHLEN+1]; //use the max path length possible
448 if ((size_t)-1 != mbstowcs(tmpbuf, _path, MAXPATHLEN))
450 return _mkdir_r(&errno, tmpbuf, _mode);
458 /* A wide-character version of mkdir().
460 EXPORT_C int wmkdir (const wchar_t *_path, mode_t _mode)
469 return _wmkdir_r(&errno, _path, _mode);
474 Sets the access permissions for the file
475 whose name is given by pathname to the bit pattern contained in mode.
476 For this call to succeed, the effective user ID of the process must match
477 the owner of the file, or the process must have appropriate privileges.
478 The owner of the file pathname always has privileges to change permission modes
481 EXPORT_C int chmod (const char *_path, mode_t _mode)
489 wchar_t tmpbuf[MAXPATHLEN+1];
490 if ((size_t)-1 != mbstowcs(tmpbuf, _path, MAXPATHLEN))
492 return _chmod_r(&errno, tmpbuf, _mode);
498 Sets the access permissions for the file specifed by file descriptor
499 whose name is given by pathname to the bit pattern contained in mode.
500 For this call to succeed, the effective user ID of the process must match
501 the owner of the file, or the process must have appropriate privileges.
502 The owner of the file pathname always has privileges to change permission modes
505 EXPORT_C int fchmod (int fd , mode_t _mode)
508 return _fchmod_r(&errno, fd, _mode);
513 /* A wide-character version of chmod().
515 EXPORT_C int wchmod (const wchar_t *_path, mode_t _mode)
524 return _wchmod_r(&errno, _path, _mode);
529 /* A wide-character version of unlink().
531 EXPORT_C int wunlink (const wchar_t *_path)
538 return _wunlink_r(&errno, _path);
545 EXPORT_C int rename (const char *oldpath, const char *newpath)
547 if((!oldpath) ||(!newpath))
553 wchar_t _old[MAXPATHLEN+1];
554 wchar_t _new[MAXPATHLEN+1];
555 if ((size_t)-1 != mbstowcs(_old, oldpath, MAXPATHLEN))
557 if ((size_t)-1 != mbstowcs(_new, newpath, MAXPATHLEN))
559 return _rename_r(&errno, _old, _new);
567 /* A wide-character version of rename().
569 EXPORT_C int wrename (const wchar_t *oldpath, const wchar_t *newpath)
572 if((!oldpath) ||(!newpath))
578 return _wrename_r(&errno, oldpath, newpath);
583 Takes a specified path name, pathname and resolves all symbolic links,
584 extra slashes (/), and references to /./ and /../.
585 The resulting absolute path name is placed in the memory location
586 pointed to by the resolved_path argument.
589 /* A wide-character version of realpath().
591 EXPORT_C wchar_t* wrealpath (const wchar_t* path, wchar_t* resolved)
593 return _wrealpath_r(&errno, path, resolved);
598 Gives access to the client's stdin
600 EXPORT_C FILE* popen (const char* command, const char* mode)
602 // Check for the validity of command.
603 // On Linux, the shell would return cannot find command to execute
610 if(strlen(command) > KMaxPath)
612 errno = ENAMETOOLONG;
615 // Check for the validity of Mode.
616 if( (!mode) || mode[0] != 'r' && mode[0] != 'w' )
623 wchar_t wcmd[KMaxPath+1];
626 if ((size_t)-1 != mbstowcs(wcmd, command, KMaxPath))
628 int fd = _wpopen_r(&errno, wcmd, mode);
631 // return value is a valid fd
632 return fdopen(fd, mode);
644 EXPORT_C FILE* wpopen (const wchar_t* command, const wchar_t* wmode)
650 sz = wcstombs(mode, wmode, 2);
651 //Check for the validity of Mode.
652 if (sz <= 0 || (mode[0] != 'r' && mode[0] != 'w'))
654 //If its neither "r" nor "w", its undefined behavior
661 if(wcslen(command) > KMaxPath)
663 errno = ENAMETOOLONG;
667 int fd = _wpopen_r(&errno, command, mode );
668 //If return Value is valid fd
671 return fdopen(fd, mode);
682 EXPORT_C int pclose(FILE* stream)
685 if (!stream || ((fd = fileno(stream)) == -1))
691 TInt err = _pclose_r(&errno, fd);
698 // reset errno just in case it has been modified by fclose
705 return fclose(stream);
708 /* A wide-character version of popen3().
710 EXPORT_C int wpopen3 (const wchar_t* file, const wchar_t* cmd, wchar_t** env, int fids[3])
718 if(wcslen(file) > KMaxPath)
720 errno = ENAMETOOLONG;
724 return _wpopen3_r(&errno, file, cmd, env, fids);
728 EXPORT_C int popen3 (const char* file, const char* cmd, char** env, int fids[3])
736 if(strlen(file) > KMaxPath)
738 errno = ENAMETOOLONG;
742 wchar_t wfile[KMaxPath+1];
743 wchar_t wcmd[KMaxPath+1];
745 wchar_t** wenv = NULL;
747 TInt ret = mbstowcs(wfile, file, KMaxPath);
748 TInt cmdlen = mbstowcs(wcmd, cmd, KMaxPath);
750 if (ret != (size_t)-1 && cmdlen != (size_t)-1)
752 //OK, we've widened the first 2 args
753 //now for the environment
755 //env will be an array of char pointers with a NULL as the last one
760 for (; env[count]; ++count) { }
765 wenv = (wchar_t **)malloc((count+1) * sizeof(wchar_t*));
772 for (int i = 0; i < count; ++i)
774 int len = strlen(env[i]) + 1;
775 wenv[i] = (wchar_t *)malloc(len * sizeof(wchar_t));
778 //coverity[leave_without_push]
783 if (mbstowcs(wenv[i], env[i], len) == (size_t)-1)
785 //coverity[leave_without_push]
797 //coverity[leave_without_push]
798 ret = wpopen3(wfile, wcmd, wenv, fids);
802 //coverity[leave_without_push]
803 ret = wpopen3(wfile, NULL, wenv, fids);
816 for (int i = 0; wenv[i]; ++i)
827 Lets the calling process obtain status information about one of its child processes.
828 If status information is available for two or more child processes,
829 the order in which their status is reported is unspecified.
831 EXPORT_C int waitpid (int pid, int* status, int options)
833 return _waitpid_r(&errno, pid, status, options);
838 Calls reentrant version of waitpid().
840 EXPORT_C int wait (int* status)
842 return _wait_r(&errno, status);
851 /* A wide-character version of a system().
853 EXPORT_C int wsystem (const wchar_t* cmd)
857 return 1; // special case, says that we do support system().
860 if(wcslen(cmd) > KMaxPath)
862 errno = ENAMETOOLONG;
866 return _wsystem_r(&errno, cmd);
870 // -----------------------------------------------------------------------------
871 // Select() : Implementation of Select for I/O multiplexing
872 // This API is used for waiting on multiple descriptors to become ready
873 // Maximum timeout to wait can also be specified
874 // Returns: System wide error code
875 // -----------------------------------------------------------------------------
877 EXPORT_C int select(int maxfd, fd_set *readfds, fd_set *writefds,
878 fd_set *exceptfds, struct timeval *tvptr)
880 return _select_r(&errno, maxfd, readfds, writefds, exceptfds, tvptr);
884 // -----------------------------------------------------------------------------
885 // aselect() : Implementation of Select for asynchronous I/O multiplexing
886 // This API is used for waiting on multiple descriptors to become ready
887 // Maximum timeout to wait can also be specified
888 // Returns: System wide error code
889 // This api does not provide C Linkage
890 // -----------------------------------------------------------------------------
892 EXPORT_C int aselect(int maxfd, fd_set *readfds, fd_set *writefds,
893 fd_set *exceptfds, struct timeval *tvptr,
894 TRequestStatus* requeststatus)
896 return _aselect_r(&errno, maxfd, readfds, writefds, exceptfds, tvptr, requeststatus);
899 // -----------------------------------------------------------------------------
900 // cancelaselect() : Implementation of Select for asynchronous I/O multiplexing
901 // This API is used for cancelling the aselect issued on requeststatus
902 // Returns: System wide error code
903 // This api does not provide C Linkage
904 // -----------------------------------------------------------------------------
906 EXPORT_C int cancelaselect(TRequestStatus* requeststatus)
908 return _cancelaselect_r(&errno, requeststatus);
911 // -----------------------------------------------------------------------------
912 // eselect() : Implementation of Select for I/O multiplexing
913 // This API is used for waiting on multiple descriptors to become ready,
914 // or for any of the TRequestStatus object in the TRequestStatus array passed
916 // Maximum timeout to wait can also be specified
917 // Returns: System wide error code
918 // This api does not provide C Linkage
919 // -----------------------------------------------------------------------------
921 EXPORT_C int eselect(int maxfd, fd_set *readfds, fd_set *writefds,
922 fd_set *exceptfds, struct timeval *tvptr, int numreqs,
923 TRequestStatus* waitarray)
925 return _eselect_r(&errno, maxfd, readfds, writefds, exceptfds, tvptr, numreqs, waitarray);
928 // -----------------------------------------------------------------------------
929 // fcntl() : Implementation of fcntl for supporting Non-Blocking I/O
930 // This API is used for setting a file descriptor as non-blocking
931 // Returns: System wide error code
932 // -----------------------------------------------------------------------------
934 EXPORT_C int fcntl (int aFid, int aCmd, ...)
940 ret = _fcntl_r(&errno, aFid,aCmd, va_arg(ap, long));
946 // -----------------------------------------------------------------------------
947 //int setecho(int fd, unsigned int echoval)
949 //Sets the echo flag for this fd.
950 // -----------------------------------------------------------------------------
953 EXPORT_C int setecho(int fd, uint8_t echoval)
955 return _setecho_r(&errno, fd, echoval);