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);
}