📄 filetype.c
字号:
#ifndef lintstatic char *sccsid = "@(#)filetype.c 4.1 ULTRIX 10/16/90";#endif/************************************************************************ * * * Copyright (c) 1990 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//* * Modification History: reverse order * * 26-Sep-1990 - Adrian Thoms (thoms@wessex) * Carved out all the magic number code into a separate file * so that it could be used as a guesser library module for the * print system. *//************************************************************************ * Modification History * * 001 Richard Hart, Oct. 21, 1987 * * Copied from 4.3 BSD code: * * file.c 4.12 (Berkeley) 11/17/85 * * 002 Richard Hart, Oct. 21, 1987 * * Added named pipes for Sys V support, and other things in the * * current Ultrix file.c * * 003 Richard Hart, Oct. 22, 1987 * * Added use of /etc/magic, like Sys V filecommand * * 004 Richard Hart, Nov. 5, 1987 * * Now uses sys/exec.h for support of a.out magic numbers * * 005 Ricky Palmer, Aug. 5, 1988 * * Ifdef'ed for vax/mips. * * 006 Jon Reeves, Nov 12, 1988 * * Fixed ifdefs to allow mode code to work. * * 007 Tim Burke, June 12, 1989 * * Added check for the tape density of DEV_38000BPI which is used * * on the TA90 tape drive. Print out "loader" if the tape has a * * media loader. * * 008 Tim Burke, Sep 13, 1989 * * Added the following TA90 densities: DEV_38000_CP, DEV_76000BPI * * and DEV_76000_CP. * * 009 Bill Dallas, Jul 05,1990 * * Added the following QIC densities DEV_8000_BPI, DEV_10000_BPI * * and DEV_16000_BPI. * * ************************************************************************//* * filetype - determine type of file */#include <sys/types.h>#include <sys/param.h>#include <sys/stat.h>#include <sys/file.h>#include <sys/ioctl.h>#include <sys/devio.h>#include <stdio.h>#include <ctype.h>#include "filetype.h"#include "magic_data.h"/*** Misc*/#define NENT 200#define BSZ 128#define FBSZ 512extern char *mfile;extern char *sys_errlist[];struct devget devget;int errno;int sys_nerr;static int in;char *troff[] = { /* new troff intermediate lang */ "x","T","res","init","font","202","V0","p1",0};char *fort[] = { "function","subroutine","common","dimension","block","integer", "real","data","double",0};char *asc[] = { "chmk","mov","tst","clr","jmp",0};char *cstart[] = { "#include", "#ifndef", "static", "stdio.h", 0};char *c[] = { "int","char","float","double","struct","extern","include",0};char *as[] = { "globl","byte","align","text","data","comm",0};char *sh[] = { "fi", "elif", "esac", "done", "export", "readonly", "trap", "PATH", "HOME", 0 };char *csh[] = { "alias", "breaksw", "endsw", "foreach", "limit", "onintr", "repeat", "setenv", "source", "path", "home", 0 };int i = 0;extern int ifile;char buf[BUFSIZ];longfiletype(file, pflg)char *file;int pflg;{ int j,nl; char ch; int minornum, majornum; long retnum; struct stat mbuf; char slink[MAXPATHLEN + 1]; struct devget *pdevget = &devget; if (ifile >= 0) close(ifile); /* close any file that was opened on a previous call */ ifile = -1; if (lstat(file, &mbuf) < 0) { if (pflg) printf("%s\n", (unsigned)errno < sys_nerr? sys_errlist[errno]: "Cannot stat"); return(MASH(UNKNOWN, UNKNOWN)); } switch (mbuf.st_mode & S_IFMT) { case S_IFLNK: if (pflg) printf("symbolic link"); j = readlink(file, slink, sizeof slink - 1); if (j >= 0) { slink[j] = '\0'; if (pflg) printf(" to %s", slink); } if (pflg) printf("\n"); return(MASH(SLINK, UNKNOWN)); case S_IFDIR: minornum = STANDARD; if (mbuf.st_mode & S_ISVTX) if (pflg) printf("append-only "); minornum = APPENDONLY; if (pflg) printf("directory\n"); return(MASH(DIRECTORY, minornum)); case S_IFPORT: if (pflg) printf("port (named pipe\n"); return(MASH(NAMEDPIPE, UNKNOWN)); case S_IFSOCK: if (pflg) printf("socket\n"); return(MASH(SOCKET, UNKNOWN)); case S_IFBLK: if (pflg) printf("block special (%d/%d)\n", major(mbuf.st_rdev), minor(mbuf.st_rdev)); return(MASH(SPECIAL, BLOCK)); case S_IFCHR: if (!pflg) return(MASH(SPECIAL, CHARACTER)); printf("character special (%d/%d)", major(mbuf.st_rdev), minor(mbuf.st_rdev)); ifile = open(file, (O_RDONLY|O_NDELAY)); if(ifile < 0) ifile = open(file, (O_WRONLY|O_NDELAY)); if(ifile < 0) { printf("\n"); return; } if(ioctl(ifile,DEVIOCGET,(char *) pdevget) < 0) { printf("\n"); } else { if((strcmp(DEV_UNKNOWN,pdevget->interface) == 0) || (pdevget->ctlr_num == -1)) { printf(" "); } else { printf(" %s #%d ", pdevget->interface, pdevget->ctlr_num); } if(!(strcmp(DEV_UNKNOWN,pdevget->device) == 0)) { printf("%s ", pdevget->device); } switch(pdevget->category) { case DEV_TAPE: printf("tape "); break; case DEV_DISK: printf("disk "); break; case DEV_TERMINAL: printf("terminal "); break; case DEV_PRINTER: printf("printer "); break; case DEV_SPECIAL: printf("special_device "); break; default: printf("unknown "); break; } if(!(pdevget->slave_num == -1)) { printf("#%d ", pdevget->slave_num); } if(pdevget->soft_count || pdevget->hard_count) { printf("errors = %d/%d ", pdevget->soft_count, pdevget->hard_count); } if(pdevget->stat & DEV_OFFLINE) { printf("offline "); } else if(pdevget->stat & DEV_WRTLCK) { printf("write-locked "); } if((pdevget->category == DEV_TAPE) && !(pdevget->stat & DEV_OFFLINE)) { if(pdevget->category_stat & DEV_LOADER) { printf("loader "); } if(pdevget->category_stat & DEV_800BPI) { printf("800_bpi\n"); } else if(pdevget->category_stat & DEV_1600BPI) { printf("1600_bpi\n"); } else if(pdevget->category_stat & DEV_6250BPI) { printf("6250_bpi\n"); } else if(pdevget->category_stat & DEV_6666BPI) { printf("6666_bpi\n"); } else if(pdevget->category_stat & DEV_8000_BPI) { printf("8000_bpi\n"); } else if(pdevget->category_stat & DEV_10000_BPI) { printf("10000_bpi\n"); } else if(pdevget->category_stat & DEV_10240BPI) { printf("10240_bpi\n"); } else if(pdevget->category_stat & DEV_16000_BPI) { printf("16000_bpi\n"); } else if(pdevget->category_stat & DEV_38000BPI) { printf("38000_bpi\n"); } else if(pdevget->category_stat & DEV_38000_CP) { printf("38000_bpi compacted\n"); } else if(pdevget->category_stat & DEV_76000BPI) { printf("76000_bpi\n"); } else if(pdevget->category_stat & DEV_76000_CP) { printf("76000_bpi compacted\n"); } else { /* * Density unknown. */ printf("unspecified density\n"); } } else if(pdevget->category == DEV_TERMINAL) { if(pdevget->category_stat & DEV_MODEM) { printf("modem_control "); if(pdevget->category_stat & DEV_MODEM_ON) { printf("on\n"); } else { printf("off\n"); } } else { printf("\n"); } } else { printf("\n"); } } return; } ifile = open(file, 0); if(ifile < 0) { if (pflg) printf("%s\n", (unsigned)errno < sys_nerr? sys_errlist[errno]: "Cannot read"); return(MASH(UNKNOWN, UNKNOWN)); } retnum = fdtype(ifile, &mbuf, &in, buf, BUFSIZ, pflg); if (retnum) { return(retnum); } if (pflg) printf("Entering grock code\n"); /* check for various types of ascii text files now using keyword lists */ if (cprog() == 1) { if (pflg) printf("c program text"); retnum = CPROG; /* checkgarbage(); */ goto outa; } else if (fortprog() == 1) { if (pflg) printf("fortran program text"); retnum = FORTPROG; goto outa; }notfort: i=0; if(ascom() == 0)goto notas; j = i-1; if(buf[i] == '.'){ i++; if(lookup(as) == 1){ if (pflg) printf("assembler program text"); retnum = ASSEMBLER; goto outa; } else if(buf[j] == '\n' && isalpha(buf[j+2])){ if (pflg) printf("roff, nroff, or eqn input text"); retnum = NROFF; goto outa; } } while(lookup(asc) == 0){ if(ascom() == 0)goto notas; while(buf[i] != '\n' && buf[i++] != ':') if(i >= in)goto notas; while(buf[i] == '\n' || buf[i] == ' ' || buf[i] == '\t')if(i++ >= in)goto notas; j = i-1; if(buf[i] == '.'){ i++; if(lookup(as) == 1){ if (pflg) printf("assembler program text"); retnum = ASSEMBLER; goto outa; } else if(buf[j] == '\n' && isalpha(buf[j+2])){ if (pflg) printf("roff, nroff, or eqn input text"); retnum = NROFF; goto outa; } } } if (pflg) printf("assembler program text"); retnum = ASSEMBLER; goto outa;notas: if (troffint(buf, in)) { if (pflg) printf("troff intermediate output text"); retnum = TROFFINT; } else if (shell(buf, in, sh)) { if (pflg) printf("%sshell commands", execmodes(&mbuf, buf)); retnum = SHELL; } else if (shell(buf, in, csh)) { if (pflg) printf("%sc-shell commands", execmodes(&mbuf, buf)); retnum = CSHELL; } else if (english(buf, in)) { if (pflg) printf("English text"); retnum = ENGLISH; } else { if (pflg) printf("ASCII text"); retnum = UNKNOWN; }outa: while(i < in) if((buf[i++]&0377) > 127){ if (pflg) printf(" with garbage\n"); return(MASH(ASCIIwGARBAGE, retnum)); } /* if next few lines in then read whole file looking for nulls ... while((in = read(ifile,buf,BUFSIZ)) > 0) for(i = 0; i < in; i++) if((buf[i]&0377) > 127){ if (pflg) printf(" with garbage\n"); return(MASH(ASCIIwGARBAGE, retnum)); } /*.... */ if (pflg) printf("\n"); return(MASH(ASCII, retnum));}mkmtab(cflg)reg int cflg;{ reg Entry *ep; reg FILE *fp; reg int lcnt = 0; auto char buf[BSZ]; auto Entry *mend; auto majnum, minnum; char *strchr(); ep = (Entry *) calloc(sizeof(Entry), NENT); if(ep == NULL) { fprintf(stderr, "file: no memory for magic table.\n"); exit(2); } mtab = ep; mend = &mtab[NENT]; fp = fopen(mfile, "r"); if(fp == NULL) { fprintf(stderr, "file: cannot open magic file <%s>.\n", mfile); exit(2); } while(fgets(buf, BSZ, fp) != NULL) { reg char *p = buf; reg char *p2, *p3; reg char opc; int count; if(*p == '\n' || *p == '#') continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -