[net.sources] Improvements

fred@gyre.UUCP (Fred Blonder) (10/16/84)

Here's a ``diff'' script for /usr/src/usr.bin/file.c on 4.2BSD, which
will produce a version of file which will recognize some additional
file types. You may not wish to include all these changes, since some
of the file types are local to the machines at U of M. The part which
recognizes compacted files by unpacking the first block and then
identiying that is cute, but slow sometimes.

The additional types it recognizes are: Simpl source code, Z80 object
code, PDP-11 object code, Pyramid object code, CVL format pictures,
compacted files, exec-able scripts and tar archives.

=========================================================================
X
X398a
X}
X
Xis_tar()	/* Is it a tar file? */	/* 25-Oct-1983 FLB */
X{
X/* This stuff is copied from tar.c. */
X#define TBLOCK	512
X#define NAMSIZ	100
Xregister struct header {
X	char name[NAMSIZ];
X	char mode[8];
X	char uid[8];
X	char gid[8];
X	char size[12];
X	char mtime[12];
X	char chksum[8];
X	char linkflag;
X	char linkname[NAMSIZ];
X	} *bp;
Xregister comp_chksum;
Xregister char *cp;
Xint header_chksum;
X
Xbp = (struct header *)buf;
X
X/* Compute checksum */
Xcomp_chksum = 0;
Xfor (cp = buf; cp < &buf[TBLOCK]; cp++)
X	comp_chksum += (cp < ((struct header *)buf)->chksum
X			|| cp >= &((struct header *)buf)->chksum[8])?
X		*cp: ' ';
X
X/* Convert checksum field to integer */
Xsscanf(((struct header *)buf)->chksum, "%o", &header_chksum);
X
Xreturn(comp_chksum == header_chksum);	/* Is checksum correct? */
X.
X352a
X
Xscom(){
X	char cc;
X	while((cc = buf[i]) == ' ' || cc == '\t' || cc == '\n')if(i++ >= in)return(0);
X	if(buf[i] == '/' && buf[i+1] == '*'){
X		i += 2;
X		while(buf[i] != '*' || buf[i+1] != '/'){
X			if(buf[i] == '\\')i += 2;
X			else i++;
X			if(i >= in)return(0);
X		}
X		if((i += 2) >= in)return(0);
X	}
X	if(buf[i] == '/' && buf[i+1] == '+'){
X		i += 2;
X		while(buf[i] != '+' || buf[i+1] != '/'){
X			if(buf[i] == '\\')i += 2;
X			else i++;
X			if(i >= in)return(0);
X		}
X		if((i += 2) >= in)return(0);
X	}
X	if(buf[i] == '\n')if(scom() == 0)return(0);
X	return(1);
X}
X
X.
X216a
X	if (scom() && lookup(simpl)) {
X		printf("simpl program text");
X		goto outa;
X		}
X
X	i=0;
X.
X215a
X	/* test for simpl source code: FLB 6/2/81 */
X.
X154a
X	if (is_tar()) {	/* 21-Oct-83 FLB */
X		printf("tar archive\n");
X		goto out;
X	}
X.
X146a
X	if(strncmp(buf, "#!", 2) == 0 ) {
X		printf("executable script\n");
X		goto out;
X	}
X
X.
X144a
X
X	case 044520:    /* 'PICT' w parity on the 'T' */
X		if ((((short *)buf)[1] & 0177777) == 0152103) {
X			printf("CVL format picture\n");
X			goto out;
X			}
X
X	case 017777:	/* compacted file: uncompact a little bit and
X			   see what it is. */
X		{
X		int fds1[2], fds2[2], pid, wpid;
X
X		printf("compacted");
X
X		pipe(fds1);
X		pipe(fds2);
X
X		if ((pid = fork()) == 0) {
X			close(ifile);
X			close(0);
X			dup(fds1[0]);
X			close(fds1[0]);
X			close(fds1[1]);
X			close(1);
X			dup(fds2[1]);
X			close(fds2[0]);
X			close(fds2[1]);
X			execl("/usr/ucb/uncompact", "uncompact", 0);
X			execl("/bin/uncompact", "uncompact", 0);
X			execl("/usr/bin/uncompact", "uncompact", 0);
X			execlp("uncompact", "uncompact", 0);
X			exit(1);
X			}
X
X		if (pid > 0) {
X
X			write(fds1[1], buf, BUFSIZ);
X			close(fds1[0]);
X			close(fds1[1]);
X
X			close(fds2[1]);
X			in = read(fds2[0], buf, BUFSIZ);
X			close(fds2[0]);
X
X			while (((wpid = wait((long *)0)) > 0)
X							&& (wpid != pid));
X
X			if (in > 0) {
X				putchar(' ');
X				goto whatsit;
X				}
X			}
X
X		close(fds1[0]);
X		close(fds1[1]);
X		close(fds2[0]);
X		close(fds2[1]);
X
X		putchar('\n');
X		goto out;
X		}
X.
X133a
X	/* Test for executable from wrong type of machine. */
X#define SWAP(x)	(((x&0377)<<8)|((x>>8)&0377))
X	case 0:
X		switch (((short *)buf)[1]) {
X			case SWAP(0407): case SWAP(0410): case SWAP(0413):
X#ifdef VAX
X			case SWAP(0414): case SWAP(0415): case SWAP(0416):
X				printf("PYRAMID executable\n");
X#else
X#  ifdef PYRAMID
X			case SWAP(0411):
X				printf("VAX executable\n");
X#  else
X				printf("screwed up executable\n");
X#  endif PYRAMID
X#endif VAX
X				goto out;
X			}
X		break;
X
X.
X124a
X		if (((short *)buf)[1])
X			printf("PDP-11 ");
X
X.
X122a
X#ifdef	Z80MAGIC	/* 20-Mar-84 FLB: ZMOB load format */
X	case Z80MAGIC:
X		printf("Z-80 ");
X#endif	Z80MAGIC
X
X.
X110c
Xwhatsit:
X	switch(*(short *)buf & 0177777) {
X.
X28a
Xchar *simpl[] = {
X	"int", "real", "string", "char", "proc", "ext", "entry", "define",
X	"INT", "REAL", "STRING", "CHAR", "PROC", "EXT", "ENTRY", "DEFINE", 0};
X.
X12a
X#include <whoami.h>
X.
-- 
					Fred Blonder (301) 454-7690
					harpo!seismo!umcp-cs!fred
					Fred@Maryland.ARPA