os/ossrv/genericopenlibs/openenvcore/libc/src/getcwd.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description:
sl@0
    15
*/
sl@0
    16
/*
sl@0
    17
* ==============================================================================
sl@0
    18
*  Name        : getcwd.cpp
sl@0
    19
*  Part of     : libc library
sl@0
    20
*  Description : gets current working directory.
sl@0
    21
*
sl@0
    22
* ============================================================================
sl@0
    23
*/
sl@0
    24
sl@0
    25
#include "sysif.h"
sl@0
    26
#include <sys/errno.h> //error codes (ENOMEM, EINVAL)
sl@0
    27
#include "sysreent.h"
sl@0
    28
#include <string.h> //strlen
sl@0
    29
#include <stdlib.h>
sl@0
    30
sl@0
    31
extern "C" {
sl@0
    32
sl@0
    33
/*
sl@0
    34
Gets the path name of the current working directory.
sl@0
    35
If a buffer is specified, the path name is placed in that buffer,
sl@0
    36
and the address of the buffer is returned. 
sl@0
    37
*/
sl@0
    38
EXPORT_C char* getcwd (char *_buf, size_t _size)
sl@0
    39
	{
sl@0
    40
	
sl@0
    41
        if (_buf != 0 && _size == 0)
sl@0
    42
		{
sl@0
    43
		errno = EINVAL;
sl@0
    44
		return NULL;
sl@0
    45
		}
sl@0
    46
	
sl@0
    47
	char * _ourbuf = _buf;	
sl@0
    48
	if (_buf==0)
sl@0
    49
		{
sl@0
    50
		if (_size >= (KMaxTInt/2))
sl@0
    51
			{
sl@0
    52
			errno=ENOMEM;
sl@0
    53
			return _buf;
sl@0
    54
			}
sl@0
    55
		//coverity[alloc_fn]
sl@0
    56
		//coverity[assign]
sl@0
    57
		_ourbuf = (char *)	malloc(_size);
sl@0
    58
		if (_ourbuf == 0)
sl@0
    59
			{
sl@0
    60
			errno=ENOMEM;
sl@0
    61
			return _buf;
sl@0
    62
			}
sl@0
    63
		}
sl@0
    64
sl@0
    65
   //we are dealing with wide characters from here so we need a temporary buffer
sl@0
    66
	wchar_t tmpbuf[KMaxFullName];
sl@0
    67
	//coverity[leave_without_push]
sl@0
    68
	wchar_t *cret = _getcwd_r(&errno, tmpbuf, _size);
sl@0
    69
sl@0
    70
	if (cret)	//we have a path
sl@0
    71
		{
sl@0
    72
		//convert it to UTF8
sl@0
    73
		size_t x = wcstombs(_ourbuf, tmpbuf, _size);	//convert the buffer
sl@0
    74
		
sl@0
    75
		//remove the trailing backslash
sl@0
    76
              int cwd_len = strlen(_ourbuf);
sl@0
    77
		if(_ourbuf[cwd_len - 1] == '\\')
sl@0
    78
			_ourbuf[cwd_len - 1] = '\0';
sl@0
    79
		
sl@0
    80
		return _ourbuf;
sl@0
    81
		}
sl@0
    82
sl@0
    83
	//deal with the fact we may have allocated our own buffer
sl@0
    84
	if (_buf != _ourbuf)  //we allocated it.
sl@0
    85
		{
sl@0
    86
			free(_ourbuf);
sl@0
    87
       	}
sl@0
    88
       	
sl@0
    89
	return NULL;
sl@0
    90
	}
sl@0
    91
sl@0
    92
} // extern "C"