filetype.c

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

C
943
字号
if(lookupe(fort) == 1) {	strcpy(Tftypes,"for");	Type = TEXT;	/* Fortran program text */	goto outa;}/*E if lookupe fort *//* */notfort:i=0;if (!ascom()) goto notas;j = i-1;if (buf[i] == '.') {	i++;	if(lookupe(as) == 1) {		strcpy(Tftypes,"asm");		Type = TEXT;	/* Assembler program text */		goto outa;	}/*T if lookupe as .. */	else	if(buf[j] == '\n' && isalpha(buf[j+2])) {			strcpy(Tftypes,"rof");			Type = TEXT;	/* roff, nroff or eqn					 * input text.					 */			goto outa;		}}/*E if buf[i] ..*/while (!lookupe(asc)) {	if (!ascom()) goto notas;	while(buf[i] != '\n' && buf[i++] != ':')		if(i >= in) goto notas;	while(buf[i] == '\n' || buf[i] == ' ' || buf[i] == '\t'		|| buf[i] == '\f' || buf[i] == NL)		if(i++ >= in) goto notas;	j = i-1;	if(buf[i] == '.') {		i++;		if(lookupe(as) == 1) {			strcpy(Tftypes,"asm");			Type = TEXT; /* ASM program text */			goto outa;		}		else if(buf[j] == '\n' && isalpha(buf[j+2])) {				strcpy(Tftypes,"rof");				Type = TEXT;	/* roff, nroff, or						 * eqn input text.						 */				goto outa;		}	}/*E if buf[i] == . */}/* while lookupe(asc) ..*/strcpy(Tftypes,"asm");Type = TEXT;	/* Assembler program text */	goto outa;/* */notas:for (i=0; i < in; i++)	if (buf[i] & 0200) {		if (((int)buf[0]==(int)'\100') && ((int)buf[1]==(int)'\357')) {			fclose(ifile);			strcpy(Tftypes,"rof");			return(BINARY);	/* troff (CAT) output */		}	fclose(ifile);	strcpy(Tftypes,"bin");	return(BINARY);	/* Data file of some type */}/*E for i=0 ..*/if (mbuf.st_mode&((S_IEXEC)|(S_IEXEC>>3)|(S_IEXEC>>6))) {	strcpy(Tftypes,"com");	Type = TEXT;	/* Commands text */	goto outa;}	else if (troffint(in)) {		strcpy(Tftypes,"rof");		Type = BINARY; /* troff intermediate output text */		}	else if (english(buf, in)) {		strcpy(Tftypes,"eng");		Type = ENGLISH;	/* English text */		}	else	{		strcpy(Tftypes,"asc");		Type = TEXT;	/* Ascii text */		}/* */outa:while(i < in)	if((!buf[i]) || ((buf[i++] & 0377) > 0176)) {		/* With garbage, assume binary		 */		fclose(ifile);		strcpy(Tftypes,"bin");		return(BINARY);	}if (((Type == TEXT) || (Type == ENGLISH))) {	/*	 *	We don't want to be mislead. If we think this is	 *	a 'text' file... Make certain of it..	 */	while((in = read(fileno(ifile),buf,MBUFSIZ)) > 0) {		for(i = 0; i < in; i++)			if((!buf[i]) || ((buf[i] & 0377) > 0176)) {				/* With garbage, assume binary				 */				fclose(ifile);				strcpy(Tftypes,"bin");				return(BINARY);			}		/* If it doesn't meet the English frequency test,		 * again assume it is TEXT of some kind.		 */		if (Type == ENGLISH)			if (!(english(buf, in))) {				/*				 * If it fails English freq test,				 * switch over to TEXT and continue				 * checking for BINARY data.				 */				strcpy(Tftypes,"asc");				Type = TEXT;			}	}/*E while in .. */	if (in < 0) {	    PERROR "\n%s: %s %s\n", caller, CANTRD, file);	    perror(caller);	    exit(FAIL);	}}/*E if Type == TEXT */	/* If the above logic didn't change our mind,	 * return the tentative file type as the actual.	 * Convert tentative specifics to generalized types	 * as/if required.	 */if (Type == ENGLISH) {	fclose(ifile);	return(TEXT);}else {	fclose(ifile);	return(Type);}}/*E Filetype() *//**//* * * Function: * *	ascom * * Function Description: * *	 * * Arguments: * *	none * * Return values: * *	Indication of what value(s) are returned. * * * Side Effects: * */ascom(){while(buf[i] == '/') { 	i++;	while(buf[i++] != '\n')if(i >= in)		return(0);	while(buf[i] == '\n')if(i++ >= in)		return(0);}/*E while buf[i] ..*/return(1);}/*E ascom *//**//* * * Function: * *	ccom * * Function Description: * *	This section will provide a description of the function. * * Arguments: * *	none * * Return values: * *	Indication of what value(s) are returned. * * * Side Effects: * *	 */ccom(){/*------*\   Code\*------*/while((ch = buf[i]) == ' ' || ch == '\t' || ch == '\n'	|| ch == '\f' || ch == NL)	if(i++ >= in)	{	return(0);}if(buf[i] == '/' && buf[i+1] == '*') {	i += 2;	while(buf[i] != '*' || buf[i+1] != '/') {		if(buf[i] == '\\')			i += 2;		else			i++;		if(i >= in) {			return(0);		}	}/* while buf[i] ..*/	if((i += 2) >= in) {		return(0);	}}/*E if buf[i] ..*/if(buf[i] == '\n')	if(ccom() == 0) {		return(0);	}return(1);}/*E ccom() *//**//* * * Function: * *	english * * Function Description: * *	This routine attempts to determine if the file contains *	english text based on the frequency (or lack thereof) *	of key letter usage in the English language.	 * * Arguments: * *	char	*bp	Pointer to the buffer of text *	int	n	Number of characters in the buffer. * * Return values: * *	Zero	if the buffer doesn't look like english text. *	Non-zero if buffer appears to be English. * * * Side Effects: * *	none	 */english(bp, n)	char	*bp;	int	n;{/* Local variables */int	ct[NASC], freq, rare, vow;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] != '\f' &&			    bp[j+1] != '\t' &&			    bp[j+1] != NL   &&			    bp[j+1] != '\n')				badpun++;	}/*E switch bp[j] */}/*E for j=0 ..*/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);}/*E english() *//**//* * * Function: * *	lookupe * * Function Description: * *	This section will provide a description of the function. * * Arguments: * *	char	*tab	?? * * Return values: * *	Indication of what value(s) are returned. * * * Side Effects: * *	 */lookupe(tab)	char	*tab[];{/* Local variables */int	k, l;/*-*/while(buf[i] == ' ' || buf[i] == '\t' || buf[i] == '\n'	|| buf[i] == '\f' || buf[i] == NL)	i++;for(j=0; tab[j] != 0; j++) {	l=0;	for(k=i; ((ch=tab[j][l++]) == buf[k] && ch != '\0');k++)		;	if(ch == '\0')		if(buf[k] == ' ' || buf[k] == '\n' || buf[k] == '\t'		    || buf[k] == '\f' || buf[k] == '{' ||			 buf[k] == NL || buf[k] == '/') {			i=k;			return(1);		}/*E if buf[k] ..*/}/*E for j=0 ..*/return(0);}/*E lookupe() *//**//* * * Function: * *	troffint * * Function Description: * *	See if the file appears to be  troff  intermediate text * * Arguments: * *	int	n	? * * Return values: * *	Indication of what value(s) are returned. * * * Side Effects: * *	 */troffint(n)	int	n;{i = 0;for (j = 0; j < 6; j++) {	if (lookupe(troff) == 0)		return(0);	if (lookupe(troff) == 0)		return(0);	while (i < n && buf[i] != '\n')		i++;	if (i++ >= n)		return(0);}/*E for j=0 .. */return(1);}/*E troffint() *//**\\**\\**\\**\\**\\**  EOM  filetype.c  **\\**\\**\\**\\**\\*//**\\**\\**\\**\\**\\**  EOM  filetype.c  **\\**\\**\\**\\**\\*/

⌨️ 快捷键说明

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