]>
Commit | Line | Data |
---|---|---|
23c9e63c DD |
1 | /* |
2 | * Copyright (c) 2000, Red Hat, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by DJ Delorie <dj@cygnus.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* see mkdir.h */ | |
17 | ||
b24c88b3 RC |
18 | #if 0 |
19 | static const char *cvsid = | |
20 | "\n%%% $Id$\n"; | |
21 | #endif | |
8507f105 | 22 | |
aa1e3b4d | 23 | #if defined(WIN32) && !defined (_CYGWIN_) |
23c9e63c | 24 | #include "win32.h" |
ac949c48 MB |
25 | #else |
26 | #include <unistd.h> | |
27 | #include <string.h> | |
28 | #include <errno.h> | |
29 | #include <sys/stat.h> | |
aa1e3b4d RC |
30 | #endif |
31 | ||
23c9e63c DD |
32 | #include <stdio.h> |
33 | ||
34 | #include "mkdir.h" | |
35 | ||
36 | int | |
85b43844 | 37 | mkdir_p (int isadir, const char *in_path) |
23c9e63c DD |
38 | { |
39 | char saved_char, *slash = 0; | |
40 | char *c; | |
85b43844 | 41 | char path[strlen (in_path) + 1]; |
aa1e3b4d RC |
42 | #if defined(WIN32) && !defined (_CYGWIN_) |
43 | DWORD d, gse; | |
85b43844 | 44 | strcpy (path, in_path); |
23c9e63c DD |
45 | |
46 | d = GetFileAttributes (path); | |
47 | if (d != 0xffffffff && d & FILE_ATTRIBUTE_DIRECTORY) | |
48 | return 0; | |
49 | ||
50 | if (isadir) | |
51 | { | |
52 | if (CreateDirectory (path, 0)) | |
53 | return 0; | |
54 | gse = GetLastError (); | |
89725f30 | 55 | if (gse != ERROR_PATH_NOT_FOUND && gse != ERROR_FILE_NOT_FOUND) |
23c9e63c DD |
56 | { |
57 | if (gse == ERROR_ALREADY_EXISTS) | |
58 | { | |
b24c88b3 RC |
59 | fprintf (stderr, |
60 | "warning: deleting \"%s\" so I can make a directory there\n", | |
1fd6d0a2 | 61 | path); |
23c9e63c | 62 | if (DeleteFileA (path)) |
1fd6d0a2 | 63 | return mkdir_p (isadir, path); |
23c9e63c DD |
64 | } |
65 | return 1; | |
66 | } | |
67 | } | |
aa1e3b4d | 68 | #else |
ac949c48 | 69 | struct stat st; |
aa1e3b4d | 70 | strcpy (path, in_path); |
23c9e63c | 71 | |
ac949c48 MB |
72 | if (stat(path,&st) == 0 && S_ISDIR(st.st_mode)) |
73 | return 0; | |
74 | ||
75 | if (isadir) | |
76 | { | |
77 | if (mkdir (path, 0777)) | |
78 | return 0; | |
79 | if (errno != ENOENT) | |
80 | { | |
81 | if (errno == EEXIST) | |
82 | { | |
83 | fprintf (stderr, | |
84 | "warning: deleting \"%s\" so I can make a directory there\n", | |
85 | path); | |
86 | if (unlink (path)) | |
87 | return mkdir_p (isadir, path); | |
88 | } | |
89 | return 1; | |
90 | } | |
91 | } | |
aa1e3b4d RC |
92 | #endif |
93 | ||
85b43844 | 94 | for (c = path; *c; c++) |
23c9e63c DD |
95 | { |
96 | if (*c == ':') | |
97 | slash = 0; | |
98 | if (*c == '/' || *c == '\\') | |
99 | slash = c; | |
100 | } | |
101 | ||
102 | if (!slash) | |
103 | return 0; | |
104 | ||
f2e49cf8 RC |
105 | // Trying to create a drive... It's time to give up. |
106 | if (((slash - path) == 2) && (path[1] == ':')) | |
107 | return 1; | |
108 | ||
23c9e63c DD |
109 | saved_char = *slash; |
110 | *slash = 0; | |
111 | if (mkdir_p (1, path)) | |
112 | { | |
113 | *slash = saved_char; | |
114 | return 1; | |
115 | } | |
f2e49cf8 | 116 | |
23c9e63c DD |
117 | *slash = saved_char; |
118 | ||
119 | if (!isadir) | |
120 | return 0; | |
121 | ||
122 | return mkdir_p (isadir, path); | |
123 | } |