]> git.mxchange.org Git - flightgear.git/blob - Makedir/makedir.cxx
96563cf1aba79094c797410b340942485eb9d88a
[flightgear.git] / Makedir / makedir.cxx
1 #include <ctype.h>    // isspace()
2 #include <stdlib.h>   // atoi()
3 #include <math.h>     // rint()
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/stat.h> // stat()
7 #include <unistd.h>   // stat()
8
9 #include <string>
10
11 #include <Bucket/bucketutils.h>
12
13 #ifdef __CYGWIN32__
14 #  define MKDIR(a) mkdir(a,S_IRWXU)     // I am just guessing at this flag
15 (NHV)
16 #endif // __CYGWIN32__
17
18 #ifdef __CYGWIN32__
19
20 // return the file path name ( foo/bar/file.ext = foo/bar )
21 static void extract_path (char *in, char *base) {
22     int len, i;
23
24     len = strlen (in);
25     strcpy (base, in);
26
27     i = len - 1;
28     while ( (i >= 0) && (in[i] != '/') ) {
29         i--;
30     }
31
32     base[i] = '\0';
33 }
34
35
36 // Make a subdirectory
37 static int my_mkdir (char *dir) {
38     struct stat stat_buf;
39     int result;
40
41     printf ("mk_dir() ");
42
43     result = stat (dir, &stat_buf);
44
45     if (result != 0) {
46         MKDIR (dir);
47         result = stat (dir, &stat_buf);
48         if (result != 0) {
49             printf ("problem creating %s\n", dir);
50         } else {
51             printf ("%s created\n", dir);
52         }
53     } else {
54         printf ("%s already exists\n", dir);
55     }
56
57     return (result);
58 }
59
60 #endif // __CYGWIN32__
61
62
63 void scenery_dir( const string& dir ) {
64     struct stat stat_buf;
65     char base_path[256], file[256], exfile[256];
66 #ifdef __CYGWIN32__
67     char tmp_path[256];
68 #endif
69     string command;
70     FILE *fd;
71         fgBUCKET p;
72         int result;
73
74     cout << "Dir = " + dir + "\n";
75
76     // stat() directory and create if needed
77     result = stat(dir.c_str(), &stat_buf);
78     if ( result != 0 ) {
79         cout << "Stat error need to create directory\n";
80
81 #ifndef __CYGWIN32__
82
83         command = "mkdir -p " + dir + "\n";
84         system( command.c_str() );
85
86 #else // __CYGWIN32__
87
88         // Cygwin crashes when trying to output to node file
89         // explicitly making directory structure seems OK on Win95
90
91         extract_path (base_path, tmp_path);
92
93         dir = tmp_path;
94         if (my_mkdir ( dir.c_str() )) { exit (-1); }
95
96         dir = base_path;
97         if (my_mkdir ( dir.c_str() )) { exit (-1); }
98
99 #endif // __CYGWIN32__
100
101     } else {
102         // assume directory exists
103                 cout << "  Allready Exists !\n";
104     }
105 }
106
107 int main(int argc, char **argv)
108 {
109         string root;
110
111         if(argc != 2)
112         {
113                 cout << "Makedir failed\n";
114                 return(10);
115         }
116         root = argv[1];
117         scenery_dir(root);
118
119         return(0);
120 }