⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list.c

📁 unix 下tar 执行程序的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* List a tar archive.
   Copyright (C) 1988 Free Software Foundation

This file is part of GNU Tar.

GNU Tar is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.

GNU Tar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Tar; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/*
 * List a tar archive.
 *
 * Also includes support routines for reading a tar archive.
 *
 * this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
 *
 * @(#)list.c 1.31 11/5/87 - gnu
 */
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef	MSDOS
#include <sys/file.h>
#endif	/* MSDOS */

#ifdef USG
#include <sys/sysmacros.h>	/* major() and minor() defined here */
#endif

char *ctime();				/* From libc.a */

#define	isodigit(c)	( ((c) >= '0') && ((c) <= '7') )

#include "tar.h"
#include "port.h"

extern FILE *msg_file;

long from_oct();			/* Decode octal number */
void demode();				/* Print file mode */

union record *head;			/* Points to current archive header */
struct stat hstat;			/* Stat struct corresponding */
int head_standard;			/* Tape header is in ANSI format */

void print_header();
void skip_file();
void skip_extended_headers();

extern char *quote_copy_string();


/*
 * Main loop for reading an archive.
 */
void
read_and(do_something)
	void (*do_something)();
{
	int status = 3;			/* Initial status at start of archive */
	int prev_status;
	extern time_t new_time;
	char save_linkflag;
	
	name_gather();			/* Gather all the names */
	open_archive(1);		/* Open for reading */

	for(;;) {
		prev_status = status;
		status = read_header();
		switch (status) {

		case 1:			/* Valid header */
			/* We should decode next field (mode) first... */
			/* Ensure incoming names are null terminated. */
			head->header.name[NAMSIZ-1] = '\0';

			if (   !name_match(head->header.name)
 			    || (f_new_files && hstat.st_mtime<new_time)
 			    || (f_exclude && check_exclude(head->header.name))) {

				int isextended = 0;

				if(   head->header.linkflag==LF_VOLHDR
 				   || head->header.linkflag==LF_MULTIVOL
				   || head->header.linkflag==LF_NAMES) {
					(*do_something)();
					continue;
				}
				/* Skip past it in the archive */
				if (head->header.isextended)
					isextended = 1;
				save_linkflag = head->header.linkflag;
				userec(head);
				if (isextended) {
/*					register union record *exhdr;

					for (;;) {
					    exhdr = findrec();
					    if (!exhdr->ext_hdr.isextended) {
					    	userec(exhdr);
					    	break;
					    }
					}
					userec(exhdr);*/
					skip_extended_headers();
				}
				/* Skip to the next header on the archive */
				if(save_linkflag != LF_DIR)
					skip_file((long)hstat.st_size);
				continue;

			}

			(*do_something)();
			continue;

			/*
			 * If the previous header was good, tell them
			 * that we are skipping bad ones.
			 */
		case 0:			/* Invalid header */
			userec(head);
			switch (prev_status) {
			case 3:		/* Error on first record */
				msg("Hmm, this doesn't look like a tar archive.");
				/* FALL THRU */
			case 2:		/* Error after record of zeroes */
			case 1:		/* Error after header rec */
				msg("Skipping to next file header...");
			case 0:		/* Error after error */
				break;
			}
			continue;

		case 2:			/* Record of zeroes */
			userec(head);
			status = prev_status;	/* If error after 0's */
			if (f_ignorez)	
				continue;
			/* FALL THRU */
		case EOF:		/* End of archive */
			break;
		}
		break;
	};

	close_archive();
	names_notfound();		/* Print names not found */
}		


/*
 * Print a header record, based on tar options.
 */
