/*static char *SCCSID = "@(#)local.c	6.2 91/01/14";*/
#define SCCSID "@(#)local.c	6.2 91/01/14"
/*
 *	IBM/Microsoft Confidential
 *
 *	Copyright (c) IBM Corporation 1987, 1989
 *	Copyright (c) Microsoft Corporation 1987, 1989
 *
 *	All Rights Reserved
 *
 */

/***	LOCAL.C - Sub-level routine for CDROM CHKDSK.
 *
 *	CHKDSK - CDROM version 1.0
 * Paul Bingman, NWSP
 * Copyright 1988, 1989 Microsoft Corporation
 *
 * This CDROM version was adapted from the Pinball version by
 *	Gregory A. Jones
 *
 *	Modification history:
 *	Paul Bingman  8/22/89  Original CDROM
 */

#define INCL_DOSERRORS
#define INCL_NOPMAPI

#include <os2.h>
#include <stdio.h>
#include "defs.h"
#include "ldefs.h"
#include <ctype.h>
#include "gdata.h"


/***	parse -- parse the CHKDSK command line
 *
 *	Scan the CHKDSK command line looking for the drive.
 *
 *	parse(c, v)
 *
 *	ENTRY   c - count of command line arguments, incl. "CHKDSK"
 *          v - list of pointers to command line arguments
 *
 *	EXIT   Number of specified drive.
 *
 *	CALLS  computmsg() for errors
 *
 *	EFFECTS None
 */

int
NEAR parse(c, v)
USHORT c;
UCHAR   **v;
{
	USHORT  i;		/* general purpose counter */
	int d = 0;	/* numeric equivalent of drive letter found	*/
	UCHAR   *array[2];

	/*
	 * process each argument (don't process "CHKDSK")
	 */

	for (i = 1; i < c; i++)
	{
		/*
		 * The only valid argument is a drive letter followed by a colon.
		 * It's an error if it's another kind of argument or
		 * if a second drive letter is specified.
		 * It's not an error if no drive letter is specified,
		 * will use the default drive.
		 */

		if (isalpha(v[i][0]) && v[i][1]==':' && d==0)
		{
			d = toupper(v[i][0]) - '@';
			if (v[i][2] != '\0')
			{
				/*
				 * An additional parameter was found on the command line.
				 * Give an error message for it.
				 */

				array[0] = &v[i][2];

				computmsg(array,
					1,
					CHK_ARG_ERROR,
					msgfilename,
					STDOUT_CODE,
					NO_RESPONSE,
					NULL,
					0);
			}
		}
		else
		{
			/*
			 * Some parameter besides drive letter.
			 * Give an error message for it.
			 */

			array[0] = v[i];

			computmsg(array,
				1,
				CHK_ARG_ERROR,
				msgfilename,
				STDOUT_CODE,
				NO_RESPONSE,
				NULL,
				0);
		}
	}

	return (d);	/* pass back the drive */
}


/***	stats -- display the CHKDSK statistics
 *
 *	Display the total KB size of the disk & KB free space
 *
 *	stats(d)
 *
 *	ENTRY  d - drive to get stats on (0=default, 1=A:, 2=B:, 26=Z:)
 *
 *	EXIT   Number of current drive.
 *
 *	CALLS  computmsg() for output
 *
 *	EFFECTS None
 */

int
NEAR stats(d)
int d;
{
	FSALLOCATE fs1;	/* struct used to get volume size info */
	USHORT  usRet;		/* general-purpose return code holder */
	UCHAR   *array[2];

	/*
	 * Get the disk information for this drive
	 */

	if (usRet = (DosQFSInfo(d, 1, (PBYTE)&fs1, sizeof(fs1))) != 0)
	{
		/*
		 * DosQFSInfo failed.
		 * Notify the user.
		 * Return the error.
		 */

		computmsg (NULL,
			0,
			CHK_UNEXP_OS_ERROR,
			msgfilename,
			STDERR_CODE,
			NO_RESPONSE,
			NULL,
			0);
		return (usRet);
	}
	else
	{

		/*
		 * Show total allocation (size) of CD-ROM
		 *
		 * To prevent overflow, I divide out the 1K early:
		 *  2 out of the total allocation units
		 *  512 out of the allocation size (can never be <512 for CDROM)
		 */
		array[0] = outbuf;

		sprintf(outbuf, "%10ld",
			(fs1.cUnit / 2L) * ((fs1.cSectorUnit * (long)(fs1.cbSector)) / 512));
		computmsg(array,
			1,
			CHK_KB_USED_STR,
			msgfilename,
			STDOUT_CODE,
			NO_RESPONSE,
			NULL,
			0);

		/*
		 * Show free space on CD-ROM (always 0)
		 */

		array[0] = outbuf;

		sprintf(outbuf, "%10ld",
			(fs1.cUnitAvail / 2L) * ((fs1.cSectorUnit * (long)(fs1.cbSector)) / 512));
		computmsg(array,
			1,
			CHK_KB_FREE_STR,
			msgfilename,
			STDOUT_CODE,
			NO_RESPONSE,
			NULL,
			0);

		return (NO_ERROR);
	}
}

/* end of main.c (CDROM - CHKDSK) */

