files.c

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

C
941
字号
{	TIMETYPE date = 0L;	if(openarch(archname) == -1)		return(0L);	if(flag)		date = entryscan(am);	/* fatals if cannot find entry */	else		date = afilescan(am);	clarch();	return(date);}TIMETYPEafilescan(name)		/* return date for named archive member file */char *name;{	int	len = strlen(name);	long	ptr;	if (fseek(arfd, first_ar_mem, 0) != 0)	{	seek_error:;		fatal1("Seek error on archive file %s", archname);	}	/*	* Hunt for the named file in each different type of	* archive format.	*/	switch (ar_type)	{	case ARpdp:		for (;;)		{			if (fread((char *)&ar_pdp,				sizeof(ar_pdp), 1, arfd) != 1)			{				if (feof(arfd))					return (0L);				break;			}			if (equaln(ar_pdp.ar_name, name, len))				return (ar_pdp.ar_date);			ptr = ar_pdp.ar_size;			ptr += (ptr & 01);			if (fseek(arfd, ptr, 1) != 0)				goto seek_error;		}		break;#ifndef pdp11	case AR5:		for (;;)		{			if (fread((char *)&arf_5, sizeof(arf_5), 1, arfd) != 1)			{				if (feof(arfd))					return (0L);				break;			}			if (equaln(arf_5.arf_name, name, len))				return (sgetl(arf_5.arf_date));			ptr = sgetl(arf_5.arf_size);			ptr += (ptr & 01);			if (fseek(arfd, ptr, 1) != 0)				goto seek_error;		}		break;	case ARport:		for (;;)		{			if (fread((char *)&ar_port, sizeof(ar_port), 1,				arfd) != 1 || !equaln(ar_port.ar_fmag,				ARFportMAG, sizeof(ar_port.ar_fmag)))			{				if (feof(arfd))					return (0L);				break;			}			if (equaln(ar_port.ar_name, name, len))			{				long date;				if (sscanf(ar_port.ar_date, "%ld", &date) != 1)				{					fatal1("Bad date field for %.14s in %s",						name, archname);				}				return (date);			}			if (sscanf(ar_port.ar_size, "%ld", &ptr) != 1)			{				fatal1("Bad size field for %.14s in archive %s",					name, archname);			}			ptr += (ptr & 01);			if (fseek(arfd, ptr, 1) != 0)				goto seek_error;		}		break;#endif	}	/*	* Only here if fread() [or equaln()] failed and not at EOF	*/	fatal1("Read error on archive %s", archname);	/*NOTREACHED*/#if 0	long date;	long nsyms;	long ptr;	nsyms = sgetl(head.ar_syms);	if(fseek(arfd, (long)( nsyms*sizeof(symbol)+sizeof(head) ), 0) == -1)		fatal("Seek error on archive file %s", archname);	for(;;)	{		if(fread(&fhead, sizeof(fhead), 1, arfd) != 1)			if(feof(arfd))				break;			else				fatal("Read error on archive %s", archname);		if(equaln(fhead.arf_name, name, 14))		{			date = sgetl(fhead.arf_date);			return(date);		}		ptr = sgetl(fhead.arf_size);		ptr = (ptr+1)&(~1);		fseek(arfd, ptr, 1);	}	return( 0L );#endif}TIMETYPEentryscan(name)		/* return date of member containing global var named */char *name;{	/*	* Hunt through each different archive format for the named	* symbol.  Note that the old archive format does not support	* this convention since there is no symbol directory to	* scan through for all defined global variables.	*/	if (ar_type == ARpdp)		return (pdpentrys(name));	if (sym_begin == 0L || num_symbols == 0L)	{	no_such_sym:;		fatal1("Cannot find symbol %s in archive %s", name, archname);	}	if (fseek(arfd, sym_begin, 0) != 0)	{	seek_error:;		fatal1("Seek error on archive file %s", archname);	}#ifndef pdp11	if (ar_type == AR5)	{		register int i;		int len = strlen(name);		if (len > 8)			len = 8;		for (i = 0; i < num_symbols; i++)		{			if (fread((char *)&ars_5, sizeof(ars_5), 1, arfd) != 1)			{			read_error:;				fatal1("Read error on archive %s", archname);			}			if (equaln(ars_5.sym_name, name, len))			{				if (fseek(arfd, sgetl(ars_5.sym_ptr), 0) != 0)					goto seek_error;				if (fread((char *)&arf_5,					sizeof(arf_5), 1, arfd) != 1)				{					goto read_error;				}				return (sgetl(arf_5.arf_date));			}		}	}	else	/* ar_type == ARport */	{		register int strtablen;		register char *offs;	/* offsets table */		register char *syms;	/* string table */		register char *strend;	/* end of string table */		register char *strbeg;		/*		* Format of the symbol directory for this format is		* as follows:	[sputl()d number_of_symbols]		*		[sputl()d first_symbol_offset]		*			...		*		[sputl()d number_of_symbols'_offset]		*		[null_terminated_string_table_of_symbols]		*/		if ((offs = calloc(num_symbols,sizeof(long))) == NULL)		{			fatal1("Cannot calloc offsets table for archive %s",				archname);		}		if (fread(offs, sizeof(long), num_symbols, arfd) != num_symbols)			goto read_error;		strtablen = sym_size - ((num_symbols + 1) * sizeof(long));		if ((syms = calloc(1,strtablen)) == NULL)		{			fatal1("Cannot calloc string table for archive %s",				archname);		}		if (fread(syms, sizeof(char), strtablen, arfd) != strtablen)			goto read_error;		strend = &syms[strtablen];		strbeg = syms;		while (syms < strend)		{			if (equal(syms, name))			{				long ptr, date;				ptr = sgetl(offs);				if (fseek(arfd, ptr, 0) != 0)					goto seek_error;				if (fread((char *)&ar_port, sizeof(ar_port), 1,					arfd) != 1 || !equaln(ar_port.ar_fmag,					ARFportMAG, sizeof(ar_port.ar_fmag)))				{					goto read_error;				}				if (sscanf(ar_port.ar_date, "%ld", &date) != 1)				{					fatal1("Bad date for %.14s, archive %s",						ar_port.ar_name, archname);				}				free(strbeg);				return (date);			}			syms += strlen(syms) + 1;			offs += sizeof(long);		}		free(strbeg);	}#endif	goto no_such_sym;#if 0	register int i;	long date;	long ptr;	long nsyms;	nsyms = sgetl(head.ar_syms);	for(i = 0; i < nsyms; i++)	{		if(fread(&symbol, sizeof(symbol), 1, arfd) != 1)badread:			fatal("Read error on archive %s", archname);		if(equaln(symbol.sym_name, name, 8))		{			ptr = sgetl(symbol.sym_ptr);			if(fseek(arfd, ptr, 0) == -1)				fatal("Seek error on archive file %s",archname);			if(fread(fhead, sizeof(fhead), 1, arfd) != 1)				goto badread;			date = sgetl(fhead.arf_date);			return(date);		}	}	fatal("Cannot find symbol %s in archive %s", name, archname);#endif}TIMETYPEpdpentrys(name)	char *name;{	long	skip;	long	last;	int	len;	register int i;#ifndef pdp11	fatal("Cannot do global variable search in pdp11 or old object file.");#endif	len = strlen(name);	if (len > 8)		len = 8;	/*	* Look through archive, an object file entry at a time.  For each	* object file, jump to its symbol table and check each external	* symbol for a match.  If found, return the date of the module	* containing the symbol.	*/	if (fseek(arfd, sizeof(short), 0) != 0)	{	seek_error:;		fatal1("Cannot seek on archive file %s", archname);	}	while (fread((char *)&ar_pdp, sizeof(ar_pdp), 1, arfd) == 1)	{		last = ftell(arfd);		if (ar_pdp.ar_size < sizeof(arobj_pdp) ||			fread((char *)&arobj_pdp, sizeof(arobj_pdp), 1, arfd)			!= 1 ||			(arobj_pdp.a_magic != 0401 &&	/* A_MAGIC0 */			arobj_pdp.a_magic != 0407 &&	/* A_MAGIC1 */			arobj_pdp.a_magic != 0410 &&	/* A_MAGIC2 */			arobj_pdp.a_magic != 0411 &&	/* A_MAGIC3 */			arobj_pdp.a_magic != 0405 &&	/* A_MAGIC4 */			arobj_pdp.a_magic != 0437)) 	/* A_MAGIC5 */		{			fatal1("%s is not an object module", ar_pdp.ar_name);		}		skip = arobj_pdp.a_text + arobj_pdp.a_data;		if (!arobj_pdp.a_flag)			skip *= 2;		if (skip >= ar_pdp.ar_size || fseek(arfd, skip, 1) != 0)			goto seek_error;		skip = ar_pdp.ar_size;		skip += (skip & 01) + last;		i = (arobj_pdp.a_syms / sizeof(ars_pdp)) + 1;		while (--i > 0)		/* look through symbol table */		{			if (fread((char *)&ars_pdp, sizeof(ars_pdp), 1, arfd)				!= 1)			{				fatal1("Read error on archive %s", archname);			}			if ((ars_pdp.n_type & 040)	/* N_EXT for pdp11 */				&& equaln(ars_pdp.n_name, name, len))			{				(void)strncpy(archmem, ar_pdp.ar_name, 14);				archmem[14] = '\0';				return (ar_pdp.ar_date);			}		}		if (fseek(arfd, skip, 0) != 0)			goto seek_error;	}	return (0L);}openarch(f)register CHARSTAR f;{	unsigned short	mag_pdp;		/* old archive format */	char		mag_5[SAR5MAG];		/* 5.0 archive format */	char		mag_port[SARportMAG];	/* port (6.0) archive format */	arfd = fopen(f, "r");	if(arfd == NULL)		return(-1);	/*	* More code for three archive formats.  Read in just enough to	* distinguish the three types and set ar_type.  Then if it is	* one of the newer archive formats, gather more info.	*/	if (fread((char *)&mag_pdp, sizeof(mag_pdp), 1, arfd) != 1)		return (-1);	if (mag_pdp == (unsigned short)ARpdpMAG)	{		ar_type = ARpdp;		first_ar_mem = ftell(arfd);		sym_begin = num_symbols = sym_size = 0L;		return (0);	}	if (fseek(arfd, 0L, 0) != 0 || fread(mag_5, SAR5MAG, 1, arfd) != 1)		return (-1);	if (equaln(mag_5, AR5MAG, SAR5MAG))	{		ar_type = AR5;		/*		* Must read in header to set necessary info		*/		if (fseek(arfd, 0L, 0) != 0 ||			fread((char *)&arh_5, sizeof(arh_5), 1, arfd) != 1)		{			return (-1);		}#ifdef pdp11		fatal1("Cannot handle 5.0 archive format for %s", archname);		/*NOTREACHED*/#else		sym_begin = ftell(arfd);		num_symbols = sgetl(arh_5.ar_syms);		first_ar_mem = sym_begin + sizeof(ars_5) * num_symbols;		sym_size = 0L;		return (0);#endif	}	if (fseek(arfd, 0L, 0) != 0 ||		fread(mag_port, SARportMAG, 1, arfd) != 1)	{		return (-1);	}	if (equaln(mag_port, ARportMAG, SARportMAG))	{		ar_type = ARport;		/*		* Must read in first member header to find out		* if there is a symbol directory		*/		if (fread((char *)&ar_port, sizeof(ar_port), 1, arfd) != 1 ||			!equaln(ARFportMAG, ar_port.ar_fmag,			sizeof(ar_port.ar_fmag)))		{			return (-1);		}#ifdef pdp11		fatal1("Cannot handle portable archive format for %s",			archname);		/*NOTREACHED*/#else		if (ar_port.ar_name[0] == '/')		{			char s[4];			if (sscanf(ar_port.ar_size, "%ld", &sym_size) != 1)				return (-1);			sym_size += (sym_size & 01);	/* round up */			if (fread(s, sizeof(s), 1, arfd) != 1)				return (-1);			num_symbols = sgetl(s);			sym_begin = ftell(arfd);			first_ar_mem = sym_begin + sym_size - sizeof(s);		}		else	/* there is no symbol directory */		{			sym_size = num_symbols = sym_begin = 0L;			first_ar_mem = ftell(arfd) - sizeof(ar_port);		}		return (0);#endif	}	fatal1("%s is not an archive", f);	/*NOTREACHED*/#if 0	if(fread(&head, sizeof(head), 1, arfd) != 1)		return(-1);	if(!equaln(head.ar_magic, ARMAG, 4))		fatal1("%s is not an archive", f);	return(0);#endif}clarch(){	if(arfd != NULL)		fclose(arfd);}/* *	Used when unlinking files. If file cannot be stat'ed or it is *	a directory, then do not remove it. */isdir(p)char *p;{	struct stat statbuf;	if(stat(p, &statbuf) == -1)		return(1);		/* no stat, no remove */	if((statbuf.st_mode&S_IFMT) == S_IFDIR)		return(1);	return(0);}

⌨️ 快捷键说明

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