Link to home
Start Free TrialLog in
Avatar of angel84
angel84

asked on

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.  
Avatar of phoffric
phoffric

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

Avatar of angel84

ASKER

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

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?

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of angel84

ASKER

it's not that clear but i done work with at with out these commants.
>> it's not that clear

If you need clarification for anything, you can just ask, and we'll be happy to provide it.