filetype.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 943 行 · 第 1/2 页
C
943 行
#ifndef lintstatic char *sccsid = "@(#)filetype.c 4.1 (ULTRIX) 7/17/90";#endif lint/**************************************************************** * * * Copyright (c) 1985 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. * * * * 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. * * * ****************************************************************//**//* * * File name: * * filetype.c * * Source file description: * * This file contains logic to determine the * general type of file specified by the caller. * ie. Is it binary, text, library, etc.. * * (further documentation below) * * It is used by the Labeled Tape Facility (LTF) * in order to determine the type that the Unix * file should take on whence it becomes an * ANSI tape file. * * * Functions: * * ascom() ... * ccom() ... * english() ... * Filetype() Top level logic to determine file type. * lookupe() ... * troffint() Check for troff intermediate file * * * Compile: * * cc -O -c filetype.c <- For Ultrix-32/32m * * cc CFLAGS=-DU11-O filetype.c <- For Ultrix-11 * * * Modification history: * ~~~~~~~~~~~~~~~~~~~~ * * revision comments * -------- ----------------------------------------------- * 01.1 23-August-89 Sam Lambert * Modified Filetype() to handle non-null terminated * "ascii" files (U32_SPR #00095). * * 01.0 09-April-85 Ray Glaser * Create orginal version. * *//**//* * -> Local includes */#include "ltfdefs.h" /* Common LTF definitions *//* * -> Local defines "required for / defined by" this module */#define MBUFSIZ 1024 /* Chunk of file to read for "typing" */#define NL 012 /* Newline character */#define NLM 123 /* Used to check for 'c' code *//* * -> Globals for this module only */char *as[] = { "globl","byte","align","text","data","comm",0};char *asc[] = { "chmk","mov","tst","clr","jmp",0};char buf[MBUFSIZ];char *c[] = { "int","char","float","double","struct","extern",0};char *com[] = { "alias", "date","cc","xref","pr","pr50","CFLAGS","lpr","rm","FILES", "tar","mdtar","sync","make","/etc","/bin","for","case", "echo","do","umask","set","stty","setenv", 00 };int errno;char *fort[] = { "function","subroutine","common","dimension","block","integer", "real","data","double",0};int i = 0;FILE *ifile;int in;char *troff[] = { /* new troff intermediate lang */ "x","T","res","init","font","202","V0","p1",0};int Type; /* Tentative file type value *//**//* * * Function: * * Filetype * * Function Description: * * This function attempts to determine the type of Unix disk file. * It returns a generalized file type indication as defined in * the include file "filetypes.h". * * Arguments: * * * char *file Points to a null terminated string * assumed to be the desired file name * to be typed. * * char *caller Points to a string defining the desired * routine name for messages to stderr. * * Return value(s): * * int value * * Zero if the file type could not be determined, else: * one of the file types defined in "filetypes.h". * Additionally, the character string variables "Tftypes" * is filled with a 3 character representation of the * true Ultrix disk file type. (see README.1 file). * * * Side Effects: * * This function outputs error messages to stderr if any problems * occur during the attempt to access/type the given file. * *//**//* * FILETYPE Determination thereof.. */Filetype(file,caller) char *caller; char *file;{/* * -> Local variables */int nl;struct stat mbuf;/*------*\ Code\*------*/strcpy(Tftypes,"???"); /* Assume we can't determine file type */mbuf = Inode;/* Check if this is a file that should not be open*/switch (mbuf.st_mode & S_IFMT) { case S_IFCHR: /* Charater special */ strcpy(Tftypes,"csp"); return(CHCTRSP); case S_IFBLK: /* Block special */ strcpy(Tftypes,"bsp"); return(BLKSP); case S_IFIFO: /* Fifo - pipe */ strcpy(Tftypes,"pip"); return(CHCTRSP);#ifndef U11 case S_IFSOCK: /* Socket ! */ strcpy(Tftypes,"soc"); return(SOCKET);#endif}/*E switch mbuf.st_mode & S_IFMT */if((ifile = fopen(file, "r")) == NULL) { PERROR "\n%s: %s %s\n", caller, CANTOPW, file); perror(caller); return(EOF);}switch (mbuf.st_mode & S_IFMT) { case S_IFLNK: /* Symbolic link */ strcpy(Tftypes,"sym"); fclose(ifile); return(SYMLNK); case S_IFDIR: /* Directory */ strcpy(Tftypes,"dir"); fclose(ifile); return(DIRECT);}/*E switch mbuf.st_mode & S_IFMT *//* * Read in a MBUFSIZ amount of data from the file for * further examination. */if ((in = read(fileno(ifile), buf, MBUFSIZ)) <= 0) if (in < 0) { PERROR "\n%s: %s %s\n", caller, CANTRD, file); perror(caller); exit(FAIL); } else { fclose(ifile); strcpy(Tftypes,"nul"); return(EMPTY); /* File appears to be empty */ }/* * This check is looking for files that are used as output * of ltf instead of using tape. NOTE: This is bogus as * the rest of all these checks are. This is the bare * minimum of checking. Should check not only for VOL1, * but also HDR1, etc., tape mark, then EOF1, etc., tape * mark. Thus this checks for VOL1 in the first 4 characters, * and makes the file type binary. */if (!strncmp(buf, "VOL1", 4)) { fclose(ifile); strcpy(Tftypes,"bin"); return(BINARY); /* Data file of some type */}switch(*(int *)buf) { case 0407: /* Old impure Format on Ultrix-11 should * be PDP-11 normal (I space only). * On Ultrix-32 - seems to indicate * a .o (object) file. */ fclose(ifile);#ifdef U11 /* check to see if relocation info is stripped */ if (((int *)buf)[7]) strcpy(Tftypes,"exe"); else#endif strcpy(Tftypes,"bin"); return(BINARY); case 0410: /* Read-only/shared text */ case 0413: /* Demand Load Format */#ifdef U11 case 0401: /* PDP-11 Standalone executable */ case 0411: /* PDP-11 Separated I & D */ case 0430: /* PDP-11 7 Overlays */ case 0431: /* PDP-11 7 Overlays */ case 0450: /* PDP-11 15 Overlays */ case 0451: /* PDP-11 15 Overlays */#endif fclose(ifile); strcpy(Tftypes,"exe"); return(BINARY); case 0177555: /* Very old archive */ case 0177545: /* Old archive */ fclose(ifile); strcpy(Tftypes,"oar"); return(BINARY); case 070707: /* CPIO data */ fclose(ifile); strcpy(Tftypes,"cpi"); return(CPIO);}/*E switch(*(int *)buf) */if(strncmp(buf, "!<arch>\n__.SYMDEF", 17) == 0 ) { fclose(ifile); strcpy(Tftypes,"arl"); return(BINARY); /* Archive Random Library */}if (strncmp(buf, "!<arch>\n", 8)==0) { fclose(ifile); strcpy(Tftypes,"arc"); return(BINARY); /* Archive */}if (mbuf.st_size % 512 == 0) { /* it may be a PRESS file */ lseek(fileno(ifile), -512L, 2); /* last block */ if ((in = read(fileno(ifile), buf, MBUFSIZ)) <= 0) { if (in < 0) { PERROR "\n%s: %s %s\n", caller, CANTRD, file); perror(caller); exit(FAIL); } } if (in > 0 && *(int *)buf == 12138) { fclose(ifile); strcpy(Tftypes,"cmp"); return(BINARY); /* Press file ..*/ }}/*E if mbuf.st_size ..*//* * See if it looks like a command file. */i = 0;while(buf[i] == ' ' || buf[i] == '#' || buf[i] == '!') { while(buf[i++] != '\n') if(i >= in) goto notcom;}/*E while buf[i] ..*/#if 0 /*for debugging*/printf("\ngoing to lookupe \n");for (x=i;x<i+80;x++) printf("%c",buf[x]);#endif 0if (lookupe(com)==1) { Type = TEXT; strcpy(Tftypes,"com"); goto outa;}notcom:i = 0;if (!ccom()) { goto notc;}while(buf[i] == '#') { j = i; while(buf[i++] != '\n') { if(i - j > 255) { fclose(ifile); strcpy(Tftypes,"adf"); return(BINARY); /* Data file of some type */ } if(i >= in) { goto notc; } }/* while buf[i++] ...*/ if(!ccom()) { goto notc; }}/*E while buf[i] ..*//* */check:if(lookupe(c) == 1) { while((ch = buf[i++]) != ';' && ch != '{') if(i >= in) { goto notc; } strcpy(Tftypes,"cc "); Type = TEXT; /* 'C' program text */ goto outa;}/*E if lookupe(c) *//* Look for newline or null character */if (!strchr(buf, '\n')) { lseek(fileno(ifile), -512L, L_XTND); /* last block */ if ((in = read(fileno(ifile), buf, MBUFSIZ)) < 0) { PERROR "\n%s: %s %s\n", caller, CANTRD, file); perror(caller); exit(FAIL); } if ((!strchr(buf, '\n')) || (!strchr(buf, '\0'))) { fclose(ifile); strcpy(Tftypes,"bin"); return(BINARY); } else { /* Re-read the first bit of the file for the following tests */ lseek(fileno(ifile), 0L, L_SET); /* beginning of file */ if ((in = read(fileno(ifile), buf, MBUFSIZ)) < 0) { PERROR "\n%s: %s %s\n", caller, CANTRD, file); perror(caller); exit(FAIL); } } }nl = 0;while(buf[i] != '(') { if(buf[i] <= 0) goto notas; if(buf[i] == ';') { i++; goto check; } if(buf[i++] == '\n') { if(nl++ > NLM) { goto notc; } if(i >= in) { goto notc; } }}/*E while(buf[i] ..*/while(buf[i] != ')') { if(buf[i++] == '\n') if(nl++ > NLM) { goto notc; } if(i >= in) { goto notc; }}/*E while buf[i] ..*/while(buf[i] != '{') { if(buf[i++] == '\n') if(nl++ > NLM) { goto notc; } if(i >= in) { goto notc; }}/*E while buf[i] ..*/strcpy(Tftypes,"cc ");Type = TEXT; /* 'C' program text */goto outa;/* */notc:i = 0;while(buf[i] == 'c' || buf[i] == '#') { while(buf[i++] != '\n') if(i >= in) goto notfort;}/*E while buf[i] ..*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?