1/*-
2 * $Id: brew_db.in,v 1.5 2006/11/15 13:36:48 bostic Exp $
3 *
4 * The following provides the information necessary to build Berkeley DB
5 * on BREW.
6 */
7
8#include <AEEAppGen.h>
9#include <AEEShell.h>
10#include <AEEFile.h>
11#include <AEEStdLib.h>
12
13#include "errno.h"
14#include "db.h"
15#include "clib_port.h"
16
17/*
18 * BREW doesn't have stdio.
19 */
20#define	EOF	(-1)			/* Declare stdio's EOF. */
21#define	stderr	((IFile *)1)		/* Flag to call DBGPRINTF. */
22#define	stdout	((IFile *)1)
23
24/*
25 * POSIX time structure/function compatibility.
26 *
27 * !!!
28 * This is likely wrong, we should probably move all Berkeley DB time-specific
29 * functionality into os/.
30 */
31struct tm {
32	int tm_sec;     /* seconds after the minute - [0,59] */
33	int tm_min;     /* minutes after the hour - [0,59] */
34	int tm_hour;    /* hours since midnight - [0,23] */
35	int tm_mday;    /* day of the month - [1,31] */
36	int tm_mon;     /* months since January - [0,11] */
37	int tm_year;    /* years since 1900 */
38	int tm_wday;    /* days since Sunday - [0,6] */
39	int tm_yday;    /* days since January 1 - [0,365] */
40	int tm_isdst;   /* daylight savings time flag */
41	/*
42	 * We don't provide tm_zone or tm_gmtoff because BREW doesn't
43	 * provide them.
44	 */
45};
46
47/*
48* The ctime() function converts the time pointed to by clock, representing
49* time in seconds since the Epoch to local time in the form of a string.
50*
51* Berkeley DB uses POSIX time values internally.
52*
53* The POSIX time function returns seconds since the Epoch, which was
54* 1970/01/01 00:00:00 UTC.
55*
56* BREW's GETUTCSECONDS() returns the number of seconds since
57* 1980/01/06 00:00:00 UTC.
58*
59* To convert from BREW to POSIX, add the seconds between 1970 and 1980 plus
60* 6 more days.
61*/
62#define	BREW_EPOCH_OFFSET	(315964800L)
63
64/*
65 * Map ANSI C library functions to BREW specific APIs.
66 */
67#define	atoi(a)			ATOI(a)
68#define	free(a)			FREE(a)
69#define	malloc(a)		MALLOC(a)
70#define	memcmp(a, b, c)		MEMCMP(a, b, c)
71#define	memmove(a, b, c)	MEMMOVE(a, b, c)
72#define	memset(a, b, c)		MEMSET(a, b, c)
73#define	realloc(a, b)		REALLOC(a, b)
74#define	snprintf		SNPRINTF
75#define	sprintf			SPRINTF
76#define	strcat(a, b)		STRCAT(a, b)
77#define	strchr(a, b)		STRCHR(a, b)
78#define	strcmp(a, b)		STRCMP(a, b)
79#define	strcpy(a, b)		STRCPY(a, b)
80#define	strdup(a)		STRDUP(a)
81#define	strlen(a)		STRLEN(a)
82#define	strncmp(a, b, c)	STRNCMP(a, b, c)
83#define	strncpy(a, b, c)	STRNCPY(a, b, c)
84#define	strrchr(a, b)		STRRCHR(a, b)
85#define	strtoul(a, b, c)	STRTOUL(a, b, c)
86#define	vsnprintf(a, b, c, d)	VSNPRINTF(a, b, c, d)
87
88/*
89 * !!!
90 * Don't #define memcpy to MEMCPY, even though it exists, because that results
91 * in a C pre-processor loop and compile failure.
92 *
93 * Don't #define memcpy to MEMMOVE directly, that results in failure as well.
94 */
95#define	memcpy			memmove
96
97/*
98 * BREW does not have concept of 'sync'.
99 *
100 * It depends on the implementation of the BREW on various platforms, but
101 * Mobilus confirms the version of BREW that ships to North America in 3G
102 * models has sync features guaranteeing safe physical writes whenever a
103 * write is performed using BREW API. Therefore, the issue is not on the
104 * applications running top of BREW, but the implementation of BREW itself
105 * that has to be checked in order to provide durability.
106 */
107#define	__os_fsync(a, b)	(0)
108#define	fflush(a)		(0)
109
110/*
111 * The ANSI C library functions for which we wrote local versions.
112 */
113int	   fclose(FILE *);
114int	   fgetc(FILE *);
115char	  *fgets(char *, int, FILE *);
116FILE	  *fopen(const char *, const char *);
117size_t	   fwrite(const void *, size_t, size_t, FILE *);
118char	  *getcwd(char *, size_t);
119struct tm *localtime(const time_t *);
120time_t	   time(time_t *);
121
122/*
123 * FILE_MANAGER_CREATE --
124 *	Instantiate file manager instance.
125 */
126#define	FILE_MANAGER_CREATE(dbenv, mgr, ret) do {			\
127	AEEApplet *__app = (AEEApplet *)GETAPPINSTANCE();		\
128	int __ret;							\
129	if ((__ret = ISHELL_CreateInstance(__app->m_pIShell,		\
130	    AEECLSID_FILEMGR, (void **)&(mgr))) == SUCCESS)		\
131		ret = 0;						\
132	else {								\
133		__db_syserr(dbenv, __ret, "ISHELL_CreateInstance");	\
134		ret = __os_posix_err(__ret);				\
135	}								\
136} while (0)
137
138/*
139 * FILE_MANAGER_ERR --
140 *	Handle file manager method error.
141 */
142#define	FILE_MANAGER_ERR(dbenv, mgr, name, op, ret) do {		\
143	int __ret;							\
144	__ret = IFILEMGR_GetLastError(mgr);				\
145	if ((name) == NULL)						\
146		__db_syserr(dbenv, __ret, "%s", op);			\
147	else								\
148		__db_syserr(dbenv, __ret, "%s: %s", name, op);		\
149	(ret) = __os_posix_err(__ret);					\
150} while (0)
151