Merge remote-tracking branch 'origin/master' into windows
[scilab.git] / scilab / modules / fileio / src / c / createdirectory.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2007 - INRIA
4 * Copyright (C) 2009 - DIGITEO - Allan CORNET
5 *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14 *
15 */
16 /*--------------------------------------------------------------------------*/
17 #ifndef _MSC_VER
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <dirent.h>
23 #else
24 #include <Windows.h>
25 #include <stdio.h>
26 #endif
27 #include "charEncoding.h"
28 #include "sci_malloc.h"
29 #include "createdirectory.h"
30 #include "isdir.h"
31 #include "splitpath.h"
32 #include "scicurdir.h"
33 #include "os_string.h"
34 #ifndef _MSC_VER
35 #define MAX_PATH PATH_MAX
36 #endif
37 /*--------------------------------------------------------------------------*/
38 #define DIRMODE 0777
39 /*--------------------------------------------------------------------------*/
40 BOOL createdirectory(const char *path)
41 {
42     BOOL bOK = FALSE;
43     if (path)
44     {
45         if (!isdir(path))
46         {
47 #ifndef _MCS_VER
48             if (mkdir(path, DIRMODE) == 0)
49             {
50                 bOK = TRUE;
51             }
52 #else
53             char path_out[MAX_PATH];
54             char drv[MAX_PATH];
55             char dir[MAX_PATH];
56             splitpath(path, TRUE, drv, dir, NULL, NULL);
57
58             os_strcpy(path_out, drv);
59             strcat(path_out, dir);
60
61             //if there is no path_out, get current dir as reference.
62             if (strlen(path_out) == 0)
63             {
64                 int err = 0;
65                 char* cur = NULL;
66                 if (strlen(path)  == 0)
67                 {
68                     return FALSE;
69                 }
70
71                 cur = scigetcwd(&err);
72                 os_strcpy(path_out, cur);
73                 FREE(cur);
74             }
75
76             if (CreateDirectoryExA(path_out, path, NULL))
77             {
78                 bOK = TRUE;
79             }
80 #endif // _MCS_VER
81
82         }
83     }
84     return bOK;
85 }
86 /*--------------------------------------------------------------------------*/