/*
* Run thru the command-line args looking for filenames, and generate a
* hexdump of each file.  If there are no filenames, we will produce a
* hexdump of stdin.
*/
:1#include "V.h"

#define BUF 64
char buf[BUF];
int  files = 0;
int  ascfl = 0;	/* ASCII or HEX dump? */

main(ac,av)
	char**av;
{	int   a;
	int   c0;
	int   fd;

:1	ac = Vinit(ac,av);

	for (a=1; a<ac; a++) {
:2		V4 "arg%3d=\"%s\"",a,av[a] D;
		c0 = av[a][0];
		if (c0 == '-' || c0 == '+') {
			switch (av[a][1]) {
				case 'a':
				case 'A':
					ascfl = 1;
:2					V2 "ASCII dump." D;
					break;
				case 'd':
				case 'D':
					Vopt(av[a]+2);
					continue;
				case 'w':
				case 'W':	/* Word size for hex dumps */
					if (sscanf(av[a]+2,"%d",&hex_word) < 1)
						hex_word = 8;
:2					V2 "hex_word=%d.",hex_word D;
					continue;
				default:
:2					V1 "Unknown option \"%s\" ignored.",av[a] D;
					;
			}
			continue;
		}
:2		V3 "File \"%s\"",av[a] D;
		if ((fd = Open2(av[a],0)) > 0) {
:2			V3 "File %d=`%s'",fd,av[a] D;
			hd(fd,av[a]);
			Close(fd);
		}
		++files;
	}
	if (files <= 0) {
:2		V3 "No files, dump stdin:" D;
		hd(0,0);
	}
	Exit(0);
}
hd(fd,file)
	char *file;
{	long b, n;

	b = 0;
	if (file) {
		n = strlen(file);
		Write(1,file,n);
		Write(1,":\n",2);
	}
	while ((n = Read(fd,buf,BUF)) > 0) {
		if (ascfl)
		     Ascfpna(1,buf,n,b);
		else Hexfpna(1,buf,n,b);
		b += n;
	}
}
