Sponsored Content
Full Discussion: malloc() + run-time issues
Top Forums Programming malloc() + run-time issues Post 302266149 by JamesGoh on Tuesday 9th of December 2008 03:44:39 PM
Old 12-09-2008
Sorry for all the confusion. To be honest I can be a bit lazy from time-to-time when trying to explain problems, mainly cause there is a lot to try and interpret. I will try my best to explain the code

Code:
void ShowUsage(char *entry,int flag)
{
	DIR *d_entry;
	struct dirent *list; 
	int status=0;
	struct stat buff;
	char *path,*parent,*char_size;
	char *temp,*production;
	int size=0;
	char *cmd,*str;	
	float file_size = 0.0;
	int val=0,olen=0,len=0;
	int prod_size=0,temp_size=0;
	int parent_size=0;	

	/*Create a char * to store
            the name of the parent directory*/	

	parent_size = strlen(entry);

	parent = (char *)malloc(parent_size);
	strcpy(parent,"");
	strncpy(parent,entry,parent_size);
	
	while(strcmp(parent,entry) != 0)
	{
		len = strlen(parent);
		*(parent+(len-1)) = NULL;	
			
	}



	if( (d_entry = opendir(parent)) == NULL)
	{
		perror("opendir");
		exit(0);
	}

	while( (list = readdir(d_entry)) != NULL)
	{
		
		temp = list->d_name;

                         /*flag=0 means we are at root level*/

		if(flag==0)
		{
                                 
                               /*Create a folder called production to store
                                 the name of the directory beneath the root
*/

			if(production != NULL)
				free(production);

			olen = strlen(parent);
			temp_size = strlen(temp);
	    	production = (char *)malloc(sizeof(temp));
			prod_size = strlen(production);
			strncpy(production,"",strlen(production));
			prod_size = strlen(production);
			temp_size = strlen(temp);
			strncpy(production,temp,strlen(temp)); 
			prod_size = strlen(production);
			
		}

		size = strlen(parent)+strlen(temp)+2;
	
			
		
		/*memory heap is possibly becoming exhausted */

		path = (char *)malloc(size); /*28 Nov this is somehow stuffed up*/
		
		

		olen = strlen(parent);

		strncpy(path,parent,strlen(parent)); 

		while(strcmp(parent,path) != 0)
		{
			len = strlen(path);
			*(path+(len-1)) = NULL;	
			/*strncpy(path,parent,olen);*/
			
		}

		strcat(path,"/");
		strcat(path,temp);

		if( stat(path,&buff) == -1)
		{
			printf("\n%s \n",path);
			perror("stat()");
		}		


		/* If folder, print it and its 
		file size out */	

		if( S_ISDIR(buff.st_mode) > 0 )
		{	
				
			if( folder_select(temp) == 1)
			{
												
				val = flag+1;
				ShowUsage(path,val);
						
													
				/*Only display sizes for high-level folders
				(from level 2 to root level (level 0) ) */

				if(flag == 0)
				{
					disk_size = disk_size + folder_size;
					printf("\n%s\t\t\t\t%12.9f gigs",path,folder_size);

					/*Write to text file*/
					
					char_size = (char *)malloc(21);

					sprintf(char_size,"%12.9f",folder_size);
					
					/*----------------------*/
					/*Experiencing issues writing data 
					 to	the file*/
					/*----------------------*/
					
					WriteToFile(entry,"\n");	
					str = (char *)malloc(sizeof(entry));
					strncpy(str,entry+1,strlen(entry)-1);				
					WriteToFile(entry,str);
					WriteToFile(entry,":");
					WriteToFile(entry,production);
					WriteToFile(entry,":");
					WriteToFile(entry,char_size);
					free(char_size);
					free(str);

				}
					
				else if(flag == 1)
				{
					folder_size = folder_size + subfolder_size;
					printf("\n%s\t\t\t\t%12.9f gigs",path,subfolder_size);							
				}	

				if(flag == 0) 
					folder_size = 0.0;
				else if(flag==1) 	
					subfolder_size = 0.0;			
					
			}
							
		}
		
		/*For files*/

		else
		{
			free(path);
			path = NULL;			

			if( folder_select(temp) == 1)
			{
				size = strlen(parent)+strlen(temp)+2;
				path = (char *)malloc(size);

				strcpy(path,parent);
				strcat(path,"/");
				strcat(path,temp);

				if( stat(path,&buff) == -1)
					perror("stat()");
				else
				{
					file_size = (float)buff.st_size;

					/*Converting everything to gigs*/

					file_size = file_size/1000000000;

					/*Increment subfolder size, depending on hierarchy */
					
					if(flag >= 2)
						subfolder_size = subfolder_size + file_size;
					else if(flag == 1)
						folder_size = folder_size + file_size;
					else if(flag == 0)
						disk_size = disk_size + file_size;
					
					
				}

			}

		}

		/*if(flag==0)
			printf("\n?");*/

		/*path = NULL;*/
		free(path);
		free(production);				
	}

	if( closedir(d_entry) == -1)
		perror("closedir");

	
	free(parent);

}

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issues using ssh from crontab to run remote script from

