summaryrefslogtreecommitdiff
path: root/src/os_posix/os_errno.c
blob: b0cb288ea74f46f7996c21dfcc39c9318956fa2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*-
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * __wt_errno --
 *	Return errno, or WT_ERROR if errno not set.
 */
int
__wt_errno(void)
{
	/*
	 * Called when we know an error occurred, and we want the system
	 * error code, but there's some chance it's not set.
	 */
	return (errno == 0 ? WT_ERROR : errno);
}

/*
 * __wt_strerror_r --
 *	POSIX implementation of strerror_r.
 */
int
__wt_strerror_r(int error, char *buf, size_t buflen)
{
	char *p;

	/* Require at least 2 bytes, printable character and trailing nul. */
	if (buflen < 2)
		return (ENOMEM);

	/*
	 * POSIX errors are non-negative integers, copy the string into the
	 * user's buffer. Return success if anything printed (we checked if
	 * the buffer had space for at least one character).
	 */
	if (error > 0 &&
	    (p = strerror(error)) != NULL && snprintf(buf, buflen, "%s", p) > 0)
		return (0);

	/* Fallback to a generic message, then guess it's a memory problem. */
	return (
	    snprintf(buf, buflen, "error return: %d", error) > 0 ? 0 : ENOMEM);
}