/* Christopher Huyler */
/* ksamp.c */
/* Solution to Project 1 part B
   - Determine the report type
   		check input args for empty(standard), -s(short), -l(long)
   - Print the CPU type and model
   		open cpu info and read lines begining with
   		vender_id, model name, and cpu MHz
   - Print the Kernel Version
   		open version and read the first three strings
   - Print the time since last boot
   		open uptime, read and convert the first integer
   		to days/hours/minutes/seconds */

#include <stdio.h>
#include <string.h>
#include "short.c"
#include "long.c"

#define MAX_LEN	80

int main(int argc, char *argv[])
{
	int reportType;
    char repTypeName[16];			/* char array for report type */
	char c1,c2;						/* input characters */

	FILE *fp;						/* file pointer */
	char lineBuf[MAX_LEN];			/* char array line buffer */
	int time,days,hrs,mins,secs;	/* integers to calc uptime */

	int interval, duration;			/* interval and duration to take readings */

/* determine report type */
	reportType = 0;
	strcpy(repTypeName,"Standard");
	if(argc > 1)
	{
		sscanf(argv[1],"%c%c",&c1,&c2);
		if(c1 != '-')
		{
			fprintf(stderr,"usage: observer [-s][-l int dur]\n");
			exit(1);
		}
		if(c2 == 's')
		{
			reportType = 1;
			strcpy(repTypeName,"Short");
		}
		if(c2 == 'l')
		{
			reportType = 2;
			strcpy(repTypeName,"Long");
			if(argv[2] == NULL || argv[3] == NULL)
			{ /* catch illegal input */
				fprintf(stderr,"usage: observer [-s][-l int dur]\n");
				exit(1);
			}
			else
			{
				interval = atoi(argv[2]);
				duration = atoi(argv[3]);
			}
		}
	}

	printf("\nReport of Kernel State:\n");

/* get CPU type and model */
	if ((fp = fopen("/proc/cpuinfo","r")) == NULL)
	{
		printf("read: can't open file\n");
		exit(1);
	}
	else
	{/* read/print three lines from cpuinfo file */
		fgets(lineBuf,MAX_LEN,fp);
		while(strncmp("vendor_id",lineBuf,9) != 0)
			fgets(lineBuf,MAX_LEN,fp);
		printf("%s",lineBuf);
		while(strncmp("model name",lineBuf,10) != 0)
			fgets(lineBuf,MAX_LEN,fp);
		printf("%s",lineBuf);
		while(strncmp("cpu MHz",lineBuf,7) != 0)
			fgets(lineBuf,MAX_LEN,fp);
		printf("%s",lineBuf);
		fclose(fp);
	}

/* get kernel version */
	if ((fp = fopen("/proc/version","r")) == NULL)
	{
		printf("read: can't open file\n");
		exit(1);
	}
	else
	{/* read/print first three words from version file */

		/*cptr = lineBuf;
		for(i=0; i<3; i++)

			while((*cptr++ = fgetc(fp)) != ' ');
		*cptr = '\0';
		printf("%s\n",lineBuf); */

		fscanf(fp,"%s",lineBuf);
		printf("%s ",lineBuf);
		fscanf(fp,"%s",lineBuf);
		printf("%s\t: ",lineBuf);
		fscanf(fp,"%s",lineBuf);
		printf("%s\n",lineBuf);
		fclose(fp);
	}

/* get time since last boot */
	if ((fp = fopen("/proc/uptime","r")) == NULL)
	{
		printf("read: can't open file\n");
		exit(1);
	}
	else
	{/* read/format/print first number from uptime file */
		time = 0;
		fscanf(fp,"%d",&time);
		days = time / 86400;
		time = time % 86400;
		hrs  = time / 3600;
		time = time % 3600;
		mins = time / 60;
		secs = time % 60;
		printf("Uptime\t\t: %d:%d:%d:%d",days,hrs,mins,secs);
		printf("\t\t(days/hours/minutes/seconds)\n");
	}

/* print information for second version */
	if(reportType > 0)
		printShort();

/* print information for third version */
	if(reportType > 1)
		printLong(interval,duration);

	printf("\n");
	exit(0);

}

