⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newlib_hooks.c

📁 umon bootloader source code, support mips cpu.
💻 C
📖 第 1 页 / 共 2 页
字号:
	return(ret);
}

int
_fstat_r(REENT *rptr, int fd, struct stat *sp)
{
#if NEWLIB_TRACE
	if (mon_getenv("NEWLIB_TRACE"))
		printf("_fstat_r(%d)\n",fd);
#endif
	newlib_not_supported(rptr,"_fstat_r");
	return(-1);
}

int
stat(const char *path, struct stat *buf)
{
	TFILE	stat;

#if NEWLIB_TRACE
	if (mon_getenv("NEWLIB_TRACE"))
		printf("stat(%s)\n",path);
#endif

	if (mon_tfsfstat((char *)path,&stat) == -1)
		return(-1);

	/* Needs more work; however, in most apps, this is all
	 * stat is used for...
	 */
	buf->st_size = stat.filsize;
	return(0);
}

_off_t
_lseek_r(REENT *rptr, int fd, _off_t offset, int whence)
{
	int ret;

#if NEWLIB_TRACE
	if (mon_getenv("NEWLIB_TRACE"))
		printf("_lseek_r(%d,%ld,%d)\n",fd,offset,whence);
#endif

	switch (whence) {
		case SEEK_END:
			mon_printf("_lseek_r() doesn't support SEEK_END\n");
			return(-1);
		case SEEK_CUR:
			whence = TFS_CURRENT;
			break;
		case SEEK_SET:
			whence = TFS_BEGIN;
			break;
	}
	ret = mon_tfsseek(tfdtable[fd].tfd,offset,whence);

	if (ret < 0)
		return(-1);

	return(ret);
}

int
_link_r(REENT *rptr, const char *oldname, const char *newname)
{
#if NEWLIB_TRACE
	if (mon_getenv("NEWLIB_TRACE"))
		printf("_link_r(%s,%s)\n",oldname,newname);
#endif

	newlib_not_supported(rptr,"_link_r");
	return(-1);
}

int
_unlink_r(REENT *rptr, const char *name)
{
#if NEWLIB_TRACE
	if (mon_getenv("NEWLIB_TRACE"))
		printf("_unlink_r(%s)\n",name);
#endif

	mon_tfsunlink((char *)name);
	return(-1);
}

int
unlink(const char *name)
{
#if NEWLIB_TRACE
	if (mon_getenv("NEWLIB_TRACE"))
		printf("unlink(%s)\n",name);
#endif

	mon_tfsunlink((char *)name);
	return(0);
}

/*******************************************************************
 *******************************************************************
 *
 * Environment variable stuff:
 */
char *
getenv(const char *name)
{
	return(mon_getenv((char *)name));
}

int
setenv(const char *name, const char *val, int clobber)
{
	if (clobber && mon_getenv((char *)name))
		return(0);

	mon_setenv((char *)name,(char *)val);
	return(1);
}

int
putenv(const char *string)	/* takes a "name=value" string */
{
	extern char *strdup(), *strchr();
	char *copy, *eq;
	
	copy = strdup(string);
	if (copy) {
		eq = strchr(copy,'=');
		if (eq) {
			*eq = 0;
			mon_setenv(copy,eq+1);
			mon_free(copy);
			return(0);		/* Succeeded */
		}
		mon_free(copy);
	}
	return(1);				/* Failed */
}


/*******************************************************************
 *******************************************************************
 *
 * Memory allocation stuff:
 * The deallocXXX functions support the optional capability to have
 * this code automatically release memory that was allocated by the
 * application but not freed...
 *
 */
#ifdef DEALLOC_MAX
static void *dealloc_tbl[DEALLOC_MAX];
static int dealloc_hwm, dealloc_tot;

static void
dealloc_add(void *entry)
{
	void **vptr = dealloc_tbl;

	if (++dealloc_tot > dealloc_hwm)
		dealloc_hwm = dealloc_tot;

	while (vptr < &dealloc_tbl[DEALLOC_MAX]) {
		if (*vptr  == 0) {
			*vptr = entry;
			return;
		}
		vptr++;
	}
}

static void
dealloc_del(void *entry)
{
	void **vptr = dealloc_tbl;

	dealloc_tot--;
	while (vptr < &dealloc_tbl[DEALLOC_MAX]) {
		if (*vptr  == entry) {
			*vptr = 0;
			return;
		}
		vptr++;
	}
}

