xref: /haiku/src/libs/posix_error_mapper/misc.cpp (revision baa26c5b4c6b16bdf9402e98e27fa88f06c4aa8a)
139d58e2fSIngo Weinhold /*
239d58e2fSIngo Weinhold  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
339d58e2fSIngo Weinhold  * Distributed under the terms of the MIT License.
439d58e2fSIngo Weinhold  */
539d58e2fSIngo Weinhold 
639d58e2fSIngo Weinhold #include <stdio.h>
739d58e2fSIngo Weinhold #include <string.h>
839d58e2fSIngo Weinhold 
939d58e2fSIngo Weinhold #include "posix_error_mapper.h"
1039d58e2fSIngo Weinhold 
1139d58e2fSIngo Weinhold 
1239d58e2fSIngo Weinhold static int*
real_errnop()1339d58e2fSIngo Weinhold real_errnop()
1439d58e2fSIngo Weinhold {
1539d58e2fSIngo Weinhold 	GET_REAL_FUNCTION(int*, _errnop, (void));
1639d58e2fSIngo Weinhold 	return sReal__errnop();
1739d58e2fSIngo Weinhold }
1839d58e2fSIngo Weinhold 
1939d58e2fSIngo Weinhold 
2039d58e2fSIngo Weinhold int*
_errnop(void)2139d58e2fSIngo Weinhold _errnop(void)
2239d58e2fSIngo Weinhold {
2339d58e2fSIngo Weinhold 	HIDDEN_FUNCTION(_errnop);
2439d58e2fSIngo Weinhold 
2539d58e2fSIngo Weinhold 	// convert errno to positive error code
2639d58e2fSIngo Weinhold 	int* error = real_errnop();
2739d58e2fSIngo Weinhold 	if (*error < 0)
2839d58e2fSIngo Weinhold 		*error = B_TO_POSITIVE_ERROR(*error);
2939d58e2fSIngo Weinhold 	return error;
3039d58e2fSIngo Weinhold }
3139d58e2fSIngo Weinhold 
3239d58e2fSIngo Weinhold 
3339d58e2fSIngo Weinhold 
3439d58e2fSIngo Weinhold WRAPPER_FUNCTION(char*, strerror, (int errorCode),
3539d58e2fSIngo Weinhold 	return sReal_strerror(B_TO_NEGATIVE_ERROR(errorCode));
3639d58e2fSIngo Weinhold )
3739d58e2fSIngo Weinhold 
3839d58e2fSIngo Weinhold 
3939d58e2fSIngo Weinhold WRAPPER_FUNCTION(int, strerror_r,
4039d58e2fSIngo Weinhold 		(int errorCode, char* buffer, size_t bufferSize),
4139d58e2fSIngo Weinhold 	return sReal_strerror_r(B_TO_NEGATIVE_ERROR(errorCode), buffer, bufferSize);
4239d58e2fSIngo Weinhold )
4339d58e2fSIngo Weinhold 
4439d58e2fSIngo Weinhold 
4539d58e2fSIngo Weinhold WRAPPER_FUNCTION(void, perror, (const char* errorPrefix),
4639d58e2fSIngo Weinhold 	// convert errno to negative error code
4739d58e2fSIngo Weinhold 	int* error = real_errnop();
4839d58e2fSIngo Weinhold 	int oldError = *error;
49*baa26c5bSIngo Weinhold 	if (*error > 0)
5039d58e2fSIngo Weinhold 		*error = B_TO_NEGATIVE_ERROR(*error);
5139d58e2fSIngo Weinhold 
5239d58e2fSIngo Weinhold 	// call the real perror()
5339d58e2fSIngo Weinhold 	sReal_perror(errorPrefix);
5439d58e2fSIngo Weinhold 
5539d58e2fSIngo Weinhold 	// reset errno
5639d58e2fSIngo Weinhold 	*error = oldError;
5739d58e2fSIngo Weinhold )
58