ecb3b31bd43fe1eec3644c0eaca9493403220532
[reactos.git] / reactos / tools / rmkdir.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5 #include <fcntl.h>
6 #ifdef _MSC_VER
7 #include <direct.h>
8 #else
9 #include <unistd.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #endif
13 #ifdef WIN32
14 #include <sys/utime.h>
15 #include <time.h>
16 #else
17 #include <sys/time.h>
18 #endif
19
20 #if defined(WIN32)
21 #define DIR_SEPARATOR_CHAR '\\'
22 #define DIR_SEPARATOR_STRING "\\"
23 #define DOS_PATHS
24 #else
25 #define DIR_SEPARATOR_CHAR '/'
26 #define DIR_SEPARATOR_STRING "/"
27 #define UNIX_PATHS
28 #endif
29
30 char* convert_path(char* origpath)
31 {
32 char* newpath;
33 int i;
34 int length;
35
36 //newpath = strdup(origpath);
37 newpath=malloc(strlen(origpath)+1);
38 strcpy(newpath,origpath);
39
40 i = 0;
41 while (newpath[i] != 0)
42 {
43 #ifdef UNIX_PATHS
44 if (newpath[i] == '\\')
45 {
46 newpath[i] = '/';
47 }
48 #else
49 #ifdef DOS_PATHS
50 if (newpath[i] == '/')
51 {
52 newpath[i] = '\\';
53 }
54 #endif
55 #endif
56 i++;
57 }
58
59 length = strlen(newpath);
60 if (length > 0)
61 {
62 if (newpath[length - 1] == DIR_SEPARATOR_CHAR)
63 newpath[length - 1] = 0;
64 }
65 return(newpath);
66 }
67
68 #define TRANSFER_SIZE (65536)
69
70 int mkdir_p(char* path)
71 {
72 if (chdir(path) == 0)
73 {
74 return(0);
75 }
76 #ifdef UNIX_PATHS
77 if (mkdir(path, 0755) != 0)
78 {
79 perror("Failed to create directory");
80 exit(1);
81 }
82 #else
83 if (mkdir(path) != 0)
84 {
85 perror("Failed to create directory");
86 exit(1);
87 }
88 #endif
89
90 if (chdir(path) != 0)
91 {
92 perror("Failed to change directory");
93 exit(1);
94 }
95 return(0);
96 }
97
98 int
99 write_created_file()
100 {
101 char filename[256];
102 int id;
103 #ifdef WIN32
104 time_t now;
105 struct utimbuf fnow;
106 #endif
107
108 strcpy(filename, ".created");
109
110 id = open(filename, S_IWRITE, S_IRUSR | S_IWUSR);
111 if (id < 0)
112 {
113 id = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
114 if (id < 0)
115 {
116 fprintf(stderr, "Cannot create file %s.\n", filename);
117 return(1);
118 }
119 }
120
121 close(id);
122
123 #ifdef WIN32
124 now = time(NULL);
125 fnow.actime = now;
126 fnow.modtime = now;
127 (int) utime(filename, &fnow);
128 #else
129 (int) utimes(filename, NULL);
130 #endif
131
132 return 0;
133 }
134
135 int main(int argc, char* argv[])
136 {
137 char* path1;
138 char* csec;
139 char buf[256];
140
141 if (argc != 2)
142 {
143 fprintf(stderr, "Too many arguments\n");
144 exit(1);
145 }
146
147 path1 = convert_path(argv[1]);
148
149 if (isalpha(path1[0]) && path1[1] == ':' && path1[2] == DIR_SEPARATOR_CHAR)
150 {
151 csec = strtok(path1, DIR_SEPARATOR_STRING);
152 sprintf(buf, "%s\\", csec);
153 chdir(buf);
154 csec = strtok(NULL, DIR_SEPARATOR_STRING);
155 }
156 else if (path1[0] == DIR_SEPARATOR_CHAR)
157 {
158 chdir(DIR_SEPARATOR_STRING);
159 csec = strtok(path1, DIR_SEPARATOR_STRING);
160 }
161 else
162 {
163 csec = strtok(path1, DIR_SEPARATOR_STRING);
164 }
165
166 while (csec != NULL)
167 {
168 if (mkdir_p(csec) > 0)
169 exit(1);
170 csec = strtok(NULL, DIR_SEPARATOR_STRING);
171 }
172
173 exit(write_created_file());
174 }