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
22 #include <dpl/errno_string.h>
23 #include <dpl/assert.h>
24 #include <dpl/exception.h>
25 #include <dpl/assert.h>
26 #include <dpl/scoped_free.h>
36 namespace // anonymous
38 const size_t DEFAULT_ERRNO_STRING_SIZE = 32;
39 } // namespace anonymous
41 std::string GetErrnoString(int error)
43 size_t size = DEFAULT_ERRNO_STRING_SIZE;
48 // Add one extra characted for end of string null value
49 char *newBuffer = static_cast<char *>(::realloc(buffer, size + 1));
55 throw std::bad_alloc();
58 // Setup reallocated buffer
60 ::memset(buffer, 0, size + 1);
62 // Try to retrieve error string
63 #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
64 // The XSI-compliant version of strerror_r() is provided if:
65 int result = ::strerror_r(error, buffer, size);
69 ScopedFree<char> scopedBufferFree(buffer);
70 return std::string(buffer);
75 // Otherwise, the GNU-specific version is provided.
76 char *result = ::strerror_r(error, buffer, size);
80 ScopedFree<char> scopedBufferFree(buffer);
81 return std::string(result);
89 // We got an invalid errno value
91 ThrowMsg(InvalidErrnoValue, "Invalid errno value: " << error);
94 // Incease buffer size and retry
99 Assert(0 && "Invalid errno value after call to strerror_r!");