2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-02-25 01:05:47 +03:00
|
|
|
// $Id$
|
2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2013-04-12 21:26:27 +04:00
|
|
|
// Copyright (C) 2001-2013 The Bochs Project
|
2001-04-10 06:02:05 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
2009-08-22 23:30:23 +04:00
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2009-01-16 21:18:59 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2001-04-10 06:02:05 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// osdep.cc
|
2008-02-06 01:57:43 +03:00
|
|
|
//
|
2001-04-10 06:02:05 +04:00
|
|
|
// Provide definition of library functions that are missing on various
|
|
|
|
// systems. The only reason this is a .cc file rather than a .c file
|
|
|
|
// is so that it can include bochs.h. Bochs.h includes all the required
|
2008-02-06 01:57:43 +03:00
|
|
|
// system headers, with appropriate #ifdefs for different compilers and
|
2001-04-10 06:02:05 +04:00
|
|
|
// platforms.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "bochs.h"
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2008-02-06 01:57:43 +03:00
|
|
|
// Missing library functions. These should work on any platform
|
2001-04-10 06:02:05 +04:00
|
|
|
// that needs them.
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if !BX_HAVE_SNPRINTF
|
merge in BRANCH-io-cleanup.
To see the commit logs for this use either cvsweb or
cvs update -r BRANCH-io-cleanup and then 'cvs log' the various files.
In general this provides a generic interface for logging.
logfunctions:: is a class that is inherited by some classes, and also
. allocated as a standalone global called 'genlog'. All logging uses
. one of the ::info(), ::error(), ::ldebug(), ::panic() methods of this
. class through 'BX_INFO(), BX_ERROR(), BX_DEBUG(), BX_PANIC()' macros
. respectively.
.
. An example usage:
. BX_INFO(("Hello, World!\n"));
iofunctions:: is a class that is allocated once by default, and assigned
as the iofunction of each logfunctions instance. It is this class that
maintains the file descriptor and other output related code, at this
point using vfprintf(). At some future point, someone may choose to
write a gui 'console' for bochs to which messages would be redirected
simply by assigning a different iofunction class to the various logfunctions
objects.
More cleanup is coming, but this works for now. If you want to see alot
of debugging output, in main.cc, change onoff[LOGLEV_DEBUG]=0 to =1.
Comments, bugs, flames, to me: todd@fries.net
2001-05-15 18:49:57 +04:00
|
|
|
/* XXX use real snprintf */
|
2001-04-10 06:02:05 +04:00
|
|
|
/* if they don't have snprintf, just use sprintf */
|
|
|
|
int bx_snprintf (char *s, size_t maxlen, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list arg;
|
|
|
|
int done;
|
|
|
|
|
|
|
|
va_start (arg, format);
|
|
|
|
done = vsprintf (s, format, arg);
|
|
|
|
va_end (arg);
|
|
|
|
|
|
|
|
return done;
|
|
|
|
}
|
2004-02-08 13:22:29 +03:00
|
|
|
|
2001-04-10 06:02:05 +04:00
|
|
|
#endif /* !BX_HAVE_SNPRINTF */
|
|
|
|
|
2004-02-08 13:22:29 +03:00
|
|
|
#if !BX_HAVE_VSNPRINTF
|
|
|
|
int bx_vsnprintf (char *s, size_t maxlen, const char *format, va_list arg)
|
|
|
|
{
|
|
|
|
return vsprintf (s, format, arg);
|
|
|
|
}
|
|
|
|
#endif /* !BX_HAVE_VSNPRINTF*/
|
2001-04-10 06:02:05 +04:00
|
|
|
|
2001-05-17 11:00:42 +04:00
|
|
|
#if (!BX_HAVE_STRTOULL && !BX_HAVE_STRTOUQ)
|
2008-02-06 01:57:43 +03:00
|
|
|
/* taken from glibc-2.2.2: strtod.c, and stripped down a lot. There are
|
|
|
|
still a few leftover references to decimal points and exponents,
|
2001-04-10 06:02:05 +04:00
|
|
|
but it works for bases 10 and 16 */
|
|
|
|
|
|
|
|
#define RETURN(val,end) \
|
|
|
|
do { if (endptr != NULL) *endptr = (char *) (end); \
|
|
|
|
return val; } while (0)
|
|
|
|
|
2009-05-03 23:21:38 +04:00
|
|
|
Bit64u bx_strtoull (const char *nptr, char **endptr, int baseignore)
|
2001-04-10 06:02:05 +04:00
|
|
|
{
|
|
|
|
int negative; /* The sign of the number. */
|
|
|
|
int exponent; /* Exponent of the number. */
|
|
|
|
|
|
|
|
/* Numbers starting `0X' or `0x' have to be processed with base 16. */
|
|
|
|
int base = 10;
|
|
|
|
|
|
|
|
/* Number of bits currently in result value. */
|
|
|
|
int bits;
|
|
|
|
|
|
|
|
/* Running pointer after the last character processed in the string. */
|
|
|
|
const char *cp, *tp;
|
|
|
|
/* Start of significant part of the number. */
|
|
|
|
const char *startp, *start_of_digits;
|
|
|
|
/* Total number of digit and number of digits in integer part. */
|
|
|
|
int dig_no;
|
|
|
|
/* Contains the last character read. */
|
|
|
|
char c;
|
|
|
|
|
2001-06-28 17:51:41 +04:00
|
|
|
Bit64s n = 0;
|
2001-04-10 06:02:05 +04:00
|
|
|
char const *p;
|
|
|
|
|
|
|
|
/* Prepare number representation. */
|
|
|
|
exponent = 0;
|
|
|
|
negative = 0;
|
|
|
|
bits = 0;
|
|
|
|
|
|
|
|
/* Parse string to get maximal legal prefix. We need the number of
|
|
|
|
characters of the integer part, the fractional part and the exponent. */
|
|
|
|
cp = nptr - 1;
|
|
|
|
/* Ignore leading white space. */
|
|
|
|
do
|
|
|
|
c = *++cp;
|
|
|
|
while (isspace (c));
|
|
|
|
|
|
|
|
/* Get sign of the result. */
|
|
|
|
if (c == '-')
|
2009-05-03 23:21:38 +04:00
|
|
|
{
|
|
|
|
negative = 1;
|
|
|
|
c = *++cp;
|
|
|
|
}
|
2001-04-10 06:02:05 +04:00
|
|
|
else if (c == '+')
|
|
|
|
c = *++cp;
|
|
|
|
|
|
|
|
if (c < '0' || c > '9')
|
2009-05-03 23:21:38 +04:00
|
|
|
{
|
|
|
|
/* It is really a text we do not recognize. */
|
|
|
|
RETURN (0, nptr);
|
|
|
|
}
|
2001-04-10 06:02:05 +04:00
|
|
|
|
|
|
|
/* First look whether we are faced with a hexadecimal number. */
|
|
|
|
if (c == '0' && tolower (cp[1]) == 'x')
|
2009-05-03 23:21:38 +04:00
|
|
|
{
|
|
|
|
/* Okay, it is a hexa-decimal number. Remember this and skip
|
|
|
|
the characters. BTW: hexadecimal numbers must not be
|
|
|
|
grouped. */
|
|
|
|
base = 16;
|
|
|
|
cp += 2;
|
|
|
|
c = *cp;
|
|
|
|
}
|
2001-04-10 06:02:05 +04:00
|
|
|
|
|
|
|
/* Record the start of the digits, in case we will check their grouping. */
|
|
|
|
start_of_digits = startp = cp;
|
|
|
|
|
|
|
|
/* Ignore leading zeroes. This helps us to avoid useless computations. */
|
|
|
|
while (c == '0')
|
|
|
|
c = *++cp;
|
|
|
|
|
|
|
|
/* If no other digit but a '0' is found the result is 0.0.
|
|
|
|
Return current read pointer. */
|
|
|
|
if ((c < '0' || c > '9')
|
|
|
|
&& (base == 16 && (c < tolower ('a') || c > tolower ('f')))
|
|
|
|
&& (base == 16 && (cp == start_of_digits || tolower (c) != 'p'))
|
|
|
|
&& (base != 16 && tolower (c) != 'e'))
|
|
|
|
{
|
|
|
|
tp = start_of_digits;
|
|
|
|
/* If TP is at the start of the digits, there was no correctly
|
|
|
|
grouped prefix of the string; so no number found. */
|
|
|
|
RETURN (0, tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember first significant digit and read following characters until the
|
|
|
|
decimal point, exponent character or any non-FP number character. */
|
|
|
|
startp = cp;
|
|
|
|
dig_no = 0;
|
|
|
|
while (1)
|
2009-05-03 23:21:38 +04:00
|
|
|
{
|
2001-04-10 06:02:05 +04:00
|
|
|
if ((c >= '0' && c <= '9')
|
|
|
|
|| (base == 16 && tolower (c) >= 'a' && tolower (c) <= 'f'))
|
|
|
|
++dig_no;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
c = *++cp;
|
2009-05-03 23:21:38 +04:00
|
|
|
}
|
2001-04-10 06:02:05 +04:00
|
|
|
|
|
|
|
/* The whole string is parsed. Store the address of the next character. */
|
|
|
|
if (endptr)
|
|
|
|
*endptr = (char *) cp;
|
|
|
|
|
2008-02-06 01:57:43 +03:00
|
|
|
if (dig_no == 0)
|
2001-04-10 06:02:05 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (p=start_of_digits; p!=cp; p++) {
|
2001-06-28 17:51:41 +04:00
|
|
|
n = n * (Bit64s)base;
|
2001-04-10 06:02:05 +04:00
|
|
|
c = tolower (*p);
|
|
|
|
c = (c >= 'a') ? (10+c-'a') : c-'0';
|
2001-06-28 17:51:41 +04:00
|
|
|
n = n + (Bit64s)c;
|
2001-04-10 06:02:05 +04:00
|
|
|
//printf ("after shifting in digit %c, n is %lld\n", *p, n);
|
|
|
|
}
|
|
|
|
return negative? -n : n;
|
|
|
|
}
|
|
|
|
#endif /* !BX_HAVE_STRTOULL */
|
|
|
|
|
|
|
|
#if BX_TEST_STRTOULL_MAIN
|
|
|
|
/* test driver for strtoull. Do not compile by default. */
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
char buf[256], *endbuf;
|
|
|
|
long l;
|
2001-06-28 17:51:41 +04:00
|
|
|
Bit64s ll;
|
2001-04-10 06:02:05 +04:00
|
|
|
while (1) {
|
|
|
|
printf ("Enter a long int: ");
|
2015-01-07 19:17:40 +03:00
|
|
|
fgets (buf, sizeof(buf), stdin);
|
2001-04-10 06:02:05 +04:00
|
|
|
l = strtoul (buf, &endbuf, 10);
|
|
|
|
printf ("As a long, %ld\n", l);
|
|
|
|
printf ("Endbuf is at buf[%d]\n", endbuf-buf);
|
2009-05-03 23:21:38 +04:00
|
|
|
ll = bx_strtoull(buf, &endbuf, 10);
|
2001-04-10 06:02:05 +04:00
|
|
|
printf ("As a long long, %lld\n", ll);
|
|
|
|
printf ("Endbuf is at buf[%d]\n", endbuf-buf);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* BX_TEST_STRTOULL_MAIN */
|
|
|
|
|
|
|
|
#if !BX_HAVE_STRDUP
|
merge in BRANCH-io-cleanup.
To see the commit logs for this use either cvsweb or
cvs update -r BRANCH-io-cleanup and then 'cvs log' the various files.
In general this provides a generic interface for logging.
logfunctions:: is a class that is inherited by some classes, and also
. allocated as a standalone global called 'genlog'. All logging uses
. one of the ::info(), ::error(), ::ldebug(), ::panic() methods of this
. class through 'BX_INFO(), BX_ERROR(), BX_DEBUG(), BX_PANIC()' macros
. respectively.
.
. An example usage:
. BX_INFO(("Hello, World!\n"));
iofunctions:: is a class that is allocated once by default, and assigned
as the iofunction of each logfunctions instance. It is this class that
maintains the file descriptor and other output related code, at this
point using vfprintf(). At some future point, someone may choose to
write a gui 'console' for bochs to which messages would be redirected
simply by assigning a different iofunction class to the various logfunctions
objects.
More cleanup is coming, but this works for now. If you want to see alot
of debugging output, in main.cc, change onoff[LOGLEV_DEBUG]=0 to =1.
Comments, bugs, flames, to me: todd@fries.net
2001-05-15 18:49:57 +04:00
|
|
|
/* XXX use real strdup */
|
2001-04-10 06:02:05 +04:00
|
|
|
char *bx_strdup(const char *str)
|
|
|
|
{
|
2009-05-03 23:21:38 +04:00
|
|
|
char *temp = (char*)malloc(strlen(str)+1);
|
|
|
|
sprintf(temp, "%s", str);
|
|
|
|
return temp;
|
2008-02-06 01:57:43 +03:00
|
|
|
|
2009-05-03 23:21:38 +04:00
|
|
|
// Well, I'm sure this isn't how strdup is REALLY implemented,
|
|
|
|
// but it works...
|
2001-04-10 06:02:05 +04:00
|
|
|
}
|
|
|
|
#endif /* !BX_HAVE_STRDUP */
|
|
|
|
|
2003-01-11 01:32:46 +03:00
|
|
|
#if !BX_HAVE_STRREV
|
|
|
|
char *bx_strrev(char *str)
|
|
|
|
{
|
|
|
|
char *p1, *p2;
|
|
|
|
|
|
|
|
if (! str || ! *str)
|
|
|
|
return str;
|
|
|
|
|
|
|
|
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
|
|
|
|
*p1 ^= *p2;
|
|
|
|
*p2 ^= *p1;
|
|
|
|
*p1 ^= *p2;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
#endif /* !BX_HAVE_STRREV */
|
|
|
|
|
2004-02-07 01:28:00 +03:00
|
|
|
#if BX_WITH_MACOS
|
|
|
|
namespace std{extern "C" {char *mktemp(char *tpl);}}
|
|
|
|
#endif
|
2003-05-07 00:28:13 +04:00
|
|
|
#if !BX_HAVE_MKSTEMP
|
|
|
|
int bx_mkstemp(char *tpl)
|
|
|
|
{
|
|
|
|
mktemp(tpl);
|
|
|
|
return ::open(tpl, O_RDWR | O_CREAT | O_TRUNC
|
|
|
|
# ifdef O_BINARY
|
|
|
|
| O_BINARY
|
|
|
|
# endif
|
|
|
|
, S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP);
|
|
|
|
}
|
|
|
|
#endif // !BX_HAVE_MKSTEMP
|
|
|
|
|
2001-04-10 06:02:05 +04:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Missing library functions, implemented for MacOS only
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if BX_WITH_MACOS
|
|
|
|
// these functions are part of MacBochs. They are not intended to be
|
|
|
|
// portable!
|
2002-12-12 18:29:45 +03:00
|
|
|
#include <Devices.h>
|
2001-04-10 06:02:05 +04:00
|
|
|
#include <Files.h>
|
|
|
|
#include <Disks.h>
|
|
|
|
|
|
|
|
int fd_read(char *buffer, Bit32u offset, Bit32u bytes)
|
|
|
|
{
|
2009-05-03 23:21:38 +04:00
|
|
|
OSErr err;
|
|
|
|
IOParam param;
|
|
|
|
|
|
|
|
param.ioRefNum=-5; // Refnum of the floppy disk driver
|
|
|
|
param.ioVRefNum=1;
|
|
|
|
param.ioPosMode=fsFromStart;
|
|
|
|
param.ioPosOffset=offset;
|
|
|
|
param.ioBuffer=buffer;
|
|
|
|
param.ioReqCount=bytes;
|
|
|
|
err = PBReadSync((union ParamBlockRec *)(¶m));
|
|
|
|
return param.ioActCount;
|
2001-04-10 06:02:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int fd_write(char *buffer, Bit32u offset, Bit32u bytes)
|
|
|
|
{
|
2009-05-03 23:21:38 +04:00
|
|
|
OSErr err;
|
|
|
|
IOParam param;
|
|
|
|
|
|
|
|
param.ioRefNum=-5; // Refnum of the floppy disk driver
|
|
|
|
param.ioVRefNum=1;
|
|
|
|
param.ioPosMode=fsFromStart;
|
|
|
|
param.ioPosOffset=offset;
|
|
|
|
param.ioBuffer=buffer;
|
|
|
|
param.ioReqCount=bytes;
|
|
|
|
err = PBWriteSync((union ParamBlockRec *)(¶m));
|
|
|
|
return param.ioActCount;
|
2001-04-10 06:02:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int fd_stat(struct stat *buf)
|
|
|
|
{
|
2009-05-03 23:21:38 +04:00
|
|
|
OSErr err;
|
|
|
|
DrvSts status;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
err = DriveStatus(1, &status);
|
|
|
|
if (status.diskInPlace <1 || status.diskInPlace > 2)
|
|
|
|
result = -1;
|
|
|
|
buf->st_mode = S_IFCHR;
|
|
|
|
return result;
|
2001-04-10 06:02:05 +04:00
|
|
|
}
|
|
|
|
#endif /* BX_WITH_MACOS */
|
2002-09-23 06:20:52 +04:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2013-04-12 21:26:27 +04:00
|
|
|
// Missing library functions, implemented for MorphOS only
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifdef __MORPHOS__
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
typedef unsigned int u_int32_t;
|
|
|
|
typedef unsigned short u_int16_t;
|
|
|
|
typedef unsigned char u_int8_t;
|
|
|
|
|
|
|
|
int fseeko(FILE *stream, off_t offset, int whence)
|
|
|
|
{
|
|
|
|
while(offset != (long) offset)
|
|
|
|
{
|
|
|
|
long pos = (offset < 0) ? LONG_MIN : LONG_MAX;
|
|
|
|
if(fseek(stream, pos, whence) != 0)
|
|
|
|
return -1;
|
|
|
|
offset -= pos;
|
|
|
|
whence = SEEK_CUR;
|
|
|
|
}
|
|
|
|
return fseek(stream, (long) offset, whence);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tm *localtime_r(const time_t *timep, struct tm *result)
|
|
|
|
{
|
|
|
|
struct tm *s = localtime(timep);
|
|
|
|
if(s == NULL)
|
|
|
|
return NULL;
|
|
|
|
*result = *s;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2002-09-23 06:20:52 +04:00
|
|
|
// New functions to replace library functions
|
|
|
|
// with OS-independent versions
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if BX_HAVE_REALTIME_USEC
|
2006-08-07 21:59:15 +04:00
|
|
|
#if defined(WIN32)
|
|
|
|
static Bit64u last_realtime64_top = 0;
|
|
|
|
static Bit64u last_realtime64_bottom = 0;
|
|
|
|
|
|
|
|
Bit64u bx_get_realtime64_usec(void)
|
|
|
|
{
|
2002-12-05 20:38:00 +03:00
|
|
|
Bit64u new_bottom = ((Bit64u) GetTickCount()) & BX_CONST64(0x0FFFFFFFF);
|
2002-12-04 22:51:51 +03:00
|
|
|
if(new_bottom < last_realtime64_bottom) {
|
2002-12-05 20:38:00 +03:00
|
|
|
last_realtime64_top += BX_CONST64(0x0000000100000000);
|
2002-12-04 22:51:51 +03:00
|
|
|
}
|
|
|
|
last_realtime64_bottom = new_bottom;
|
|
|
|
Bit64u interim_realtime64 =
|
2002-12-05 20:38:00 +03:00
|
|
|
(last_realtime64_top & BX_CONST64(0xFFFFFFFF00000000)) |
|
|
|
|
(new_bottom & BX_CONST64(0x00000000FFFFFFFF));
|
|
|
|
return interim_realtime64*(BX_CONST64(1000));
|
2002-12-04 22:51:51 +03:00
|
|
|
}
|
2006-08-07 21:59:15 +04:00
|
|
|
#elif BX_HAVE_GETTIMEOFDAY
|
|
|
|
Bit64u bx_get_realtime64_usec(void)
|
|
|
|
{
|
|
|
|
timeval thetime;
|
|
|
|
gettimeofday(&thetime,0);
|
|
|
|
Bit64u mytime;
|
|
|
|
mytime=(Bit64u)thetime.tv_sec*(Bit64u)1000000+(Bit64u)thetime.tv_usec;
|
|
|
|
return mytime;
|
|
|
|
}
|
|
|
|
#endif
|
2002-09-23 06:20:52 +04:00
|
|
|
#endif
|