~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to dttools/src/delete_dir.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
#include "delete_dir.h"
 
9
#include "stringtools.h"
 
10
 
 
11
#include <dirent.h>
 
12
#include <limits.h>
 
13
#include <stdlib.h>
 
14
#include <string.h>
 
15
#include <stdio.h>
 
16
#include <unistd.h>
 
17
#include <errno.h>
 
18
 
 
19
int delete_dir( const char *dirname )
 
20
{
 
21
        char subdir[PATH_MAX];
 
22
        int result;
 
23
        struct dirent *d;
 
24
        DIR *dir;
 
25
 
 
26
        dir = opendir(dirname);
 
27
        if(!dir) {
 
28
                if(errno==ENOTDIR) {
 
29
                        result = unlink(dirname);
 
30
                        if(result==0) {
 
31
                                return 1;
 
32
                        } else {
 
33
                                return 0;
 
34
                        }
 
35
                } else if(errno==ENOENT) {
 
36
                        return 1;
 
37
                } else {
 
38
                        return 0;
 
39
                }
 
40
        }
 
41
 
 
42
        result = 1;
 
43
 
 
44
        while((d=readdir(dir))) {
 
45
                if(!strcmp(d->d_name,".")) continue;
 
46
                if(!strcmp(d->d_name,"..")) continue;
 
47
                sprintf(subdir,"%s/%s",dirname,d->d_name);
 
48
                if(!delete_dir(subdir)) {
 
49
                        result = 0;
 
50
                }
 
51
        }
 
52
 
 
53
        closedir(dir);
 
54
 
 
55
        if(rmdir(dirname)!=0) {
 
56
                result = 0;
 
57
        }
 
58
 
 
59
        return result;
 
60
}