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

📄 util.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
字号:
#include <u.h>#include <libc.h>#include <bio.h>#include <String.h>#include <ctype.h>#include <thread.h>#include "wiki.h"void*erealloc(void *v, ulong n){	v = realloc(v, n);	if(v == nil)		sysfatal("out of memory reallocating %lud", n);	setmalloctag(v, getcallerpc(&v));	return v;}void*emalloc(ulong n){	void *v;	v = malloc(n);	if(v == nil)		sysfatal("out of memory allocating %lud", n);	memset(v, 0, n);	setmalloctag(v, getcallerpc(&n));	return v;}char*estrdup(char *s){	int l;	char *t;	if (s == nil)		return nil;	l = strlen(s)+1;	t = emalloc(l);	memmove(t, s, l);	setmalloctag(t, getcallerpc(&s));	return t;}char*estrdupn(char *s, int n){	int l;	char *t;	l = strlen(s);	if(l > n)		l = n;	t = emalloc(l+1);	memmove(t, s, l);	t[l] = '\0';	setmalloctag(t, getcallerpc(&s));	return t;}char*strlower(char *s){	char *p;	for(p=s; *p; p++)		if('A' <= *p && *p <= 'Z')			*p += 'a'-'A';	return s;}String*s_appendsub(String *s, char *p, int n, Sub *sub, int nsub){	int i, m;	char *q, *r, *ep;	ep = p+n;	while(p<ep){		q = ep;		m = -1;		for(i=0; i<nsub; i++){			if(sub[i].sub && (r = strstr(p, sub[i].match)) && r < q){				q = r;				m = i;			}		}		s = s_nappend(s, p, q-p);		p = q;		if(m >= 0){			s = s_append(s, sub[m].sub);			p += strlen(sub[m].match);		}	}	return s;}String*s_appendlist(String *s, ...){	char *x;	va_list arg;	va_start(arg, s);	while(x = va_arg(arg, char*))		s = s_append(s, x);	va_end(arg);	return s;}intopentemp(char *template){	int fd, i;	char *p;	p = estrdup(template);	fd = -1;	for(i=0; i<10; i++){		mktemp(p);		if(access(p, 0) < 0 && (fd=create(p, ORDWR|ORCLOSE, 0444)) >= 0)			break;		strcpy(p, template);	}	if(fd >= 0)		strcpy(template, p);	free(p);	return fd;}

⌨️ 快捷键说明

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