os/ossrv/genericopenlibs/openenvcore/libc/src/getcwd.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/getcwd.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,92 @@
     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:
    1.18 +*/
    1.19 +/*
    1.20 +* ==============================================================================
    1.21 +*  Name        : getcwd.cpp
    1.22 +*  Part of     : libc library
    1.23 +*  Description : gets current working directory.
    1.24 +*
    1.25 +* ============================================================================
    1.26 +*/
    1.27 +
    1.28 +#include "sysif.h"
    1.29 +#include <sys/errno.h> //error codes (ENOMEM, EINVAL)
    1.30 +#include "sysreent.h"
    1.31 +#include <string.h> //strlen
    1.32 +#include <stdlib.h>
    1.33 +
    1.34 +extern "C" {
    1.35 +
    1.36 +/*
    1.37 +Gets the path name of the current working directory.
    1.38 +If a buffer is specified, the path name is placed in that buffer,
    1.39 +and the address of the buffer is returned. 
    1.40 +*/
    1.41 +EXPORT_C char* getcwd (char *_buf, size_t _size)
    1.42 +	{
    1.43 +	
    1.44 +        if (_buf != 0 && _size == 0)
    1.45 +		{
    1.46 +		errno = EINVAL;
    1.47 +		return NULL;
    1.48 +		}
    1.49 +	
    1.50 +	char * _ourbuf = _buf;	
    1.51 +	if (_buf==0)
    1.52 +		{
    1.53 +		if (_size >= (KMaxTInt/2))
    1.54 +			{
    1.55 +			errno=ENOMEM;
    1.56 +			return _buf;
    1.57 +			}
    1.58 +		//coverity[alloc_fn]
    1.59 +		//coverity[assign]
    1.60 +		_ourbuf = (char *)	malloc(_size);
    1.61 +		if (_ourbuf == 0)
    1.62 +			{
    1.63 +			errno=ENOMEM;
    1.64 +			return _buf;
    1.65 +			}
    1.66 +		}
    1.67 +
    1.68 +   //we are dealing with wide characters from here so we need a temporary buffer
    1.69 +	wchar_t tmpbuf[KMaxFullName];
    1.70 +	//coverity[leave_without_push]
    1.71 +	wchar_t *cret = _getcwd_r(&errno, tmpbuf, _size);
    1.72 +
    1.73 +	if (cret)	//we have a path
    1.74 +		{
    1.75 +		//convert it to UTF8
    1.76 +		size_t x = wcstombs(_ourbuf, tmpbuf, _size);	//convert the buffer
    1.77 +		
    1.78 +		//remove the trailing backslash
    1.79 +              int cwd_len = strlen(_ourbuf);
    1.80 +		if(_ourbuf[cwd_len - 1] == '\\')
    1.81 +			_ourbuf[cwd_len - 1] = '\0';
    1.82 +		
    1.83 +		return _ourbuf;
    1.84 +		}
    1.85 +
    1.86 +	//deal with the fact we may have allocated our own buffer
    1.87 +	if (_buf != _ourbuf)  //we allocated it.
    1.88 +		{
    1.89 +			free(_ourbuf);
    1.90 +       	}
    1.91 +       	
    1.92 +	return NULL;
    1.93 +	}
    1.94 +
    1.95 +} // extern "C"