bug 14909: getshortpathname and getlongpathname return values with '\' instead of '/'
[scilab.git] / scilab / modules / fileio / src / c / getlongpathname.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Allan CORNET
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 /*--------------------------------------------------------------------------*/
16 #ifdef _MSC_VER
17 #include <windows.h>
18 #endif
19 #include <string.h>
20 #include "getlongpathname.h"
21 #include "sci_malloc.h"
22 #include "charEncoding.h"
23 #include "os_string.h"
24 /*--------------------------------------------------------------------------*/
25 #ifdef _MSC_VER
26 #ifndef MAX_PATH_LONG
27 #define MAX_PATH_LONG 32767
28 #endif
29 #endif
30 /*--------------------------------------------------------------------------*/
31 char *getlongpathname(const char *shortpathname, BOOL *convertok)
32 {
33     char *LongName = NULL;
34     wchar_t *wcshortpathname = to_wide_string(shortpathname);
35     if (wcshortpathname)
36     {
37         wchar_t *wcLongName = getlongpathnameW(wcshortpathname, convertok);
38         FREE(wcshortpathname);
39         if (wcLongName)
40         {
41             LongName = wide_string_to_UTF8(wcLongName);
42             FREE(wcLongName);
43             wcLongName = NULL;
44         }
45         else
46         {
47             LongName = os_strdup(shortpathname);
48             *convertok = FALSE;
49         }
50     }
51     else
52     {
53         LongName = os_strdup(shortpathname);
54         *convertok = FALSE;
55     }
56     return LongName;
57 }
58 /*--------------------------------------------------------------------------*/
59 wchar_t *getlongpathnameW(const wchar_t *wcshortpathname, BOOL *convertok)
60 {
61     wchar_t *wcLongName = NULL;
62
63 #ifdef _MSC_VER
64     /* first we try to call to know path length */
65     int length = GetLongPathNameW(wcshortpathname, NULL, 0);
66     if (length <= 0 )
67     {
68         length = MAX_PATH_LONG;
69     }
70
71     wcLongName = (wchar_t*)MALLOC((length + 1) * sizeof(wchar_t));
72
73     if (wcLongName)
74     {
75         /* second converts path */
76         if (GetLongPathNameW(wcshortpathname, wcLongName, length))
77         {
78             *convertok = TRUE;
79         }
80         else
81         {
82             /* FAILED */
83             if (wcLongName)
84             {
85                 wcscpy(wcLongName, wcshortpathname);
86             }
87             *convertok = FALSE;
88         }
89     }
90     else
91     {
92         /* FAILED */
93         *convertok = FALSE;
94     }
95 #else
96     /* Linux */
97     int len = (int)wcslen(wcshortpathname) + 1;
98     wcLongName = (wchar_t*)MALLOC(len * sizeof(wchar_t));
99     if (wcLongName)
100     {
101         wcscpy(wcLongName, wcshortpathname);
102     }
103     *convertok = FALSE;
104 #endif
105     return wcLongName;
106 }
107 /*--------------------------------------------------------------------------*/