/* PCLIB.C                                */
/* PCPIP miscellaneous routines           */

istring(string,max)
char *string;
int max;
{	int c,count;
	count = 0;
	c = getchar();
	if (c >= 'a' && c<= 'z')
	   c = c - 'a' + 'A';
	while (c != 10 && count < max)
	   { *string = c;
	   string = string +1;
	   count = count +1;
	   c =getchar();
           if (c >= 'a' && c<= 'z')
	     c = c - 'a' + 'A';
	   }
	*string = '\0';
	return;
}

pchain(cluster,next,fat)
int cluster,next;
char *fat;
	{
	char c;
	int offset;
	char b1,b2,*bp;
	int n1,n2,n3,bi1,bi2;
	int w1,w2,w3,w4;

/* Get nibbles from previous fat      */
	bi1 = 0;
	bi2 = 0;
	offset = cluster + cluster/2;
	b1 = *(fat + offset);
	b2 = *(fat + offset + 1);
	bi1 = b1;
	bi2 = b2;
	bp = &bi1;
	bp = bp+1;
	*bp = 0;
	bp = &bi2;
	bp = bp+1;
	*bp = 0;
	w1 = bi1/16;
	w3 = bi2/16;
	w2 = bi1-(w1*16);
	w4 = bi2-(w3*16);
/* Break next cluster number into nibbles */
	n1 = next/256;
	n2 = (next - n1*256)/16;
	n3 = next - n1*256 - n2*16;
  
/* Determine values for byte 1 and 2 */
	if (2*(cluster/2) == cluster)
	   {
	   bi1 = n2*16 + n3;
	   bi2 = w3*16 + n1;
	   }

	if (2*(cluster/2) != cluster)
	   {
	   bi1 = n3*16 + w2;
	   bi2 = n1*16 + n2;
	   }

/* And store in the fat               */
	b1 = bi1;
	b2 = bi2;
	*(fat + offset) = b1;
	*(fat + offset + 1) = b2;
	return;
}

getfat(cluster,fat)
int cluster;
char *fat;
/**************************************************/                    
/* GETFAT                                         */
/*                                                */
/* Reads the FAT entry for the input cluster and  */
/* returns the next cluster (FFF for EOF)         */
/*                                                */
/**************************************************/
{
	int offset,nextsec;
	char b1,b2,*bp;
	int n1,n2,n3,n4,bi1,bi2;
	bi1 = 0;
	bi2 = 0;
	offset = cluster + cluster/2;
	b1 = *(fat + offset);
	b2 = *(fat + offset + 1);
	bi1 = b1;
	bi2 = b2;
	bp = &bi1;
	bp = bp+1;
	*bp = 0;
	bp = &bi2;
	bp = bp+1;
	*bp = 0;
	n1 = bi1/16;
	n3 = bi2/16;
	n2 = bi1-(n1*16);
	n4 = bi2-(n3*16);
	if (2*(cluster/2) == cluster)
                nextsec = n4*256 + n1*16 + n2;
	if (2*(cluster/2) != cluster)
		nextsec = n3*256 + n4*16 + n1;
	return(nextsec);
}

getstring(len,string,addr)
int len;
char string[],*addr;
{
	int i,j;
	char *dispaddr;
	for (i = 0;i < len;i = i+1)
		{
		dispaddr = addr + i;
		string[i] = *dispaddr;
		}
	string[len] = '\0';
	return;
}

cname(fname,name,ext)
char fname[],name[],ext[];
{	int i,j;
	j = 0;
	for (i = 0;i < 8;i = i+1)
		{
		if (name[i] != ' ' && name[i] != '\0')
			{
			fname[j] = name[i];
			j = j+1;
			}
		}
	for (i = 0;i < 3;i = i+1)
		{
		if (ext[i] != ' ' && ext[i] != '\0')
			{
			if (i == 0)
			 {fname[j] = '.';
			  j = j+1;
			 }
			fname[j] = ext[i];
			j = j+1;
			}
		}
	fname[j] = '\0';
	return;
}

getone()
/**************************************************/                    
/* GETONE                                         */
/*                                                */
/* This routine accepts and returns a single      */
/* character from the keyboard (must hit return)  */
/*                                                */
/**************************************************/
{	int c,d;
	c = getchar();
	if (c >= 'A' && c<= 'Z')
	   c = c + 'a' - 'A';
	d = c;
	while (d != 10)
	   { d = getchar();
	   }
	return c;
}
/********        END OF PCLIB.C      *************/
