]> git.lyx.org Git - lyx.git/blob - src/support/mkdir.cpp
Revert qprocess code. Revisions reverted: 22026, 22030, 22044, 22048,
[lyx.git] / src / support / mkdir.cpp
1 /**
2  * \file mkdir.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/lyxlib.h"
14 #include "support/debug.h"
15 #include "support/FileName.h"
16
17 #ifdef HAVE_SYS_STAT_H
18 # include <sys/stat.h>
19 #endif
20 #ifdef HAVE_SYS_TYPES_H
21 # include <sys/types.h>
22 #endif
23 #include <fcntl.h>
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #ifdef HAVE_DIRECT_H
28 # include <direct.h>
29 #endif
30 #ifdef _WIN32
31 # include <windows.h>
32 #endif
33
34 namespace lyx {
35 namespace support {
36
37
38 int mymkdir(char const * pathname, unsigned long int mode)
39 {
40         LYXERR0("MKDIR" << pathname);
41         // FIXME: why don't we have mode_t in lyx::mkdir prototype ??
42 #if HAVE_MKDIR
43 # if MKDIR_TAKES_ONE_ARG
44         // MinGW32
45         return ::mkdir(pathname);
46         // FIXME: "Permissions of created directories are ignored on this system."
47 # else
48         // POSIX
49         return ::mkdir(pathname, mode_t(mode));
50 # endif
51 #elif defined(_WIN32)
52         // plain Windows 32
53         return CreateDirectory(pathname, 0) != 0 ? 0 : -1;
54         // FIXME: "Permissions of created directories are ignored on this system."
55 #elif HAVE__MKDIR
56         return ::_mkdir(pathname);
57         // FIXME: "Permissions of created directories are ignored on this system."
58 #else
59 #   error "Don't know how to create a directory on this system."
60 #endif
61
62 }
63
64 int mkdir(FileName const & pathname, unsigned long int mode)
65 {
66         return mymkdir(pathname.toFilesystemEncoding().c_str(), mode);
67 }
68
69
70 // adapted from zlib-1.2.3/contrib/minizip/miniunz.c
71 int makedir(char * newdir, unsigned long int mode)
72 {
73         char *buffer;
74         char *p;
75         int     len = (int)strlen(newdir);
76
77         if (len <= 0)
78                 return 1;
79
80         buffer = (char*)malloc(len+1);
81         strcpy(buffer,newdir);
82
83         if (buffer[len-1] == '/')
84                 buffer[len-1] = '\0';
85         if (mymkdir(buffer, mode) == 0) {
86                 free(buffer);
87                 return 0;
88         }
89
90         p = buffer + 1;
91         while (1) {
92                 char hold;
93
94                 while(*p && *p != '\\' && *p != '/')
95                         p++;
96                 hold = *p;
97                 *p = 0;
98                 if (mymkdir(buffer, mode) != 0) {
99                         free(buffer);
100                         return 1;
101                 }
102                 if (hold == 0)
103                         break;
104                 *p++ = hold;
105         }
106         free(buffer);
107         return 0;
108 }
109
110
111 } // namespace support
112 } // namespace lyx