Update contrib.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
17 * ==============================================================================
19 * Part of : libc library
20 * Description : gets current working directory.
22 * ============================================================================
26 #include <sys/errno.h> //error codes (ENOMEM, EINVAL)
28 #include <string.h> //strlen
34 Gets the path name of the current working directory.
35 If a buffer is specified, the path name is placed in that buffer,
36 and the address of the buffer is returned.
38 EXPORT_C char* getcwd (char *_buf, size_t _size)
41 if (_buf != 0 && _size == 0)
47 char * _ourbuf = _buf;
50 if (_size >= (KMaxTInt/2))
57 _ourbuf = (char *) malloc(_size);
65 //we are dealing with wide characters from here so we need a temporary buffer
66 wchar_t tmpbuf[KMaxFullName];
67 //coverity[leave_without_push]
68 wchar_t *cret = _getcwd_r(&errno, tmpbuf, _size);
70 if (cret) //we have a path
73 size_t x = wcstombs(_ourbuf, tmpbuf, _size); //convert the buffer
75 //remove the trailing backslash
76 int cwd_len = strlen(_ourbuf);
77 if(_ourbuf[cwd_len - 1] == '\\')
78 _ourbuf[cwd_len - 1] = '\0';
83 //deal with the fact we may have allocated our own buffer
84 if (_buf != _ourbuf) //we allocated it.