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

« back to all changes in this revision

Viewing changes to dttools/src/create_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 <unistd.h>
 
9
#include <string.h>
 
10
#include <errno.h>
 
11
#include <stdlib.h>
 
12
#include <sys/stat.h>
 
13
#include <sys/types.h>
 
14
#ifdef HAS_ALLOCA_H
 
15
#include <alloca.h>
 
16
#endif
 
17
 
 
18
#include "debug.h"
 
19
 
 
20
int create_dir( const char *path, int mode )
 
21
{
 
22
        char *temp;
 
23
        char *delim;
 
24
        char oldchar;
 
25
        int result;
 
26
 
 
27
        temp = alloca(strlen(path)+1);
 
28
        strcpy(temp,path);
 
29
 
 
30
        delim = temp;
 
31
 
 
32
        while( (delim = strchr(delim,'/')) ) {
 
33
 
 
34
                if(delim==temp) {
 
35
                        delim++;
 
36
                        continue;
 
37
                }
 
38
 
 
39
                oldchar = *delim;
 
40
                *delim = 0;
 
41
 
 
42
                result = mkdir(temp,mode);
 
43
                if(result!=0) {
 
44
                        if(errno==EEXIST) {
 
45
                                /* no problem, keep going */
 
46
                        } else {
 
47
                                return 0;
 
48
                        }
 
49
                } else {
 
50
                        /* ok, made it successfully */
 
51
                }
 
52
 
 
53
                *delim = oldchar;
 
54
                delim++;
 
55
        }
 
56
 
 
57
        /* Now, last chance */
 
58
 
 
59
        result = mkdir(temp,mode);
 
60
        if(result!=0) {
 
61
                if(errno==EEXIST) {
 
62
                        return 1;
 
63
                } else {
 
64
                        return 0;
 
65
                }
 
66
        } else {
 
67
                return 1;
 
68
        }
 
69
}
 
70