libcimp.c
来自「适合KS8695X」· C语言 代码 · 共 828 行 · 第 1/2 页
C
828 行
/****************************************************************************
*
* SciTech MGL Graphics Library
*
* ========================================================================
*
* The contents of this file are subject to the SciTech MGL Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.scitechsoft.com/mgl-license.txt
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
*
* The Initial Developer of the Original Code is SciTech Software, Inc.
* All Rights Reserved.
*
* ========================================================================
*
* Language: ANSI C
* Environment: Any
*
* Description: Module to implement a the OS specific side of the Binary
* Portable DLL C runtime library. The functions in here
* are imported into the Binary Portable DLL's to implement
* OS specific services.
*
****************************************************************************/
#include "pmapi.h"
#if defined(__WIN32_VXD__) || defined(__NT_DRIVER__)
#include "drvlib/peloader.h"
#include "drvlib/attrib.h"
#include "drvlib/libc/init.h"
#define __BUILDING_PE_LOADER__
#include "drvlib/libc/file.h"
#if defined(__WIN32_VXD__)
#include "vxdfile.h"
#endif
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <time.h>
#include <signal.h>
#include <fcntl.h>
#if defined(__GNUC__) || defined(__UNIX__)
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#else
#include <io.h>
#endif
#include "drvlib/attrib.h"
#include "drvlib/libc/init.h"
#define __BUILDING_PE_LOADER__
#include "drvlib/libc/file.h"
#if defined(__WINDOWS__) || defined(TNT) || defined(__RTTARGET__)
#define WIN32_LEAN_AND_MEAN
#define STRICT
#include <windows.h>
#endif
#ifdef __MSDOS__
#include <dos.h>
#endif
#ifdef __OS2__
#define INCL_DOS
#define INCL_DOSERRORS
#define INCL_SUB
#include <os2.h>
#endif
#endif
/* No text or binary modes for Unix */
#ifndef O_BINARY
#define O_BINARY 0
#define O_TEXT 0
#endif
/*--------------------------- Global variables ----------------------------*/
#if defined(__WIN32_VXD__) || defined(__NT_DRIVER__)
#define MAX_FILES 16
static FILE *openHandles[MAX_FILES] = {NULL};
#endif
/* <stdlib.h> stub functions */
void _CDECL stub_abort(void);
int _CDECL stub_atexit(void (*)(void));
void * _CDECL stub_calloc(size_t _nelem, size_t _size);
void _CDECL stub_exit(int _status);
void _CDECL stub_free(void *_ptr);
char * _CDECL stub_getenv(const char *_name);
void * _CDECL stub_malloc(size_t _size);
void * _CDECL stub_realloc(void *_ptr, size_t _size);
int _CDECL stub_system(const char *_s);
int _CDECL stub_putenv(const char *_val);
/* <libc/file.h> stub functions */
int _CDECL stub_open(const char *_path, int _oflag, unsigned _mode);
int _CDECL stub_access(const char *_path, int _amode);
int _CDECL stub_close(int _fildes);
off_t _CDECL stub_lseek(int _fildes, off_t _offset, int _whence);
size_t _CDECL stub_read(int _fildes, void *_buf, size_t _nbyte);
int _CDECL stub_unlink(const char *_path);
size_t _CDECL stub_write(int _fildes, const void *_buf, size_t _nbyte);
int _CDECL stub_isatty(int _fildes);
/* <stdio.h> stub functions */
int _CDECL stub_remove(const char *_filename);
int _CDECL stub_rename(const char *_old, const char *_new);
/* <time.h> stub functions */
time_t _CDECL stub_time(time_t *_tod);
/* <signal.h> stub functions */
int _CDECL stub_raise(int);
void * _CDECL stub_signal(int, void *);
/* <drvlib/attrib.h> functions */
#define stub_OS_setfileattr _OS_setfileattr
#define stub_OS_getcurrentdate _OS_getcurrentdate
LIBC_imports _VARAPI ___imports = {
sizeof(LIBC_imports),
/* <stdlib.h> exports */
stub_abort,
stub_atexit,
stub_calloc,
stub_exit,
stub_free,
stub_getenv,
stub_malloc,
stub_realloc,
stub_system,
stub_putenv,
/* <libc/file.h> exports */
stub_open,
stub_access,
stub_close,
stub_lseek,
stub_read,
stub_unlink,
stub_write,
stub_isatty,
/* <stdio.h> exports */
stub_remove,
stub_rename,
/* <signal.h> functions */
stub_raise,
stub_signal,
/* <time.h> exports */
stub_time,
/* <drvlib/attrib.h> exports */
stub_OS_setfileattr,
stub_OS_getcurrentdate,
};
/*---------------------- Stub function implementation ---------------------*/
/* <stdlib.h> stub functions */
void _CDECL stub_abort(void)
{
#if !defined( __WIN32_VXD__) && !defined(__NT_DRIVER__)
abort();
#endif
}
int _CDECL stub_atexit(void (*func)(void))
{
#if !defined( __WIN32_VXD__) && !defined(__NT_DRIVER__)
return atexit((void(*)(void))func);
#else
return -1;
#endif
}
void * _CDECL stub_calloc(size_t _nelem, size_t _size)
{ return __PM_calloc(_nelem,_size); }
void _CDECL stub_exit(int _status)
{
#if !defined( __WIN32_VXD__) && !defined(__NT_DRIVER__)
exit(_status);
#endif
}
void _CDECL stub_free(void *_ptr)
{ __PM_free(_ptr); }
char * _CDECL stub_getenv(const char *_name)
{
#if defined( __WIN32_VXD__) || defined(__NT_DRIVER__)
return NULL;
#else
return getenv(_name);
#endif
}
void * _CDECL stub_malloc(size_t _size)
{ return __PM_malloc(_size); }
void * _CDECL stub_realloc(void *_ptr, size_t _size)
{ return __PM_realloc(_ptr,_size); }
int _CDECL stub_system(const char *_s)
{
#if defined(__WINDOWS__) || defined(__WIN32_VXD__) || defined(__NT_DRIVER__) || defined(__SMX32__) || defined(__RTTARGET__)
(void)_s;
return -1;
#else
return system(_s);
#endif
}
int _CDECL stub_putenv(const char *_val)
{
#if defined( __WIN32_VXD__) || defined(__NT_DRIVER__)
return -1;
#else
return putenv((char*)_val);
#endif
}
time_t _CDECL stub_time(time_t *_tod)
{
#if defined( __WIN32_VXD__) || defined(__NT_DRIVER__)
return 0;
#else
return time(_tod);
#endif
}
#if defined(__MSDOS__)
#if defined(TNT) && defined(_MSC_VER)
void _CDECL _OS_setfileattr(const char *filename,unsigned attrib)
{ SetFileAttributes((LPSTR)filename, (DWORD)attrib); }
#else
void _CDECL _OS_setfileattr(const char *filename,unsigned attrib)
{ _dos_setfileattr(filename,attrib); }
#endif
#elif defined(__WIN32_VXD__)
#define USE_LOCAL_FILEIO
#define USE_LOCAL_GETDATE
/* <libc/file.h> stub functions */
int _CDECL stub_open(const char *_path, int _oflag, unsigned _mode)
{
char mode[10];
int i;
/* Find an empty file handle to use */
for (i = 3; i < MAX_FILES; i++) {
if (!openHandles[i])
break;
}
if (openHandles[i])
return -1;
/* Find the open flags to use */
if (_oflag & ___O_TRUNC)
strcpy(mode,"w");
else if (_oflag & ___O_CREAT)
strcpy(mode,"a");
else
strcpy(mode,"r");
if (_oflag & ___O_BINARY)
strcat(mode,"b");
if (_oflag & ___O_TEXT)
strcat(mode,"t");
/* Open the file and store the file handle */
if ((openHandles[i] = fopen(_path,mode)) == NULL)
return -1;
return i;
}
int _CDECL stub_access(const char *_path, int _amode)
{ return -1; }
int _CDECL stub_close(int _fildes)
{
if (_fildes >= 3 && openHandles[_fildes]) {
fclose(openHandles[_fildes]);
openHandles[_fildes] = NULL;
}
return 0;
}
off_t _CDECL stub_lseek(int _fildes, off_t _offset, int _whence)
{
if (_fildes >= 3) {
fseek(openHandles[_fildes],_offset,_whence);
return ftell(openHandles[_fildes]);
}
return 0;
}
size_t _CDECL stub_read(int _fildes, void *_buf, size_t _nbyte)
{
if (_fildes >= 3)
return fread(_buf,1,_nbyte,openHandles[_fildes]);
return 0;
}
int _CDECL stub_unlink(const char *_path)
{
WORD error;
if (initComplete) {
if (R0_DeleteFile((char*)_path,0,&error))
return 0;
return -1;
}
else
return i_remove(_path);
}
size_t _CDECL stub_write(int _fildes, const void *_buf, size_t _nbyte)
{
if (_fildes >= 3)
return fwrite(_buf,1,_nbyte,openHandles[_fildes]);
return _nbyte;
}
int _CDECL stub_isatty(int _fildes)
{ return 0; }
/* <stdio.h> stub functions */
int _CDECL stub_remove(const char *_filename)
{ return stub_unlink(_filename); }
int _CDECL stub_rename(const char *_old, const char *_new)
{ return -1; }
void _CDECL _OS_setfileattr(const char *filename,unsigned attrib)
{
WORD error;
if (initComplete)
R0_SetFileAttributes((char*)filename,attrib,&error);
}
/* Return the current date in days since 1/1/1980 */
ulong _CDECL _OS_getcurrentdate(void)
{
DWORD date;
VTD_Get_Date_And_Time(&date);
return date;
}
#elif defined(__NT_DRIVER__)
#define USE_LOCAL_FILEIO
#define USE_LOCAL_GETDATE
/* <libc/file.h> stub functions */
int _CDECL stub_open(const char *_path, int _oflag, unsigned _mode)
{
char mode[10];
int i;
/* Find an empty file handle to use */
for (i = 3; i < MAX_FILES; i++) {
if (!openHandles[i])
break;
}
if (openHandles[i])
return -1;
/* Find the open flags to use */
if (_oflag & ___O_TRUNC)
strcpy(mode,"w");
else if (_oflag & ___O_CREAT)
strcpy(mode,"a");
else
strcpy(mode,"r");
if (_oflag & ___O_BINARY)
strcat(mode,"b");
if (_oflag & ___O_TEXT)
strcat(mode,"t");
/* Open the file and store the file handle */
if ((openHandles[i] = fopen(_path,mode)) == NULL)
return -1;
return i;
}
int _CDECL stub_close(int _fildes)
{
if (_fildes >= 3 && openHandles[_fildes]) {
fclose(openHandles[_fildes]);
openHandles[_fildes] = NULL;
}
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?