/* Magnetic Tape Manipulation Program
** mtm [-sn] [-lm] [-bp] [unit]
**
** skip n files
** list m files
** buffer size pK bytes
** on unit 
**
** Assumes definition of raw magnetic tapes and the 
** mapping of 4-7 into physical drives 0-3
*/
char *buf;
int bcnt;
int filecnt;
int fd;
int ccnt;
int unit;
char *file ;

main(argc,argv)
int argc;
char *argv[];
{
	char *p;
	int nskip, nlist, skip, list;
	int finis();

	signal(2,finis);
	signal(3,finis);
	unit = nskip = nlist = 0;
	skip = 0;
	list = 1;
	bcnt = 2;
	file = "/dev/rmt0 ";
	filecnt=1;
	argv++;

	while(--argc) {
		p = *argv++;
		if(*p == '-'){
			++p;
			switch(*p){

			case 's':
				skip++;
				list--;
				nskip = atoi(++p);
				break;
	
			case 'l':
				list++;
				nlist = atoi(++p);
				break;
	
			case 'b':
				bcnt = atoi(++p);
				if(bcnt < 30) break;
				printf("Requested buffer too big %dK\n",bcnt);
				exit(8);
			default:
				printf("Bad option\n");
				exit(8);
			}
		}
		else{

			if((*p < '0') || (*p> '9')){
				printf("Syntax Error\n");
				exit(8);
			}
			unit = atoi(p);
		}
	}

	bcnt = bcnt<<10;
	if ((buf = sbrk(bcnt)) < 0) {
		printf("requested buffer too big - %dK\n",bcnt);
		exit(8);
	}

	unit = unit | 04;	/* force to no rewind unit number */
	ccnt = 1;
	if(unit > 9)
		ccnt = 2;
	itoa(unit,ccnt,&file[8]);
	if((fd = open(file, 0)) < 0) {
		printf("Tape Open Error %s\n",file);
		exit(8);
	}

	if(skip)
		files(nskip,0);
	if(list){
		if(nlist == 0)
			nlist = -1;
		files(nlist,1);
		finis();
	}
}

files(nfiles,lflag)
int nfiles, lflag;
{
	int cnt, prevcnt, reccnt, eof;
	if(nfiles == 0)return;
	eof=0;
	for(; nfiles--; filecnt++){
		cnt = reccnt = prevcnt = 0;
		do{
			prevcnt = cnt;
			if((cnt = read(fd,buf,bcnt)) == 0){	/* EOF */
				if(eof){	/* double eof */
					printf("Double EOF after file %d\n",
						--filecnt);
					finis();
				}
				else{
					eof=1;
					if(lflag)
						printf("      %d Records\n",reccnt);
				}
			}
			else{
				eof = 0;
				if((++reccnt == 1) && lflag) printf("File %d:\n",filecnt);
				if(cnt<0){	/* error */
					if(lflag) printf("      Record %d - ERROR\n",reccnt);
				}
				else{	/*data read*/
					if(lflag && cnt != prevcnt)
						printf("      Record %d - %d bytes\n",
							reccnt,cnt);
				}
			}
		}while(eof==0);
	}
}

atoi(cp)
char *cp;
{
	int sum;
	int *minus;

	sum = minus =0;

	if(*cp == '-') {
		minus++;
		*cp++;
	}

	while (*cp <= '9' && *cp >= '0') {
		sum =* 10;
		sum =+ (*cp++ - '0');
	}

	if(minus) sum = -sum;
	return(sum);
}


/* convert "numb" to "width" ASCII characters in "buf" with leading zeros. */

itoa(numb,width,array)
char *array;
{
	int i, *minus;

	minus = 0;
	i = width;
	while (i--) array[i] = '0';

	if(numb < 0) {
		minus++;
		numb = -numb;
	}

	array[width] = '\0';
	while(width) {
		array[--width] = numb%10 + '0';
		numb =/ 10;
	}

	return(minus);
}
finis()
{
	unit =- 4;
	ccnt = 1;
	if(unit > 9)
		ccnt = 2;
	itoa(unit,ccnt,&file[8]);
	close(fd);
	fd = open(file, 0);
	close(fd);
	printf("DONE\n");
	exit(0);
}