I have a solaris9 x86 server using ssh as follows: SSH Version Sun_SSH_1.0, protocol versions 1.5/2.0. The remote server solaris9 sparc has exactly the same version ssh installed. I am running a script on my server which includes the following command to run a script on the remote server:... (4 Replies)
Discussion started by: frustrated1
4 Replies

2. UNIX for Dummies Questions & Answers

Date time issues on RHEL

Hi, I am facing a weird problem with the 'Date'. If I check date multiple times in a short interval I see a different time altogether. Here is an example $ date Tue Jul 15 02:07:22 PDT 2008 $ date Tue Jul 15 02:07:23 PDT 2008 $ date Tue Jul 15 03:20:42 PDT 2008 $ date Tue Jul 15... (5 Replies)
Discussion started by: sumitb74
5 Replies

3. Shell Programming and Scripting

Trying to run a script over ssh, having issues with tty assignment

Hey folks! I'm trying to run a tcpdump command that monitors for possible malicious traffic. I want to be able to run this on any of several remote boxes that I monitor which all have different capture interfaces. I've gotten the script to the point where it logs into the box and attempts to run... (8 Replies)
Discussion started by: JASI
8 Replies

4. IP Networking

Erratic ping time issues

Hello, I administer a bunch of Apple XServes running OS X Server and I have one in particular that is annoying me since I brought it online. The host is a dual quad core Intel CPU. en0 is attached to a routable network and en1 is attached to a non-routable network (private switch that all the... (0 Replies)
Discussion started by: gnat
0 Replies

5. Shell Programming and Scripting

Incrementing a time by one hour issues

Hi all, I need your help to increment a time by one hour. The difficulty is the time is in a string format and not a value cat file | awk '{print $1,$2}' 09/02/2011 20:11 09/03/2011 20:11 I want to change the time to be as follows 09/02/2011 21:11 or even 09/02/2011 20:21 Can... (2 Replies)
Discussion started by: Junes
2 Replies

6. Shell Programming and Scripting

PERL, Date & Time issues

Hello All, This is my first script in PERL. Hence require your help in moving further. I have a script which should populate the values for Today, Yesterday output. For which I use timeFrame as a variable to obtain the time in hrs:mm as 10:00. All I want is, I want my timeFrame to start... (4 Replies)
Discussion started by: sathyaonnuix
4 Replies

7. Shell Programming and Scripting

Time zone issues in UNIX flavors

Hello All, I am in process of migrating all my scripts from AIX box to Linux box. In one of my script I calculate my last week date with the below command $ TZ=EDT+172 date +%F 2012-12-13 $ uname -a AIX 1 7 000B29AAD400 Now when I tried running the same in Linux, it gives a false... (2 Replies)
Discussion started by: sathyaonnuix
2 Replies

8. Shell Programming and Scripting

Perl program to run a Shell script issues...

Hi all, I have the following Perl script which is intended to run a Shell script and generate some logging for the purposes of tracking weather or not the script ran. I get an error, of course, since I don't know what I'm doing really. Here is the code: #!/opt/perl/bin/perl -w ... (14 Replies)
Discussion started by: zixzix01
14 Replies

9. Shell Programming and Scripting

Issues while trying to run a shell script using the command sh <filename.prog>

Hi, I'm facing issues while trying to run a sample program on Linux. If I try to run the script using the command "sh <filename.prog>", it doesn't work. But, if I try to execute it using the command "ksh <filename.prog>", it works fine. Even ". ./filename.prog" works fine. Can you... (6 Replies)
Discussion started by: venkatesh17
6 Replies
All times are GMT -4. The time now is 08:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy