#define _GNU_SOURCE
#include <sys/time.h>
#include <stdio.h>      /* standard I/O                */
#include <stdlib.h>     /* for exit   - 1 occurrence   */
#include <unistd.h>
#include <pthread.h>
#include <db.h>


#include <math.h>       /* for sin, exp etc.           */
#include <string.h>     /* for strcpy - 3 occurrences  */
#include <time.h>
#include <ctype.h>

struct timeval starttime,endtime;
double te0, te1, te2, test;
FILE *fp;
int i;
//char *test;

void title() {
        printf("\tlsnet is a product of the linuxsociety team\n");
        printf("\tThis is version  dated \n");
        printf("\t\n\n");
return;
}

void usage() {
        printf("Usage: lsnet -[option]\n");
        printf("Options:\n");
        printf("        -h: display this help menu\n");
        printf("        -a: run all daemons\n");
        printf("        -1: run cpu daemon\n");
        printf("        -2: run mem daemon\n");
        printf("        -3: run net daemon\n");
        printf("\n");
        printf("\n");
}

int Writer (char *test) {
	FILE *fp;
	fp=fopen("test.txt", "a");
	int counter;
	for (counter = 1; counter <= 10; counter++)
	{
		fprintf(fp, "%s\n", test);
	}
	fclose(fp);
//	printf("%d\n",test);
	return 0;
}


//int power(int base, int n)
//{printf("%d %d %d\n", i, power(2,i), power(-3,i));

int All() {
	//title();
	//char *test = "Thu Feb 19 23:23:24 2009 119.00+24.91 0.00 0.99 74.05 0.03 0.00 0.02+13.44 0.00 0.48 86.00 0.00 0.02 0.06+32.06 0.00 0.66 67.24 0.03 0.00 0.02+15.95 0.00 0.36 83.68 0.00 0.00 0.00";

char tenminarray[] = tenminute();
	Writer(test);
	//fp=fopen("lsnet.conf", "r");

	//fscanf(fp, "%lf", &x);
	//test=x;
	//fclose(fp);
        return 0;
}


int main(int argc, char **argv) {   
        int o;
        while((o=getopt(argc, argv, "ha123")) != EOF) {
                switch(o) {
                        case 'h':
                                usage();
                                exit(1);
                                break;
                        case 'a':
                                All();
                                exit(1);
                                break;
                        case '1':
                                title();
                                //system("memdir/lsmem");
                                //fp=fopen("memdir/lsmem.log", "r");
                                //fscanf(fp, "%lf", &x);
                                //test=x;
                                //fclose(fp);
                                printf("\n\n\tfinished.\n");
                                printf("\nLSBENCH Results:");
                                printf("\n\tMemory index:           %.3f \n", test);
                                exit(1);
                                break;
                        case '2':
                                title();
                                //system("fpudir/lsfpu");
                                //fp=fopen("fpudir/lsfpu.log", "r");
                                //fscanf(fp, "%lf", &x);
                                //test=x;
                                //fclose(fp);
                                printf("\n\n\tfinished.\n");
                                printf("\nLSBENCH Results:");
                                printf("\n\tFPU index:              %lf\n", test);
                                exit(1);
                                break;
                        case '3':
                                title();
                               // system("aludir/lsalu");                           
                                //fp=fopen("aludir/lsalu.log", "r");
                                //fscanf(fp, "%lf", &x);
                                //test=x;
                                //fclose(fp);
                                printf("\n\n\tfinished.\n");
                                printf("\nLSBENCH Results:");
                                printf("\n\tALU index:              %lf\n", test);
                                exit(1);
                                break;  
                       default:
                                break;
                        }
        }   
        while(optind) {
                title();
                usage();        
                printf("\nError: No run options specified!\n\n");
                exit(1);        
        }
                                
                        
return 0;
}


##########Determine number of cores in system
#define MAXLINE 1000
int getline(char line[], int max)
int strindex(char source[], char searchfor[]);
char pattern[] = "cpu"; /* pattern to search for */


cores() {

char line[MAXLINE];
int coresdetected = 0;

while (getline(line, MAXLINE) > 0)
	if (strindex(line, pattern) >= 0) 
	{
		printf("%s", line);
		coresdetected++;
	}
coresdetected = coresdetected - 1;
return coresdetected;
}


/* getline: get line into s, return length */
int getline(char s[], int lim)
{
	int c, i;
	i = 0;
	while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
		s[i++] = c;
	if (c == '\n')
		s[i++] = c;
	s[i] = '\0';
	return i;
}

/* strindex: return index of t in s, -1 if none */
int strindex(char s[], char t[])
{
	int i, j, k;
	for (i = 0; s[i] != '\0'; i++) 
	{
		for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
			;
		if (k > 0 && t[k] == '\0')
			return i;
	}
	return -1;
}



my $coresdetected = 0;
open(CPUINFO, "/proc/stat") or return undef;
while(<CPUINFO>){
        chomp;
        if(/^cpu\d+/)
        {
                $coresdetected++;
        }
}
close(CPUINFO);
print "CORES detected: $coresdetected\n";
$coresdetected = --$coresdetected;
#print "decremented: $coresdetected\n";
}
##############################################


tenminute(){


}


oneminute(){

}

cpu(){

}

net(){

}

mem(){

}
