2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file errno_string.h
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of errno string
23 #include <dpl/errno_string.h>
24 #include <dpl/assert.h>
25 #include <dpl/exception.h>
26 #include <dpl/assert.h>
27 #include <dpl/scoped_free.h>
37 namespace // anonymous
39 const size_t DEFAULT_ERRNO_STRING_SIZE = 32;
40 } // namespace anonymous
42 std::string GetErrnoString(int error)
44 size_t size = DEFAULT_ERRNO_STRING_SIZE;
49 // Add one extra characted for end of string null value
50 char *newBuffer = static_cast<char *>(::realloc(buffer, size + 1));
56 throw std::bad_alloc();
59 // Setup reallocated buffer
61 ::memset(buffer, 0, size + 1);
63 // Try to retrieve error string
64 #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
65 // The XSI-compliant version of strerror_r() is provided if:
66 int result = ::strerror_r(error, buffer, size);
70 ScopedFree<char> scopedBufferFree(buffer);
71 return std::string(buffer);
76 // Otherwise, the GNU-specific version is provided.
77 char *result = ::strerror_r(error, buffer, size);
81 ScopedFree<char> scopedBufferFree(buffer);
82 return std::string(result);
90 // We got an invalid errno value
92 ThrowMsg(InvalidErrnoValue, "Invalid errno value: " << error);
95 // Incease buffer size and retry
100 Assert(0 && "Invalid errno value after call to strerror_r!");