os/ossrv/genericopenlibs/cstdlib/LPOSIX/SYSCALLS.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// connectors for re-entrant system calls
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "SYSIF.H"
sl@0
    19
#include "LPOSIX.H"
sl@0
    20
sl@0
    21
#include <reent.h>
sl@0
    22
#include <unistd.h>
sl@0
    23
#include <fcntl.h>		// for open()
sl@0
    24
#include <sys/ioctl.h>
sl@0
    25
#include <stdarg.h>
sl@0
    26
#include <sys/errno.h>
sl@0
    27
#include <stdio_r.h>		// for popen3
sl@0
    28
#include <stdlib_r.h>		// for system
sl@0
    29
#include <utf.h>
sl@0
    30
#include <string.h>
sl@0
    31
sl@0
    32
sl@0
    33
extern "C" {
sl@0
    34
sl@0
    35
/**
sl@0
    36
Opens the file which name is stored in the file string.
sl@0
    37
sl@0
    38
@return	On Success, a non-negative integer representing the lowest numbered unused file descriptor.
sl@0
    39
		On Failure, returns -1, errno may be set.		
sl@0
    40
*/
sl@0
    41
EXPORT_C int open (const char *file, int flags, ...)
sl@0
    42
	{
sl@0
    43
	va_list ap;
sl@0
    44
	int ret;
sl@0
    45
sl@0
    46
	struct _reent *r = _REENT2;
sl@0
    47
	if (!r)
sl@0
    48
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
    49
sl@0
    50
	va_start (ap, flags);
sl@0
    51
	ret = _open_r (r, file, flags, va_arg (ap, int));
sl@0
    52
	va_end (ap);
sl@0
    53
	return ret;
sl@0
    54
	}
sl@0
    55
sl@0
    56
/**
sl@0
    57
A wide_character version of a open().
sl@0
    58
*/
sl@0
    59
EXPORT_C int wopen (const wchar_t *file, int flags, ...)
sl@0
    60
	{
sl@0
    61
	va_list ap;
sl@0
    62
	int ret;
sl@0
    63
sl@0
    64
	struct _reent *r = _REENT2;
sl@0
    65
	if (!r)
sl@0
    66
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
    67
sl@0
    68
	va_start (ap, flags);
sl@0
    69
	ret = _wopen_r (r, file, flags, va_arg (ap, int));
sl@0
    70
	va_end (ap);
sl@0
    71
	return ret;
sl@0
    72
	}
sl@0
    73
sl@0
    74
/** A reentrant version of open().
sl@0
    75
*/
sl@0
    76
EXPORT_C int _open_r (struct _reent *r, const char *name, int mode, int perms)
sl@0
    77
	{
sl@0
    78
	wchar_t _widename[KMaxFileName+1];
sl@0
    79
sl@0
    80
	if (-1 != mbstowcs(_widename, name, KMaxFileName))
sl@0
    81
		{
sl@0
    82
		MSystemInterface& sysIf=Interface(r);
sl@0
    83
		return sysIf.open(_widename,mode,perms,r->_errno);
sl@0
    84
		}
sl@0
    85
	
sl@0
    86
	MapError(EILSEQ, r->_errno);		
sl@0
    87
	return 0;	//null file pointer
sl@0
    88
sl@0
    89
	}
sl@0
    90
sl@0
    91
/** A reentrant version of wopen().
sl@0
    92
*/
sl@0
    93
EXPORT_C int _wopen_r (struct _reent *r, const wchar_t *name, int mode, int perms)
sl@0
    94
	{
sl@0
    95
	MSystemInterface& sysIf=Interface(r);
sl@0
    96
	return sysIf.open(name,mode,perms,r->_errno);
sl@0
    97
	}
sl@0
    98
sl@0
    99
/**
sl@0
   100
Reads a block of data of the length specified by cnt.
sl@0
   101
sl@0
   102
@return On Success, return a non-negative integer indicating the number of bytes actually read.
sl@0
   103
		On Failure, returns -1, errno may be set.		
sl@0
   104
*/
sl@0
   105
EXPORT_C int read (int fd, char *buf, size_t cnt)
sl@0
   106
	{
sl@0
   107
	struct _reent *r = _REENT2;
sl@0
   108
	if (!r)
sl@0
   109
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   110
	return _read_r (r, fd, buf, cnt);
sl@0
   111
	}
sl@0
   112
sl@0
   113
/** A reentrant version of read().
sl@0
   114
*/
sl@0
   115
EXPORT_C int _read_r (struct _reent *r, int fd, char *buf, size_t nbyte)
sl@0
   116
	{
sl@0
   117
	MSystemInterface& sysIf=Interface(r);
sl@0
   118
	return sysIf.read(fd,buf,nbyte,r->_errno);
sl@0
   119
	}
sl@0
   120
sl@0
   121
/**
sl@0
   122
Writes a block of data of the length specified by cnt.
sl@0
   123
sl@0
   124
@return On Success, returns the number of bytes written to the file. The number
sl@0
   125
		shall never be greater than cnt.
sl@0
   126
		On Failure, returns -1, errno may be set.
sl@0
   127
*/
sl@0
   128
EXPORT_C int write (int fd, const char *buf, size_t cnt)
sl@0
   129
	{
sl@0
   130
	struct _reent *r = _REENT2;
sl@0
   131
	if (!r)
sl@0
   132
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   133
	return _write_r (r, fd, buf, cnt);
sl@0
   134
	}
sl@0
   135
sl@0
   136
/** A reentrant version of write().
sl@0
   137
*/
sl@0
   138
EXPORT_C int _write_r (struct _reent *r, int fd, const char *buf, size_t nbyte)
sl@0
   139
	{
sl@0
   140
	MSystemInterface& sysIf=Interface(r);
sl@0
   141
	return sysIf.write(fd,buf,nbyte,r->_errno);
sl@0
   142
	}
sl@0
   143
sl@0
   144
/**
sl@0
   145
Close a file.
sl@0
   146
sl@0
   147
@return	On Success, returns 0. 
sl@0
   148
		On Failure, returns -1, errno may be set.
sl@0
   149
*/
sl@0
   150
EXPORT_C int close (int fd)
sl@0
   151
	{
sl@0
   152
	struct _reent *r = _REENT2;
sl@0
   153
	if (!r)
sl@0
   154
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   155
	return _close_r (r, fd);
sl@0
   156
	}
sl@0
   157
sl@0
   158
sl@0
   159
/** A reentrant version of close().
sl@0
   160
*/
sl@0
   161
EXPORT_C int _close_r (struct _reent *r, int fd)
sl@0
   162
	{
sl@0
   163
	MSystemInterface& sysIf=Interface(r);
sl@0
   164
	return sysIf.close(fd,r->_errno);
sl@0
   165
	}
sl@0
   166
sl@0
   167
/**
sl@0
   168
Synchronizes a file's in-memory state with that on the physical medium.
sl@0
   169
sl@0
   170
@param fd Is the file descriptor for the file to be synchronized.
sl@0
   171
sl@0
   172
@return On Success, returns 0. 
sl@0
   173
		On Failure, returns -1, errno may be set.
sl@0
   174
*/
sl@0
   175
EXPORT_C int fsync (int fd)
sl@0
   176
	{
sl@0
   177
	struct _reent *r = _REENT2;
sl@0
   178
	if (!r)
sl@0
   179
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   180
	return _fsync_r (r, fd);
sl@0
   181
	}
sl@0
   182
sl@0
   183
/** A reentrant version of fsync().
sl@0
   184
*/
sl@0
   185
EXPORT_C int _fsync_r (struct _reent *r, int fd)
sl@0
   186
	{
sl@0
   187
	MSystemInterface& sysIf=Interface(r);
sl@0
   188
	return sysIf.fsync(fd,r->_errno);
sl@0
   189
	}
sl@0
   190
sl@0
   191
/**
sl@0
   192
Repositions the read/write file offset.
sl@0
   193
@return a nonnegative integer that indicates the file pointer value. 
sl@0
   194
@param fd Is the file descriptor of an open file.
sl@0
   195
@param pos Specifies the number of bytes to offset the file pointer 
sl@0
   196
from a specified file origin.
sl@0
   197
@param whence Specifies the location from which to start seeking.
sl@0
   198
*/
sl@0
   199
EXPORT_C off_t lseek (int fd, off_t pos, int whence)
sl@0
   200
	{
sl@0
   201
	return _lseek_r (_REENT, fd, pos, whence);
sl@0
   202
	}
sl@0
   203
sl@0
   204
/** A reentrant version of fseek().
sl@0
   205
*/
sl@0
   206
EXPORT_C off_t _lseek_r (struct _reent *r, int fd, off_t pos, int whence)
sl@0
   207
	{
sl@0
   208
	MSystemInterface& sysIf=Interface(r);
sl@0
   209
	return sysIf.lseek(fd,pos,whence,r->_errno);
sl@0
   210
	}
sl@0
   211
sl@0
   212
/**
sl@0
   213
Gets information about the named file and writes it to the area that buf points to.
sl@0
   214
The system must be able to search all directories leading to the file; 
sl@0
   215
however, read, write, or execute permission of the file is not required.
sl@0
   216
sl@0
   217
@param fd Is a file descriptor referring to a file for which status is returned.
sl@0
   218
@param st Points to a stat structure where status information about the file is to be placed.
sl@0
   219
sl@0
   220
@return On Success, returns 0. 
sl@0
   221
		On Failure, returns -1, errno may be set.
sl@0
   222
*/
sl@0
   223
EXPORT_C int fstat (int fd, struct stat *st)
sl@0
   224
	{
sl@0
   225
	struct _reent *r = _REENT2;
sl@0
   226
	if (!r)
sl@0
   227
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   228
	return _fstat_r (r, fd, st);
sl@0
   229
	}
sl@0
   230
sl@0
   231
/** A reentrant version of fstat().
sl@0
   232
*/
sl@0
   233
EXPORT_C int _fstat_r (struct _reent *r, int fd, struct stat *st) 
sl@0
   234
	{
sl@0
   235
	MSystemInterface& sysIf=Interface(r);
sl@0
   236
	return sysIf.fstat(fd,st,r->_errno);
sl@0
   237
	}
sl@0
   238
sl@0
   239
/**
sl@0
   240
Gets the size of a file. 
sl@0
   241
sl@0
   242
@return On Success, returns 0. 
sl@0
   243
		On Failure, returns -1, errno may be set.
sl@0
   244
*/
sl@0
   245
EXPORT_C int stat (const char *name, struct stat *st)
sl@0
   246
	{
sl@0
   247
	struct _reent *r = _REENT2;
sl@0
   248
	if (!r)
sl@0
   249
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   250
	return _stat_r (r, name, st);
sl@0
   251
	}
sl@0
   252
sl@0
   253
/** A reentrant version of stat().
sl@0
   254
*/
sl@0
   255
EXPORT_C int _stat_r (struct _reent *r, const char *name, struct stat *st) 
sl@0
   256
	{
sl@0
   257
	wchar_t tmpbuf[KMaxFullName+1];	
sl@0
   258
	if (-1 != mbstowcs(tmpbuf, name, KMaxFullName))
sl@0
   259
		{
sl@0
   260
		MSystemInterface& sysIf=Interface(r);
sl@0
   261
		return sysIf.stat(tmpbuf, st, r->_errno);
sl@0
   262
		}
sl@0
   263
	MapError(EILSEQ, r->_errno);		
sl@0
   264
	return -1;
sl@0
   265
	}
sl@0
   266
sl@0
   267
/** 
sl@0
   268
A wide_character version of a stat().
sl@0
   269
*/
sl@0
   270
EXPORT_C int wstat (const wchar_t *name, struct stat *st)
sl@0
   271
	{
sl@0
   272
	struct _reent *r = _REENT2;
sl@0
   273
	if (!r)
sl@0
   274
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   275
	return _wstat_r (r, name, st);
sl@0
   276
	}
sl@0
   277
sl@0
   278
/** A reentrant version of wstat().
sl@0
   279
*/
sl@0
   280
EXPORT_C int _wstat_r (struct _reent *r, const wchar_t *name, struct stat *st) 
sl@0
   281
	{
sl@0
   282
	MSystemInterface& sysIf=Interface(r);
sl@0
   283
	return sysIf.stat(name,st,r->_errno);
sl@0
   284
	}
sl@0
   285
sl@0
   286
/**
sl@0
   287
Duplicates an open file descriptor. 
sl@0
   288
		
sl@0
   289
@param aFid Is the file descriptor to duplicate.
sl@0
   290
sl@0
   291
@return On Success, returns a non-negative integer, namely the duplicated file descriptor, which
sl@0
   292
		is the lowest available descriptor. 
sl@0
   293
		On Failure, returns -1, errno may be set.
sl@0
   294
*/
sl@0
   295
EXPORT_C int dup (int aFid)
sl@0
   296
	{
sl@0
   297
	struct _reent *r = _REENT2;
sl@0
   298
	if (!r)
sl@0
   299
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   300
	return _dup_r(r, aFid);
sl@0
   301
	}
sl@0
   302
sl@0
   303
/** A reentrant version of dup().
sl@0
   304
*/
sl@0
   305
EXPORT_C int _dup_r (struct _reent *r, int aFid)
sl@0
   306
	{
sl@0
   307
	MSystemInterface& sysIf=Interface(r);
sl@0
   308
	return sysIf.dup(aFid,r->_errno);
sl@0
   309
	}
sl@0
   310
sl@0
   311
/**
sl@0
   312
Function duplicates an open file descriptor. 
sl@0
   313
sl@0
   314
@param aFid1 Is the file descriptor to duplicate.
sl@0
   315
@param aFid2 Is the file descriptor that filedes is duplicated onto.
sl@0
   316
sl@0
   317
@return On Success, returns a non-negative integer, namely the duplicated file descriptor, which
sl@0
   318
		is the lowest available descriptor. 
sl@0
   319
		On Failure, returns -1, errno may be set.
sl@0
   320
*/
sl@0
   321
EXPORT_C int dup2 (int aFid1, int aFid2)
sl@0
   322
	{
sl@0
   323
	struct _reent *r = _REENT2;
sl@0
   324
	if (!r)
sl@0
   325
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   326
	return _dup2_r(r, aFid1, aFid2);
sl@0
   327
	}
sl@0
   328
sl@0
   329
/** A reentrant version of dup2().
sl@0
   330
*/
sl@0
   331
EXPORT_C int _dup2_r (struct _reent *r, int aFid1, int aFid2)
sl@0
   332
	{
sl@0
   333
	MSystemInterface& sysIf=Interface(r);
sl@0
   334
	return sysIf.dup2(aFid1,aFid2,r->_errno);
sl@0
   335
	}
sl@0
   336
sl@0
   337
/**
sl@0
   338
Performs a variety of device-specific control functions on device special files.
sl@0
   339
sl@0
   340
@return On Success, returns a value other than -1 that depends upon the STREAMS device control function. 
sl@0
   341
        On Failure, return -1, errno may be set.
sl@0
   342
*/
sl@0
   343
EXPORT_C int ioctl (int aFid, int aCmd, void* aParam)
sl@0
   344
	{
sl@0
   345
	struct _reent *r = _REENT2;
sl@0
   346
	if (!r)
sl@0
   347
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   348
	return _ioctl_r(r, aFid, aCmd, aParam);
sl@0
   349
	}
sl@0
   350
sl@0
   351
/** A reentrant version of ioctl().
sl@0
   352
*/
sl@0
   353
EXPORT_C int _ioctl_r (struct _reent *r, int aFid, int aCmd, void* aParam)
sl@0
   354
	{
sl@0
   355
	MSystemInterface& sysIf=Interface(r);
sl@0
   356
	return sysIf.ioctl(aFid,aCmd,aParam,r->_errno);
sl@0
   357
	}
sl@0
   358
sl@0
   359
/**
sl@0
   360
Gets the path name of the current working directory.
sl@0
   361
If a buffer is specified, the path name is placed in that buffer,
sl@0
   362
and the address of the buffer is returned. 
sl@0
   363
@return If successful returns buf, if a non-null pointer was specified, 
sl@0
   364
or the address of the allocated memory otherwise. 
sl@0
   365
@param _buf Points to the buffer to copy the current working directory to, 
sl@0
   366
or NULL if getcwd() should allocate the buffer.
sl@0
   367
@param _size Is the size, in bytes, of the array of characters that buf points to.
sl@0
   368
*/
sl@0
   369
EXPORT_C char* getcwd (char *_buf, size_t _size)
sl@0
   370
	{
sl@0
   371
	return _getcwd_r(_REENT,_buf,_size);
sl@0
   372
	}
sl@0
   373
sl@0
   374
/** 
sl@0
   375
A wide_character version of a getcwd().
sl@0
   376
*/
sl@0
   377
EXPORT_C wchar_t* wgetcwd (wchar_t *_buf, size_t _size)
sl@0
   378
	{
sl@0
   379
	return _wgetcwd_r(_REENT,_buf,_size);
sl@0
   380
	}
sl@0
   381
sl@0
   382
/** A reentrant version of getcwd().
sl@0
   383
*/
sl@0
   384
EXPORT_C char* _getcwd_r (struct _reent *r, char *_buf, size_t _size)
sl@0
   385
	{
sl@0
   386
	char * _ourbuf = _buf;
sl@0
   387
	if (_buf==0)
sl@0
   388
		{
sl@0
   389
		_ourbuf=(char*)User::Alloc(_size);
sl@0
   390
		if (_ourbuf==0)
sl@0
   391
			{
sl@0
   392
			r->_errno=ENOMEM;
sl@0
   393
			return _buf;
sl@0
   394
			}
sl@0
   395
		}
sl@0
   396
sl@0
   397
	//we are dealing with wide characters from here so we need a temporary buffer
sl@0
   398
	wchar_t tmpbuf[KMaxFullName];
sl@0
   399
sl@0
   400
	MSystemInterface& sysIf=Interface(r);
sl@0
   401
	wchar_t * rval = sysIf.getcwd((wchar_t*)tmpbuf, _size, r->_errno);
sl@0
   402
	
sl@0
   403
	if (rval)	//we have a path
sl@0
   404
		{
sl@0
   405
		//convert it to UTF8
sl@0
   406
		size_t x = wcstombs(_ourbuf, tmpbuf, _size);	//convert the buffer
sl@0
   407
		return _ourbuf;
sl@0
   408
		}
sl@0
   409
	//deal with the fact we may have allocated our own buffer
sl@0
   410
	if (_buf != _ourbuf)  //we allocated it.
sl@0
   411
		User::Free(_ourbuf);
sl@0
   412
	return NULL;
sl@0
   413
	}
sl@0
   414
sl@0
   415
/** A wide-character version of reentrant of getcwd().
sl@0
   416
*/
sl@0
   417
EXPORT_C wchar_t * _wgetcwd_r (struct _reent *r, wchar_t *_buf, size_t _size)
sl@0
   418
	{
sl@0
   419
	if (_buf==0)
sl@0
   420
		{
sl@0
   421
		_buf=(wchar_t *)User::Alloc(_size*sizeof(wchar_t));
sl@0
   422
		if (_buf==0)
sl@0
   423
			{
sl@0
   424
			r->_errno=ENOMEM;
sl@0
   425
			return _buf;
sl@0
   426
			}
sl@0
   427
		}
sl@0
   428
	MSystemInterface& sysIf=Interface(r);
sl@0
   429
	return sysIf.getcwd(_buf,_size,r->_errno);
sl@0
   430
	}
sl@0
   431
sl@0
   432
/**
sl@0
   433
Changes the current working directory to be pathname. The current directory is the
sl@0
   434
beginning point for file searches when path names are not absolute. 
sl@0
   435
If the chdir() function fails, the current working directory remains unchanged.
sl@0
   436
sl@0
   437
@param _path Is the path name of a directory.
sl@0
   438
sl@0
   439
@return On Success, returns 0. 
sl@0
   440
		On Failure, returns -1, errno may be set.
sl@0
   441
*/
sl@0
   442
EXPORT_C int chdir (const char *_path)
sl@0
   443
	{
sl@0
   444
	struct _reent *r = _REENT2;
sl@0
   445
	if (!r)
sl@0
   446
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   447
	return _chdir_r(r, _path);
sl@0
   448
	}
sl@0
   449
sl@0
   450
/** A reentrant version of chdir().
sl@0
   451
*/
sl@0
   452
EXPORT_C int _chdir_r (struct _reent *r, const char *_path)
sl@0
   453
	{
sl@0
   454
	//we need to use a wide buffer and convert
sl@0
   455
	wchar_t tmpbuf[KMaxFullName+1];		//use the max path length possible
sl@0
   456
	if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
sl@0
   457
		{
sl@0
   458
		MSystemInterface& sysIf=Interface(r);
sl@0
   459
		return sysIf.chdir(tmpbuf, r->_errno);
sl@0
   460
		}
sl@0
   461
	MapError(EILSEQ, r->_errno);		
sl@0
   462
	return -1;
sl@0
   463
	}
sl@0
   464
sl@0
   465
/** A wide-character version of chdir().
sl@0
   466
*/
sl@0
   467
EXPORT_C int wchdir (const wchar_t *_path)
sl@0
   468
	{
sl@0
   469
	struct _reent *r = _REENT2;
sl@0
   470
	if (!r)
sl@0
   471
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   472
	return _wchdir_r(r, _path);
sl@0
   473
	}
sl@0
   474
sl@0
   475
/** A reentrant version of wchdir().
sl@0
   476
*/
sl@0
   477
EXPORT_C int _wchdir_r (struct _reent *r, const wchar_t *_path)
sl@0
   478
	{
sl@0
   479
	MSystemInterface& sysIf=Interface(r);
sl@0
   480
	return sysIf.chdir(_path,r->_errno);
sl@0
   481
	}
sl@0
   482
sl@0
   483
/**
sl@0
   484
Removes an empty directory whose name is given by pathname.
sl@0
   485
The directory must not have any entries other than dot (.) and dot-dot (..).
sl@0
   486
sl@0
   487
@param _path Points to the directory that the rmdir() function removes.
sl@0
   488
sl@0
   489
@return On Success, returns 0. 
sl@0
   490
		On Failure, returns -1, errno may be set.
sl@0
   491
*/
sl@0
   492
EXPORT_C int rmdir (const char *_path)
sl@0
   493
	{
sl@0
   494
	struct _reent *r = _REENT2;
sl@0
   495
	if (!r)
sl@0
   496
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   497
	return _rmdir_r(r, _path);
sl@0
   498
	}
sl@0
   499
sl@0
   500
/** A reentrant version of rmdir().
sl@0
   501
*/
sl@0
   502
EXPORT_C int _rmdir_r (struct _reent *r, const char *_path)
sl@0
   503
	{
sl@0
   504
	wchar_t tmpbuf[KMaxFullName+1];		//use the max path length possible
sl@0
   505
	if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
sl@0
   506
		{
sl@0
   507
		MSystemInterface& sysIf=Interface(r);
sl@0
   508
		return sysIf.rmdir(tmpbuf, r->_errno);
sl@0
   509
		}
sl@0
   510
	MapError(EILSEQ, r->_errno);		
sl@0
   511
	return -1;
sl@0
   512
	}
sl@0
   513
sl@0
   514
/** A wide-character version of rmdir().
sl@0
   515
*/
sl@0
   516
EXPORT_C int wrmdir (const wchar_t *_path)
sl@0
   517
	{
sl@0
   518
	struct _reent *r = _REENT2;
sl@0
   519
	if (!r)
sl@0
   520
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   521
	return _wrmdir_r(r,_path);
sl@0
   522
	}
sl@0
   523
sl@0
   524
/** A reentrant version of wrmdir().
sl@0
   525
*/
sl@0
   526
EXPORT_C int _wrmdir_r (struct _reent *r, const wchar_t *_path)
sl@0
   527
	{
sl@0
   528
	MSystemInterface& sysIf=Interface(r);
sl@0
   529
	return sysIf.rmdir(_path,r->_errno);
sl@0
   530
	}
sl@0
   531
sl@0
   532
/**
sl@0
   533
Creates a new directory with the specified path name. 
sl@0
   534
The file permissions of the new directory are initialized from the specified mode. 
sl@0
   535
sl@0
   536
@param _path Specifies the name of the new directory. The path name can be absolute or relative. 
sl@0
   537
	   If the specified path name is relative, the directory is created based upon your current
sl@0
   538
	   working directory.
sl@0
   539
@param _mode Is a bitwise-OR field that specifies what permissions the directory has when it is created.
sl@0
   540
sl@0
   541
@return On Success, returns 0. 
sl@0
   542
		On Failure, returns -1, errno may be set. Does not create a directory.
sl@0
   543
*/
sl@0
   544
EXPORT_C int mkdir (const char *_path, mode_t _mode)
sl@0
   545
	{
sl@0
   546
	struct _reent *r = _REENT2;
sl@0
   547
	if (!r)
sl@0
   548
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   549
	return _mkdir_r(r,_path,_mode);
sl@0
   550
	}
sl@0
   551
sl@0
   552
/** A reentrant version of mkdir().
sl@0
   553
*/
sl@0
   554
EXPORT_C int _mkdir_r (struct _reent *r, const char *_path, mode_t _mode)
sl@0
   555
	{
sl@0
   556
	//we need to use a wide buffer and convert
sl@0
   557
	wchar_t tmpbuf[KMaxFullName+1];		//use the max path length possible
sl@0
   558
	if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
sl@0
   559
		{
sl@0
   560
		MSystemInterface& sysIf=Interface(r);
sl@0
   561
		return sysIf.mkdir(tmpbuf, _mode, r->_errno);
sl@0
   562
		}
sl@0
   563
	MapError(EILSEQ, r->_errno);		
sl@0
   564
	return -1;
sl@0
   565
	}
sl@0
   566
sl@0
   567
/** A wide-character version of mkdir().
sl@0
   568
*/
sl@0
   569
EXPORT_C int wmkdir (const wchar_t *_path, mode_t _mode)
sl@0
   570
	{
sl@0
   571
	struct _reent *r = _REENT2;
sl@0
   572
	if (!r)
sl@0
   573
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   574
	return _wmkdir_r(r, _path, _mode);
sl@0
   575
	}
sl@0
   576
sl@0
   577
/** A reentrant version of wmkdir().
sl@0
   578
*/
sl@0
   579
EXPORT_C int _wmkdir_r (struct _reent *r, const wchar_t *_path, mode_t _mode)
sl@0
   580
	{
sl@0
   581
	MSystemInterface& sysIf=Interface(r);
sl@0
   582
	return sysIf.mkdir(_path,_mode,r->_errno);
sl@0
   583
	}
sl@0
   584
sl@0
   585
/**
sl@0
   586
Sets the access permissions for the file whose name is given by pathname to the bit
sl@0
   587
pattern contained in mode. For this call to succeed, the effective user ID of the
sl@0
   588
process must match the owner of the file, or the process must have appropriate privileges. 
sl@0
   589
The owner of the file pathname always has privileges to change permission modes and file attributes.
sl@0
   590
sl@0
   591
@param _path Points to the name of the file.
sl@0
   592
@param _mode Is a bitwise-or field that specifies the new permission modes for path name.
sl@0
   593
sl@0
   594
@return On Success, returns 0. 
sl@0
   595
		On Failure, returns -1, errno may be set.
sl@0
   596
*/
sl@0
   597
EXPORT_C int chmod (const char *_path, mode_t _mode)
sl@0
   598
	{
sl@0
   599
	struct _reent *r = _REENT2;
sl@0
   600
	if (!r)
sl@0
   601
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   602
	return _chmod_r(r, _path, _mode);
sl@0
   603
	}
sl@0
   604
sl@0
   605
/** A reentrant version of chmod().
sl@0
   606
*/
sl@0
   607
EXPORT_C int _chmod_r (struct _reent *r, const char *_path, mode_t _mode)
sl@0
   608
	{
sl@0
   609
	wchar_t tmpbuf[KMaxFullName+1];	
sl@0
   610
	if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
sl@0
   611
		{
sl@0
   612
		MSystemInterface& sysIf=Interface(r);
sl@0
   613
		return sysIf.chmod(tmpbuf,_mode,r->_errno);
sl@0
   614
		}
sl@0
   615
	MapError(EILSEQ, r->_errno);		
sl@0
   616
	return -1;
sl@0
   617
	}
sl@0
   618
sl@0
   619
/** A wide-character version of chmod().
sl@0
   620
*/
sl@0
   621
EXPORT_C int wchmod (const wchar_t *_path, mode_t _mode)
sl@0
   622
	{
sl@0
   623
	struct _reent *r = _REENT2;
sl@0
   624
	if (!r)
sl@0
   625
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   626
	return _wchmod_r(r, _path, _mode);
sl@0
   627
	}
sl@0
   628
sl@0
   629
/** A reentrant version of wchmod().
sl@0
   630
*/
sl@0
   631
EXPORT_C int _wchmod_r (struct _reent *r, const wchar_t *_path, mode_t _mode)
sl@0
   632
	{
sl@0
   633
	MSystemInterface& sysIf=Interface(r);
sl@0
   634
	return sysIf.chmod(_path,_mode,r->_errno);
sl@0
   635
	}
sl@0
   636
sl@0
   637
/**
sl@0
   638
Removes a link to a file, and decrements the link count of the referenced file by one. When
sl@0
   639
the file's link count becomes 0 and no process has the file open, the space occupied by the
sl@0
   640
file is freed, and the file is no longer accessible. If one or more processes have the file
sl@0
   641
open when the last link is removed, the link is removed before unlink() returns, but the
sl@0
   642
removal of the file contents is postponed until all references to the file are closed.
sl@0
   643
sl@0
   644
@param _path Points to the path name that names the file to be unlinked.
sl@0
   645
sl@0
   646
@return On Success, returns 0. 
sl@0
   647
		On Failure, returns -1, errno may be set.
sl@0
   648
*/
sl@0
   649
EXPORT_C int unlink (const char *_path)
sl@0
   650
	{
sl@0
   651
	struct _reent *r = _REENT2;
sl@0
   652
	if (!r)
sl@0
   653
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   654
	return _unlink_r(r, _path);
sl@0
   655
	}
sl@0
   656
sl@0
   657
/** A reentrant version of unlink().
sl@0
   658
*/
sl@0
   659
EXPORT_C int _unlink_r (struct _reent *r, const char *_path)
sl@0
   660
	{
sl@0
   661
	wchar_t tmpbuf[KMaxFullName+1];		
sl@0
   662
	if (-1 != mbstowcs(tmpbuf, _path, KMaxFullName))
sl@0
   663
		{
sl@0
   664
		MSystemInterface& sysIf=Interface(r);
sl@0
   665
		return sysIf.unlink(tmpbuf, r->_errno);
sl@0
   666
		}
sl@0
   667
	MapError(EILSEQ, r->_errno);		
sl@0
   668
	return -1;
sl@0
   669
	}
sl@0
   670
sl@0
   671
/** A wide-character version of unlink().
sl@0
   672
*/
sl@0
   673
EXPORT_C int wunlink (const wchar_t *_path)
sl@0
   674
	{
sl@0
   675
	struct _reent *r = _REENT2;
sl@0
   676
	if (!r)
sl@0
   677
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   678
	return _wunlink_r(r, _path);
sl@0
   679
	}
sl@0
   680
sl@0
   681
/** A wide-character version of reentrant of unlink().
sl@0
   682
*/
sl@0
   683
EXPORT_C int _wunlink_r (struct _reent *r, const wchar_t *_path)
sl@0
   684
	{
sl@0
   685
	MSystemInterface& sysIf=Interface(r);
sl@0
   686
	return sysIf.unlink(_path,r->_errno);
sl@0
   687
	}
sl@0
   688
sl@0
   689
/**
sl@0
   690
Renames a file. 
sl@0
   691
sl@0
   692
@param oldpath Points to the path name of the file to be renamed. The path name can be
sl@0
   693
	   absolute or relative. If a relative path name is given, the file is searched from
sl@0
   694
	   the current working directory.
sl@0
   695
@param newpath Points to the path name of the file. The path name can be absolute or relative. 
sl@0
   696
	   If a relative path name is given, the file is searched from the current working directory.
sl@0
   697
sl@0
   698
@return On Success, returns 0. 
sl@0
   699
		On Failure, returns -1, errno may be set. Does not change either the file named
sl@0
   700
		by old or the file named by new (if either exists).
sl@0
   701
*/
sl@0
   702
EXPORT_C int rename (const char *oldpath, const char *newpath)
sl@0
   703
	{
sl@0
   704
	struct _reent *r = _REENT2;
sl@0
   705
	if (!r)
sl@0
   706
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   707
	return _rename_r (r, oldpath, newpath);
sl@0
   708
	}
sl@0
   709
sl@0
   710
/** A reentrant version of rename().
sl@0
   711
*/
sl@0
   712
EXPORT_C int _rename_r (struct _reent *r, const char *oldpath, const char *newpath)
sl@0
   713
	{
sl@0
   714
	wchar_t _old[KMaxFullName+1];		
sl@0
   715
	wchar_t _new[KMaxFullName+1];		
sl@0
   716
	if (-1 != mbstowcs(_old, oldpath, KMaxFullName))
sl@0
   717
		{
sl@0
   718
		if (-1 != mbstowcs(_new, newpath, KMaxFullName))
sl@0
   719
			{
sl@0
   720
			MSystemInterface& sysIf=Interface(r);
sl@0
   721
			return sysIf.rename(_old, _new, r->_errno);
sl@0
   722
			}
sl@0
   723
		}
sl@0
   724
	MapError(EILSEQ, r->_errno);		
sl@0
   725
	return -1;
sl@0
   726
	}
sl@0
   727
sl@0
   728
/** A wide-character version of rename().
sl@0
   729
*/
sl@0
   730
EXPORT_C int wrename (const wchar_t *oldpath, const wchar_t *newpath)
sl@0
   731
	{
sl@0
   732
	struct _reent *r = _REENT2;
sl@0
   733
	if (!r)
sl@0
   734
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
   735
	return _wrename_r (r, oldpath, newpath);
sl@0
   736
	}
sl@0
   737
sl@0
   738
/** A wide-character version of reentrant of rename().
sl@0
   739
*/
sl@0
   740
EXPORT_C int _wrename_r (struct _reent *r, const wchar_t *oldpath, const wchar_t *newpath)
sl@0
   741
	{
sl@0
   742
	MSystemInterface& sysIf=Interface(r);
sl@0
   743
	return sysIf.rename(oldpath,newpath,r->_errno);
sl@0
   744
	}
sl@0
   745
sl@0
   746
/**
sl@0
   747
Takes a specified path name, pathname and resolves all symbolic links,
sl@0
   748
extra slashes (/), and references to /./ and /../. 
sl@0
   749
The resulting absolute path name is placed in the memory location 
sl@0
   750
pointed to by the resolved_path argument.
sl@0
   751
@return resolved_path. 
sl@0
   752
When an error occurs,returns a null pointer, setsresolved_path 
sl@0
   753
to the path name that caused the error.
sl@0
   754
@param path Points to the path name that you want resolved to an absolute form. 
sl@0
   755
This may be either a relative or absolute path name. 
sl@0
   756
All but the final component of this path name must exist when you call realpath().
sl@0
   757
@param resolved Points to the location where the canonical version
sl@0
   758
of pathname is to be placed. 
sl@0
   759
*/
sl@0
   760
EXPORT_C char* realpath (const char* path, char* resolved)
sl@0
   761
	{
sl@0
   762
	return _realpath_r(_REENT, path, resolved);
sl@0
   763
	}
sl@0
   764
sl@0
   765
/** A wide-character version of realpath().
sl@0
   766
*/
sl@0
   767
EXPORT_C wchar_t* wrealpath (const wchar_t* path, wchar_t* resolved)
sl@0
   768
	{
sl@0
   769
	return _wrealpath_r(_REENT, path, resolved);
sl@0
   770
	}
sl@0
   771
sl@0
   772
/** A wide-character version of reentrant of realpath().
sl@0
   773
*/
sl@0
   774
EXPORT_C wchar_t * _wrealpath_r (struct _reent *r, const wchar_t *relpath, wchar_t *resolved)
sl@0
   775
	{
sl@0
   776
sl@0
   777
	TPtr16 name((TText16*)resolved,MAXPATHLEN);
sl@0
   778
	TParse path;
sl@0
   779
	MSystemInterface& sysIf=Interface(r);
sl@0
   780
	TInt err = sysIf.ResolvePath(path, relpath, &name);
sl@0
   781
	if (!err)
sl@0
   782
		{
sl@0
   783
		err = path.SetNoWild(path.DriveAndPath(),NULL,&name);
sl@0
   784
		if (!err)
sl@0
   785
			{
sl@0
   786
			name = path.FullName();
sl@0
   787
			name.ZeroTerminate();
sl@0
   788
			return resolved;
sl@0
   789
			}
sl@0
   790
		}
sl@0
   791
	MapError(err,r->_errno);
sl@0
   792
	return 0;
sl@0
   793
	}
sl@0
   794
sl@0
   795
/** A reentrant version of realpath().
sl@0
   796
*/
sl@0
   797
EXPORT_C char* _realpath_r (struct _reent *r, const char *relpath, char *resolved)
sl@0
   798
	{
sl@0
   799
sl@0
   800
	TFileName name;
sl@0
   801
	TInt err;
sl@0
   802
	
sl@0
   803
	TParse path;
sl@0
   804
	MSystemInterface& sysIf=Interface(r);
sl@0
   805
sl@0
   806
	wchar_t _wrelpath[KMaxFileName];
sl@0
   807
sl@0
   808
	if (-1 != mbstowcs(_wrelpath, relpath , KMaxFileName))
sl@0
   809
		{
sl@0
   810
		err = sysIf.ResolvePath(path, _wrelpath, &name);
sl@0
   811
		if (!err)
sl@0
   812
			{
sl@0
   813
			err = path.SetNoWild(path.DriveAndPath(),NULL,&name);
sl@0
   814
			if (!err)
sl@0
   815
				{
sl@0
   816
				name = path.FullName();
sl@0
   817
sl@0
   818
				if (-1 != wcstombs(resolved, (wchar_t*)name.PtrZ(), KMaxFileName))
sl@0
   819
					return resolved;
sl@0
   820
				else
sl@0
   821
					{
sl@0
   822
					err = EILSEQ;
sl@0
   823
					}
sl@0
   824
				}
sl@0
   825
			}
sl@0
   826
		}
sl@0
   827
		else
sl@0
   828
		{
sl@0
   829
		err = EILSEQ;
sl@0
   830
		}
sl@0
   831
sl@0
   832
	MapError(err,r->_errno);
sl@0
   833
	return 0;
sl@0
   834
	}
sl@0
   835
sl@0
   836
/**
sl@0
   837
Gives access to the client's stdin.
sl@0
   838
sl@0
   839
@return On Success, returns a pointer to an open stream, used to read or write to the pipe.
sl@0
   840
		On Failure, return a null pointer.
sl@0
   841
*/
sl@0
   842
EXPORT_C int popen3 (const char* cmd, const char* mode, char** env, int fids[3])
sl@0
   843
	{
sl@0
   844
    struct _reent *r = _REENT2;
sl@0
   845
	if (!r)
sl@0
   846
		return NULL; // Memory for library globals is not allocated (errno not set).
sl@0
   847
	return _popen3_r (r,cmd,mode,env,fids);
sl@0
   848
	}
sl@0
   849
sl@0
   850
/** A wide-character version of popen3().
sl@0
   851
*/
sl@0
   852
EXPORT_C int wpopen3 (const wchar_t* cmd, const wchar_t* mode, wchar_t** env, int fids[3])
sl@0
   853
	{
sl@0
   854
    struct _reent *r = _REENT2;
sl@0
   855
	if (!r)
sl@0
   856
		return NULL; // Memory for library globals is not allocated (errno not set).
sl@0
   857
	return _wpopen3_r (r,cmd,mode,env,fids);
sl@0
   858
	}
sl@0
   859
sl@0
   860
/** A reentrant version of a popen3().
sl@0
   861
*/
sl@0
   862
EXPORT_C int _popen3_r (struct _reent *r, const char* cmd, const char* mode, char** env, int fids[3])
sl@0
   863
	{
sl@0
   864
sl@0
   865
	wchar_t wcmd[MAXPATHLEN+1];
sl@0
   866
	wchar_t wmode[MAXPATHLEN+1];
sl@0
   867
sl@0
   868
	wchar_t ** wenv = NULL;
sl@0
   869
	wchar_t * buf = NULL;
sl@0
   870
	
sl@0
   871
	TInt ret = 0;
sl@0
   872
sl@0
   873
	if ((-1 != mbstowcs(wcmd, cmd, MAXPATHLEN)) && 
sl@0
   874
		(-1 != mbstowcs(wmode, mode, MAXPATHLEN)))
sl@0
   875
		{
sl@0
   876
		//OK, we've widened the first 2 args
sl@0
   877
		//now for the environment
sl@0
   878
sl@0
   879
		//env is basically an array of char pointers with a NULL as the last one
sl@0
   880
		if (env)
sl@0
   881
			{
sl@0
   882
			//OK we have a ptr to something
sl@0
   883
			//count the number of entries and get their lengths so we can work out how much space
sl@0
   884
			//is needed for the new one
sl@0
   885
sl@0
   886
			TInt count = 0;
sl@0
   887
			TInt total = 0;
sl@0
   888
			while (env[count] != NULL)
sl@0
   889
				{
sl@0
   890
				total+= strlen(env[count])+1;
sl@0
   891
				count++;
sl@0
   892
				}
sl@0
   893
			//total has number of bytes in the strings
sl@0
   894
			//max number of unicode chars is with a 1 to 1 mapping.
sl@0
   895
			wenv = (wchar_t**)malloc(1 + count*sizeof(wchar_t*));
sl@0
   896
			buf = (wchar_t*)malloc(2*total);
sl@0
   897
			
sl@0
   898
			if (!(wenv && buf))		//we've had a malloc failure
sl@0
   899
				{
sl@0
   900
				r->_errno = ENOMEM;
sl@0
   901
				goto bailout;
sl@0
   902
				}
sl@0
   903
sl@0
   904
			wchar_t* p = buf;
sl@0
   905
sl@0
   906
			TInt ret;
sl@0
   907
			for (TInt x = 0; x < count; x++)
sl@0
   908
				{
sl@0
   909
				wenv[count] = p;
sl@0
   910
				ret = mbstowcs(p, env[count], MAXPATHLEN);
sl@0
   911
				if (ret >= 0)
sl@0
   912
					{
sl@0
   913
						p += ret;	//step to next bit of space
sl@0
   914
					}
sl@0
   915
				else
sl@0
   916
					{
sl@0
   917
					r->_errno = EILSEQ;
sl@0
   918
					goto bailout;
sl@0
   919
					}
sl@0
   920
sl@0
   921
				}
sl@0
   922
			}
sl@0
   923
sl@0
   924
sl@0
   925
		ret =  _wpopen3_r(r, wcmd, wmode, wenv, fids);
sl@0
   926
		}
sl@0
   927
	else
sl@0
   928
		{
sl@0
   929
		r->_errno = EILSEQ;
sl@0
   930
		}
sl@0
   931
sl@0
   932
	//don't lose the memory
sl@0
   933
bailout:
sl@0
   934
	free(wenv);
sl@0
   935
	free(buf);
sl@0
   936
sl@0
   937
	return ret;
sl@0
   938
	}
sl@0
   939
sl@0
   940
/** A wide-character version of reentrant of popen3().
sl@0
   941
*/
sl@0
   942
EXPORT_C int _wpopen3_r (struct _reent *r, const wchar_t* cmd, const wchar_t* mode, wchar_t** env, int fids[3])
sl@0
   943
	{
sl@0
   944
	// Find the full path of the thing we are executing...
sl@0
   945
	const wchar_t* cp=cmd;
sl@0
   946
	while (*cp==L' ')
sl@0
   947
		++cp;	// skip leading spaces
sl@0
   948
	wchar_t file[MAXPATHLEN+1];
sl@0
   949
	TInt i=0;
sl@0
   950
	wchar_t c=0;
sl@0
   951
	for (i=0; i<MAXPATHLEN; i++, cp++)
sl@0
   952
		{
sl@0
   953
		c=*cp;
sl@0
   954
		file[i]=c;
sl@0
   955
		if (c==L' ' || c==L'\t' || c==L'\0')	// stop at first space, tab or \0  
sl@0
   956
			break;
sl@0
   957
		}
sl@0
   958
	file[i]=L'\0';
sl@0
   959
	wchar_t resolved[MAXPATHLEN+1];
sl@0
   960
	if(_wrealpath_r(r, file, resolved)==0)
sl@0
   961
		return -1;	// no such file
sl@0
   962
sl@0
   963
	// Strip leading whitespace from the rest of the commandline
sl@0
   964
	for (; i<MAXPATHLEN;i++,cp++)
sl@0
   965
		{
sl@0
   966
		c=*cp;
sl@0
   967
		if (c=='\0')
sl@0
   968
			break;
sl@0
   969
		if ((c!=' ') && (c!='\t'))
sl@0
   970
			break;
sl@0
   971
		}
sl@0
   972
sl@0
   973
	fids[0]=-2;
sl@0
   974
	fids[1]=-2;
sl@0
   975
	fids[2]=-2;
sl@0
   976
	const wchar_t* mp=mode;
sl@0
   977
	while (*mp)
sl@0
   978
		{
sl@0
   979
		wchar_t c=*mp++;
sl@0
   980
		if (c==L'r')
sl@0
   981
			fids[0]=-1;
sl@0
   982
		else if (c==L'w')
sl@0
   983
			fids[1]=-1;
sl@0
   984
		else if (c==L'e')
sl@0
   985
			fids[2]=-1;
sl@0
   986
		}
sl@0
   987
sl@0
   988
	MSystemInterface& sysIf=Interface(r);
sl@0
   989
	return sysIf.popen3(resolved,cp,mode,env,fids,r->_errno);
sl@0
   990
	}
sl@0
   991
sl@0
   992
/**
sl@0
   993
Lets the calling process obtain status information about one of its child processes.
sl@0
   994
If status information is available for two or more child processes, the order in
sl@0
   995
which their status is reported is unspecified.
sl@0
   996
sl@0
   997
@param pid Specifies a set of child processes for which the status is requested
sl@0
   998
@param status Specifies the location to which the child process' exit status is stored.
sl@0
   999
@param options Is the bitwise inclusive-OR of zero or more of the following flags.
sl@0
  1000
sl@0
  1001
@return On Success, returns a value equal to the process ID of the child process.
sl@0
  1002
		On Failure, returns -1 and errno may be set OR returns 0 if the status is not available
sl@0
  1003
		for the specified process and it's set not to hang in the options.
sl@0
  1004
*/
sl@0
  1005
EXPORT_C int waitpid (int pid, int* status, int options)
sl@0
  1006
	{
sl@0
  1007
	struct _reent *r = _REENT2;
sl@0
  1008
	if (!r)
sl@0
  1009
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
  1010
	return _waitpid_r (r, pid, status, options);
sl@0
  1011
	}
sl@0
  1012
sl@0
  1013
/** A reentrant version of waitpid().
sl@0
  1014
*/
sl@0
  1015
EXPORT_C int _waitpid_r (struct _reent *r, int pid, int* status, int options)
sl@0
  1016
	{
sl@0
  1017
	MSystemInterface& sysIf=Interface(r);
sl@0
  1018
	return sysIf.waitpid(pid,status,options,r->_errno);
sl@0
  1019
	}
sl@0
  1020
sl@0
  1021
/**
sl@0
  1022
Calls reentrant version of waitpid().
sl@0
  1023
*/
sl@0
  1024
EXPORT_C int wait (int* status)
sl@0
  1025
	{
sl@0
  1026
	struct _reent *r = _REENT2;
sl@0
  1027
	if (!r)
sl@0
  1028
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
  1029
	return _waitpid_r (r, -1, status, 0);
sl@0
  1030
	}
sl@0
  1031
sl@0
  1032
/** A reentrant version of wait().
sl@0
  1033
*/
sl@0
  1034
EXPORT_C int _wait_r (struct _reent *r, int* status)
sl@0
  1035
	{
sl@0
  1036
	return _waitpid_r (r,-1,status,0);
sl@0
  1037
	}
sl@0
  1038
sl@0
  1039
/**
sl@0
  1040
Execute command.
sl@0
  1041
sl@0
  1042
@param cmd Null-terminated string containing the system command to be executed.
sl@0
  1043
sl@0
  1044
@return On Success, the command interpreter returns an adequate value; generally 0
sl@0
  1045
        indicates that the action performed by the command interpreter terminated
sl@0
  1046
        with no errors. 
sl@0
  1047
        On Failure, return -1.
sl@0
  1048
*/
sl@0
  1049
EXPORT_C int system (const char* cmd)
sl@0
  1050
	{
sl@0
  1051
	struct _reent *r = _REENT2;
sl@0
  1052
	if (!r)
sl@0
  1053
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
  1054
	return _system_r (r, cmd);
sl@0
  1055
	}
sl@0
  1056
sl@0
  1057
/** A reentrant version of system().
sl@0
  1058
*/
sl@0
  1059
EXPORT_C int _system_r (struct _reent *r, const char* cmd)
sl@0
  1060
	{
sl@0
  1061
	if (cmd==0)
sl@0
  1062
		return 1;	// special case, says that we do support system().
sl@0
  1063
	int fids[3];
sl@0
  1064
	int pid=_popen3_r(r, cmd, "", 0, fids);
sl@0
  1065
	if (pid<0)
sl@0
  1066
		return -1;
sl@0
  1067
	int status=0;
sl@0
  1068
	pid=_waitpid_r (r,pid,&status,0);
sl@0
  1069
	if (pid<0)
sl@0
  1070
		return -1;
sl@0
  1071
	return status;
sl@0
  1072
	}
sl@0
  1073
sl@0
  1074
/** A wide-character version of a system().
sl@0
  1075
*/
sl@0
  1076
EXPORT_C int wsystem (const wchar_t* cmd)
sl@0
  1077
	{
sl@0
  1078
	struct _reent *r = _REENT2;
sl@0
  1079
	if (!r)
sl@0
  1080
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
  1081
	return _wsystem_r (r, cmd);
sl@0
  1082
	}
sl@0
  1083
sl@0
  1084
/** A wide-character version of reentrant of system().
sl@0
  1085
*/
sl@0
  1086
EXPORT_C int _wsystem_r (struct _reent *r, const wchar_t* cmd)
sl@0
  1087
	{
sl@0
  1088
	if (cmd==0)
sl@0
  1089
		return 1;	// special case, says that we do support system().
sl@0
  1090
	int fids[3];
sl@0
  1091
	int pid=_wpopen3_r(r, cmd, (wchar_t*)L"", 0, fids);
sl@0
  1092
	if (pid<0)
sl@0
  1093
		return -1;
sl@0
  1094
	int status=0;
sl@0
  1095
	pid=_waitpid_r (r,pid,&status,0);
sl@0
  1096
	if (pid<0)
sl@0
  1097
		return -1;
sl@0
  1098
	return status;
sl@0
  1099
	}
sl@0
  1100
sl@0
  1101
} // extern "C"
sl@0
  1102
