sl@0: /* sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: */ sl@0: /* sl@0: * ============================================================================== sl@0: * Name : getcwd.cpp sl@0: * Part of : libc library sl@0: * Description : gets current working directory. sl@0: * sl@0: * ============================================================================ sl@0: */ sl@0: sl@0: #include "sysif.h" sl@0: #include //error codes (ENOMEM, EINVAL) sl@0: #include "sysreent.h" sl@0: #include //strlen sl@0: #include sl@0: sl@0: extern "C" { sl@0: sl@0: /* sl@0: Gets the path name of the current working directory. sl@0: If a buffer is specified, the path name is placed in that buffer, sl@0: and the address of the buffer is returned. sl@0: */ sl@0: EXPORT_C char* getcwd (char *_buf, size_t _size) sl@0: { sl@0: sl@0: if (_buf != 0 && _size == 0) sl@0: { sl@0: errno = EINVAL; sl@0: return NULL; sl@0: } sl@0: sl@0: char * _ourbuf = _buf; sl@0: if (_buf==0) sl@0: { sl@0: if (_size >= (KMaxTInt/2)) sl@0: { sl@0: errno=ENOMEM; sl@0: return _buf; sl@0: } sl@0: //coverity[alloc_fn] sl@0: //coverity[assign] sl@0: _ourbuf = (char *) malloc(_size); sl@0: if (_ourbuf == 0) sl@0: { sl@0: errno=ENOMEM; sl@0: return _buf; sl@0: } sl@0: } sl@0: sl@0: //we are dealing with wide characters from here so we need a temporary buffer sl@0: wchar_t tmpbuf[KMaxFullName]; sl@0: //coverity[leave_without_push] sl@0: wchar_t *cret = _getcwd_r(&errno, tmpbuf, _size); sl@0: sl@0: if (cret) //we have a path sl@0: { sl@0: //convert it to UTF8 sl@0: size_t x = wcstombs(_ourbuf, tmpbuf, _size); //convert the buffer sl@0: sl@0: //remove the trailing backslash sl@0: int cwd_len = strlen(_ourbuf); sl@0: if(_ourbuf[cwd_len - 1] == '\\') sl@0: _ourbuf[cwd_len - 1] = '\0'; sl@0: sl@0: return _ourbuf; sl@0: } sl@0: sl@0: //deal with the fact we may have allocated our own buffer sl@0: if (_buf != _ourbuf) //we allocated it. sl@0: { sl@0: free(_ourbuf); sl@0: } sl@0: sl@0: return NULL; sl@0: } sl@0: sl@0: } // extern "C"