xref: /haiku/src/system/libroot/posix/errno.c (revision 24df65921befcd0ad0c5c7866118f922da61cb96)
15af32e75SAxel Dörfler /*
25af32e75SAxel Dörfler ** Copyright 2003-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
35af32e75SAxel Dörfler ** Distributed under the terms of the MIT License.
45af32e75SAxel Dörfler */
55af32e75SAxel Dörfler 
65af32e75SAxel Dörfler /* Provides user space storage for "errno", located in TLS
75af32e75SAxel Dörfler  */
85af32e75SAxel Dörfler 
95af32e75SAxel Dörfler #include <errno.h>
105af32e75SAxel Dörfler 
115af32e75SAxel Dörfler #include "support/TLS.h"
125af32e75SAxel Dörfler #include "tls.h"
135af32e75SAxel Dörfler 
145af32e75SAxel Dörfler 
155af32e75SAxel Dörfler int *
_errnop(void)165af32e75SAxel Dörfler _errnop(void)
175af32e75SAxel Dörfler {
185af32e75SAxel Dörfler 	return (int *)tls_address(TLS_ERRNO_SLOT);
195af32e75SAxel Dörfler }
205af32e75SAxel Dörfler 
215af32e75SAxel Dörfler 
225af32e75SAxel Dörfler // This is part of the Linuxbase binary specification
235af32e75SAxel Dörfler // and is referenced by some code in libgcc.a.
245af32e75SAxel Dörfler // ToDo: maybe we even want to include this always
255af32e75SAxel Dörfler #ifdef __linux__
265af32e75SAxel Dörfler extern int *(*__errno_location)(void) __attribute__ ((weak, alias("_errnop")));
275af32e75SAxel Dörfler #endif
285af32e75SAxel Dörfler 
2939d58e2fSIngo Weinhold 
3039d58e2fSIngo Weinhold // #pragma mark -
3139d58e2fSIngo Weinhold 
3239d58e2fSIngo Weinhold 
3339d58e2fSIngo Weinhold int
_to_positive_error(int error)3439d58e2fSIngo Weinhold _to_positive_error(int error)
3539d58e2fSIngo Weinhold {
3639d58e2fSIngo Weinhold 	if (error < 0)
37*24df6592SIngo Weinhold 		return error == B_NO_MEMORY ? -B_POSIX_ENOMEM : -error;
3839d58e2fSIngo Weinhold 	return error;
3939d58e2fSIngo Weinhold }
4039d58e2fSIngo Weinhold 
4139d58e2fSIngo Weinhold 
4239d58e2fSIngo Weinhold int
_to_negative_error(int error)4339d58e2fSIngo Weinhold _to_negative_error(int error)
4439d58e2fSIngo Weinhold {
4539d58e2fSIngo Weinhold 	return error > 0 ? -error : error;
4639d58e2fSIngo Weinhold }
47