/*  This file is part of lsnet. 

    Lsnet is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Lsnet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with lsnet.  If not, see <http://www.gnu.org/licenses/>.
*/

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

int meminfo() 
{
	//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; 
	int totalmem;
	//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];

        if( ( fp = fopen( "/proc/meminfo", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening /proc/meminfo\n" );
                exit( 1 );
        }
        while( fgets(buf, sizeof(buf), fp) != NULL)
        {
		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);
                                                }
                                        }
                                }
                        }
                }

	}

	fclose( fp );

	totalmem = atoi(memtotal);

        return (totalmem);
}

