1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1999,2008 Oracle.  All rights reserved.
5 *
6 * $Id: os_errno.c,v 12.9 2008/01/08 20:58:43 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13/*
14 * __os_get_errno_ret_zero --
15 *	Return the last system error, including an error of zero.
16 *
17 * PUBLIC: int __os_get_errno_ret_zero __P((void));
18 */
19int
20__os_get_errno_ret_zero()
21{
22	/* This routine must be able to return the same value repeatedly. */
23	return (errno);
24}
25
26/*
27 * We've seen cases where system calls failed but errno was never set.  For
28 * that reason, __os_get_errno() and __os_get_syserr set errno to EAGAIN if
29 * it's not already set, to work around the problem.  For obvious reasons,
30 * we can only call this function if we know an error has occurred, that
31 * is, we can't test the return for a non-zero value after the get call.
32 *
33 * __os_get_errno --
34 *	Return the last ANSI C "errno" value or EAGAIN if the last error
35 *	is zero.
36 *
37 * PUBLIC: int __os_get_errno __P((void));
38 */
39int
40__os_get_errno()
41{
42	/* This routine must be able to return the same value repeatedly. */
43	return (__os_get_syserr());
44}
45
46#if 0
47/*
48 * __os_get_neterr --
49 *      Return the last network-related error or EAGAIN if the last
50 *	error is zero.
51 *
52 * PUBLIC: int __os_get_neterr __P((void));
53 */
54int
55__os_get_neterr()
56{
57	/* This routine must be able to return the same value repeatedly. */
58	return (__os_get_syserr());
59}
60#endif
61
62/*
63 * __os_get_syserr --
64 *	Return the last system error or EAGAIN if the last error is zero.
65 *
66 * PUBLIC: int __os_get_syserr __P((void));
67 */
68int
69__os_get_syserr()
70{
71	/* This routine must be able to return the same value repeatedly. */
72	if (errno == 0)
73		__os_set_errno(EAGAIN);
74	return (errno);
75}
76
77/*
78 * __os_set_errno --
79 *	Set the value of errno.
80 *
81 * PUBLIC: void __os_set_errno __P((int));
82 */
83void
84__os_set_errno(evalue)
85	int evalue;
86{
87	/*
88	 * This routine is called by the compatibility interfaces (DB 1.85,
89	 * dbm and hsearch).  Force values > 0, that is, not one of DB 2.X
90	 * and later's public error returns.  If something bad has happened,
91	 * default to EFAULT -- a nasty return.  Otherwise, default to EINVAL.
92	 * As the compatibility APIs aren't included on Windows, the Windows
93	 * version of this routine doesn't need this behavior.
94	 */
95	errno =
96	    evalue >= 0 ? evalue : (evalue == DB_RUNRECOVERY ? EFAULT : EINVAL);
97}
98
99/*
100 * __os_strerror --
101 *	Return a string associated with the system error.
102 *
103 * PUBLIC: char *__os_strerror __P((int, char *, size_t));
104 */
105char *
106__os_strerror(error, buf, len)
107	int error;
108	char *buf;
109	size_t len;
110{
111	/* No translation is needed in the POSIX layer. */
112	(void)strncpy(buf, strerror(error), len - 1);
113	buf[len - 1] = '\0';
114
115	return (buf);
116}
117
118/*
119 * __os_posix_err
120 *	Convert a system error to a POSIX error.
121 *
122 * PUBLIC: int __os_posix_err __P((int));
123 */
124int
125__os_posix_err(error)
126	int error;
127{
128	return (error);
129}
130