os/ossrv/genericopenlibs/openenvcore/libc/src/Fmscalls.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/libc/src/Fmscalls.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,960 @@
     1.4 +/*
     1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: connectors for re-entrant system calls
    1.18 +*/
    1.19 +
    1.20 +
    1.21 +// connectors for re-entrant system calls
    1.22 +
    1.23 +#include "sysif.h"
    1.24 +#include "lposix.h"
    1.25 +#include <unistd.h>
    1.26 +#include <fcntl.h>		// for open()
    1.27 +#include <sys/ioctl.h>
    1.28 +#include <stdarg.h> 
    1.29 +#include <sys/errno.h>
    1.30 +#include <sys/stat.h>
    1.31 +#include <utf.h>
    1.32 +#include <string.h>
    1.33 +#include <unistd.h>
    1.34 +#include <sys/types.h>
    1.35 +#include "sysreent.h"
    1.36 +#include <sys/aeselect.h>
    1.37 +#include "aeselectreent.h"
    1.38 +#include "fdesc.h"
    1.39 +#include "stdio_r.h"		// for popen3
    1.40 +#include "stdlib_r.h"		// for system
    1.41 +
    1.42 +#if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__)))
    1.43 +#include "libc_wsd_defs.h"
    1.44 +#endif
    1.45 +#define	MAXPATHLEN	260	/* E32STD.H: KMaxFullName + 4 to avoid data loss */
    1.46 +
    1.47 +extern "C" {
    1.48 +
    1.49 +/*
    1.50 +Opens the file which name is stored in the file string.
    1.51 +*/
    1.52 +EXPORT_C int open (const char *file, int flags, ...)
    1.53 +	{
    1.54 +	va_list argList; 
    1.55 +	register int perms;
    1.56 +	
    1.57 +	if(!file) 
    1.58 +		{
    1.59 +    	errno = EFAULT ;
    1.60 +    	return -1 ;	//null file pointer
    1.61 +		}
    1.62 +	
    1.63 +	va_start (argList, flags);
    1.64 +	perms = va_arg(argList,int);
    1.65 +	va_end (argList);
    1.66 +
    1.67 +	//If mode is invalid
    1.68 +	if( perms  > (S_IRWXU | S_IRWXG | S_IRWXO ) )
    1.69 +		{
    1.70 +		//make it read-write for all
    1.71 +		perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    1.72 +		}
    1.73 +	wchar_t _widename[MAXPATHLEN+1];
    1.74 +
    1.75 +	if ((size_t)-1 != mbstowcs(_widename, file, MAXPATHLEN))
    1.76 +		{
    1.77 +		return _open_r (&errno, _widename, flags, perms);  
    1.78 +		}
    1.79 +	else
    1.80 +		{
    1.81 +		errno = EILSEQ;		
    1.82 +		return -1;	// Illegal Sequence of wide characeters
    1.83 +		}
    1.84 +
    1.85 +	}
    1.86 +
    1.87 +/*
    1.88 +A wide_character version of a open().
    1.89 +*/
    1.90 +EXPORT_C int wopen (const wchar_t *file, int flags, ...)
    1.91 +	{
    1.92 +	va_list ap;
    1.93 +	int ret;
    1.94 +	register int perms;
    1.95 +	
    1.96 +	if(!file)
    1.97 +		{
    1.98 +    	errno = EFAULT ;
    1.99 +    	return -1 ;	
   1.100 +		}
   1.101 +		
   1.102 +	va_start (ap, flags);
   1.103 +	perms = va_arg(ap,int);
   1.104 +	va_end (ap);
   1.105 +	
   1.106 +	//If mode is invalid
   1.107 +	if( perms  > (S_IRWXU | S_IRWXG | S_IRWXO ) )
   1.108 +		{
   1.109 +		//make it read-write for all
   1.110 +		perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
   1.111 +		}
   1.112 +
   1.113 +	ret = _wopen_r (&errno, file, flags, perms);
   1.114 +	
   1.115 +	return ret;
   1.116 +	}
   1.117 +
   1.118 +
   1.119 +/*
   1.120 +Reads a block of data of the length specified by cnt.
   1.121 +*/
   1.122 +EXPORT_C int read (int fd, void *buf, size_t cnt)
   1.123 +	{
   1.124 +	if((int)cnt == 0)       
   1.125 +		{
   1.126 +			return 0 ;
   1.127 +		}	
   1.128 +	if((int)cnt < 0) 
   1.129 +		{
   1.130 +	  	errno = EINVAL ;
   1.131 +	  	return -1 ;
   1.132 +		}
   1.133 +	if(!buf)
   1.134 +	  	{
   1.135 +	  	errno = EFAULT ;
   1.136 +	  	return -1 ;
   1.137 +	  	}
   1.138 +	
   1.139 +	//If the fd corresponding to STDIN
   1.140 +	if(fd == STDIN_FILENO && (Backend()->GetDesc(STDIN_FILENO))->Attributes() == KConsoleFd) 
   1.141 +		{
   1.142 +		int len = 0;
   1.143 +		char ch = '\0';
   1.144 +		int ret = -1;
   1.145 +		do
   1.146 +			{
   1.147 +			ret = _read_r(&errno, STDIN_FILENO, &ch, 1);
   1.148 +
   1.149 +			//Copy requested count of data, ignore the rest
   1.150 +			if(ret > 0 && len < cnt )
   1.151 +				{
   1.152 +				((char*)buf)[len] = ch;
   1.153 +				len++;
   1.154 +				}
   1.155 +			else if(ret < 0)
   1.156 +				{
   1.157 +				break;
   1.158 +				}
   1.159 +			
   1.160 +			}while(ch != '\n');
   1.161 +		
   1.162 +		//Handle error case.
   1.163 +		if(ret == -1)
   1.164 +			{
   1.165 +			return ret;
   1.166 +			}
   1.167 +
   1.168 +		return len;
   1.169 +		}
   1.170 +	else
   1.171 +		{
   1.172 +		return _read_r(&errno, fd, (char*)buf, cnt);
   1.173 +		}
   1.174 +	}
   1.175 +
   1.176 +
   1.177 +/*
   1.178 +Writes a block of data of the length specified by cnt.
   1.179 +*/
   1.180 +EXPORT_C int write (int fd, const void *buf, size_t cnt)
   1.181 +	{
   1.182 +		if((int)cnt == 0) 
   1.183 +		{
   1.184 +	   	return 0;
   1.185 +		}
   1.186 +	if((int)cnt < 0) 
   1.187 +		{
   1.188 +	   	errno = EINVAL;
   1.189 +	   	return -1;
   1.190 +		}
   1.191 +	if(!buf) 
   1.192 +	   	{
   1.193 +	   	errno = EFAULT ;
   1.194 +	   	return -1 ;
   1.195 +	   	}
   1.196 +	return _write_r(&errno, fd, (char*)buf, cnt);
   1.197 +	}
   1.198 +
   1.199 +
   1.200 +/*
   1.201 +Close a file.
   1.202 +*/
   1.203 +EXPORT_C int close (int fd)
   1.204 +	{
   1.205 +	return  _close_r(&errno, fd);
   1.206 +	}
   1.207 +
   1.208 +
   1.209 +
   1.210 +/*
   1.211 +Synchronizes a file's in-memory state with that on the physical medium.
   1.212 +*/
   1.213 +EXPORT_C int fsync (int fd)
   1.214 +	{
   1.215 +	return _fsync_r(&errno, fd);
   1.216 +	}
   1.217 +
   1.218 +/*
   1.219 +Repositions the read/write file offset.
   1.220 +*/
   1.221 +EXPORT_C off_t lseek (int fd, off_t pos, int whence)
   1.222 +	{
   1.223 +	return _lseek_r(&errno, fd, pos, whence);
   1.224 +	}
   1.225 +
   1.226 +
   1.227 +/*
   1.228 +Gets information about the named file and writes it to the area that buf points to.
   1.229 +The system must be able to search all directories leading to the file; 
   1.230 +however, read, write, or execute permission of the file is not required.
   1.231 +*/
   1.232 +EXPORT_C int fstat (int fd, struct stat *st)
   1.233 +	{
   1.234 +	if(!st)
   1.235 +	   {
   1.236 +	    errno = EFAULT ;
   1.237 +	    return -1 ;	
   1.238 +	   }
   1.239 +	return _fstat_r(&errno, fd, st);
   1.240 +	}
   1.241 +
   1.242 +
   1.243 +/*
   1.244 +Gets the size of a file. 
   1.245 +*/
   1.246 +EXPORT_C int stat (const char *name, struct stat *st)
   1.247 +	{
   1.248 +	if(!st)
   1.249 +	   {
   1.250 +	    errno = EFAULT ;
   1.251 +	    return -1 ;	
   1.252 +	   }
   1.253 +    
   1.254 +    wchar_t tmpbuf[MAXPATHLEN+1];	
   1.255 +	if ((size_t)-1 != mbstowcs(tmpbuf, name, MAXPATHLEN))
   1.256 +		{
   1.257 +		return _stat_r(&errno, tmpbuf, st);
   1.258 +		}
   1.259 +	errno = EILSEQ;		
   1.260 +	return -1;
   1.261 +	}
   1.262 +
   1.263 +
   1.264 +EXPORT_C int utime (const char *name, const struct utimbuf *filetimes)
   1.265 +	{
   1.266 +	if(!name) 
   1.267 +		{
   1.268 +		errno = EFAULT ;
   1.269 +		return -1 ;
   1.270 +		}
   1.271 +
   1.272 +	wchar_t tmpbuf[MAXPATHLEN+1];	
   1.273 +	if ((size_t)-1 != mbstowcs(tmpbuf, name, MAXPATHLEN))
   1.274 +		{
   1.275 +		return _utime_r (&errno, tmpbuf, filetimes);
   1.276 +		}
   1.277 +	
   1.278 +	errno = EILSEQ;		
   1.279 +	return -1;
   1.280 +	}
   1.281 +
   1.282 +
   1.283 +
   1.284 +/*
   1.285 +A wide_character version of a stat().
   1.286 +*/
   1.287 +EXPORT_C int wstat (const wchar_t *name, struct stat *st)
   1.288 +	{
   1.289 +			if( !name || !st )
   1.290 +			{
   1.291 +					errno = EFAULT;
   1.292 +					return -1;
   1.293 +			}
   1.294 +	return _wstat_r (&errno, name, st);
   1.295 +	}
   1.296 +
   1.297 +
   1.298 +/*
   1.299 +duplicates an open file descriptor.
   1.300 +*/
   1.301 +EXPORT_C int dup (int aFid)
   1.302 +	{
   1.303 +	return _dup_r(&errno, aFid);
   1.304 +	}
   1.305 +
   1.306 +
   1.307 +/*
   1.308 +function duplicates an open file descriptor.
   1.309 +*/
   1.310 +EXPORT_C int dup2 (int aFid1, int aFid2)
   1.311 +	{
   1.312 +	return _dup2_r(&errno, aFid1, aFid2);
   1.313 +	}
   1.314 +
   1.315 +
   1.316 +/*
   1.317 +sorms a variety of device-specific control functions on device special files.
   1.318 +*/
   1.319 +EXPORT_C int ioctl (int aFid, unsigned long aCmd, ...)
   1.320 +	{
   1.321 +	void* aParam ;
   1.322 +	va_list  Vlist ;
   1.323 +	int ret;
   1.324 +	va_start(Vlist , aCmd ) ;
   1.325 +	aParam = va_arg(Vlist , void *)  ;
   1.326 +	if(!aParam) 
   1.327 +		{
   1.328 +		errno = EFAULT ; 
   1.329 +		return -1 ;
   1.330 +		}
   1.331 +
   1.332 +	ret = _ioctl_r(&errno, aFid, aCmd, aParam);
   1.333 +	
   1.334 +	va_end(Vlist) ;
   1.335 +	return ret;
   1.336 +	}
   1.337 +
   1.338 +
   1.339 +
   1.340 +/*
   1.341 +Gets the path name of the current working directory.
   1.342 +If a buffer is specified, the path name is placed in that buffer,
   1.343 +and the address of the buffer is returned.
   1.344 +*/
   1.345 +
   1.346 +/*
   1.347 +A wide_character version of a getcwd().
   1.348 +*/
   1.349 +EXPORT_C wchar_t* wgetcwd (wchar_t *_buf, size_t _size)
   1.350 +	{
   1.351 +	return _wgetcwd_r(&errno, _buf, _size);
   1.352 +	}
   1.353 +
   1.354 +
   1.355 +/*
   1.356 +Changes the current working directory to be pathname. 
   1.357 +The current directory is the beginning point for file 
   1.358 +searches when path names are not absolute. 
   1.359 +If the chdir() function fails, the current working directory remains unchanged.
   1.360 +*/
   1.361 +EXPORT_C int chdir (const char *_path)
   1.362 +	{
   1.363 +	if(!_path) 
   1.364 +		{
   1.365 +		errno = EFAULT;
   1.366 +		return -1;
   1.367 +	  	}
   1.368 +
   1.369 +	//we need to use a wide buffer and convert
   1.370 +	wchar_t tmpbuf[MAXPATHLEN+1];		//use the max path length possible
   1.371 +	if ((size_t)-1 != mbstowcs(tmpbuf, _path, MAXPATHLEN))
   1.372 +		{
   1.373 +		return _chdir_r(&errno, tmpbuf);
   1.374 +		}
   1.375 +	errno = EILSEQ;		
   1.376 +	return -1;
   1.377 +	}
   1.378 +
   1.379 +
   1.380 +/* A wide-character version of chdir().
   1.381 +*/
   1.382 +EXPORT_C int wchdir (const wchar_t *_path)
   1.383 +	{
   1.384 +
   1.385 +	if(!_path) 
   1.386 +		{
   1.387 +		errno = EFAULT;
   1.388 +		return -1;
   1.389 +	  	}
   1.390 +
   1.391 +	return _wchdir_r(&errno, _path);
   1.392 +	}
   1.393 +
   1.394 +
   1.395 +/*
   1.396 +Removes an empty directory whose name is given by pathname.
   1.397 +The directory must not have any entries other than dot (.) and dot-dot (..).
   1.398 +*/
   1.399 +EXPORT_C int rmdir (const char *_path)
   1.400 +	{
   1.401 +	if(!_path)
   1.402 +		{
   1.403 +		errno = EFAULT ;
   1.404 +		return -1 ;
   1.405 +		}
   1.406 +
   1.407 +	if(!strcmp((_path + strlen(_path) -1 ) , ".") ) 
   1.408 +		{
   1.409 +		errno = EINVAL ;
   1.410 +		return -1 ;  
   1.411 +		}
   1.412 +
   1.413 +	wchar_t tmpbuf[MAXPATHLEN+1];		//use the max path length possible
   1.414 +	if ((size_t)-1 != mbstowcs(tmpbuf, _path, MAXPATHLEN))
   1.415 +		{
   1.416 +		return _rmdir_r(&errno, tmpbuf);
   1.417 +		}
   1.418 +	errno = EILSEQ;		
   1.419 +	return -1;
   1.420 +	}
   1.421 +
   1.422 +
   1.423 +
   1.424 +/* A wide-character version of rmdir().
   1.425 +*/
   1.426 +EXPORT_C int wrmdir (const wchar_t *_path)
   1.427 +	{
   1.428 +			if( !_path )
   1.429 +			{
   1.430 +				 errno = EFAULT;
   1.431 +				 return -1;
   1.432 +			}
   1.433 +	return _wrmdir_r(&errno, _path);
   1.434 +	}
   1.435 +
   1.436 +
   1.437 +/*
   1.438 +Creates a new directory with the specified path name. 
   1.439 +The file permissions of the new directory are initialized from the specified mode. 
   1.440 +*/
   1.441 +EXPORT_C int mkdir (const char *_path, mode_t _mode)
   1.442 +	{
   1.443 +	if(!_path)
   1.444 +		{
   1.445 +		errno = EFAULT ;
   1.446 +		return -1 ;
   1.447 +		}
   1.448 +
   1.449 +	//we need to use a wide buffer and convert
   1.450 +	wchar_t tmpbuf[MAXPATHLEN+1];		//use the max path length possible
   1.451 +	if ((size_t)-1 != mbstowcs(tmpbuf, _path, MAXPATHLEN))
   1.452 +		{
   1.453 +		return _mkdir_r(&errno, tmpbuf, _mode);
   1.454 +		}
   1.455 +	errno = EILSEQ;		
   1.456 +	return -1;
   1.457 +	}
   1.458 +
   1.459 +
   1.460 +
   1.461 +/* A wide-character version of mkdir().
   1.462 +*/
   1.463 +EXPORT_C int wmkdir (const wchar_t *_path, mode_t _mode)
   1.464 +	{
   1.465 +
   1.466 +	if(!_path)
   1.467 +		{
   1.468 +		errno = EFAULT ;
   1.469 +		return -1 ;
   1.470 +		}
   1.471 +	
   1.472 +	return _wmkdir_r(&errno, _path, _mode);
   1.473 +	}
   1.474 +
   1.475 +
   1.476 +/*
   1.477 +Sets the access permissions for the file 
   1.478 +whose name is given by pathname to the bit pattern contained in mode. 
   1.479 +For this call to succeed, the effective user ID of the process must match 
   1.480 +the owner of the file, or the process must have appropriate privileges. 
   1.481 +The owner of the file pathname always has privileges to change permission modes 
   1.482 +and file attributes.
   1.483 +*/
   1.484 +EXPORT_C int chmod (const char *_path, mode_t _mode)
   1.485 +	{
   1.486 +	if(!_path)
   1.487 +		{
   1.488 +		errno = EFAULT ;
   1.489 +		return -1 ;
   1.490 +		}
   1.491 +
   1.492 +	wchar_t tmpbuf[MAXPATHLEN+1];	
   1.493 +	if ((size_t)-1 != mbstowcs(tmpbuf, _path, MAXPATHLEN))
   1.494 +		{
   1.495 +		return _chmod_r(&errno, tmpbuf, _mode);
   1.496 +		}
   1.497 +	errno = EILSEQ;		
   1.498 +	return -1;
   1.499 +	}
   1.500 +/*
   1.501 +Sets the access permissions for the file specifed by file descriptor
   1.502 +whose name is given by pathname to the bit pattern contained in mode. 
   1.503 +For this call to succeed, the effective user ID of the process must match 
   1.504 +the owner of the file, or the process must have appropriate privileges. 
   1.505 +The owner of the file pathname always has privileges to change permission modes 
   1.506 +and file attributes.
   1.507 +*/
   1.508 +EXPORT_C int fchmod (int fd , mode_t _mode)
   1.509 +	{
   1.510 +	
   1.511 +		return _fchmod_r(&errno, fd, _mode);
   1.512 +	}
   1.513 +
   1.514 +
   1.515 +
   1.516 +/* A wide-character version of chmod().
   1.517 +*/
   1.518 +EXPORT_C int wchmod (const wchar_t *_path, mode_t _mode)
   1.519 +	{
   1.520 +	
   1.521 +	if(!_path)
   1.522 +		{
   1.523 +		errno = EFAULT ;
   1.524 +		return -1 ;
   1.525 +		}
   1.526 +	
   1.527 +	return _wchmod_r(&errno, _path, _mode);
   1.528 +	}
   1.529 +
   1.530 +
   1.531 +
   1.532 +/* A wide-character version of unlink().
   1.533 +*/
   1.534 +EXPORT_C int wunlink (const wchar_t *_path)
   1.535 +	{
   1.536 +			if( !_path )
   1.537 +			{
   1.538 +					errno = EFAULT;
   1.539 +					return -1;
   1.540 +			}
   1.541 +	return _wunlink_r(&errno, _path);
   1.542 +	}
   1.543 +
   1.544 +
   1.545 +/*
   1.546 +Renames a file.
   1.547 +*/
   1.548 +EXPORT_C int rename (const char *oldpath, const char *newpath)
   1.549 +	{
   1.550 +	if((!oldpath) ||(!newpath))
   1.551 +		{
   1.552 +		errno = EFAULT ;
   1.553 +		return -1 ;
   1.554 +		}
   1.555 +
   1.556 +	wchar_t _old[MAXPATHLEN+1];		
   1.557 +	wchar_t _new[MAXPATHLEN+1];		
   1.558 +	if ((size_t)-1 != mbstowcs(_old, oldpath, MAXPATHLEN))
   1.559 +		{
   1.560 +		if ((size_t)-1 != mbstowcs(_new, newpath, MAXPATHLEN))
   1.561 +			{
   1.562 +			return _rename_r(&errno, _old, _new);
   1.563 +			}
   1.564 +		}
   1.565 +	errno = EILSEQ;		
   1.566 +	return -1;
   1.567 +	}
   1.568 +
   1.569 +
   1.570 +/* A wide-character version of rename().
   1.571 +*/
   1.572 +EXPORT_C int wrename (const wchar_t *oldpath, const wchar_t *newpath)
   1.573 +	{
   1.574 +	
   1.575 +	if((!oldpath) ||(!newpath))
   1.576 +		{
   1.577 +		errno = EFAULT ;
   1.578 +		return -1 ;
   1.579 +		}
   1.580 +	
   1.581 +	return _wrename_r(&errno, oldpath, newpath);
   1.582 +	}
   1.583 +
   1.584 +
   1.585 +/*
   1.586 +Takes a specified path name, pathname and resolves all symbolic links,
   1.587 +extra slashes (/), and references to /./ and /../. 
   1.588 +The resulting absolute path name is placed in the memory location 
   1.589 +pointed to by the resolved_path argument.
   1.590 +*/
   1.591 +
   1.592 +/* A wide-character version of realpath().
   1.593 +*/
   1.594 +EXPORT_C wchar_t* wrealpath (const wchar_t* path, wchar_t* resolved)
   1.595 +	{
   1.596 +	return _wrealpath_r(&errno, path, resolved);
   1.597 +	}
   1.598 +
   1.599 +
   1.600 +/*
   1.601 +Gives access to the client's stdin
   1.602 +*/
   1.603 +EXPORT_C FILE* popen (const char* command, const char* mode)
   1.604 +	{
   1.605 +	// Check for the validity of command.
   1.606 +	// On Linux, the shell would return cannot find command to execute
   1.607 +	if (command == NULL)
   1.608 +		{
   1.609 +		errno = ENOENT;
   1.610 +		return NULL;
   1.611 +		}
   1.612 +		
   1.613 +	if(strlen(command) > KMaxPath)
   1.614 +		{
   1.615 +		errno = ENAMETOOLONG;
   1.616 +		return NULL;
   1.617 +		}
   1.618 +	// Check for the validity of Mode.
   1.619 +	if( (!mode) || mode[0] != 'r' && mode[0] != 'w' )
   1.620 +		{
   1.621 +		// Invalid mode
   1.622 +		errno = EINVAL;
   1.623 +		return NULL;
   1.624 +		}
   1.625 +		
   1.626 +	wchar_t wcmd[KMaxPath+1];
   1.627 +		
   1.628 +	// Widen command
   1.629 +	if ((size_t)-1 != mbstowcs(wcmd, command, KMaxPath))
   1.630 +		{
   1.631 +		int fd = _wpopen_r(&errno, wcmd, mode);
   1.632 +		if (fd > 0)
   1.633 +			{
   1.634 +			// return value is a valid fd
   1.635 +			return fdopen(fd, mode);
   1.636 +			}
   1.637 +		}
   1.638 +	else
   1.639 +		{
   1.640 +		errno = EILSEQ;
   1.641 +		}
   1.642 +
   1.643 +	return NULL;
   1.644 +	}
   1.645 +	
   1.646 +	
   1.647 +EXPORT_C FILE* wpopen (const wchar_t* command, const wchar_t* wmode)
   1.648 +	{
   1.649 +	char mode[2];
   1.650 +	size_t sz;
   1.651 +
   1.652 +	mode[0] = '\0';
   1.653 +	sz = wcstombs(mode, wmode, 2);
   1.654 +	//Check for the validity of Mode.
   1.655 +	if (sz <= 0 || (mode[0] != 'r' && mode[0] != 'w'))
   1.656 +		{
   1.657 +		//If its neither "r" nor "w", its undefined behavior
   1.658 +		errno = EINVAL;
   1.659 +		return NULL;
   1.660 +		}
   1.661 +
   1.662 +	if(command)
   1.663 +		{
   1.664 +		if(wcslen(command) > KMaxPath)
   1.665 +			{
   1.666 +			errno = ENAMETOOLONG;
   1.667 +			return NULL;
   1.668 +			}
   1.669 +			
   1.670 +		int fd = _wpopen_r(&errno, command, mode );
   1.671 +		//If return Value is valid fd
   1.672 +		if (fd > 0)
   1.673 +			{
   1.674 +			return fdopen(fd, mode);
   1.675 +			}
   1.676 +		}
   1.677 +	else
   1.678 +		{
   1.679 +		errno = ENOENT;
   1.680 +		}
   1.681 +	return NULL;
   1.682 +	}
   1.683 +
   1.684 +
   1.685 +EXPORT_C int pclose(FILE* stream)
   1.686 +	{
   1.687 +	TInt fd;
   1.688 +	if (!stream || ((fd = fileno(stream)) == -1))
   1.689 +		{
   1.690 +		errno = EINVAL;
   1.691 +		return -1;
   1.692 +		}
   1.693 +		
   1.694 +	TInt err = _pclose_r(&errno, fd);
   1.695 +	
   1.696 +	if (err != 0)
   1.697 +		{
   1.698 +		if (errno == ECHILD)
   1.699 +			{
   1.700 +			fclose(stream);
   1.701 +			// reset errno just in case it has been modified by fclose
   1.702 +			errno = ECHILD;
   1.703 +			}
   1.704 +		
   1.705 +		return -1;
   1.706 +		}
   1.707 +		
   1.708 +	return fclose(stream);
   1.709 +	}
   1.710 +	
   1.711 +/* A wide-character version of popen3().
   1.712 +*/
   1.713 +EXPORT_C int wpopen3 (const wchar_t* file, const wchar_t* cmd, wchar_t** env, int fids[3])
   1.714 +	{
   1.715 +	if (file == NULL)
   1.716 +		{
   1.717 +		errno = ENOENT;
   1.718 +		return -1;
   1.719 +		}
   1.720 +		
   1.721 +	if(wcslen(file) > KMaxPath)
   1.722 +		{
   1.723 +		errno = ENAMETOOLONG;
   1.724 +		return -1;	
   1.725 +		}
   1.726 +		
   1.727 +	return _wpopen3_r(&errno, file, cmd, env, fids);
   1.728 +	}
   1.729 +
   1.730 +
   1.731 +EXPORT_C int popen3 (const char* file, const char* cmd, char** env, int fids[3])
   1.732 +	{
   1.733 +	if (file == NULL)
   1.734 +		{
   1.735 +		errno = ENOENT;
   1.736 +		return -1;
   1.737 +		}
   1.738 +		
   1.739 +	if(strlen(file) > KMaxPath)
   1.740 +		{
   1.741 +		errno = ENAMETOOLONG;
   1.742 +		return -1;	
   1.743 +		}
   1.744 +		
   1.745 +	wchar_t wfile[KMaxPath+1];
   1.746 +	wchar_t wcmd[KMaxPath+1];
   1.747 +	
   1.748 +	wchar_t** wenv = NULL;
   1.749 +	
   1.750 +	TInt ret = mbstowcs(wfile, file, KMaxPath);
   1.751 +	TInt cmdlen = mbstowcs(wcmd, cmd, KMaxPath);
   1.752 +	
   1.753 +	if (ret != (size_t)-1 && cmdlen != (size_t)-1)
   1.754 +		{
   1.755 +		//OK, we've widened the first 2 args
   1.756 +		//now for the environment
   1.757 +
   1.758 +		//env will be an array of char pointers with a NULL as the last one
   1.759 +		if (env)
   1.760 +			{
   1.761 +			TInt count = 0;
   1.762 +			
   1.763 +			for (; env[count]; ++count) { }
   1.764 +
   1.765 +			//coverity[alloc_fn]
   1.766 +			//coverity[assign]
   1.767 +			
   1.768 +			wenv = (wchar_t **)malloc((count+1) * sizeof(wchar_t*));
   1.769 +			if (!wenv)
   1.770 +				{
   1.771 +				errno = ENOMEM;
   1.772 +				return -1;
   1.773 +				}
   1.774 +			
   1.775 +			for (int i = 0; i < count; ++i)
   1.776 +				{
   1.777 +				int len = strlen(env[i]) + 1;
   1.778 +				wenv[i] = (wchar_t *)malloc(len * sizeof(wchar_t));
   1.779 +				if (wenv[i] == NULL)
   1.780 +					{
   1.781 +					//coverity[leave_without_push]
   1.782 +
   1.783 +					errno = ENOMEM;
   1.784 +					goto bailout;
   1.785 +					}
   1.786 +				if (mbstowcs(wenv[i], env[i], len) == (size_t)-1)
   1.787 +					{
   1.788 +					//coverity[leave_without_push]
   1.789 +					errno = EILSEQ;
   1.790 +					wenv[i+1] = NULL;
   1.791 +					goto bailout;
   1.792 +					}
   1.793 +				}
   1.794 +				
   1.795 +			wenv[count] = 0;
   1.796 +			}
   1.797 +
   1.798 +		if (cmdlen)
   1.799 +			{
   1.800 +			//coverity[leave_without_push]
   1.801 +			ret =  wpopen3(wfile, wcmd, wenv, fids);
   1.802 +			}
   1.803 +		else
   1.804 +			{
   1.805 +			//coverity[leave_without_push]
   1.806 +			ret = wpopen3(wfile, NULL, wenv, fids);
   1.807 +			}
   1.808 +			
   1.809 +		}
   1.810 +	else
   1.811 +		{
   1.812 +		errno = EILSEQ;
   1.813 +		return -1;
   1.814 +		}
   1.815 +
   1.816 +bailout:
   1.817 +	if (wenv)
   1.818 +		{
   1.819 +		for (int i = 0; wenv[i]; ++i)
   1.820 +			{
   1.821 +			free(wenv[i]);
   1.822 +			}
   1.823 +		free(wenv);
   1.824 +		}
   1.825 +
   1.826 +	return ret;
   1.827 +	}
   1.828 +
   1.829 +/*
   1.830 +Lets the calling process obtain status information about one of its child processes.
   1.831 +If status information is available for two or more child processes,
   1.832 +the order in which their status is reported is unspecified.
   1.833 +*/
   1.834 +EXPORT_C int waitpid (int pid, int* status, int options)
   1.835 +	{
   1.836 +	return _waitpid_r(&errno, pid, status, options);
   1.837 +	}
   1.838 +
   1.839 +
   1.840 +/*
   1.841 +Calls reentrant version of waitpid().
   1.842 +*/
   1.843 +EXPORT_C int wait (int* status)
   1.844 +	{
   1.845 +	return _wait_r(&errno, status);
   1.846 +	}
   1.847 +
   1.848 +
   1.849 +
   1.850 +/*
   1.851 +Execute command.
   1.852 +*/
   1.853 +
   1.854 +/* A wide-character version of a system().
   1.855 +*/
   1.856 +EXPORT_C int wsystem (const wchar_t* cmd)
   1.857 +	{
   1.858 +	if (cmd==0)
   1.859 +		{
   1.860 +		return 1;	// special case, says that we do support system().	
   1.861 +		}
   1.862 +
   1.863 +	if(wcslen(cmd) > KMaxPath)
   1.864 +		{
   1.865 +		errno = ENAMETOOLONG;
   1.866 +		return -1;	
   1.867 +		}
   1.868 +		
   1.869 +	return _wsystem_r(&errno, cmd);
   1.870 +	}
   1.871 +
   1.872 +
   1.873 +// -----------------------------------------------------------------------------
   1.874 +// Select() : Implementation of Select for I/O multiplexing
   1.875 +// This API is used for waiting on multiple descriptors to become ready
   1.876 +// Maximum timeout to wait can also be specified
   1.877 +// Returns: System wide error code
   1.878 +// -----------------------------------------------------------------------------
   1.879 +//
   1.880 +EXPORT_C int select(int maxfd, fd_set *readfds, fd_set *writefds,
   1.881 +					fd_set *exceptfds, struct timeval *tvptr)
   1.882 +	{	
   1.883 +	return _select_r(&errno, maxfd, readfds, writefds, exceptfds, tvptr);
   1.884 +	}
   1.885 +} // extern "C"
   1.886 +
   1.887 +// -----------------------------------------------------------------------------
   1.888 +// aselect() : Implementation of Select for asynchronous I/O multiplexing
   1.889 +// This API is used for waiting on multiple descriptors to become ready
   1.890 +// Maximum timeout to wait can also be specified
   1.891 +// Returns: System wide error code
   1.892 +// This api does not provide C Linkage
   1.893 +// -----------------------------------------------------------------------------
   1.894 +//
   1.895 +EXPORT_C int aselect(int maxfd, fd_set *readfds, fd_set *writefds,
   1.896 +					fd_set *exceptfds, struct timeval *tvptr,
   1.897 +					TRequestStatus* requeststatus)
   1.898 +	{	
   1.899 +	return _aselect_r(&errno, maxfd, readfds, writefds, exceptfds, tvptr, requeststatus);
   1.900 +	}
   1.901 +
   1.902 +// -----------------------------------------------------------------------------
   1.903 +// cancelaselect() : Implementation of Select for asynchronous I/O multiplexing
   1.904 +// This API is used for cancelling the aselect issued on requeststatus
   1.905 +// Returns: System wide error code
   1.906 +// This api does not provide C Linkage
   1.907 +// -----------------------------------------------------------------------------
   1.908 +//
   1.909 +EXPORT_C int cancelaselect(TRequestStatus* requeststatus)
   1.910 +	{	
   1.911 +	return _cancelaselect_r(&errno, requeststatus);
   1.912 +	}
   1.913 +
   1.914 +// -----------------------------------------------------------------------------
   1.915 +// eselect() : Implementation of Select for I/O multiplexing
   1.916 +// This API is used for waiting on multiple descriptors to become ready,
   1.917 +// or for any of the TRequestStatus object in the TRequestStatus array passed
   1.918 +// to be signalled
   1.919 +// Maximum timeout to wait can also be specified
   1.920 +// Returns: System wide error code
   1.921 +// This api does not provide C Linkage
   1.922 +// -----------------------------------------------------------------------------
   1.923 +//
   1.924 +EXPORT_C int eselect(int maxfd, fd_set *readfds, fd_set *writefds,
   1.925 +					fd_set *exceptfds, struct timeval *tvptr, int numreqs,
   1.926 +					TRequestStatus* waitarray)
   1.927 +	{	
   1.928 +	return _eselect_r(&errno, maxfd, readfds, writefds, exceptfds, tvptr, numreqs, waitarray);
   1.929 +	}
   1.930 +
   1.931 +// -----------------------------------------------------------------------------
   1.932 +// fcntl() : Implementation of fcntl for supporting Non-Blocking I/O 
   1.933 +// This API is used for setting a file descriptor as non-blocking
   1.934 +// Returns: System wide error code
   1.935 +// -----------------------------------------------------------------------------
   1.936 +//
   1.937 +EXPORT_C int fcntl (int aFid, int aCmd, ...)
   1.938 +	{
   1.939 +	va_list ap;
   1.940 +	va_start(ap, aCmd);
   1.941 +
   1.942 +	int ret;
   1.943 +	ret = _fcntl_r(&errno, aFid,aCmd, va_arg(ap, long)); 
   1.944 +	
   1.945 + 	va_end(ap);
   1.946 +	return ret ;
   1.947 +	}
   1.948 +
   1.949 +// -----------------------------------------------------------------------------
   1.950 +//int setecho(int fd, unsigned int echoval)
   1.951 +//
   1.952 +//Sets the echo flag for this fd.
   1.953 +// -----------------------------------------------------------------------------
   1.954 +
   1.955 +extern "C" {
   1.956 +EXPORT_C int setecho(int fd, uint8_t echoval)
   1.957 +	{
   1.958 +	return _setecho_r(&errno, fd, echoval);
   1.959 +	}
   1.960 +} //extern "C"
   1.961 +
   1.962 +
   1.963 +