filetype.c

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 1,067 行 · 第 1/2 页

C
1,067
字号
			switch(opc) {			case '=':				ep->e_opcode = EQ;				break;			case '>':				ep->e_opcode = GT;				break;			case '<':				ep->e_opcode = LT;				break;			case 'x':				ep->e_opcode = ANY;				break;			default:				p--;			}		}		if(ep->e_opcode != ANY) {			if(ep->e_type != STR)				ep->e_value.num = atolo(p);			else {				ep->e_value.str = (char *)malloc(strlen(p) + 1);				p3 = ep->e_value.str;				while (*p != '\0') 				    if (*p == '\\')					switch (*++p) {					case 'n':						*p3++ = '\n';						p++;						break;					case 't':						*p3++ = '\t';						p++;						break;					case 'b':						*p3++ = '\b';						p++;						break;					case 'r':						*p3++ = '\r';						p++;						break;					case 'f':						*p3++ = '\f';						p++;						break;					case '0':					case '1':					case '2':					case '3':					case '4':					case '5':					case '6':					case '7':					case '8':					case '9':						{						int value = 0,						    base = 10;						while (*p >= '0' && *p <= '9') {							if (*p == '0' && value == 0) base = 8;							value = value*base + (*p++ - '0');							}						*p3++ = (unsigned char)value;						break;						}					default:						*p3++ = *p++;				}			   else				*p3++ = *p++;			*p3 = '\0';			p3 = (char *)fre_comp(ep->e_value.str);			if (p3 == 0) fprintf(stderr, "file: bad reg exp in magic file: %s\n", ep->e_value.str);			else {				free(ep->e_value.str);				ep->e_value.str = p3;			}			}		}		while(*p2 == '\t')			*p2++;			/* MAJOR/MINOR TYPE NUMBERS */		if (sscanf(p2, "%d,%d", &majnum, &minnum) < 2)			fprintf(stderr, "file: invalid major,minor numbers in magic file.\n");		ep->e_retcode = MASH(majnum, minnum);		p2 = strchr(p2, '\t');		while(*p2 == '\t')			*p2++;			/* STRING */		ep->e_str = (char *)malloc(strlen(p2) + 1);		p = ep->e_str;		while(*p2 != '\n') {			if(*p2 == '%')				ep->e_opcode |= SUB;			*p++ = *p2++;		}		*p = NULL;		ep++;		if(ep >= mend) {			fprintf(stderr, "file: magic table overflow.\n");			exit(2);		}	}	ep->e_off = -2L*(long)maxexecfn;}printmtab(){reg Entry *ep;	printf("level   offset  type    opcode  major,minor	value   string\n");	for(ep = mtab; ep->e_off > -2L*(long)maxexecfn; ep++) {		printf("%d\t%d\t%d\t%d\t%d,%d\t",			ep->e_level, ep->e_off, ep->e_type, ep->e_opcode, 			(short)(ep->e_retcode>>16),			(short)(ep->e_retcode & 0177777));		if (ep->e_type == STR) {			printstr(ep->e_value.str, 50);			printf("\t");			}		else			printf("%#lo\t", ep->e_value.num);		printf("%s", ep->e_str);		if (ep->e_opcode & SUB)			printf("\tsubst");		printf("\n");	}}printstr(p, n)unsigned char *p;int n;{	register unsigned char *sp;	register int c;	for (sp = p, c = 0; *sp != '\0' && c++ < n; sp++)		if (isprint(*sp)) printf("%c", *sp);		else if (*sp == '\n') printf("\\n");		else if (*sp == '\r') printf("\\r");		else if (*sp == '\t') printf("\\t");		else if (*sp == '\b') printf("\\b");		else if (*sp == '\b') printf("\\f");		else printf("\\%#o", *sp);}longatolo(s)reg	char	*s;{	reg	char	c;	reg	char	*fmt = "%ld";	auto	long	j = 0L;	if(*s == '0') {		s++;		if(*s == 'x') {			s++;			fmt = "%lx";		} else			fmt = "%lo";	}	sscanf(s, fmt, &j);	return(j);}char *execmodes(mbuf, fbuf)struct stat *mbuf;char *fbuf;{	static char msg_buf[26];	msg_buf[0] = '\0';	if (mbuf->st_mode & S_ISUID)		strncat(msg_buf, "setuid ", 7);	if (mbuf->st_mode & S_ISGID)		strcat(msg_buf, "setgid ");	if (mbuf->st_mode & S_ISVTX)		strcat(msg_buf, "sticky ");	return(msg_buf);}#ifdef vaxchar *symtable(mbuf, fbuf)struct stat *mbuf;char *fbuf;{	struct exec ex;	struct stat stb;	ex = *(struct exec *)fbuf;	if (fstat(ifile, &stb) < 0)		return("");	if (((int *)buf[4])!= 0) 	if ((int)N_STROFF(ex)+sizeof(off_t) > stb.st_size)		return ("version 7 style symbol table");	return ("");}#else !vaxchar *symtable(mbuf, fbuf)struct stat *mbuf;char *fbuf;{	return("");}#endif vaxchar *(*execfns[])() = { symtable, symtable, execmodes };longckmtab(mbuf, pflg)struct stat *mbuf;int pflg;{	reg	Entry	*ep;	reg	char	*p;	reg	int	lev1 = 0;	int fn;	long retcode;	auto	union	{		long	l;		char	ch[4];		}	val, revval;	static	char	init = 0;	struct matcher {		char *start, *finish;		} *match;	char pbuf[256]; /* these fixed length arrays are bad. fix then sometime */	char sbuf[256];	char *sptr;	int slen = 0;	int match_len;	if(!init) {		mkmtab(0);		init = 1;	}	for(ep = mtab; ep->e_off > -2L * (long)maxexecfn; ep++) {		if(lev1) {			if(ep->e_level != 1)				break;			if (pflg && (slen > 0)) putchar(' ');			slen = 0;		} else if(ep->e_level == 1)			continue;		if (pflg && ep->e_off < 0L) {			fn = -1 * ep->e_off;			if (fn > maxexecfn)				sprintf(sbuf, ep->e_str, " ???? ");			else				sprintf(sbuf, ep->e_str, (char *)(*execfns[fn])(mbuf, buf));			slen = strlen(sbuf);			printf(sbuf);			continue;			}					p = &buf[ep->e_off];		switch(ep->e_type) {		case STR:		{				if((match = (struct matcher *)fre_exec(p,ep->e_value.str)) <= (struct matcher *) 0)				continue;			match_len = match->finish - match->start;			strncpy(pbuf, match->start, match_len);			pbuf[match_len] = '\0';			break;		}		case BYTE:			val.l = (long)(*(unsigned char *) p);			break;		case SHORT:			val.l = (long)(*(unsigned short *) p);			break;		case LONG:			val.l = (*(long *) p);			break;		}		if (ep->e_type != STR)		switch(ep->e_opcode & ~SUB) {		case EQ:			if(val.l != ep->e_value.num)				continue;			break;		case GT:			if(val.l <= ep->e_value.num)				continue;			break;		case LT:			if(val.l >= ep->e_value.num)				continue;			break;		}		if (pflg)			if(ep->e_opcode & SUB) { /* we really need the SysV printf here to return */					         /* the number of characters printed.		*/				sprintf(sbuf, ep->e_str, (ep->e_type == STR) ? (int)pbuf : val.l);				sptr = sbuf;				}			else				sptr=ep->e_str;		slen = strlen(sptr);		printf(sptr);		retcode = ep->e_retcode;		lev1 = 1;	}	if(lev1) {		if (pflg) putchar('\n');		return(retcode);	}	return(0);}int ci;cprog(){	int j, nl;	char ch;	ci = 0;	if (lookup(cstart) == 1)		return(1);	if(ccom() == 0) return(0);	while(buf[ci] == '#'){		j = ci;		while(buf[ci++] != '\n'){			if(ci - j > 255)				return(0);			if(ci >= in) return(0);		}		if(ccom() == 0) return(0);	}check:	if(lookup(c) == 1){		if (buf[0] == '%' && buf[1] == '!') return(0);	/* actually postscript */		while((ch = buf[ci++]) != ';' && ch != '{')if(ci >= in) return(0);		return(1);	}	nl = 0;	while(buf[ci] != '('){		if(buf[ci] <= 0)			return(0);		if(buf[ci] == ';'){			ci++; 			goto check; 		}		if(buf[ci++] == '\n')			if(nl++ > 6) return(0);		if(ci >= in) return(0);	}	while(buf[ci] != ')'){		if(buf[ci++] == '\n')			if(nl++ > 6) return(0);		if(ci >= in) return(0);	}	while(buf[ci] != '{'){		if(buf[ci++] == '\n')			if(nl++ > 6) return(0);		if(ci >= in) return(0);	}	if (buf[0] == '%' && buf[1] == '!') return(0);	/* actually postscript */	return(1);}ccom(){	char cc;	while((cc = buf[ci]) == ' ' || cc == '\t' || cc == '\n')if(ci++ >= in)return(0);	if(buf[ci] == '/' && buf[ci+1] == '*'){		ci += 2;		while(buf[ci] != '*' || buf[ci+1] != '/'){			if(buf[ci] == '\\')ci += 2;			else ci++;			if(ci >= in)return(0);		}		if((ci += 2) >= in)return(0);	}	if(buf[ci] == '\n')if(ccom() == 0)return(0);	return(1);}intfortprog(){	int i = 0;	while(buf[i] == 'c' || buf[i] == '#'){		while(buf[i++] != '\n')if(i >= in) return(0);	}	if(lookup(fort) == 1)		return(1);	return(0);}ascom(){	while(buf[i] == '/'){		i++;		while(buf[i++] != '\n')if(i >= in)return(0);		while(buf[i] == '\n')if(i++ >= in)return(0);	}	return(1);}sccs() {	reg int i;	if(buf[0] == 1 && buf[1] == 'h')		for(i=2; i<=6; i++)			if(isdigit(buf[i])) continue;			else return(0);	else		return(0);	return(1);}troffint(bp, n)char *bp;int n;{	int k;	i = 0;	for (k = 0; k < 6; k++) {		if (lookup(troff) == 0)			return(0);		if (lookup(troff) == 0)			return(0);		while (i < n && buf[i] != '\n')			i++;		if (i++ >= n)			return(0);	}	return(1);}lookup(tab)char *tab[];{	char r;	int k,j,l;	while(buf[i] == ' ' || buf[i] == '\t' || buf[i] == '\n')i++;	for(j=0; tab[j] != 0; j++){		l=0;		for(k=i; ((r=tab[j][l++]) == buf[k] && r != '\0');k++);		if(r == '\0')			if(buf[k] == ' ' || buf[k] == '\n' || buf[k] == '\t'			    || buf[k] == '{' || buf[k] == '/' || buf[k] == '>'){				i=k;				return(1);			}	}	return(0);}english (bp, n)char *bp;{# define NASC 128	int ct[NASC], j, vow, freq, rare;	int badpun = 0, punct = 0;	if (n<50) return(0); /* no point in statistics on squibs */	for(j=0; j<NASC; j++)		ct[j]=0;	for(j=0; j<n; j++)	{		if (bp[j]<NASC)			ct[bp[j]|040]++;		switch (bp[j])		{		case '.': 		case ',': 		case ')': 		case '%':		case ';': 		case ':': 		case '?':			punct++;			if ( j < n-1 &&			    bp[j+1] != ' ' &&			    bp[j+1] != '\n')				badpun++;		}	}	if (badpun*5 > punct)		return(0);	vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u'];	freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n'];	rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z'];	if (2*ct[';'] > ct['e']) return(0);	if ( (ct['>']+ct['<']+ct['/'])>ct['e']) return(0); /* shell file test */	return (vow*5 >= n-ct[' '] && freq >= 10*rare);}shell(bp, n, tab)	char *bp;	int n;	char *tab[];{	i = 0;	do {		if (buf[i] == '#' || buf[i] == ':')			while (i < n && buf[i] != '\n')				i++;		if (++i >= n)			break;		if (lookup(tab) == 1)			return (1);	} while (i < n);	return (0);}

⌨️ 快捷键说明

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