]> cygwin.com Git - cygwin-apps/setup.git/blob - mkdir.cc
2002-07-15 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / mkdir.cc
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
18 #if 0
19 static const char *cvsid =
20 "\n%%% $Id$\n";
21 #endif
22
23 #if defined(WIN32) && !defined (_CYGWIN_)
24 #include "win32.h"
25 #endif
26
27 #include <stdio.h>
28
29 #include "mkdir.h"
30
31 int
32 mkdir_p (int isadir, const char *in_path)
33 {
34 char saved_char, *slash = 0;
35 char *c;
36 char path[strlen (in_path) + 1];
37 #if defined(WIN32) && !defined (_CYGWIN_)
38 DWORD d, gse;
39 strcpy (path, in_path);
40
41 d = GetFileAttributes (path);
42 if (d != 0xffffffff && d & FILE_ATTRIBUTE_DIRECTORY)
43 return 0;
44
45 if (isadir)
46 {
47 if (CreateDirectory (path, 0))
48 return 0;
49 gse = GetLastError ();
50 if (gse != ERROR_PATH_NOT_FOUND && gse != ERROR_FILE_NOT_FOUND)
51 {
52 if (gse == ERROR_ALREADY_EXISTS)
53 {
54 fprintf (stderr,
55 "warning: deleting \"%s\" so I can make a directory there\n",
56 path);
57 if (DeleteFileA (path))
58 return mkdir_p (isadir, path);
59 }
60 return 1;
61 }
62 }
63 #else
64 strcpy (path, in_path);
65
66 /* stat */
67 /* if file exists and is a dir return */
68 /* if we want a dir at this point
69 call makedir
70 if ok return 0
71 if fails due to present file,
72 rmove the file and return 1
73 else if fails due to missing fail/path
74 end block */
75 #endif
76
77 for (c = path; *c; c++)
78 {
79 if (*c == ':')
80 slash = 0;
81 if (*c == '/' || *c == '\\')
82 slash = c;
83 }
84
85 if (!slash)
86 return 0;
87
88 // Trying to create a drive... It's time to give up.
89 if (((slash - path) == 2) && (path[1] == ':'))
90 return 1;
91
92 saved_char = *slash;
93 *slash = 0;
94 if (mkdir_p (1, path))
95 {
96 *slash = saved_char;
97 return 1;
98 }
99
100 *slash = saved_char;
101
102 if (!isadir)
103 return 0;
104
105 return mkdir_p (isadir, path);
106 }
This page took 0.039166 seconds and 5 git commands to generate.