]> cygwin.com Git - cygwin-apps/setup.git/blob - mkdir.cc
2002-04-29 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 #include "win32.h"
24
25 #include <stdio.h>
26
27 #include "mkdir.h"
28
29 int
30 mkdir_p (int isadir, const char *in_path)
31 {
32 char saved_char, *slash = 0;
33 char *c;
34 DWORD d, gse;
35 char path[strlen (in_path) + 1];
36 strcpy (path, in_path);
37
38 d = GetFileAttributes (path);
39 if (d != 0xffffffff && d & FILE_ATTRIBUTE_DIRECTORY)
40 return 0;
41
42 if (isadir)
43 {
44 if (CreateDirectory (path, 0))
45 return 0;
46 gse = GetLastError ();
47 if (gse != ERROR_PATH_NOT_FOUND && gse != ERROR_FILE_NOT_FOUND)
48 {
49 if (gse == ERROR_ALREADY_EXISTS)
50 {
51 fprintf (stderr,
52 "warning: deleting \"%s\" so I can make a directory there\n",
53 path);
54 if (DeleteFileA (path))
55 return mkdir_p (isadir, path);
56 }
57 return 1;
58 }
59 }
60
61 for (c = path; *c; c++)
62 {
63 if (*c == ':')
64 slash = 0;
65 if (*c == '/' || *c == '\\')
66 slash = c;
67 }
68
69 if (!slash)
70 return 0;
71
72 // Trying to create a drive... It's time to give up.
73 if (((slash - path) == 2) && (path[1] == ':'))
74 return 1;
75
76 saved_char = *slash;
77 *slash = 0;
78 if (mkdir_p (1, path))
79 {
80 *slash = saved_char;
81 return 1;
82 }
83
84 *slash = saved_char;
85
86 if (!isadir)
87 return 0;
88
89 return mkdir_p (isadir, path);
90 }
This page took 0.039555 seconds and 5 git commands to generate.