cd & chdir factorized
[scilab.git] / scilab / modules / fileio / src / c / scicurdir.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) INRIA
4  * Copyright (C) ENPC
5  * Copyright (C) DIGITEO - 2009 - Allan CORNET
6  *
7  * This file must be used under the terms of the CeCILL.
8  * This source file is licensed as described in the file COPYING, which
9  * you should have received as part of this distribution.  The terms
10  * are also available at
11  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12  *
13  */
14 #include <string.h>
15 #ifdef _MSC_VER
16         #include <windows.h>
17         #include <direct.h>
18         #include <errno.h>
19 #else
20         #include <unistd.h>
21         #define GETCWD(x,y) getcwd(x,y)
22 #endif
23 #include "sciprint.h"
24 #include "scicurdir.h"
25 #include "MALLOC.h"
26 #include "localization.h"
27 #include "charEncoding.h"
28 #include "warningmode.h"
29 #include "PATH_MAX.h"
30 /*--------------------------------------------------------------------------*/
31 int scichdirW(wchar_t *wcpath)
32 {
33 #ifndef _MSC_VER
34         char *path = NULL;
35         if (wcpath == NULL)
36         {
37                 return 1;
38         }
39
40         path = wide_string_to_UTF8(wcpath);
41         if (path == NULL)
42         {
43                 return 1;
44         }
45
46         if (chdir(path) == -1)
47         {
48                 if ( getWarningMode() ) sciprint(_("Can't go to directory %s.\n"), path);
49                 if (path) {FREE(path); path = NULL;}
50                 return 1;
51         }
52
53         if (path) {FREE(path); path = NULL;}
54
55 #else
56         if (wcpath == NULL)
57         {
58                 return 1;
59         }
60
61         if ( _wchdir(wcpath) )
62         {
63                 switch (errno)
64                 {
65                 case ENOENT:
66                         {
67                                 if ( getWarningMode() ) 
68                                 {
69                                         char *path = wide_string_to_UTF8(wcpath);
70                                         if (path)
71                                         {
72                                                 sciprint(_("Can't go to directory %s.\n"), path);
73                                                 FREE(path);
74                                                 path = NULL;
75                                         }
76                                 }
77                         }
78                         break;
79                 case EINVAL:
80                         {
81                                 if ( getWarningMode() ) sciprint(_("Invalid buffer.\n"));
82                         }
83                         break;
84                 default:
85                         {
86                                 if ( getWarningMode() ) sciprint(_("Unknown error.\n"));
87                         }
88                 }
89                 return 1;
90         }
91 #endif
92         return 0;
93 }
94 /*--------------------------------------------------------------------------*/
95 int scichdir(char *path)
96 {
97         int ierr = 1;
98         wchar_t *wcpath = NULL;
99         if (path == NULL) return ierr;
100         wcpath = to_wide_string(path);
101         if (wcpath == NULL) return ierr;
102         ierr = scichdirW(wcpath);
103         FREE(wcpath);
104         wcpath = NULL;
105         return ierr;
106 }
107 /*--------------------------------------------------------------------------*/
108 wchar_t * scigetcwdW(int *err)
109 {
110         wchar_t *wcCurrentDir = NULL;
111         
112 #ifndef _MSC_VER
113         char currentDir[PATH_MAX + 1]:
114         if (GETCWD(cur_dir, PATH_MAX) == NULL)
115         {
116                 if ( getWarningMode() ) sciprint(_("Can't get current directory.\n"));
117                 *err = 1;
118         }
119         else
120         {
121                 wcCurrentDir = to_wide_string(currentDir);
122                 *err = 0;
123         }
124 #else
125         wchar_t wcdir[PATH_MAX + 1];
126         if ( _wgetcwd(wcdir, PATH_MAX) == NULL )
127         {
128                 if ( getWarningMode() ) sciprint(_("Can't get current directory.\n"));
129                 *err = 1;
130         }
131         else
132         {
133                 wcCurrentDir = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(wcdir) + 1));
134                 if (wcCurrentDir)
135                 {
136                         wcscpy(wcCurrentDir, wcdir);
137                 }
138         }
139 #endif
140         return wcCurrentDir;
141 }
142 /*--------------------------------------------------------------------------*/
143 char * scigetcwd(int *err)
144 {
145         char *currentDir = NULL;
146         wchar_t *wcCurrentDir = scigetcwdW(err);
147         if (wcCurrentDir)
148         {
149                 currentDir = wide_string_to_UTF8(wcCurrentDir);
150                 FREE(wcCurrentDir);
151                 wcCurrentDir = NULL;
152         }
153         return currentDir;
154 }
155 /*--------------------------------------------------------------------------*/