Okay, I've tried for days now and still can't find the source of error in my malloc() call. Im going to expand the problem out to best describe what Im trying to do. (n.b the code is attached in a word document for further reading)
Basically I have a routine called ShowUsage() which calculates how much space a folder at a particular level of the hard disk consumes. This is done for every single folder level within the disk. Due to the nature of subfolders, I have made ShowUsage() call itself within the function body.
The summing procedure is done in a while loop which I have given in pseudo code below
Also, from the root to 1st folder level, ShowUsage will display the amount of disk-space consumed for a particular folder (which includes all the summed data for subfolders and files beneath the current folder).
When running the debugger, ShowUsage() works fine for 3 folders, but on the 4th folder it fails at this call
To ensure that sufficient heap memory is available, I do this at the end of the while loop
Obviously this has worked for the first 3 folders, but somehow stuffs up at the 4th folder. I have even tried making path = NULL; before freeing it and after freeing it,but this has made no difference.
Im pretty stumped at this point. Please see the attachements for the entire function's code.
Please make the program legible with proper indentation, providing more documentation and pasting it in the thread within CODE tags. As of now it is too hard to read besides there's no documentation that states what part of the code is doing what.
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);
}
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:
/*entry = parent directory to be opened and read
flag = current level of the folder hierarchy in numbers (0=root, 1= root subfolder, 2 = subfolder beneath 1 etc...)
*/
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 we haven't finished reading through the current
directory, keep cycling through the while loop*/
while( (list = readdir(d_entry)) != NULL)
{
/*Get the folder/file within the current directory*/
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
direcctory
*/
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));
/*Next 2 lines are debugging code */
prod_size = strlen(production);
temp_size = strlen(temp);
/*Assign production the name of the
current folder or file being read*/
strncpy(production,temp,strlen(temp));
prod_size = strlen(production);
}
size = strlen(parent)+strlen(temp)+2;
/*path will store the directory to be
passed to the self-invoked call of ShowUsage() */
path = (char *)malloc(size);
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 )
{
/*Check if temp is a folder. If this is true (indicated by folder_select() returning 1) do the following below*/
if( folder_select(temp) == 1)
{
val = flag+1;
ShowUsage(path,val);
/*For root level folders*/
if(flag == 0)
{
disk_size = disk_size + folder_size;
/*n.b folder_size is a global variable which shows the size of a sub-folder */
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);
}
/*For folders beneath the root folder*/
else if(flag == 1)
{
/*subfolder_size is another global which shows the size of folders beneath folders at flag==1 level*/
folder_size = folder_size + subfolder_size;
printf("\n%s\t\t\t\t%12.9f gigs",path,subfolder_size);
}
/*Reset globals to 0, to ensure accurate calculations and readings on next pass*/
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;
}
}
}
free(path);
free(production);
}
if( closedir(d_entry) == -1)
perror("closedir");
free(parent);
}
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)
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)
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)
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)
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)
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)
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)
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)
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)