• C

remove directory and it's contents

Hi
I have c program to remove directory and it's contents. the program works with remove the files from all the directory but it doesn't remove the subdirectory. any help please.  
angel84Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffricCommented:
In unix, there is the rmdir command:
    http://linux.die.net/man/3/rmdir

"The following example shows how to remove a directory named /home/cnd/mod1."
#include <unistd.h>

int status;
...
status = rmdir("/home/cnd/mod1");

Open in new window

0
angel84Author Commented:
This is my code. could you please see what is the wrong with it.
void Remove_directory(char *to)
{
		
	DIR *dir = NULL;
	struct dirent entry;
	struct dirent *entryPtr = NULL;
	int retval = 0;
	
	char pathName1[PATH_MAX + 1];

	/* Open the given directory, if we can. */	
	dir = opendir( to );
	if( dir == NULL )
	{		
		printf( "Error opening %s \n", to );
		return 0;
	}
	

	retval = readdir_r( dir, &entry, &entryPtr );
	while( entryPtr != NULL )
	{
		struct stat entryInfo;
		
		if( ( strncmp( entry.d_name, ".", PATH_MAX ) == 0 ) ||
		    ( strncmp( entry.d_name, "..", PATH_MAX ) == 0 ) ) {
		    retval = readdir_r( dir, &entry, &entryPtr );
		    continue;
		}
 		
		
		(void)strncpy( pathName1, to, PATH_MAX );
		(void)strncat( pathName1, "/", PATH_MAX );
		(void)strncat( pathName1, entry.d_name, PATH_MAX );
		
		if( lstat( pathName1, &entryInfo ) == 0 )
		{
			count++;
			
			if( S_ISDIR( entryInfo.st_mode ) )
			{			/* directory */
				
				 rmdir ( pathName1);
				 process_directory( pathName1 );
				
				
			}
			
			else if( S_ISREG( entryInfo.st_mode ) )
			{	
				
				unlink (pathName1);
			}
			
			else if( S_ISLNK( entryInfo.st_mode ) )
			{	
				char targetName[PATH_MAX + 1];
				if( readlink( pathName1, targetName, PATH_MAX ) != -1 )
				{
					unlink (pathName1);
					
				}
				else
				{
					printf( "\t%s -> (invalid symbolic link!)\n", pathName1 );
				}
			}
		} else
		{
			printf( "Error statting %s\n",pathName1 );
		}
rmdir ( pathName1);
		retval = readdir_r( dir, &entry, &entryPtr );
	}
	
	
	(void)closedir( dir );
	
	
}

Open in new window

0
phoffricCommented:
I don't really understand your function.

Did you step through it with your debugger? If so, where exactly did you find a problem? If not, please download ddd or another GUI debugger (if you do not already have a GUI debugger) to help you identify the problem.

Why do you call rmdir even if the returned value is not a directory at line 72?

Why do you call unlink several times?

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Infinity08Commented:
You probably shouldn't call rmdir before you have deleted all files from it ;)

Move the rmdir call after the process_directory call, and see if that works better.


Btw : always check return values etc. to check for error conditions after every function call. This would have caught this problem easily for you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
you could use the system function to launch a commandline rm command for deleting file branches.

if you pipe the output of the rm command to a log file, a zero length of that file would indicate success. for deleting that log file i would use remove function.

Sara
0
fridomCEO/ProgrammerCommented:
Infinity 08 comment will set you on the right track. At first you have to decent down all directories, then you have to remove all the files in that directory on coming back to the directory you can start using rmdir

rmdir() deletes a directory, which must be empty.

You can either use recursion  to go down into the directory as you are coming along If you come back and see if there are more files and you won't find one you can run rmdir.

0
angel84Author Commented:
it's not that clear but i done work with at with out these commants.
0
Infinity08Commented:
>> it's not that clear

If you need clarification for anything, you can just ask, and we'll be happy to provide it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.