1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/*@@
@file File.c
@date September 6th 1999
@author Gabrielle Allen
@desc
File Handling routines
@enddesc
@version $Header$
@@*/
#include "cctk.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "cctk_WarnLevel.h"
#include "cctk_FortranString.h"
#include "util_String.h"
static char *rcsid = "$Header$";
CCTK_FILEVERSION(util_File_c)
/********************************************************************
********************* Local Data Types ***********************
********************************************************************/
/********************************************************************
********************* Local Routine Prototypes *********************
********************************************************************/
/********************************************************************
********************* Other Routine Prototypes *********************
********************************************************************/
/********************************************************************
********************* Local Data *****************************
********************************************************************/
/* some systems (eg. Windows NT) don't define this macro */
#ifndef S_ISDIR
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
/* Some systems don't have mode_t and only pass one argument to mkdir. */
#ifdef HAVE_MODE_T
#define MKDIR_WRAPPER(a,b) mkdir(a,b)
#else
#define MKDIR_WRAPPER(a,b) mkdir(a)
#endif /* HAVE_MODE_T */
/********************************************************************
********************* External Routines **********************
********************************************************************/
/*@@
@routine CCTK_CreateDirectory
@date Tue May 2 21:38:48 2000
@author Tom Goodale
@desc
Makes all directories necessary for the path to exist.
@enddesc
@calls
@calledby
@history
@endhistory
@var mode
@vdesc permissions of the directory to be created
@vtype int
@vio in
@vcomment
This is the unix mode, e.g. 0755
@endvar
@var pathname
@vdesc Name of directory to create
@vtype const char *
@vio in
@vcomment
@endvar
@returntype int
@returndesc
0 - success
-1 - failure
@endreturndesc
@@*/
int CCTK_CreateDirectory(int mode, const char *pathname)
{
int retval;
const char *path;
char *current;
const char *token;
struct stat statbuf;
path = pathname;
retval = 0;
current = (char *)malloc((strlen(pathname)+1)*sizeof(char));
if(current)
{
current[0] = '\0';
while((token = Util_StrSep(&path, "/")))
{
/* Treat first token carefully. */
if(*current == '\0')
{
if(*token == '\0')
{
strcpy(current, "/");
}
else
{
strcpy(current,token);
}
}
else
{
sprintf(current,"%s/%s", current, token);
}
if(stat(current, &statbuf))
{
CCTK_VInfo("Cactus","Creating directory: \"%s\"", current);
if(MKDIR_WRAPPER(current, mode) == -1)
{
CCTK_VWarn(0,__LINE__,__FILE__,"Cactus","Failed to create directory \"%s\"", current);
retval = -2;
}
}
else
{
if(! S_ISDIR(statbuf.st_mode))
{
CCTK_VWarn(0,__LINE__,__FILE__,"Cactus","\"%s\" exists but is not a directory", current);
retval = -3;
}
}
if(retval)
{
break;
}
}
if(! retval)
{
/* Deal with last component of path */
if(path - pathname < strlen(pathname))
{
if(stat(pathname, &statbuf))
{
CCTK_VInfo("Cactus","Creating directory: \"%s\"", pathname);
if(MKDIR_WRAPPER(pathname, mode) == -1)
{
CCTK_VWarn(0,__LINE__,__FILE__,"Cactus","Failed to create directory \"%s\"", pathname);
retval = -2;
}
}
else
{
if(! S_ISDIR(statbuf.st_mode))
{
CCTK_VWarn(0,__LINE__,__FILE__,"Cactus", "\"%s\" exists but is not a directory", pathname);
retval = -3;
}
}
}
}
free(current);
}
else
{
CCTK_Warn(0,__LINE__,__FILE__,"Cactus", "Failed to allocate some temporary memory");
retval = -1;
}
return retval;
}
void CCTK_FCALL CCTK_FNAME(CCTK_CreateDirectory)
(int *ierr, int *mode, ONE_FORTSTRING_ARG)
{
ONE_FORTSTRING_CREATE(arg1)
*ierr = CCTK_CreateDirectory(*mode,arg1);
free(arg1);
}
/********************************************************************
********************* Local Routines *************************
********************************************************************/
|