sl@0
  1103
#include <estlib.h>
sl@0
  1104
sl@0
  1105
/** Dubious asynchronous interface to ioctl, must be called from C++
sl@0
  1106
sl@0
  1107
@return On Success, returns a value other than -1.
sl@0
  1108
		On Failure, returns -1 and errno may be set.
sl@0
  1109
*/
sl@0
  1110
EXPORT_C int ioctl (int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
sl@0
  1111
	{
sl@0
  1112
	struct _reent *r = _REENT2;
sl@0
  1113
	if (!r)
sl@0
  1114
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
  1115
	return _ioctl_r(r, aFid, aCmd, aParam, aStatus);
sl@0
  1116
	}
sl@0
  1117
sl@0
  1118
/** A reentrant version of a ioctl().
sl@0
  1119
*/
sl@0
  1120
EXPORT_C int _ioctl_r (struct _reent *r, int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
sl@0
  1121
	{
sl@0
  1122
	MSystemInterface& sysIf=Interface(r);
sl@0
  1123
	return sysIf.ioctl(aFid,aCmd,aParam,aStatus,r->_errno);
sl@0
  1124
	}
sl@0
  1125
sl@0
  1126
EXPORT_C int ioctl_complete (int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
sl@0
  1127
	{
sl@0
  1128
	struct _reent *r = _REENT2;
sl@0
  1129
	if (!r)
sl@0
  1130
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
  1131
	return _ioctl_complete_r(r, aFid, aCmd, aParam, aStatus);
sl@0
  1132
	}
sl@0
  1133
sl@0
  1134
/** A reentrant version of a ioctl_complete().
sl@0
  1135
*/
sl@0
  1136
EXPORT_C int _ioctl_complete_r (struct _reent *r, int aFid, int aCmd, void* aParam, TRequestStatus& aStatus)
sl@0
  1137
	{
sl@0
  1138
	MSystemInterface& sysIf=Interface(r);
sl@0
  1139
	return sysIf.ioctl_complete(aFid,aCmd,aParam,aStatus,r->_errno);
sl@0
  1140
	}
sl@0
  1141
sl@0
  1142
EXPORT_C int ioctl_cancel (int aFid)
sl@0
  1143
	{
sl@0
  1144
	struct _reent *r = _REENT2;
sl@0
  1145
	if (!r)
sl@0
  1146
		return -1; // Memory for library globals is not allocated (errno not set).
sl@0
  1147
	return _ioctl_cancel_r(r, aFid);
sl@0
  1148
	}
sl@0
  1149
sl@0
  1150
/** A reentrant version of a ioctl_cancel().
sl@0
  1151
*/
sl@0
  1152
EXPORT_C int _ioctl_cancel_r (struct _reent *r, int aFid)
sl@0
  1153
	{
sl@0
  1154
	MSystemInterface& sysIf=Interface(r);
sl@0
  1155
	return sysIf.ioctl_cancel(aFid,r->_errno);
sl@0
  1156
	}
sl@0
  1157