1c6eff1c4311b33f525c979cb95d92a601e8f848
[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 #else
17 #define DIR_SEPARATOR_CHAR '/'
18 #define DIR_SEPARATOR_STRING "/"
19 #endif
20
21 char* convert_path(char* origpath)
22 {
23 char* newpath;
24 int i;
25
26 //newpath = strdup(origpath);
27 newpath=malloc(strlen(origpath)+1);
28 strcpy(newpath,origpath);
29
30 i = 0;
31 while (newpath[i] != 0)
32 {
33 #ifdef UNIX_PATHS
34 if (newpath[i] == '\\')
35 {
36 newpath[i] = '/';
37 }
38 #else
39 #ifdef DOS_PATHS
40 if (newpath[i] == '/')
41 {
42 newpath[i] = '\\';
43 }
44 #endif
45 #endif
46 i++;
47 }
48 return(newpath);
49 }
50
51 #define TRANSFER_SIZE (65536)
52
53 int mkdir_p(char* path)
54 {
55 if (chdir(path) == 0)
56 {
57 return(0);
58 }
59 #ifdef UNIX_PATHS
60 if (mkdir(path, 0755) != 0)
61 {
62 perror("Failed to create directory");
63 exit(1);
64 }
65 #else
66 if (mkdir(path) != 0)
67 {
68 perror("Failed to create directory");
69 exit(1);
70 }
71 #endif
72
73 if (chdir(path) != 0)
74 {
75 perror("Failed to change directory");
76 exit(1);
77 }
78 return(0);
79 }
80
81 int main(int argc, char* argv[])
82 {
83 char* path1;
84 char* csec;
85 char buf[256];
86
87 if (argc != 2)
88 {
89 fprintf(stderr, "Too many arguments\n");
90 exit(1);
91 }
92
93 path1 = convert_path(argv[1]);
94
95 if (isalpha(path1[0]) && path1[1] == ':' && path1[2] == DIR_SEPARATOR_CHAR)
96 {
97 csec = strtok(path1, DIR_SEPARATOR_STRING);
98 sprintf(buf, "%s\\", csec);
99 chdir(buf);
100 csec = strtok(NULL, DIR_SEPARATOR_STRING);
101 }
102 else if (path1[0] == DIR_SEPARATOR_CHAR)
103 {
104 chdir(DIR_SEPARATOR_STRING);
105 csec = strtok(path1, DIR_SEPARATOR_STRING);
106 }
107 else
108 {
109 csec = strtok(path1, DIR_SEPARATOR_STRING);
110 }
111
112 while (csec != NULL)
113 {
114 mkdir_p(csec);
115 csec = strtok(NULL, DIR_SEPARATOR_STRING);
116 }
117
118 exit(0);
119 }