xref: /reactos/boot/freeldr/tools/rmkdir.c (revision c2c66aff)
1*c2c66affSColin Finck #include <stdio.h>
2*c2c66affSColin Finck #include <string.h>
3*c2c66affSColin Finck #include <stdlib.h>
4*c2c66affSColin Finck #include <sys/stat.h>
5*c2c66affSColin Finck #include <unistd.h>
6*c2c66affSColin Finck #include <ctype.h>
7*c2c66affSColin Finck 
8*c2c66affSColin Finck #define DOS_PATH_CHAR    '\\'
9*c2c66affSColin Finck #define UNIX_PATH_CHAR    '/'
10*c2c66affSColin Finck 
11*c2c66affSColin Finck #if defined (__DJGPP__) || defined (__WIN32__)
12*c2c66affSColin Finck #define DOS_PATHS
13*c2c66affSColin Finck #define PATH_CHAR        '\\'
14*c2c66affSColin Finck #define PATH_CHAR_STR    "\\"
15*c2c66affSColin Finck #else
16*c2c66affSColin Finck #define UNIX_PATHS
17*c2c66affSColin Finck #define PATH_CHAR        '/'
18*c2c66affSColin Finck #define PATH_CHAR_STR    "/"
19*c2c66affSColin Finck #endif
20*c2c66affSColin Finck 
ConvertPathCharacters(char * Path)21*c2c66affSColin Finck void ConvertPathCharacters(char *Path)
22*c2c66affSColin Finck {
23*c2c66affSColin Finck     int        i;
24*c2c66affSColin Finck 
25*c2c66affSColin Finck     i = 0;
26*c2c66affSColin Finck     while (Path[i] != 0)
27*c2c66affSColin Finck     {
28*c2c66affSColin Finck         if (Path[i] == DOS_PATH_CHAR || Path[i] == UNIX_PATH_CHAR)
29*c2c66affSColin Finck         {
30*c2c66affSColin Finck             Path[i] = PATH_CHAR;
31*c2c66affSColin Finck         }
32*c2c66affSColin Finck 
33*c2c66affSColin Finck         i++;
34*c2c66affSColin Finck     }
35*c2c66affSColin Finck }
36*c2c66affSColin Finck 
MakeDirectory(char * Directory)37*c2c66affSColin Finck int MakeDirectory(char *Directory)
38*c2c66affSColin Finck {
39*c2c66affSColin Finck     char    CurrentDirectory[1024];
40*c2c66affSColin Finck 
41*c2c66affSColin Finck     getcwd(CurrentDirectory, 1024);
42*c2c66affSColin Finck 
43*c2c66affSColin Finck     if (chdir(Directory) == 0)
44*c2c66affSColin Finck     {
45*c2c66affSColin Finck         chdir(CurrentDirectory);
46*c2c66affSColin Finck         return 0;
47*c2c66affSColin Finck     }
48*c2c66affSColin Finck 
49*c2c66affSColin Finck #if defined (UNIX_PATHS) || defined (__DJGPP__)
50*c2c66affSColin Finck     if (mkdir(Directory, 0755) != 0)
51*c2c66affSColin Finck     {
52*c2c66affSColin Finck         perror("Failed to create directory");
53*c2c66affSColin Finck         return 1;
54*c2c66affSColin Finck     }
55*c2c66affSColin Finck #else
56*c2c66affSColin Finck     if (mkdir(Directory) != 0)
57*c2c66affSColin Finck     {
58*c2c66affSColin Finck         perror("Failed to create directory");
59*c2c66affSColin Finck         return 1;
60*c2c66affSColin Finck     }
61*c2c66affSColin Finck #endif
62*c2c66affSColin Finck 
63*c2c66affSColin Finck     if (chdir(Directory) != 0)
64*c2c66affSColin Finck     {
65*c2c66affSColin Finck         perror("Failed to change directory");
66*c2c66affSColin Finck         return 1;
67*c2c66affSColin Finck     }
68*c2c66affSColin Finck 
69*c2c66affSColin Finck     chdir(CurrentDirectory);
70*c2c66affSColin Finck 
71*c2c66affSColin Finck     return 0;
72*c2c66affSColin Finck }
73*c2c66affSColin Finck 
main(int argc,char * argv[])74*c2c66affSColin Finck int main(int argc, char* argv[])
75*c2c66affSColin Finck {
76*c2c66affSColin Finck     if (argc != 2)
77*c2c66affSColin Finck     {
78*c2c66affSColin Finck         fprintf(stderr, "Wrong number of arguments\n");
79*c2c66affSColin Finck         exit(1);
80*c2c66affSColin Finck     }
81*c2c66affSColin Finck 
82*c2c66affSColin Finck     ConvertPathCharacters(argv[1]);
83*c2c66affSColin Finck 
84*c2c66affSColin Finck     return MakeDirectory(argv[1]);
85*c2c66affSColin Finck }
86