void
list_archive()
{
	extern char *save_name;
	int	isextended = 0; /* Flag to remember if head is extended */
	
	/* Save the record */
	saverec(&head);

	/* Print the header record */
	if (f_verbose) {
		if (f_verbose > 1)
			decode_header(head, &hstat, &head_standard, 0);
		print_header();
	}

	if(f_gnudump && head->header.linkflag==LF_DUMPDIR) {
		size_t	size, written, check;
		char	*data;
		extern int errno;
		extern long save_totsize;
		extern long save_sizeleft;

		userec(head);
		if(f_multivol) {
			save_name = head->header.name;
			save_totsize=hstat.st_size;
		}
		for(size = hstat.st_size;size>0;size-=written) {
			if(f_multivol)
				save_sizeleft=size;
			data = findrec()->charptr;
			if(data==NULL) {
				msg("EOF in archive file?");
				break;
			}
			written = endofrecs()->charptr - data;
			if(written>size)
				written=size;
			errno=0;
			check=fwrite(data,sizeof(char), written, msg_file);
			userec((union record *)(data+written - 1));
			if(check!=written) {
				msg_perror("only wrote %ld of %ld bytes to file %s",check, written,head->header.name);
				skip_file((long)(size)-written);
				break;
			}
		}
		if(f_multivol)
			save_name = 0;
		saverec((union record **) 0);	/* Unsave it */
		fputc('\n',msg_file);
		fflush(msg_file);
		return;

	}
	saverec((union record **) 0);	/* Unsave it */
	/* Check to see if we have an extended header to skip over also */
	if (head->header.isextended) 
		isextended = 1;
		
	/* Skip past the header in the archive */
	userec(head);

	/*
 	 * If we needed to skip any extended headers, do so now, by
 	 * reading extended headers and skipping past them in the 
	 * archive.
	 */
	if (isextended) {
/*		register union record *exhdr;

		for (;;) {
			exhdr = findrec();

			if (!exhdr->ext_hdr.isextended) {
				userec(exhdr);
				break;
			}
			userec(exhdr);
		}*/
		skip_extended_headers();
	}
			
	if(f_multivol)
		save_name=head->header.name;
	/* Skip to the next header on the archive */
		
	skip_file((long) hstat.st_size);
		
	if(f_multivol)
		save_name = 0;
}


/*
 * Read a record that's supposed to be a header record.
 * Return its address in "head", and if it is good, the file's
 * size in hstat.st_size.
 *
 * Return 1 for success, 0 if the checksum is bad, EOF on eof,
 * 2 for a record full of zeros (EOF marker).
 *
 * You must always userec(head) to skip past the header which this
 * routine reads.
 */
int
read_header()
{
	register int	i;
	register long	sum, recsum;
	register char	*p;
	register union record *header;
	long	from_oct();

	header = findrec();
	head = header;		/* This is our current header */
	if (NULL == header)
		return EOF;

	recsum = from_oct(8,  header->header.chksum);

	sum = 0;
	p = header->charptr;
	for (i = sizeof(*header); --i >= 0;) {
		/*
		 * We can't use unsigned char here because of old compilers,
		 * e.g. V7.
		 */
		sum += 0xFF & *p++;
	}

	/* Adjust checksum to count the "chksum" field as blanks. */
	for (i = sizeof(header->header.chksum); --i >= 0;)
		sum -= 0xFF & header->header.chksum[i];
	sum += ' '* sizeof header->header.chksum;	

	if (sum == recsum) {
		/*
		 * Good record.  Decode file size and return.
		 */
		if (header->header.linkflag == LF_LINK)
			hstat.st_size = 0;	/* Links 0 size on tape */
		else
			hstat.st_size = from_oct(1+12, header->header.size);
		return 1;
	}

	if (sum == 8*' ') {
		/*
		 * This is a zeroed record...whole record is 0's except
		 * for the 8 blanks we faked for the checksum field.
		 */
		return 2;
	}

	return 0;
}


/* 
 * Decode things from a file header record into a "struct stat".
 * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
 * Standard" tar format or regular old tar format.
 *
 * read_header() has already decoded the checksum and length, so we don't.
 *
 * If wantug != 0, we want the uid/group info decoded from Unix Standard
 * tapes (for extraction).  If == 0, we are just printing anyway, so save time.
 *
 * decode_header should NOT be called twice for the same record, since the
 * two calls might use different "wantug" values and thus might end up with
 * different uid/gid for the two calls.  If anybody wants the uid/gid they
 * should decode it first, and other callers should decode it without uid/gid
 * before calling a routine, e.g. print_header, that assumes decoded data.
 */
decode_header(header, st, stdp, wantug)
	register union record	*header;
	register struct stat	*st;
	int	*stdp;
	int	wantug;
{

	long from_oct();

	st->st_mode = from_oct(8,  header->header.mode);
	st->st_mtime = from_oct(1+12, header->header.mtime);
	if(f_gnudump) {
		st->st_atime = from_oct(1+12, header->header.atime);
		st->st_ctime = from_oct(1+12, header->header.ctime);
	}
	
	if (0==strcmp(header->header.magic, TMAGIC)) {
		/* Unix Standard tar archive */
		*stdp = 1;

⌨️ 快捷键说明

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