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