files.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 941 行 · 第 1/2 页
C
941 行
#ifndef lintstatic char *sccsid = "@(#)files.c 4.1 (ULTRIX) 8/17/88";#endif lint/* ------------------------------------------------------------------ *//* | Copyright Unpublished, MIPS Computer Systems, Inc. All Rights | *//* | Reserved. This software contains proprietary and confidential | *//* | information of MIPS and its suppliers. Use, disclosure or | *//* | reproduction is prohibited without the prior express written | *//* | consent of MIPS. | *//* ------------------------------------------------------------------ *//* $Header: files.c,v 1.5 86/07/11 13:07:01 dce Exp $ *//* * ke 3/7/85 Added ifdef for 4.2bsd file system stuff. Also sys/types.h * and sys/dir.h now get included defs. * ke 3/13/85 Added DPATH extensions to do path searching for implicit * rules. What changed for this file was the call to findfl() * now has two arguments. The second argument was added to * findfl() which is the macro name with the path information. */#include "defs"#include <sys/stat.h>#include <pwd.h>#include <ar.h>/* UNIX DEPENDENT PROCEDURES */#define equaln !strncmp/** For 6.0, create a make which can understand all three archive* formats. This is kind of tricky, and <ar.h> isn't any help.* Note that there is no sgetl() and sputl() on the pdp11, so* make cannot handle anything but the one format there.*/char archmem[64]; /* archive file member name to search for */char archname[64]; /* archive file to be opened */int ar_type; /* to distiguish which archive format we have */#define ARpdp 1#define AR5 2#define ARport 3long first_ar_mem; /* where first archive member header is at */long sym_begin; /* where the symbol lookup starts */long num_symbols; /* the number of symbols available */long sym_size; /* length of symbol directory file *//** Defines for all the different archive formats. See next comment* block for justification for not using <ar.h>'s versions.*/#define ARpdpMAG 0177545 /* old format (short) magic number */#define AR5MAG "<ar>" /* 5.0 format magic string */#define SAR5MAG 4 /* 5.0 format magic string length */#define ARportMAG "!<arch>\n" /* Port. (6.0) magic string */#define SARportMAG 8 /* Port. (6.0) magic string length */#define ARFportMAG "`\n" /* Port. (6.0) end of header string *//** These are the archive file headers for the three formats. Note* that it really doesn't matter if these structures are defined* here. They are correct as of the respective archive format* releases. If the archive format is changed, then since backwards* compatability is the desired behavior, a new structure is added* to the list.*/struct /* pdp11 -- old archive format */{ char ar_name[14]; /* '\0' terminated */ long ar_date; /* native machine bit representation */ char ar_uid; /* " */ char ar_gid; /* " */ int ar_mode; /* " */ long ar_size; /* " */} ar_pdp;struct /* pdp11 a.out header */{ short a_magic; unsigned a_text; unsigned a_data; unsigned a_bss; unsigned a_syms; /* length of symbol table */ unsigned a_entry; char a_unused; char a_hitext; char a_flag; char a_stamp;} arobj_pdp;struct /* pdp11 a.out symbol table entry */{ char n_name[8]; /* null-terminated name */ int n_type; unsigned n_value;} ars_pdp;struct /* UNIX 5.0 archive header format: vax family; 3b family */{ char ar_magic[SAR5MAG]; /* AR5MAG */ char ar_name[16]; /* ' ' terminated */ char ar_date[4]; /* sgetl() accessed */ char ar_syms[4]; /* sgetl() accessed */} arh_5;struct /* UNIX 5.0 archive symbol format: vax family; 3b family */{ char sym_name[8]; /* ' ' terminated */ char sym_ptr[4]; /* sgetl() accessed */} ars_5;struct /* UNIX 5.0 archive member format: vax family; 3b family */{ char arf_name[16]; /* ' ' terminated */ char arf_date[4]; /* sgetl() accessed */ char arf_uid[4]; /* " */ char arf_gid[4]; /* " */ char arf_mode[4]; /* " */ char arf_size[4]; /* " */} arf_5;struct /* Portable (6.0) archive format: vax family; 3b family */{ char ar_name[16]; /* '/' terminated */ char ar_date[12]; /* left-adjusted; decimal ascii; blank filled */ char ar_uid[6]; /* " */ char ar_gid[6]; /* " */ char ar_mode[8]; /* left-adjusted; octal ascii; blank filled */ char ar_size[10]; /* left-adjusted; decimal ascii; blank filled */ char ar_fmag[2]; /* special end-of-header string (ARFportMAG) */} ar_port;#if 0 /* no longer used! */ /* * New common object version of files.c */ char archmem[64]; char archname[64]; /* name of archive library */ struct ar_hdr head; /* archive file header */ struct ar_sym symbol; /* archive file symbol table entry */ struct arf_hdr fhead; /* archive file object file header */#endifTIMETYPE afilescan();TIMETYPE entryscan();TIMETYPE pdpentrys();FILE *arfd;char BADAR[] = "BAD ARCHIVE";TIMETYPEexists(pname)NAMEBLOCK pname;{ register CHARSTAR s; struct stat buf; TIMETYPE lookarch(); CHARSTAR filename; filename = pname->namep; if(any(filename, LPAREN)) return(lookarch(filename)); if(stat(filename,&buf) < 0) { s = findfl(filename); if(s != (CHARSTAR )-1) { pname->alias = copys(s); if(stat(pname->alias, &buf) == 0) return(buf.st_mtime); } return(0); } else return(buf.st_mtime);}TIMETYPEprestime(){ TIMETYPE t; time(&t); return(t);}#ifndef BSD4XFSTATIC char n15[15];FSTATIC CHARSTAR n15end = &n15[14];#endif BSD4XDEPBLOCKsrchdir(pat, mkchain, nextdbl)register CHARSTAR pat; /* pattern to be matched in directory */int mkchain; /* nonzero if results to be remembered */DEPBLOCK nextdbl; /* final value for chain */{ int i, nread; CHARSTAR dirname, dirpref, endir, filepat, p; char temp[MAXPATHLEN + 1]; char fullname[MAXPATHLEN + 1]; CHARSTAR p1, p2; NAMEBLOCK q; DEPBLOCK thisdbl; OPENDIR od; int dirofl = 0; static opendirs = 0; PATTERN patp;#ifdef BSD4X struct direct *entry; DIR * dirf;#else struct direct entry[32]; FILE * dirf;#endif BSD4X thisdbl = 0; if(mkchain == NO) for(patp=firstpat ; patp!=0 ; patp = patp->nextpattern) if(equal(pat,patp->patval)) return(0); patp = ALLOC(pattern); patp->nextpattern = firstpat; firstpat = patp; patp->patval = copys(pat); endir = 0; for(p=pat; *p!=CNULL; ++p) if(*p==SLASH) endir = p; if(endir==0) { dirname = "."; dirpref = ""; filepat = pat; } else { *endir = CNULL; dirpref = concat(pat, "/", temp); filepat = endir+1; dirname = temp; } dirf = NULL; for(od=firstod ; od!=0; od = od->nextopendir) if(equal(dirname, od->dirn)) { dirf = od->dirfc;#ifdef BSD4X rewinddir(dirf); /* start over at the beginning */#else fseek(dirf,0L,0); /* start over at the beginning */#endif BSD4X break; } if(dirf == NULL) {#ifdef BSD4X dirf = opendir(dirname);#else dirf = fopen(dirname, "r");#endif BSD4X if(++opendirs < MAXODIR) { od = ALLOC(opendir); od->nextopendir = firstod; firstod = od; od->dirfc = dirf; od->dirn = copys(dirname); } else dirofl = 1; } if(dirf == NULL) { fprintf(stderr, "Directory %s: ", dirname); fatal("Cannot open"); } else#ifdef BSD4X while((entry = readdir(dirf)) != NULL){ if( amatch(entry->d_name,filepat) ) { concat(dirpref,entry->d_name,fullname); if( (q=srchname(fullname)) ==0) q = makename(copys(fullname)); if(mkchain) { thisdbl = ALLOC(depblock); thisdbl->nextdep = nextdbl; thisdbl->depname = q; nextdbl = thisdbl; } } }#else do { nread = fread(entry,sizeof(entry[0]),32,dirf) ; for(i=0; i<nread; ++i) if(entry[i].d_ino!= 0) { p1 = entry[i].d_name; p2 = n15; while( (p2<n15end) && (*p2++ = *p1++)!=CNULL ); if( amatch(n15,filepat) ) { concat(dirpref,n15,fullname); if( (q=srchname(fullname)) ==0) q = makename(copys(fullname)); if(mkchain) { thisdbl = ALLOC(depblock); thisdbl->nextdep = nextdbl; thisdbl->depname = q; nextdbl = thisdbl; } } } } while(nread==32);#endif BSD4X if(endir != 0) *endir = SLASH; if(dirofl)#ifdef BSD4X closedir(dirf);#else fclose(dirf);#endif BSD4X return(thisdbl);}/* stolen from glob through find */amatch(s, p)CHARSTAR s, p;{ register int cc, scc, k; int c, lc; scc = *s; lc = 077777; switch (c = *p) { case LSQUAR: k = 0; while (cc = *++p) { switch (cc) { case RSQUAR: if (k) return(amatch(++s, ++p)); else return(0); case MINUS: k |= lc <= scc & scc <= (cc=p[1]); } if(scc==(lc=cc)) k++; } return(0); case QUESTN: caseq: if(scc) return(amatch(++s, ++p)); return(0); case STAR: return(umatch(s, ++p)); case 0: return(!scc); } if(c==scc) goto caseq; return(0);}umatch(s, p)register CHARSTAR s, p;{ if(*p==0) return(1); while(*s) if(amatch(s++,p)) return(1); return(0);}/* look inside archives for notations a(b) and a((b)) a(b) is file member b in archive a a((b)) is entry point b in object archive a*/TIMETYPElookarch(filename)register CHARSTAR filename;{ register int i; CHARSTAR p, q; char s[15]; int nc, objarch; TIMETYPE la(); for(p = filename; *p!= LPAREN ; ++p); i = p - filename; strncpy(archname, filename, i); archname[i] = CNULL; if(archname[0] == CNULL) fatal1("Null archive name `%s'", filename); p++; if(*p == LPAREN) { objarch = YES; ++p; if((q = strchr(p, RPAREN)) == NULL) q = p + strlen(p); strncpy(s,p,q-p); s[q-p] = CNULL; } else { objarch = NO; if((q = strchr(p, RPAREN)) == NULL) q = p + strlen(p); i = q - p; strncpy(archmem, p, i); archmem[i] = CNULL; nc = 14; if(archmem[0] == CNULL) fatal1("Null archive member name `%s'", filename); if(q = strrchr(archmem, SLASH)) ++q; else q = archmem; strncpy(s, q, nc); s[nc] = CNULL; } return(la(s, objarch));}TIMETYPEla(am,flag)register char *am;register int flag;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?