static void
deallocate(void)
{
	void **vptr = dealloc_tbl;

	while (vptr < &dealloc_tbl[DEALLOC_MAX]) {
		if (*vptr != 0) {
			mon_free((char *)*vptr);
		}
		vptr++;
	}
	if (mon_getenv("NEWLIB_TRACE")) {
		if (dealloc_hwm > DEALLOC_MAX) {
			mon_printf("Dealloc@exit high water mark = %d (max=%d)\n",
				dealloc_hwm,DEALLOC_MAX);
		}
		mon_printf("Dealloc@exit released %d blocks\n",dealloc_tot);
	}
}
#else
#define dealloc_add(a)
#define dealloc_del(a)
#define deallocate()
#endif


void *
_malloc_r(REENT *rptr, size_t size)
{
	void *mem;

	mem = mon_malloc(size);
	dealloc_add(mem);
	return(mem);
}

void *
_calloc_r(REENT *rptr, size_t nelem, size_t sizeelem)
{
	char *mem;
	int	size;

	size = nelem*sizeelem;
	mem = mon_malloc(size);
	if (mem) {
		memset(mem,0,size);
		dealloc_add(mem);
	}
	return(mem);
}

void
_free_r(REENT *rptr, void *ptr)
{
	dealloc_del(ptr);
	return(mon_free(ptr));
}

void *
_realloc_r(REENT *rptr, void *ptr, size_t size)
{
	void *mem;

	mem = mon_realloc(ptr,size);
	dealloc_add(mem);
	return(mem);
}

void *
#ifdef USE_PTRDIFF
_sbrk_r(REENT *rptr, ptrdiff_t size)
#else
_sbrk_r(REENT *rptr, size_t size)
#endif
{
#if NEWLIB_TRACE
	if (mon_getenv("NEWLIB_TRACE"))
		printf("_sbrk_r(%ld)\n",size);
#endif

	newlib_not_supported(rptr,"_sbrk_r");
	return(0);
}

/*******************************************************************
 *******************************************************************
 *
 * Miscellaneous stuff:
 */
#define MAX_EXIT_FUNCS	16

static int		exitftot;
static void		(*exitftbl[MAX_EXIT_FUNCS])();

void
_exit(int val)
{
	int	idx;

#if NEWLIB_TRACE
	if (mon_getenv("NEWLIB_TRACE"))
		printf("_exit(%d)\n",val);
#endif

	newlib_tfdlock();
	/* Ignore stdin/stdout/stderr slots: */
	for(idx=3;idx<MAXTFDS;idx++) {
		if (tfdtable[idx].inuse) {
			mon_printf("WARNING: exit with '%s' left opened\n",
				tfdtable[idx].name);
		}
	}
	newlib_tfdunlock();

	/* Call each of the functions loaded by atexit()...
	 */
	for(idx=0;idx<exitftot;idx++)
		exitftbl[idx]();

	/* Release any memory that was allocated, but not freed by
	 * the application...
	 */
	deallocate();

	mon_appexit(val);
}

int
atexit(void (*func)(void))
{
#if NEWLIB_TRACE
	if (mon_getenv("NEWLIB_TRACE"))
		printf("atexit(%lx)\n",(long)func);
#endif

	if (exitftot >= MAX_EXIT_FUNCS) {
		mon_printf("atexit() overflow\n");
		return(-1);
	}

	exitftbl[exitftot++] = func;
	return(0);
}


/*
 * Allocate a file buffer, or switch to unbuffered I/O.
 * Per the ANSI C standard, ALL tty devices default to line buffered.
 *
 * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
 * optimisation) right after the fstat() that finds the buffer size.
 */
void
__smakebuf(FILE *fp)
{
	void *p;
	int flags = __SNPT;
	size_t size = 256;

	if (fp->_flags & __SNBF) {
		fp->_bf._base = fp->_p = fp->_nbuf;
		fp->_bf._size = 1;
		return;
	}
	if ((p = malloc(size)) == NULL) {
		fp->_flags |= __SNBF;
		fp->_bf._base = fp->_p = fp->_nbuf;
		fp->_bf._size = 1;
		return;
	}
	flags |= __SMBF;
	fp->_bf._base = fp->_p = p;
	fp->_bf._size = size;

	/* Not sure how to tell if the FP is a tty, so we just assume
	 * it is here (not sure if this is ok).
	 */
// 	if (isatty(fp->_file))
 		flags |= __SLBF;

	fp->_flags |= flags;
}

/*
 * system():
 * Not really a newlib hook, but this is a convenient place for it...
 */
int
system(const char *cmd)
{
#if NEWLIB_TRACE
	if (mon_getenv("NEWLIB_TRACE"))
		printf("system(%s)\n",cmd);
#endif

	mon_docommand((char *)cmd,0);
	return(0);
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -