* Build bin2res and cabman in bootstrap makefiles
[reactos.git] / reactos / tools / rmkdir.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5 #ifdef _MSC_VER
6 #include <direct.h>
7 #else
8 #include <unistd.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #endif
12
13 #if defined(WIN32)
14 #define DIR_SEPARATOR_CHAR '\\'
15 #define DIR_SEPARATOR_STRING "\\"
16 #define DOS_PATHS
17 #else
18 #define DIR_SEPARATOR_CHAR '/'
19 #define DIR_SEPARATOR_STRING "/"
20 #define UNIX_PATHS
21 #endif
22
23 char* convert_path(char* origpath)
24 {
25 char* newpath;
26 int i;
27
28 //newpath = strdup(origpath);
29 newpath=malloc(strlen(origpath)+1);
30 strcpy(newpath,origpath);
31
32 i = 0;
33 while (newpath[i] != 0)
34 {
35 #ifdef UNIX_PATHS
36 if (newpath[i] == '\\')
37 {
38 newpath[i] = '/';
39 }
40 #else
41 #ifdef DOS_PATHS
42 if (newpath[i] == '/')
43 {
44 newpath[i] = '\\';
45 }
46 #endif
47 #endif
48 i++;
49 }
50 return(newpath);
51 }
52
53 #define TRANSFER_SIZE (65536)
54
55 int mkdir_p(char* path)
56 {
57 if (chdir(path) == 0)
58 {
59 return(0);
60 }
61 #ifdef UNIX_PATHS
62 if (mkdir(path, 0755) != 0)
63 {
64 perror("Failed to create directory");
65 exit(1);
66 }
67 #else
68 if (mkdir(path) != 0)
69 {
70 perror("Failed to create directory");
71 exit(1);
72 }
73 #endif
74
75 if (chdir(path) != 0)
76 {
77 perror("Failed to change directory");
78 exit(1);
79 }
80 return(0);
81 }
82
83 int main(int argc, char* argv[])
84 {
85 char* path1;
86 char* csec;
87 char buf[256];
88
89 if (argc != 2)
90 {
91 fprintf(stderr, "Too many arguments\n");
92 exit(1);
93 }
94
95 path1 = convert_path(argv[1]);
96
97 if (isalpha(path1[0]) && path1[1] == ':' && path1[2] == DIR_SEPARATOR_CHAR)
98 {
99 csec = strtok(path1, DIR_SEPARATOR_STRING);
100 sprintf(buf, "%s\\", csec);
101 chdir(buf);
102 csec = strtok(NULL, DIR_SEPARATOR_STRING);
103 }
104 else if (path1[0] == DIR_SEPARATOR_CHAR)
105 {
106 chdir(DIR_SEPARATOR_STRING);
107 csec = strtok(path1, DIR_SEPARATOR_STRING);
108 }
109 else
110 {
111 csec = strtok(path1, DIR_SEPARATOR_STRING);
112 }
113
114 while (csec != NULL)
115 {
116 mkdir_p(csec);
117 csec = strtok(NULL, DIR_SEPARATOR_STRING);
118 }
119
120 exit(0);
121 }