rdt.c

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

C
2,753
字号
				else if( strcmp(a, "6250")==0)					strcpy(magtdev, "/dev/rmt9");				else					{					/*					 * The name following '-D' is used					 * literally as the tape device name.					 */					s1 = magtdev;					s2 = a;					for(i=0; (*s1++ = *s2++) && i < MAXLEN; i++);					if(i >= MAXLEN)						errorexit("%s: file name too long", a, 0);					}				break;			case 'L':				if(strlen(++a) > 6)					errorexit("maximum label length is 6", 0, 0);				strcpy(label, a);				upper(label);				break;			case 'B':				sscanf(++a, "%5s", bsize);				blocksize = num(bsize);				if(blocksize > MAXBLKSIZE || blocksize < 4)					errorexit("invalid blocksize (max %d).",					MAXBLKSIZE, 0);				break;			case 'I':				s1 = inputfile;				s2 = ++a;				for(i=0; (*s1++ = *s2++) && i < MAXLEN; i++);				if(i >= MAXLEN)					{					error("%s: file name too long", a, 0);					break;					}				if(*a == 0 || *a == '-')					iflag = -1;				else					{					iflag = 1;					if((ifp = fopen(inputfile, "r")) == NULL)						errorexit("cannot open %s", inputfile, 0);					}				break;#if 0/*			case 'R':				sscanf(++a, "%d", &reclength);				break;			case 'O':				if(func == CREATE)					errorexit("the -O option does not apply to the 'c' function.", 0, 0);				sscanf(++a, "%d", &oskip);				if(oskip < 0)					errorexit("invalid number of skipped files", 0, 0);				oflag = YES;				break;			case 'S':				sscanf(++a, "%d", &nskip);				if(nskip < 0)					errorexit("invalid number of skipped files", 0, 0);				/*				 * each LTF file entry consists of				 * three actual tape files.				 */				nskip *= 3;				break;			case 'P':				if(*++a == 0)					errorexit("no position file specified", 0, 0);				/* check for an occurrence request */				s1 = a;				s1 += strlen(a) - 1;				if(*s1 == '}')					{					while(--s1 != a && *s1 >= '0' && *s1 <= '9');					if(*s1 == '{')						{						*s1 = 0;						if(*++s1 < '1' || *s1 > '9')							errorexit("invalid occurrence request in position file.", 0, 0);						sscanf(s1, "%d", &poscount);						posnth = poscount;						}					else						poscount = -1;					}				else					poscount = -1;				if(! make_num(a, posname, posnum))					errorexit("invalid file name for -P option", 0, 0);				pos = -1;				break;			case 'N':				use_versnum = YES;				cmp = mstrcmp_dot;				break;			case 'W':				warning_only = YES;				break;			case 'M':				rep_misslinks = YES;				break;			case 'T':				toggle = YES;				if(func == TABLE)					error("the -T option does not apply when listing files.", 0, 0);				break;*/#endif				default:				error("%c: unknown flag", *a, 0);				usage();			}	if(reclength > MAXRECSIZE || reclength < 1)		errorexit("invalid record length (max %d).", MAXRECSIZE, 0);	if(func == CREATE || ! oflag)		rew();	/*	 * If LTF is to create a new tape, magtdev will be opened	 * for writing in createtp().	 */	if(func != CREATE && ((magtfp = fopen(magtdev, "r")) == NULL))		errorexit("cannot open %s for reading", magtdev, 0);	if(func == TABLE || func == EXTRACT)		{		int	dumpflag = 0;		for( ; argc > 0; argc--, argv++)			{			if(! strcmp(*argv,"-d"))				{				if(func == EXTRACT)					{					/*					 * dumpflag is not set to 0 if toggle					 * is NO because we want to check					 * in scantape() if DD and FUF have					 * together been specified for one					 * file.					 */					if(toggle)						dumpflag = 0;					dumpflag |= DD;					}				else					error("the -d option does not apply when listing files", 0, 0);				}			else if(! strcmp(*argv,"-u"))				{				if(func == EXTRACT)					{					if(toggle)						dumpflag = 0;					dumpflag |= FUF;					}				else					error("the -u option does not apply when listing files", 0, 0);				}			else if(! strcmp(*argv, "-"))				dumpflag = 0;			else if(! strcmp(*argv, "-b") || ! strcmp(*argv, "-t"))				error("-b and -t flags are not needed to list or extract files", 0, 0);			else if((*argv)[0] == '-')				error("%s: unknown flag", *argv, 0);			else				{				rec_args(*argv, dumpflag);				if(! toggle)					dumpflag = 0;				}			}		if(iflag == 1)			rec_file(ifp, '\0', "");		else if(iflag == -1)			rec_file(stdin, inputfile[0], "");		scantape();		}	else if(func == WRITE)		{		if(iflag == 1)			fclose(ifp);		writetp(argc, argv, iflag, inputfile);		}	else if(func == CREATE)		{		if(iflag == 1)			fclose(ifp);		createtp(argc, argv, iflag, inputfile);		}	else errorexit("no function specified", 0, 0);	}/*--------------------------------------------------------------------- * *  REC_ARGS  -  sorts file arguments into 'filestat' structure. * *-------------------------------------------------------------------*/rec_args(filename, dumpflag)char	*filename;int	dumpflag;	{	struct	filestat *fstat;	char	*l;	fstat = (struct filestat *) malloc(sizeof(*fstat));	if(fstat == NULL)		errorexit("Too many arguments.  Out of memory.", 0, 0);	fstat->f_next = f_head;	f_head = fstat;	fstat->f_src[0] = 0;	l = filename;	l += strlen(filename) - 1;	/* check for an occurrence request */	if(*l == '}')		{		while(*--l >= '0' && *l <= '9');		if(*l == '{')			{			*l = 0;			if(*++l < '1' || *l > '9')				{				error("invalid occurrence request .  {%s part of %s ignored.", l, filename);				fstat->f_numleft = -1;				}			else				sscanf(l, "%d", &fstat->f_numleft);			}		else			fstat->f_numleft = -1;		}	else		fstat->f_numleft = -1;	if(make_num(filename, fstat->f_dest, fstat->f_version))		{		numrecs++;		fstat->f_flags = dumpflag;		}	else		{		f_head = fstat->f_next;		free((char *)fstat);		}	return;	}/*--------------------------------------------------------------------- * *  ERROR  -  prints non-fatal error messages.  Arguments are passed *	      to 'error' like this because fprintf does not recognize *	      the %r format option. * *-------------------------------------------------------------------*/error(s1, s2, s3)char	*s1, *s2, *s3;	{	fprintf(stderr, "%s WARNING: ", progname);	fprintf(stderr, s1, s2, s3);	fprintf(stderr, "\n");	}/*--------------------------------------------------------------------- * *  ERROREXIT  -  prints fatal error messages. * *-------------------------------------------------------------------*/errorexit(s1, s2, s3)char	*s1, *s2, *s3;	{	fprintf(stderr, "%s ERROR: ", progname);	fprintf(stderr, s1, s2, s3);	fprintf(stderr, "\n");	exit(1);	}/*--------------------------------------------------------------------- * *  UPPER  -  converts lower-case letters in the string 's' to *	      upper-case. * *-------------------------------------------------------------------*/upper(s)char	*s;	{	while(*s != 0)		{		if(islower(*s))			*s = toupper(*s);		s++;		}	}/*--------------------------------------------------------------------- * *  LOWER  -  converts upper-case letters in the string 's' to *	      lower-case. * *-------------------------------------------------------------------*/lower(s)char	*s;	{	while(*s != 0)		{		if(isupper(*s))			*s = tolower(*s);		s++;		}	}/*--------------------------------------------------------------------- * *  SCANTAPE  -  scans the tape for the TABLE and EXTRACT functions. * *-------------------------------------------------------------------*/scantape()	{	char	name[MAXLEN];		/*					 * extracted file name according					 * to the tape labels.					 */	char	xname[MAXLEN];		/*					 * actual extracted file name					 * if different from 'name'					 * above.					 */	long	ret;	long	xtractf();	long	charcnt;		/*					 * character count of locally-					 * appended binary files.					 * This is just a precaution					 * in case the last character					 * of a binary file is the					 * same as the padding					 * character.					 */	long	modtime;		/*					 * modification time of					 * locally-appended files					 */	int	verno_exp;		/* expanded version of version number */	char	verno_exp_a[6];		/* ascii representation of verno_exp */	char	pathname[77];	char	cat_misc[15];		/* misc. string used for concatenating */	int	mode;			/* file mode */	int	uid;			/* user id */	int	gid;			/* group id */	int	linkflag;		/* set if link() is successful */	int	lnk_fsecno;		/* file section no. of head link file */	int	lnk_fseqno;		/* file sequence no. of head link file */	char	lnk_name[MAXLEN];	/* file name of head link file */	struct	xlinkbuf *lp;	struct	filestat *lookup();	struct	filestat *fstat;	static	char sdate[13];		/* creation-date string */	int	i;/* Read the VOLUME LABEL (VOL1) from the input tape.*/	if(! oflag)		{		i = read(fileno(magtfp), labelbuf, LABSIZE);		if(i < 0)			errorexit("cannot read tape.  Incorrect tape density???", 0, 0);		else if(i == 0)			errorexit("cannot read	VOL1  from tape.  Empty tape???", 0, 0);			l_labelid[0] = 0;		sscanf(labelbuf, "%3s%1d%6s", l_labelid, &l_labelno, l_volid);#ifdef DEBUG	pr_label();#endif		if(strcmp(l_labelid, "VOL"))			{			errorexit("%s: illegal label format (VOL)", l_labelid, 0);			}		if(strcmp(l_volid, label))				strcpy(label, l_volid);				upper(label);			fprintf(stderr,"\nVolume label is: %s\n\n", l_volid);		}	else if(oskip && skip(oskip) != oskip)		errorexit("there aren't that many files to skip (-O).", 0, 0);	/*	 * turn off oflag because the -S option does not skip	 * over valid end-of-tape marks like -O option does.	 * Re: skip()	 */	oflag = NO;	if(nskip && (skip(nskip) != nskip))		errorexit("there aren't that many files to skip (-S).", 0, 0);	for(;;)		{		pathname[0] = 0;		labelbuf[BUFSIZE] = 0;		charcnt = 0L;		modtime = 0L;		linkflag = NO;		lnk_fsecno = 0;		lnk_fseqno = 0;/*   Read  HDR1  from the tape.*/		if(read(fileno(magtfp), labelbuf, LABSIZE) <= 0)			{			if(pos == -1)				not_on_tape();			exit(0);			}		sscanf(labelbuf, "%3s%1d%17s%6s%4d%4d%4d%2d %5s %5s%*7c%13s",		l_labelid, &l_labelno, l_filename, l_volid, &l_fsecno,		&l_fseqno, &l_gen, &l_genver, l_credate, l_expirdate, l_systemid);		lower(l_filename);		if( ! strcmp(l_labelid, "EOV"))			{			if(pos == -1)				not_on_tape();			exit(0);			}#ifdef DEBUG	pr_label();#endif		if(strcmp(l_labelid, "HDR") || l_labelno != 1)			{			errorexit("%s%d: illegal label format (HDR1)",			l_labelid, l_labelno);			}/*   Read  HDR2  from the tape.*/		if(read(fileno(magtfp), labelbuf, LABSIZE) <= 0)			errorexit("cannot read HDR2 from tape", 0, 0);		sscanf(labelbuf, "%3s%1d%c%5d%5d", l_labelid,		&l_labelno, &l_recformat, &l_blklen, &l_reclen);#ifdef DEBUG	pr_label();#endif		if(strcmp(l_labelid, "HDR") || l_labelno != 2)			{			errorexit("%s%d: illegal label format (HDR2)",			l_labelid, l_labelno);			}/*   Ask the question:  IS THIS ONE OF OUR TAPES ?*/		if(! strcmp(l_systemid, SYSNAME))			{/*   Read  HDR3  from the tape (if one of ours)..*/			if(read(fileno(magtfp), labelbuf, LABSIZE) <= 0)				errorexit("cannot read HDR3 from tape", 0, 0);			labelbuf[LABSIZE] = 0;#ifdef DEBUG	pr_label();#endif			sscanf(labelbuf, "%3s%1d%76s", l_labelid,			&l_labelno, pathname);			if(strcmp(l_labelid, "HDR") || l_labelno != 3)				{				errorexit("%s%d: illegal label format (HDR3)",				l_labelid, l_labelno);				}/*	WHAT DOES THE FOLLOWING TEST IMPLY ?*/			if(labelbuf[4] != ' ')				lower(pathname);/*   Read  HDR4  from the tape. Ultrix native mode tapes always have   HDR1  thru  HDR4  labels.*/			if(read(fileno(magtfp), labelbuf, LABSIZE) < 0)				errorexit("cannot read HDR4 from tape", 0, 0);#ifdef DEBUG	pr_label();#endif			sscanf(labelbuf, "%3s%1d%4o%4d%4d%4d%4d%10ld%10ld", l_labelid,			&l_labelno, &mode, &uid, &gid,			&lnk_fsecno, &lnk_fseqno, &charcnt, &modtime);			if(strcmp(l_labelid, "HDR") || l_labelno != 4)				{				errorexit("%s%d: illegal label format (HDR4)",				l_labelid, l_labelno);				}/*   We only try to read	HDR5  "if"  this is a native mode Ultrix tape   and the current file has (or is) a link.*/			if(func == TABLE && lnk_fsecno != 0 && lnk_fseqno != 0)				{				if(read(fileno(magtfp), labelbuf, LABSIZE) < 0)					errorexit("cannot read HDR5 from tape", 0, 0);#ifdef DEBUG	pr_label();#endif				sscanf(labelbuf, "%3s%1d%76s", l_labelid,				&l_labelno, lnk_name);				if(strcmp(l_labelid, "HDR") || l_labelno != 5)					{					errorexit("%s%d: illegal label format (HDR5)",					l_labelid, l_labelno);					}				}			}		/* check if file is an FUF */		else if(! strcmp(l_systemid, "DECFILE11A"))			{			int	num1 = 0;			int	num2 = 0;			if((read(fileno(magtfp), labelbuf, LABSIZE)) <= 0)				{

⌨️ 快捷键说明

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