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

📄 util.c

📁 使用BorlandC++4.5编译的一个MUD客户端程序
💻 C
字号:
/* util.c: Miscellaneous utility functions */
/* $Id: util.c 2.2 1995/10/27 20:03:08 tsurace Release $ */
 
#include "vt.h"
#include <signal.h>

#ifndef __WIN32__
#include <pwd.h>
#endif
 
extern int rows;
extern void endpwent();


#ifndef __WIN32__ /* These routines are replace by modal dialogs in win32 */
void vterror(s)
	char *s;
{
	char c;
 
	clrscr();
	scroll(0, rows - 1);
	bflushfunc();
	puts(s);
	write(1, "Dump core? ", 12);
	while (!strchr("YyNn", c = getch()));
	tty_mode(0);
	if (c == 'Y' || c == 'y') {
		write(1, "Yes\n", 4);
		abort();
	} else
		write(1, "No\n", 3);
	exit(1);
}

void vtdie(s)
	char *s;
{
	coutput(s);
	cleanup();
	exit(1);
}

void vtexit()
{
    cleanup();
    exit(0);
}
#endif /* Not __WIN32__ */

void regerror(s)
	char *s;
{
	vtc_errmsg = s;
}

char *dmalloc(size)
	size_t size;
{
	char *ret;
 
	if (size <= 0)
		return NULL;
	ret = tmalloc(size);
	if (!ret)
		vterror("malloc() failed");
	return ret;
}
 
char *drealloc(ptr, oldsize, newsize)
	char *ptr;
	size_t oldsize, newsize;
{
	char *ret;
 
	if (!ptr)
		return dmalloc(newsize);
	ret = trealloc(ptr, oldsize, newsize);
	if (!ret)
		vterror("realloc() failed");
	return ret;
}
 
void dfree(ptr, size)
	char *ptr;
	size_t size;
{
	if (ptr)
		tfree(ptr, size);
}
 
void cleanup()
{
	tty_mode(0);
	scroll(0, rows - 1);
	cmove(0, rows - 1);
	bflushfunc();
	cleanup_rmt();
}
 
#ifdef memset
void vtmemset(loc, val, len)
	char *loc;
	int val, len;
{
	while (len--)
		*loc++ = val;
}
#endif
 
/* Credits to Leo Plotkin and Anton Rang */
char *expand(s)
	char *s;
{
	String *buf = &wbufs[0];
	struct passwd *uinfo;
	char *homedir, *rest;
 
	homedir = getenv("HOME");
	if (*s != '~' || !homedir)
		return s;
	if (*++s == '/' || !*s) {
		s_acpy(buf, homedir);
		s_acat(buf, s);
		return buf->c.s;
	}
	rest = strchr(s, '/');
	if (rest)
		*rest = '\0';
	setpwent();
	uinfo = getpwnam(s + 1);
	if (!uinfo)
		return s;
	endpwent();
	if (rest)
		*rest = '/';
	s_acat(buf, uinfo->pw_dir);
	s_acat(buf, rest ? rest : "");
	return buf->c.s;
}

char *vt_itoa(num)
	int num;
{
	static char buf[32];
	char *p = &buf[31];
	int sign = 0;
 
	if (num < 0) {
		sign = 1;
		num = -num;
	}
	if (!num)
		*--p = '0';
	while (num) {
		*--p = num % 10 + '0';
		num /= 10;
	}
	if (sign)
		*--p = '-';
	return p;
}
 
int smatch(p, s)
	char *p, *s;
{
	while (*p) {
		if (*p == '*') {
			while (*++p == '?' || *p == '*') {
				if (*p == '?' && !*s++)
					return 0;
			}
			if (!*p)
				return 1;
			for (; *s; s++) {
				if (lcase(*s) == lcase(*p) && smatch(p, s))
					return 1;
			}
			return 0;
		} else if (*p == '?') {
			if (!*s++)
				return 0;
			p++;
		} else if (lcase(*p++) != lcase(*s++))
			return 0;
	}
	return !*s;
}
 

⌨️ 快捷键说明

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