//int maxtextbuffer = sizeof(char)*250;
//#define SIZE sizeof(char)*250
#define SIZE 255

int meminfo(int memfrequency, int memarray1[9]) 
{
	memset ((void*) memarray1, '\0', sizeof(memarray1));
	//sleep(1);
	sleep(memfrequency);
	//printf("In meminfo, memfrequency slept: %d\n", memfrequency);
        FILE* fp;
        int count = 0;
	int count2;
	//my($memsize,$memfree,$buffers,$cached,$memused,$swaptotal,$swapfree,$swapused);
	char buf[SIZE];
	char temp[2];
	char bufstr[sizeof(temp)*12];
	char memtotal[sizeof(bufstr)+1];
	char memfree[sizeof(bufstr)+1];
	char buffers[sizeof(bufstr)+1];
	char cached[sizeof(bufstr)+1];
	char swaptotal[sizeof(bufstr)+1];
	char swapfree[sizeof(bufstr)+1];
	int memused, swapused;

	//int linenum = 0;
        if( ( fp = fopen( "/proc/meminfo", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening /proc/meminfo\n" );
                exit( 1 );
        }
        while( fgets(buf, sizeof(buf), fp) != NULL)
        {
		//if (linenum <= cores && linenum != 0)
		//{


		//MemTotal:      2075412 kB
		//MemFree:        417812 kB
		//Buffers:        730692 kB
		count = 0;

			//MemTotal:       776960 kB
		if (buf[count] == 'M'){
                	if (buf[count+1] == 'e'){
                        	if (buf[count+2] == 'm'){
                                	if (buf[count+3] == 'T'){ //count+13 till end of buf is frequency
                                        	if (buf[count+4] == 'o')
                                                {
								count2 = 9;
								//temp[0]=buf[count2];
								while(buf[count2] == ' ') {
									count2++;
								}
								temp[0]=buf[count2];
								temp[1]=0;
								strcpy(bufstr, temp);
								//count2++;
								for( (count2 = count2 + 1); (count2 <= strlen(buf) && buf[count2] != ' '); count2++){
									temp[0]=buf[count2];
									temp[1]=0;
									strcat(bufstr,temp);									

								}
								//memtotal = bufstr;
								strcpy(memtotal, bufstr);
								//memarray1[1] = atoi(bufstr);
                                                }
                                        }
                                }
                        }
                }
			//MemFree:         39520 kB
                if (buf[count] == 'M'){
                        if (buf[count+1] == 'e'){
                                if (buf[count+2] == 'm'){
                                        if (buf[count+3] == 'F'){ //count+13 till end of buf is frequency
                                                if (buf[count+4] == 'r')
                                                {
                                                                count2 = 8;
                                                                //temp[0]=buf[count2];
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								temp[0]=buf[count2];
								temp[1]=0;
                                                                strcpy(bufstr, temp);
								//count2++;
                                                                for( (count2 = count2 + 1); (count2 <= strlen(buf)&& buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                 
                                                                }
								//memfree = bufstr;
								strcpy(memfree, bufstr);
								//memarray1[2] = atoi(bufstr);
                                                }
                                        }
                                }
                        }
                }
			//Buffers:        193200 kB
                if (buf[count] == 'B'){
                        if (buf[count+1] == 'u'){
                                if (buf[count+2] == 'f'){
                                        if (buf[count+3] == 'f'){ //count+13 till end of buf is frequency
                                                if (buf[count+4] == 'e')
                                                {
                                                                count2 = 8;
                                                                //temp[0]=buf[count2];
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								temp[0]=buf[count2];
								temp[1]=0;
                                                                strcpy(bufstr, temp);
								//count2++;
                                                                for( (count2 = count2 + 1); (count2 <= strlen(buf)&& buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                 
                                                                }
								//buffers = bufstr;
								strcpy(buffers, bufstr);
								//memarray1[3] = atoi(bufstr);
                                                }
                                        }
                                }
                        }
                }
			//Cached:         222660 kB
                if (buf[count] == 'C'){
                        if (buf[count+1] == 'a'){
                                if (buf[count+2] == 'c'){
                                        if (buf[count+3] == 'h'){ //count+13 till end of buf is frequency
                                                if (buf[count+4] == 'e')
                                                {
                                                                count2 = 7;
                                                                //temp[0]=buf[count2];
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								temp[0]=buf[count2];
								temp[1]=0;
                                                                strcpy(bufstr, temp);
								//count2++;
                                                                for( (count2 = count2 + 1); (count2 <= strlen(buf)&& buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                 
                                                                }
								//cached = bufstr;
								strcpy(cached, bufstr);
								//memarray1[4] = atoi(bufstr);
                                                }
                                        }
                                }
                        }
                }
			//SwapTotal:      674720 kB
                if (buf[count] == 'S'){
                        if (buf[count+1] == 'w'){
                                if (buf[count+2] == 'a'){
                                        if (buf[count+3] == 'p'){ //count+13 till end of buf is frequency
                                                if (buf[count+4] == 'T')
                                                {
                                                                count2 = 10;
                                                                //temp[0]=buf[count2];
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								temp[0]=buf[count2];
								temp[1]=0;
                                                                strcpy(bufstr, temp);
								//count2++;
                                                                for( (count2 = count2 + 1); (count2 <= strlen(buf)&& buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                 
                                                                }
								//swaptotal = bufstr;
								strcpy(swaptotal, bufstr);
								//memarray1[6] = atoi(bufstr);
                                                }
                                        }
                                }
                        }
                }
			//SwapFree:       672600 kB
                if (buf[count] == 'S'){
                        if (buf[count+1] == 'w'){
                                if (buf[count+2] == 'a'){
                                        if (buf[count+3] == 'p'){ //count+13 till end of buf is frequency
                                                if (buf[count+4] == 'F')
                                                {
                                                                count2 = 9;
                                                                //temp[0]=buf[count2];
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								temp[0]=buf[count2];
								temp[1]=0;
                                                                strcpy(bufstr, temp);
								//count2++;
                                                                for( (count2 = count2 + 1); (count2 <= strlen(buf)&& buf[count2] != ' '); count2++)
								{
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                 
                                                                }
								//swapfree = bufstr;
								strcpy(swapfree, bufstr);
								//memarray1[7] = atoi(bufstr);
                                                }
                                        }
                                }
                        }
                }

	}
	fclose( fp );
	//memused = memtotal - memfree;
	//swapused = swaptotal - swapfree;


	//memsize, memfree, buffers, cached, memused, swaptotal, swapfree, swapused
	//printf("memtotal: %s\n", memtotal);
	//printf("memfree: %s\n", memfree);
	//printf("buffers: %s\n", buffers);
	//printf("cached: %s\n", cached);
	//printf("memused: %s\n", memused);
	//printf("swaptotal: %s\n", swaptotal);
	//printf("swapfree: %s\n", swapfree);
	//printf("swapused: %s\n", swapused);

	


	memarray1[1] = atoi(memtotal);
	memarray1[2] = atoi(memfree);
	memarray1[3] = atoi(buffers);
	memarray1[4] = atoi(cached);
	memused = memarray1[1] - memarray1[2];
	memarray1[5] = memused;
	memarray1[6] = atoi(swaptotal);
	memarray1[7] = atoi(swapfree);
	swapused = memarray1[6] - memarray1[7];
	memarray1[8] = swapused;

	//printf("memtotal: %d ", memarray1[1]);
	//printf("memfree: %d ", memarray1[2]);
	//printf("buffers: %d ", memarray1[3]);
	//printf("cached: %d ", memarray1[4]);
	//printf("memused: %d ", memarray1[5]);

	//printf("swaptotal: %d\n", memarray1[6]);
	//printf("swapfree: %d\n", memarray1[7]);
        return (0);
}

