#include <dpb.h>
#include <xfbuf.h>

char *strip_path();

/*

	Tell all about a file or device

*/

#define SY 0x04		/* System attribute, */
#define HI 0x02		/* Hidden attribute, */
#define RO 0x01		/* Read only attribute. */
#define AR 0x20		/* Archive, */
#define VO 0x08		/* Volume ID */
#define DI 0x10		/* directory */

main(argc,argv)
int argc;
char *argv[];
{
char *name,arg[80],path[80];
int drive,n,handle,attrib;
struct _xfbuf xfbuf;
unsigned i[4];
long n1,n2;


	if (argc < 2) {
		printf("TELL: Tom Jennings  1 Nov 91 (13 Apr 1986)\r\n");
		printf("TELL tells all about a file or drive or \r\n");
		printf("device. Please use it like: TELL name. Keep trying,\r\n");
		printf("you'll figure it out eventually.\r\n");
		exit(1);
	}

	cpyarg(arg,argv[1]);
	stolower(arg);
	name= strip_path(path,arg);

	if ((path[1] == ':') && (strlen(name) == 0)) {
		drive= tolower(path[0]) - 'a' + 1;
		if (_getdpb(drive,&dpb) == -1) {
			printf("Be serious, there is no drive %c:\r\n",drive + 'A');
			exit(1);
		}
		printf("%c: is a BLOCK DEVICE:\r\n",drive + 'A' - 1);
		_free(drive,i);			/* get disk stuff, */
		n1= i[3]; n1*= i[2]; n1*= i[0];	/* total clust * bytes/sec * secs/clust */
		n2= i[1]; n2*= i[2]; n2*= i[0];	/* free clust * bytes/sec * secs/clust */

		printf("Total space:       %8lu\r\n",n1);
		printf("Free space:        %8lu\r\n",n2);
		printf("Physical sector size: %5u\r\n",dpb.sector_size);
		printf("Sectors/cluster:      %5u\r\n", dpb.secs_clust);
		printf("First FAT sector:     %5u\r\n",dpb.first_FAT);
		printf("Total FATs:           %5u\r\n",dpb.FAT_count);
		printf("Root dir entries:     %5u\r\n",dpb.root_entries);
		printf("First data sector:    %5u\r\n",dpb.first_sector);
		printf("Total clusters:       %5u\r\n",dpb.max_cluster);
		printf("Sectors/FAT:          %5u\r\n",dpb.FAT_size);
		printf("First dir sector:     %5u\r\n",dpb.dir_sector);
		printf("Media byte:            0x%x\r\n",dpb.media);
		exit(0);
	}

	xfbuf.s_attrib= 0xff;
	n= _find(arg,0,&xfbuf);

	if (n == 0) {
		printf("'%s' doesn't exist and is therefore NOTHING. Get it?\r\n",arg);
		exit(0);
	}
	attrib= 0;
	handle= open(arg,0);
	if (handle != -1) attrib= _ioctl(0,handle,0,0);

	if (attrib & 0x80) {
		printf("%s is a CHARACTER DEVICE:\r\n",arg);
		if (attrib & 0x01) printf("   Console Input\r\n");
		if (attrib & 0x02) printf("   Console Output\r\n");
		if (attrib & 0x04) printf("   Null Device\r\n");
		if (attrib & 0x08) printf("   Clock Device\r\n");
		if (attrib & 0x10) printf("   Fast Device\r\n");
		if (attrib & 0x20) printf("   Raw Mode\r\n");
		  else printf("   Cooked Mode\r\n");
		if (attrib & 0x40) printf("   EOF on Input\r\n");
		if (attrib & 0x4000) printf("   Accepts IOCTL strings\r\n");

	} else {
		printf("%s is a FILE:\r\n",arg);
		if (xfbuf.f_attrib & DI) printf("   Directory\r\n");
		if (xfbuf.f_attrib & AR) printf("   Modified\r\n");
		if (xfbuf.f_attrib & SY) printf("   System File\r\n");
		if (xfbuf.f_attrib & RO) printf("   Read Only\r\n");
		if (xfbuf.f_attrib & HI) printf("   Hidden File\r\n");
		if (xfbuf.f_attrib & VO) printf("   Volume ID\r\n");

		date(xfbuf.date); 
		time(xfbuf.time); 
		printf("   %lu bytes\r\n",xfbuf.fsize);
	}
	exit(0);
}
/* Display the date from the packed word.

 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
 ------- year ------- --- mon --- ---- day -----
 */

char _mon[12][4] = {
	"Jan",
	"Feb",
	"Mar",
	"Apr",
	"May",
	"Jun",
	"Jul",
	"Aug",
	"Sep",
	"Oct",
	"Nov",
	"Dec" };

date(v)
unsigned v;
{
int day,mon,yr;

	day= v & 0x1f;
	mon= (v >> 5) & 0x0f;
	yr= (v >> 9) & 0x7f;
	printf("   %02u %s %2u\r\n",day,_mon[mon - 1],yr+ 80);
}
/* Display MSDOS packed time
15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
---- hour --- ----- mins ------- ----- secs ----
*/
time(v)
unsigned v;
{
int hr,mins,sec;

	sec= v & 0x1f;
	mins= (v >> 5) & 0x3f;
	hr= (v >> 11) & 0x1f;
	printf("   %02u:%02u\r\n",hr,mins);
}

