1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LPOSIX/SYSCALLS.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,1157 @@
1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// connectors for re-entrant system calls
1.18 +//
1.19 +//
1.20 +
1.21 +#include "SYSIF.H"
1.22 +#include "LPOSIX.H"
1.23 +
1.24 +#include <reent.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 <stdio_r.h> // for popen3
1.31 +#include <stdlib_r.h> // for system
1.32 +#include <utf.h>
1.33 +#include <string.h>
1.34 +
1.35 +
1.36 +extern "C" {
1.37 +
1.38 +/**
1.39 +Opens the file which name is stored in the file string.
1.40 +
1.41 +@return On Success, a non-negative integer representing the lowest numbered unused file descriptor.
1.42 + On Failure, returns -1, errno may be set.
1.43 +*/
1.44 +EXPORT_C int open (const char *file, int flags, ...)
1.45 + {
1.46 + va_list ap;
1.47 + int ret;
1.48 +
1.49 + struct _reent *r = _REENT2;
1.50 + if (!r)
1.51 + return -1; // Memory for library globals is not allocated (errno not set).
1.52 +
1.53 + va_start (ap, flags);
1.54 + ret = _open_r (r, file, flags, va_arg (ap, int));
1.55 + va_end (ap);
1.56 + return ret;
1.57 + }
1.58 +
1.59 +/**
1.60 +A wide_character version of a open().
1.61 +*/
1.62 +EXPORT_C int wopen (const wchar_t *file, int flags, ...)
1.63 + {
1.64 + va_list ap;
1.65 + int ret;
1.66 +
1.67 + struct _reent *r = _REENT2;
1.68 + if (!r)
1.69 + return -1; // Memory for library globals is not allocated (errno not set).
1.70 +
1.71 + va_start (ap, flags);
1.72 + ret = _wopen_r (r, file, flags, va_arg (ap, int));
1.73 + va_end (ap);
1.74 + return ret;
1.75 + }
1.76 +
1.77 +/** A reentrant version of open().
1.78 +*/
1.79 +EXPORT_C int _open_r (struct _reent *r, const char *name, int mode, int perms)
1.80 + {
1.81 + wchar_t _widename[KMaxFileName+1];
1.82 +
1.83 + if (-1 != mbstowcs(_widename, name, KMaxFileName))
1.84 + {
1.85 + MSystemInterface& sysIf=Interface(r);
1.86 + return sysIf.open(_widename,mode,perms,r->_errno);
1.87 + }
1.88 +
1.89 + MapError(EILSEQ, r->_errno);
1.90 + return 0; //null file pointer
1.91 +
1.92 + }
1.93 +
1.94 +/** A reentrant version of wopen().
1.95 +*/
1.96 +EXPORT_C int _wopen_r (struct _reent *r, const wchar_t *name, int mode, int perms)
1.97 + {
1.98 + MSystemInterface& sysIf=Interface(r);
1.99 + return sysIf.open(name,mode,perms,r->_errno);
1.100 + }
1.101 +
1.102 +/**
1.103 +Reads a block of data of the length specified by cnt.
1.104 +
1.105 +@return On Success, return a non-negative integer indicating the number of bytes actually read.
1.106 + On Failure, returns -1, errno may be set.
1.107 +*/
1.108 +EXPORT_C int read (int fd, char *buf, size_t cnt)
1.109 + {
1.110 + struct _reent *r = _REENT2;
1.111 + if (!r)
1.112 + return -1; // Memory for library globals is not allocated (errno not set).
1.113 + return _read_r (r, fd, buf, cnt);
1.114 + }
1.115 +
1.116 +/** A reentrant version of read().
1.117 +*/
1.118 +EXPORT_C int _read_r (struct _reent *r, int fd, char *buf, size_t nbyte)
1.119 + {
1.120 + MSystemInterface& sysIf=Interface(r);
1.121 + return sysIf.read(fd,buf,nbyte,r->_errno);
1.122 + }
1.123 +
1.124 +/**
1.125 +Writes a block of data of the length specified by cnt.
1.126 +
1.127 +@return On Success, returns the number of bytes written to the file. The number
1.128 + shall never be greater than cnt.
1.129 + On Failure, returns -1, errno may be set.
1.130 +*/
1.131 +EXPORT_C int write (int fd, const char *buf, size_t cnt)
1.132 + {
1.133 + struct _reent *r = _REENT2;
1.134 + if (!r)
1.135 + return -1; // Memory for library globals is not allocated (errno not set).
1.136 + return _write_r (r, fd, buf, cnt);
1.137 + }
1.138 +
1.139 +/** A reentrant version of write().
1.140 +*/
1.141 +EXPORT_C int _write_r (struct _reent *r, int fd, const char *buf, size_t nbyte)
1.142 + {
1.143 + MSystemInterface& sysIf=Interface(r);
1.144 + return sysIf.write(fd,buf,nbyte,r->_errno);
1.145 + }
1.146 +
1.147 +/**
1.148 +Close a file.
1.149 +
1.150 +@return On Success, returns 0.
1.151 + On Failure, returns -1, errno may be set.
1.152 +*/
1.153 +EXPORT_C int close (int fd)
1.154 + {
1.155 + struct _reent *r = _REENT2;
1.156 + if (!r)
1.157 + return -1; // Memory for library globals is not allocated (errno not set).
1.158 + return _close_r (r, fd);
1.159 + }
1.160 +
1.161 +
1.162 +/** A reentrant version of close().
1.163 +*/
1.164 +EXPORT_C int _close_r (struct _reent *r, int fd)
1.165 + {
1.166 + MSystemInterface& sysIf=Interface(r);
1.167 + return sysIf.close(fd,r->_errno);
1.168 + }
1.169 +
1.170 +/**
1.171 +Synchronizes a file's in-memory state with that on the physical medium.
1.172 +
1.173 +@param fd Is the file descriptor for the file to be synchronized.
1.174 +
1.175 +@return On Success, returns 0.
1.176 + On Failure, returns -1, errno may be set.
1.177 +*/
1.178 +EXPORT_C int fsync (int fd)
1.179 + {
1.180 + struct _reent *r = _REENT2;
1.181 + if (!r)
1.182 + return -1; // Memory for library globals is not allocated (errno not set).
1.183 + return _fsync_r (r, fd);
1.184 + }
1.185 +
1.186 +/** A reentrant version of fsync().
1.187 +*/
1.188 +EXPORT_C int _fsync_r (struct _reent *r, int fd)
1.189 + {
1.190 + MSystemInterface& sysIf=Interface(r);
1.191 + return sysIf.fsync(fd,r->_errno);
1.192 + }
1.193 +
1.194 +/**
1.195 +Repositions the read/write file offset.
1.196 +@return a nonnegative integer that indicates the file pointer value.
1.197 +@param fd Is the file descriptor of an open file.
1.198 +@param pos Specifies the number of bytes to offset the file pointer
1.199 +from a specified file origin.
1.200 +@param whence Specifies the location from which to start seeking.
1.201 +*/
1.202 +EXPORT_C off_t lseek (int fd, off_t pos, int whence)
1.203 + {
1.204 + return _lseek_r (_REENT, fd, pos, whence);
1.205 + }
1.206 +
1.207 +/** A reentrant version of fseek().
1.208 +*/
1.209 +EXPORT_C off_t _lseek_r (struct _reent *r, int fd, off_t pos, int whence)
1.210 + {
1.211 + MSystemInterface& sysIf=Interface(r);
1.212 + return sysIf.lseek(fd,pos,whence,r->_errno);
1.213 + }
1.214 +
1.215 +/**
1.216 +Gets information about the named file and writes it to the area that buf points to.
1.217 +The system must be able to search all directories leading to the file;
1.218 +however, read, write, or execute permission of the file is not required.
1.219 +
1.220 +@param fd Is a file descriptor referring to a file for which status is returned.
1.221 +@param st Points to a stat structure where status information about the file is to be placed.
1.222 +
1.223 +@return On Success, returns 0.
1.224 + On Failure, returns -1, errno may be set.
1.225 +*/
1.226 +EXPORT_C int fstat (int fd, struct stat *st)
1.227 + {
1.228 + struct _reent *r = _REENT2;
1.229 + if (!r)
1.230 + return -1; // Memory for library globals is not allocated (errno not set).
1.231 + return _fstat_r (r, fd, st);
1.232 + }
1.233 +
1.234 +/** A reentrant version of fstat().
1.235 +*/
1.236 +EXPORT_C int _fstat_r (struct _reent *r, int fd, struct stat *st)
1.237 + {
1.238 + MSystemInterface& sysIf=Interface(r);
1.239 + return sysIf.fstat(fd,st,r->_errno);
1.240 + }
1.241 +
1.242 +/**
1.243 +Gets the size of a file.
1.244 +
1.245 +@return On Success, returns 0.
1.246 + On Failure, returns -1, errno may be set.
1.247 +*/
1.248 +EXPORT_C int stat (const char *name, struct stat *st)
1.249 + {
1.250 + struct _reent *r = _REENT2;
1.251 + if (!r)
1.252 + return -1; // Memory for library globals is not allocated (errno not set).
1.253 + return _stat_r (r, name, st);
1.254 + }
1.255 +
1.256 +/** A reentrant version of stat().
1.257 +*/
1.258 +EXPORT_C int _stat_r (struct _reent *r, const char *name, struct stat *st)
1.259 + {
1.260 + wchar_t tmpbuf[KMaxFullName+1];
1.261 + if (-1 != mbstowcs(tmpbuf, name, KMaxFullName))
1.262 + {
1.263 + MSystemInterface& sysIf=Interface(r);
1.264 + return sysIf.stat(tmpbuf, st, r->_errno);
1.265 + }
1.266 + MapError(EILSEQ, r->_errno);
1.267 + return -1;
1.268 + }
1.269 +
1.270 +/**
1.271 +A wide_character version of a stat().
1.272 +*/
1.273 +EXPORT_C int wstat (const wchar_t *name, struct stat *st)
1.274 + {
1.275 + struct _reent *r = _REENT2;
1.276 + if (!r)
1.277 + return -1; // Memory for library globals is not allocated (errno not set).
1.278 + return _wstat_r (r, name, st);
1.279 + }
1.280 +
1.281 +/** A reentrant version of wstat().
1.282 +*/
1.283 +EXPORT_C int _wstat_r (struct _reent *r, const wchar_t *name, struct stat *st)
1.284 + {
1.285 + MSystemInterface& sysIf=Interface(r);
1.286 + return sysIf.stat(name,st,r->_errno);
1.287 + }
1.288 +
1.289 +/**
1.290 +Duplicates an open file descriptor.
1.291 +
1.292 +@param aFid Is the file descriptor to duplicate.
1.293 +
1.294 +@return On Success, returns a non-negative integer, namely the duplicated file descriptor, which
1.295 + is the lowest available descriptor.
1.296 + On Failure, returns -1, errno may be set.
1.297 +*/
1.298 +EXPORT_C int dup (int aFid)
1.299 + {
1.300 + struct _reent *r = _REENT2;
1.301 + if (!r)
1.302 + return -1; // Memory for library globals is not allocated (errno not set).
1.303 + return _dup_r(r, aFid);
1.304 + }
1.305 +
1.306 +/** A reentrant version of dup().
1.307 +*/
1.308 +EXPORT_C int _dup_r (struct _reent *r, int aFid)
1.309 + {
1.310 + MSystemInterface& sysIf=Interface(r);
1.311 + return sysIf.dup(aFid,r->_errno);
1.312 + }
1.313 +
1.314 +/**
1.315 +Function duplicates an open file descriptor.
1.316 +
1.317 +@param aFid1 Is the file descriptor to duplicate.
1.318 +@param aFid2 Is the file descriptor that filedes is duplicated onto.
1.319 +
1.320 +@return On Success, returns a non-negative integer, namely the duplicated file descriptor, which
1.321 + is the lowest available descriptor.
1.322 + On Failure, returns -1, errno may be set.
1.323 +*/
1.324 +EXPORT_C int dup2 (int aFid1, int aFid2)
1.325 + {
1.326 + struct _reent *r = _REENT2;
1.327 + if (!r)
1.328 + return -1; // Memory for library globals is not allocated (errno not set).
1.329 + return _dup2_r(r, aFid1, aFid2);
1.330 + }
1.331 +
1.332 +/** A reentrant version of dup2().
1.333 +*/
1.334 +EXPORT_C int _dup2_r (struct _reent *r, int aFid1, int aFid2)
1.335 + {
1.336 + MSystemInterface& sysIf=Interface(r);
1.337 + return sysIf.dup2(aFid1,aFid2,r->_errno);
1.338 + }
1.339 +
1.340 +/**
1.341 +Performs a variety of device-specific control functions on device special files.
1.342 +
1.343 +@return On Success, returns a value other than -1 that depends upon the STREAMS device control function.
1.344 + On Failure, return -1, errno may be set.
1.345 +*/
1.346 +EXPORT_C int ioctl (int aFid, int aCmd, void* aParam)
1.347 + {
1.348 + struct _reent *r = _REENT2;
1.349 + if (!r)
1.350 + return -1; // Memory for library globals is not allocated (errno not set).
1.351 + return _ioctl_r(r, aFid, aCmd, aParam);
1.352 + }
1.353 +
1.354 +/** A reentrant version of ioctl().
1.355 +*/
1.356 +EXPORT_C int _ioctl_r (struct _reent *r, int aFid, int aCmd, void* aParam)
1.357 + {
1.358 + MSystemInterface& sysIf=Interface(r);
1.359 + return sysIf.ioctl(aFid,aCmd,aParam,r->_errno);
1.360 + }
1.361 +
1.362 +/**
1.363 +Gets the path name of the current working directory.
1.364 +If a buffer is specified, the path name is placed in that buffer,
1.365 +and the address of the buffer is returned.
1.366 +@return If successful returns buf, if a non-null pointer was specified,
1.367 +or the address of the allocated memory otherwise.
1.368 +@param _buf Points to the buffer to copy the current working directory to,
1.369 +or NULL if getcwd() should allocate the buffer.
1.370 +@param _size Is the size, in bytes, of the array of characters that buf points to.
1.371 +*/
1.372 +EXPORT_C char* getcwd (char *_buf, size_t _size)
1.373 + {
1.374 + return _getcwd_r(_REENT,_buf,_size);
1.375 + }
1.376 +
1.377 +/**
1.378 +A wide_character version of a getcwd().
1.379 +*/
1.380 +EXPORT_C wchar_t* wgetcwd (wchar_t *_buf, size_t _size)
1.381 + {
1.382 + return _wgetcwd_r(_REENT,_buf,_size);
1.383 + }
1.384 +
1.385 +/** A reentrant version of getcwd().
1.386 +*/
1.387 +EXPORT_C char* _getcwd_r (struct _reent *r, char *_buf, size_t _size)
1.388 + {
1.389 + char * _ourbuf = _buf;
1.390 + if (_buf==0)
1.391 + {
1.392 + _ourbuf=(char*)User::Alloc(_size);
1.393 + if (_ourbuf==0)
1.394 + {
1.395 + r->_errno=ENOMEM;
1.396 + return _buf;
1.397 + }
1.398 + }
1.399 +
1.400 + //we are dealing with wide characters from here so we need a temporary buffer
1.401 + wchar_t tmpbuf[KMaxFullName];
1.402 +
1.403 + MSystemInterface& sysIf=Interface(r);
1.404 + wchar_t * rval = sysIf.getcwd((wchar_t*)tmpbuf, _size, r->_errno);
1.405 +
1.406 + if (rval) //we have a path
1.407 + {
1.408 + //convert it to UTF8
1.409 + size_t x = wcstombs(_ourbuf, tmpbuf, _size); //convert the buffer
1.410 + return _ourbuf;
1.411 + }
1.412 + //deal with the fact we may have allocated our own buffer
1.413 + if (_buf != _ourbuf) //we allocated it.
1.414 + User::Free(_ourbuf);
1.415 + return NULL;
1.416 + }
1.417 +
1.418 +/** A wide-character version of reentrant of getcwd().
1.419 +*/
1.420 +EXPORT_C wchar_t * _wgetcwd_r (struct _reent *r, wchar_t *_buf, size_t _size)
1.421 + {
1.422 + if (_buf==0)
1.423 + {
1.424 + _buf=(wchar_t *)User::Alloc(_size*sizeof(wchar_t));
1.425 + if (_buf==0)
1.426 + {
1.427 + r->_errno=ENOMEM;
1.428 + return _buf;
1.429 + }
1.430 + }
1.431 + MSystemInterface& sysIf=Interface(r);
1.432 + return sysIf.getcwd(_buf,_size,r->_errno);
1.433 + }
1.434 +
1.435 +/**
1.436 +Changes the current working directory to be pathname. The current directory is the
1.437 +beginning point for file searches when path names are not absolute.
1.438 +If the chdir() function fails, the current working directory remains unchanged.
1.439 +
1.440 +@param _path Is the path name of a directory.
1.441 +
1.442 +@return On Success, returns 0.
1.443 + On Failure, returns -1, errno may be set.
1.444 +*/
1.445 +EXPORT_C int chdir (const char *_path)
1.446 + {
1.447 + struct _reent *r = _REENT2;
1.448 + if (!r)
1.449 + return -1; // Memory for library globals is not allocated (errno not set).
1.450 + return _chdir_r(r, _path);
1.451 + }
1.452 +
1.453 +/** A reentrant version of chdir().
1.454 +*/
1.455 +EXPORT_C int _chdir_r (struct _reent *r, const char *_path)
1.456 + {
1.457 + //we need to use a wide buffer and convert
1.458 + wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible
1.459 + if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
1.460 + {
1.461 + MSystemInterface& sysIf=Interface(r);
1.462 + return sysIf.chdir(tmpbuf, r->_errno);
1.463 + }
1.464 + MapError(EILSEQ, r->_errno);
1.465 + return -1;
1.466 + }
1.467 +
1.468 +/** A wide-character version of chdir().
1.469 +*/
1.470 +EXPORT_C int wchdir (const wchar_t *_path)
1.471 + {
1.472 + struct _reent *r = _REENT2;
1.473 + if (!r)
1.474 + return -1; // Memory for library globals is not allocated (errno not set).
1.475 + return _wchdir_r(r, _path);
1.476 + }
1.477 +
1.478 +/** A reentrant version of wchdir().
1.479 +*/
1.480 +EXPORT_C int _wchdir_r (struct _reent *r, const wchar_t *_path)
1.481 + {
1.482 + MSystemInterface& sysIf=Interface(r);
1.483 + return sysIf.chdir(_path,r->_errno);
1.484 + }
1.485 +
1.486 +/**
1.487 +Removes an empty directory whose name is given by pathname.
1.488 +The directory must not have any entries other than dot (.) and dot-dot (..).
1.489 +
1.490 +@param _path Points to the directory that the rmdir() function removes.
1.491 +
1.492 +@return On Success, returns 0.
1.493 + On Failure, returns -1, errno may be set.
1.494 +*/
1.495 +EXPORT_C int rmdir (const char *_path)
1.496 + {
1.497 + struct _reent *r = _REENT2;
1.498 + if (!r)
1.499 + return -1; // Memory for library globals is not allocated (errno not set).
1.500 + return _rmdir_r(r, _path);
1.501 + }
1.502 +
1.503 +/** A reentrant version of rmdir().
1.504 +*/
1.505 +EXPORT_C int _rmdir_r (struct _reent *r, const char *_path)
1.506 + {
1.507 + wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible
1.508 + if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
1.509 + {
1.510 + MSystemInterface& sysIf=Interface(r);
1.511 + return sysIf.rmdir(tmpbuf, r->_errno);
1.512 + }
1.513 + MapError(EILSEQ, r->_errno);
1.514 + return -1;
1.515 + }
1.516 +
1.517 +/** A wide-character version of rmdir().
1.518 +*/
1.519 +EXPORT_C int wrmdir (const wchar_t *_path)
1.520 + {
1.521 + struct _reent *r = _REENT2;
1.522 + if (!r)
1.523 + return -1; // Memory for library globals is not allocated (errno not set).
1.524 + return _wrmdir_r(r,_path);
1.525 + }
1.526 +
1.527 +/** A reentrant version of wrmdir().
1.528 +*/
1.529 +EXPORT_C int _wrmdir_r (struct _reent *r, const wchar_t *_path)
1.530 + {
1.531 + MSystemInterface& sysIf=Interface(r);
1.532 + return sysIf.rmdir(_path,r->_errno);
1.533 + }
1.534 +
1.535 +/**
1.536 +Creates a new directory with the specified path name.
1.537 +The file permissions of the new directory are initialized from the specified mode.
1.538 +
1.539 +@param _path Specifies the name of the new directory. The path name can be absolute or relative.
1.540 + If the specified path name is relative, the directory is created based upon your current
1.541 + working directory.
1.542 +@param _mode Is a bitwise-OR field that specifies what permissions the directory has when it is created.
1.543 +
1.544 +@return On Success, returns 0.
1.545 + On Failure, returns -1, errno may be set. Does not create a directory.
1.546 +*/
1.547 +EXPORT_C int mkdir (const char *_path, mode_t _mode)
1.548 + {
1.549 + struct _reent *r = _REENT2;
1.550 + if (!r)
1.551 + return -1; // Memory for library globals is not allocated (errno not set).
1.552 + return _mkdir_r(r,_path,_mode);
1.553 + }
1.554 +
1.555 +/** A reentrant version of mkdir().
1.556 +*/
1.557 +EXPORT_C int _mkdir_r (struct _reent *r, const char *_path, mode_t _mode)
1.558 + {
1.559 + //we need to use a wide buffer and convert
1.560 + wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible
1.561 + if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
1.562 + {
1.563 + MSystemInterface& sysIf=Interface(r);
1.564 + return sysIf.mkdir(tmpbuf, _mode, r->_errno);
1.565 + }
1.566 + MapError(EILSEQ, r->_errno);
1.567 + return -1;
1.568 + }
1.569 +
1.570 +/** A wide-character version of mkdir().
1.571 +*/
1.572 +EXPORT_C int wmkdir (const wchar_t *_path, mode_t _mode)
1.573 + {
1.574 + struct _reent *r = _REENT2;
1.575 + if (!r)
1.576 + return -1; // Memory for library globals is not allocated (errno not set).
1.577 + return _wmkdir_r(r, _path, _mode);
1.578 + }
1.579 +
1.580 +/** A reentrant version of wmkdir().
1.581 +*/
1.582 +EXPORT_C int _wmkdir_r (struct _reent *r, const wchar_t *_path, mode_t _mode)
1.583 + {
1.584 + MSystemInterface& sysIf=Interface(r);
1.585 + return sysIf.mkdir(_path,_mode,r->_errno);
1.586 + }
1.587 +
1.588 +/**
1.589 +Sets the access permissions for the file whose name is given by pathname to the bit
1.590 +pattern contained in mode. For this call to succeed, the effective user ID of the
1.591 +process must match the owner of the file, or the process must have appropriate privileges.
1.592 +The owner of the file pathname always has privileges to change permission modes and file attributes.
1.593 +
1.594 +@param _path Points to the name of the file.
1.595 +@param _mode Is a bitwise-or field that specifies the new permission modes for path name.
1.596 +
1.597 +@return On Success, returns 0.
1.598 + On Failure, returns -1, errno may be set.
1.599 +*/
1.600 +EXPORT_C int chmod (const char *_path, mode_t _mode)
1.601 + {
1.602 + struct _reent *r = _REENT2;
1.603 + if (!r)
1.604 + return -1; // Memory for library globals is not allocated (errno not set).
1.605 + return _chmod_r(r, _path, _mode);
1.606 + }
1.607 +
1.608 +/** A reentrant version of chmod().
1.609 +*/
1.610 +EXPORT_C int _chmod_r (struct _reent *r, const char *_path, mode_t _mode)
1.611 + {
1.612 + wchar_t tmpbuf[KMaxFullName+1];
1.613 + if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
1.614 + {
1.615 + MSystemInterface& sysIf=Interface(r);
1.616 + return sysIf.chmod(tmpbuf,_mode,r->_errno);
1.617 + }
1.618 + MapError(EILSEQ, r->_errno);
1.619 + return -1;
1.620 + }
1.621 +
1.622 +/** A wide-character version of chmod().
1.623 +*/
1.624 +EXPORT_C int wchmod (const wchar_t *_path, mode_t _mode)
1.625 + {
1.626 + struct _reent *r = _REENT2;
1.627 + if (!r)
1.628 + return -1; // Memory for library globals is not allocated (errno not set).
1.629 + return _wchmod_r(r, _path, _mode);
1.630 + }
1.631 +
1.632 +/** A reentrant version of wchmod().
1.633 +*/
1.634 +EXPORT_C int _wchmod_r (struct _reent *r, const wchar_t *_path, mode_t _mode)
1.635 + {
1.636 + MSystemInterface& sysIf=Interface(r);
1.637 + return sysIf.chmod(_path,_mode,r->_errno);
1.638 + }
1.639 +
1.640 +/**
1.641 +Removes a link to a file, and decrements the link count of the referenced file by one. When
1.642 +the file's link count becomes 0 and no process has the file open, the space occupied by the
1.643 +file is freed, and the file is no longer accessible. If one or more processes have the file
1.644 +open when the last link is removed, the link is removed before unlink() returns, but the
1.645 +removal of the file contents is postponed until all references to the file are closed.
1.646 +
1.647 +@param _path Points to the path name that names the file to be unlinked.
1.648 +
1.649 +@return On Success, returns 0.
1.650 + On Failure, returns -1, errno may be set.
1.651 +*/
1.652 +EXPORT_C int unlink (const char *_path)
1.653 + {
1.654 + struct _reent *r = _REENT2;
1.655 + if (!r)
1.656 + return -1; // Memory for library globals is not allocated (errno not set).
1.657 + return _unlink_r(r, _path);
1.658 + }
1.659 +
1.660 +/** A reentrant version of unlink().
1.661 +*/
1.662 +EXPORT_C int _unlink_r (struct _reent *r, const char *_path)
1.663 + {
1.664 + wchar_t tmpbuf[KMaxFullName+1];
1.665 + if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
1.666 + {
1.667 + MSystemInterface& sysIf=Interface(r);
1.668 + return sysIf.unlink(tmpbuf, r->_errno);
1.669 + }
1.670 + MapError(EILSEQ, r->_errno);
1.671 + return -1;
1.672 + }
1.673 +
1.674 +/** A wide-character version of unlink().
1.675 +*/
1.676 +EXPORT_C int wunlink (const wchar_t *_path)
1.677 + {
1.678 + struct _reent *r = _REENT2;
1.679 + if (!r)
1.680 + return -1; // Memory for library globals is not allocated (errno not set).
1.681 + return _wunlink_r(r, _path);
1.682 + }
1.683 +
1.684 +/** A wide-character version of reentrant of unlink().
1.685 +*/
1.686 +EXPORT_C int _wunlink_r (struct _reent *r, const wchar_t *_path)
1.687 + {
1.688 + MSystemInterface& sysIf=Interface(r);
1.689 + return sysIf.unlink(_path,r->_errno);
1.690 + }
1.691 +
1.692 +/**
1.693 +Renames a file.
1.694 +
1.695 +@param oldpath Points to the path name of the file to be renamed. The path name can be
1.696 + absolute or relative. If a relative path name is given, the file is searched from
1.697 + the current working directory.
1.698 +@param newpath Points to the path name of the file. The path name can be absolute or relative.
1.699 + If a relative path name is given, the file is searched from the current working directory.
1.700 +
1.701 +@return On Success, returns 0.
1.702 + On Failure, returns -1, errno may be set. Does not change either the file named
1.703 + by old or the file named by new (if either exists).
1.704 +*/
1.705 +EXPORT_C int rename (const char *oldpath, const char *newpath)
1.706 + {
1.707 + struct _reent *r = _REENT2;
1.708 + if (!r)
1.709 + return -1; // Memory for library globals is not allocated (errno not set).
1.710 + return _rename_r (r, oldpath, newpath);
1.711 + }
1.712 +
1.713 +/** A reentrant version of rename().
1.714 +*/
1.715 +EXPORT_C int _rename_r (struct _reent *r, const char *oldpath, const char *newpath)
1.716 + {
1.717 + wchar_t _old[KMaxFullName+1];
1.718 + wchar_t _new[KMaxFullName+1];
1.719 + if (-1 != mbstowcs(_old, oldpath, KMaxFullName))
1.720 + {
1.721 + if (-1 != mbstowcs(_new, newpath, KMaxFullName))
1.722 + {
1.723 + MSystemInterface& sysIf=Interface(r);
1.724 + return sysIf.rename(_old, _new, r->_errno);
1.725 + }
1.726 + }
1.727 + MapError(EILSEQ, r->_errno);
1.728 + return -1;
1.729 + }
1.730 +
1.731 +/** A wide-character version of rename().
1.732 +*/
1.733 +EXPORT_C int wrename (const wchar_t *oldpath, const wchar_t *newpath)
1.734 + {
1.735 + struct _reent *r = _REENT2;
1.736 + if (!r)
1.737 + return -1; // Memory for library globals is not allocated (errno not set).
1.738 + return _wrename_r (r, oldpath, newpath);
1.739 + }
1.740 +
1.741 +/** A wide-character version of reentrant of rename().
1.742 +*/
1.743 +EXPORT_C int _wrename_r (struct _reent *r, const wchar_t *oldpath, const wchar_t *newpath)
1.744 + {
1.745 + MSystemInterface& sysIf=Interface(r);
1.746 + return sysIf.rename(oldpath,newpath,r->_errno);
1.747 + }
1.748 +
1.749 +/**
1.750 +Takes a specified path name, pathname and resolves all symbolic links,
1.751 +extra slashes (/), and references to /./ and /../.
1.752 +The resulting absolute path name is placed in the memory location
1.753 +pointed to by the resolved_path argument.
1.754 +@return resolved_path.
1.755 +When an error occurs,returns a null pointer, setsresolved_path
1.756 +to the path name that caused the error.
1.757 +@param path Points to the path name that you want resolved to an absolute form.
1.758 +This may be either a relative or absolute path name.
1.759 +All but the final component of this path name must exist when you call realpath().
1.760 +@param resolved Points to the location where the canonical version
1.761 +of pathname is to be placed.
1.762 +*/
1.763 +EXPORT_C char* realpath (const char* path, char* resolved)
1.764 + {
1.765 + return _realpath_r(_REENT, path, resolved);
1.766 + }
1.767 +
1.768 +/** A wide-character version of realpath().
1.769 +*/
1.770 +EXPORT_C wchar_t* wrealpath (const wchar_t* path, wchar_t* resolved)
1.771 + {
1.772 + return _wrealpath_r(_REENT, path, resolved);
1.773 + }
1.774 +
1.775 +/** A wide-character version of reentrant of realpath().
1.776 +*/
1.777 +EXPORT_C wchar_t * _wrealpath_r (struct _reent *r, const wchar_t *relpath, wchar_t *resolved)
1.778 + {
1.779 +
1.780 + TPtr16 name((TText16*)resolved,MAXPATHLEN);
1.781 + TParse path;
1.782 + MSystemInterface& sysIf=Interface(r);
1.783 + TInt err = sysIf.ResolvePath(path, relpath, &name);
1.784 + if (!err)
1.785 + {
1.786 + err = path.SetNoWild(path.DriveAndPath(),NULL,&name);
1.787 + if (!err)
1.788 + {
1.789 + name = path.FullName();
1.790 + name.ZeroTerminate();
1.791 + return resolved;
1.792 + }
1.793 + }
1.794 + MapError(err,r->_errno);
1.795 + return 0;
1.796 + }
1.797 +
1.798 +/** A reentrant version of realpath().
1.799 +*/
1.800 +EXPORT_C char* _realpath_r (struct _reent *r, const char *relpath, char *resolved)
1.801 + {
1.802 +
1.803 + TFileName name;
1.804 + TInt err;
1.805 +
1.806 + TParse path;
1.807 + MSystemInterface& sysIf=Interface(r);
1.808 +
1.809 + wchar_t _wrelpath[KMaxFileName];
1.810 +
1.811 + if (-1 != mbstowcs(_wrelpath, relpath , KMaxFileName))
1.812 + {
1.813 + err = sysIf.ResolvePath(path, _wrelpath, &name);
1.814 + if (!err)
1.815 + {
1.816 + err = path.SetNoWild(path.DriveAndPath(),NULL,&name);
1.817 + if (!err)
1.818 + {
1.819 + name = path.FullName();
1.820 +
1.821 + if (-1 != wcstombs(resolved, (wchar_t*)name.PtrZ(), KMaxFileName))
1.822 + return resolved;
1.823 + else
1.824 + {
1.825 + err = EILSEQ;
1.826 + }
1.827 + }
1.828 + }
1.829 + }
1.830 + else
1.831 + {
1.832 + err = EILSEQ;
1.833 + }
1.834 +
1.835 + MapError(err,r->_errno);
1.836 + return 0;
1.837 + }
1.838 +
1.839 +/**
1.840 +Gives access to the client's stdin.
1.841 +
1.842 +@return On Success, returns a pointer to an open stream, used to read or write to the pipe.
1.843 + On Failure, return a null pointer.
1.844 +*/
1.845 +EXPORT_C int popen3 (const char* cmd, const char* mode, char** env, int fids[3])
1.846 + {
1.847 + struct _reent *r = _REENT2;
1.848 + if (!r)
1.849 + return NULL; // Memory for library globals is not allocated (errno not set).
1.850 + return _popen3_r (r,cmd,mode,env,fids);
1.851 + }
1.852 +
1.853 +/** A wide-character version of popen3().
1.854 +*/
1.855 +EXPORT_C int wpopen3 (const wchar_t* cmd, const wchar_t* mode, wchar_t** env, int fids[3])
1.856 + {
1.857 + struct _reent *r = _REENT2;
1.858 + if (!r)
1.859 + return NULL; // Memory for library globals is not allocated (errno not set).
1.860 + return _wpopen3_r (r,cmd,mode,env,fids);
1.861 + }
1.862 +
1.863 +/** A reentrant version of a popen3().
1.864 +*/
1.865 +EXPORT_C int _popen3_r (struct _reent *r, const char* cmd, const char* mode, char** env, int fids[3])
1.866 + {
1.867 +
1.868 + wchar_t wcmd[MAXPATHLEN+1];
1.869 + wchar_t wmode[MAXPATHLEN+1];
1.870 +
1.871 + wchar_t ** wenv = NULL;
1.872 + wchar_t * buf = NULL;
1.873 +
1.874 + TInt ret = 0;
1.875 +
1.876 + if ((-1 != mbstowcs(wcmd, cmd, MAXPATHLEN)) &&
1.877 + (-1 != mbstowcs(wmode, mode, MAXPATHLEN)))
1.878 + {
1.879 + //OK, we've widened the first 2 args
1.880 + //now for the environment
1.881 +
1.882 + //env is basically an array of char pointers with a NULL as the last one
1.883 + if (env)
1.884 + {
1.885 + //OK we have a ptr to something
1.886 + //count the number of entries and get their lengths so we can work out how much space
1.887 + //is needed for the new one
1.888 +
1.889 + TInt count = 0;
1.890 + TInt total = 0;
1.891 + while (env[count] != NULL)
1.892 + {
1.893 + total+= strlen(env[count])+1;
1.894 + count++;
1.895 + }
1.896 + //total has number of bytes in the strings
1.897 + //max number of unicode chars is with a 1 to 1 mapping.
1.898 + wenv = (wchar_t**)malloc(1 + count*sizeof(wchar_t*));
1.899 + buf = (wchar_t*)malloc(2*total);
1.900 +
1.901 + if (!(wenv && buf)) //we've had a malloc failure
1.902 + {
1.903 + r->_errno = ENOMEM;
1.904 + goto bailout;
1.905 + }
1.906 +
1.907 + wchar_t* p = buf;
1.908 +
1.909 + TInt ret;
1.910 + for (TInt x = 0; x < count; x++)
1.911 + {
1.912 + wenv[count] = p;
1.913 + ret = mbstowcs(p, env[count], MAXPATHLEN);
1.914 + if (ret >= 0)
1.915 + {
1.916 + p += ret; //step to next bit of space
1.917 + }
1.918 + else
1.919 + {
1.920 + r->_errno = EILSEQ;
1.921 + goto bailout;
1.922 + }
1.923 +
1.924 + }
1.925 + }
1.926 +
1.927 +
1.928 + ret = _wpopen3_r(r, wcmd, wmode, wenv, fids);
1.929 + }
1.930 + else
1.931 + {
1.932 + r->_errno = EILSEQ;
1.933 + }
1.934 +
1.935 + //don't lose the memory
1.936 +bailout:
1.937 + free(wenv);
1.938 + free(buf);
1.939 +
1.940 + return ret;
1.941 + }
1.942 +
1.943 +/** A wide-character version of reentrant of popen3().
1.944 +*/
1.945 +EXPORT_C int _wpopen3_r (struct _reent *r, const wchar_t* cmd, const wchar_t* mode, wchar_t** env, int fids[3])
1.946 + {
1.947 + // Find the full path of the thing we are executing...
1.948 + const wchar_t* cp=cmd;
1.949 + while (*cp==L' ')
1.950 + ++cp; // skip leading spaces
1.951 + wchar_t file[MAXPATHLEN+1];
1.952 + TInt i=0;
1.953 + wchar_t c=0;
1.954 + for (i=0; i<MAXPATHLEN; i++, cp++)
1.955 + {
1.956 + c=*cp;
1.957 + file[i]=c;
1.958 + if (c==L' ' || c==L'\t' || c==L'\0') // stop at first space, tab or \0
1.959 + break;
1.960 + }
1.961 + file[i]=L'\0';
1.962 + wchar_t resolved[MAXPATHLEN+1];
1.963 + if(_wrealpath_r(r, file, resolved)==0)
1.964 + return -1; // no such file
1.965 +
1.966 + // Strip leading whitespace from the rest of the commandline
1.967 + for (; i<MAXPATHLEN;i++,cp++)
1.968 + {
1.969 + c=*cp;
1.970 + if (c=='\0')
1.971 + break;
1.972 + if ((c!=' ') && (c!='\t'))
1.973 + break;
1.974 + }
1.975 +
1.976 + fids[0]=-2;
1.977 + fids[1]=-2;
1.978 + fids[2]=-2;
1.979 + const wchar_t* mp=mode;
1.980 + while (*mp)
1.981 + {
1.982 + wchar_t c=*mp++;
1.983 + if (c==L'r')
1.984 + fids[0]=-1;
1.985 + else if (c==L'w')
1.986 + fids[1]=-1;
1.987 + else if (c==L'e')
1.988 + fids[2]=-1;
1.989 + }
1.990 +
1.991 + MSystemInterface& sysIf=Interface(r);
1.992 + return sysIf.popen3(resolved,cp,mode,env,fids,r->_errno);
1.993 + }
1.994 +
1.995 +/**
1.996 +Lets the calling process obtain status information about one of its child processes.
1.997 +If status information is available for two or more child processes, the order in
1.998 +which their status is reported is unspecified.
1.999 +
1.1000 +@param pid Specifies a set of child processes for which the status is requested
1.1001 +@param status Specifies the location to which the child process' exit status is stored.
1.1002 +@param options Is the bitwise inclusive-OR of zero or more of the following flags.
1.1003 +
1.1004 +@return On Success, returns a value equal to the process ID of the child process.
1.1005 + On Failure, returns -1 and errno may be set OR returns 0 if the status is not available
1.1006 + for the specified process and it's set not to hang in the options.
1.1007 +*/
1.1008 +EXPORT_C int waitpid (int pid, int* status, int options)
1.1009 + {
1.1010 + struct _reent *r = _REENT2;
1.1011 + if (!r)
1.1012 + return -1; // Memory for library globals is not allocated (errno not set).
1.1013 + return _waitpid_r (r, pid, status, options);
1.1014 + }
1.1015 +
1.1016 +/** A reentrant version of waitpid().
1.1017 +*/
1.1018 +EXPORT_C int _waitpid_r (struct _reent *r, int pid, int* status, int options)
1.1019 + {
1.1020 + MSystemInterface& sysIf=Interface(r);
1.1021 + return sysIf.waitpid(pid,status,options,r->_errno);
1.1022 + }
1.1023 +
1.1024 +/**
1.1025 +Calls reentrant version of waitpid().
1.1026 +*/
1.1027 +EXPORT_C int wait (int* status)
1.1028 + {
1.1029 + struct _reent *r = _REENT2;
1.1030 + if (!r)
1.1031 + return -1; // Memory for library globals is not allocated (errno not set).
1.1032 + return _waitpid_r (r, -1, status, 0);
1.1033 + }
1.1034 +
1.1035 +/** A reentrant version of wait().
1.1036 +*/
1.1037 +EXPORT_C int _wait_r (struct _reent *r, int* status)
1.1038 + {
1.1039 + return _waitpid_r (r,-1,status,0);
1.1040 + }
1.1041 +
1.1042 +/**
1.1043 +Execute command.
1.1044 +
1.1045 +@param cmd Null-terminated string containing the system command to be executed.
1.1046 +
1.1047 +@return On Success, the command interpreter returns an adequate value; generally 0
1.1048 + indicates that the action performed by the command interpreter terminated
1.1049 + with no errors.
1.1050 + On Failure, return -1.
1.1051 +*/
1.1052 +EXPORT_C int system (const char* cmd)
1.1053 + {
1.1054 + struct _reent *r = _REENT2;
1.1055 + if (!r)
1.1056 + return -1; // Memory for library globals is not allocated (errno not set).
1.1057 + return _system_r (r, cmd);
1.1058 + }
1.1059 +
1.1060 +/** A reentrant version of system().
1.1061 +*/
1.1062 +EXPORT_C int _system_r (struct _reent *r, const char* cmd)
1.1063 + {
1.1064 + if (cmd==0)
1.1065 + return 1; // special case, says that we do support system().
1.1066 + int fids[3];
1.1067 + int pid=_popen3_r(r, cmd, "", 0, fids);
1.1068 + if (pid<0)
1.1069 + return -1;
1.1070 + int status=0;
1.1071 + pid=_waitpid_r (r,pid,&status,0);
1.1072 + if (pid<0)
1.1073 + return -1;
1.1074 + return status;
1.1075 + }
1.1076 +
1.1077 +/** A wide-character version of a system().
1.1078 +*/
1.1079 +EXPORT_C int wsystem (const wchar_t* cmd)
1.1080 + {
1.1081 + struct _reent *r = _REENT2;
1.1082 + if (!r)
1.1083 + return -1; // Memory for library globals is not allocated (errno not set).
1.1084 + return _wsystem_r (r, cmd);
1.1085 + }
1.1086 +
1.1087 +/** A wide-character version of reentrant of system().
1.1088 +*/
1.1089 +EXPORT_C int _wsystem_r (struct _reent *r, const wchar_t* cmd)
1.1090 + {
1.1091 + if (cmd==0)
1.1092 + return 1; // special case, says that we do support system().
1.1093 + int fids[3];
1.1094 + int pid=_wpopen3_r(r, cmd, (wchar_t*)L"", 0, fids);
1.1095 + if (pid<0)
1.1096 + return -1;
1.1097 + int status=0;
1.1098 + pid=_waitpid_r (r,pid,&status,0);
1.1099 + if (pid<0)
1.1100 + return -1;
1.1101 + return status;
1.1102 + }
1.1103 +
1.1104 +} // extern "C"
1.1105 +
1.1106 +#include <estlib.h>
1.1107 +
1.1108 +/** Dubious asynchronous interface to ioctl, must be called from C++
1.1109 +
1.1110 +@return On Success, returns a value other than -1.
1.1111 + On Failure, returns -1 and errno may be set.
1.1112 +*/
1.1113 +EXPORT_C int ioctl (int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
1.1114 + {
1.1115 + struct _reent *r = _REENT2;
1.1116 + if (!r)
1.1117 + return -1; // Memory for library globals is not allocated (errno not set).
1.1118 + return _ioctl_r(r, aFid, aCmd, aParam, aStatus);
1.1119 + }
1.1120 +
1.1121 +/** A reentrant version of a ioctl().
1.1122 +*/
1.1123 +EXPORT_C int _ioctl_r (struct _reent *r, int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
1.1124 + {
1.1125 + MSystemInterface& sysIf=Interface(r);
1.1126 + return sysIf.ioctl(aFid,aCmd,aParam,aStatus,r->_errno);
1.1127 + }
1.1128 +
1.1129 +EXPORT_C int ioctl_complete (int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
1.1130 + {
1.1131 + struct _reent *r = _REENT2;
1.1132 + if (!r)
1.1133 + return -1; // Memory for library globals is not allocated (errno not set).
1.1134 + return _ioctl_complete_r(r, aFid, aCmd, aParam, aStatus);
1.1135 + }
1.1136 +
1.1137 +/** A reentrant version of a ioctl_complete().
1.1138 +*/
1.1139 +EXPORT_C int _ioctl_complete_r (struct _reent *r, int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
1.1140 + {
1.1141 + MSystemInterface& sysIf=Interface(r);
1.1142 + return sysIf.ioctl_complete(aFid,aCmd,aParam,aStatus,r->_errno);
1.1143 + }
1.1144 +
1.1145 +EXPORT_C int ioctl_cancel (int aFid)
1.1146 + {
1.1147 + struct _reent *r = _REENT2;
1.1148 + if (!r)
1.1149 + return -1; // Memory for library globals is not allocated (errno not set).
1.1150 + return _ioctl_cancel_r(r, aFid);
1.1151 + }
1.1152 +
1.1153 +/** A reentrant version of a ioctl_cancel().
1.1154 +*/
1.1155 +EXPORT_C int _ioctl_cancel_r (struct _reent *r, int aFid)
1.1156 + {
1.1157 + MSystemInterface& sysIf=Interface(r);
1.1158 + return sysIf.ioctl_cancel(aFid,r->_errno);
1.1159 + }
1.1160 +