cd & chdir factorized
[scilab.git] / scilab / modules / fileio / sci_gateway / c / sci_chdir.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2006 - INRIA - Allan CORNET
4  * Copyright (C) 2009 - DIGITEO - Allan CORNET
5  * 
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at    
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12 /*--------------------------------------------------------------------------*/
13 #include "gw_fileio.h"
14 #include "stack-c.h"
15 #include "Scierror.h"
16 #include "scicurdir.h"
17 #include "localization.h"
18 #include "expandPathVariable.h"
19 #include "MALLOC.h"
20 #include "localization.h"
21 #include "api_common.h"
22 #include "api_string.h"
23 #include "api_double.h"
24 #include "api_boolean.h"
25 #include "isdir.h"
26 #include "charEncoding.h"
27 /*--------------------------------------------------------------------------*/
28 int sci_chdir(char *fname,unsigned long fname_len)
29 {
30         int *piAddressVarOne = NULL;
31         wchar_t *pStVarOne = NULL;
32         int lenStVarOne = 0;
33         int m1 = 0, n1 = 0;
34
35         wchar_t *expandedPath = NULL;
36
37         Rhs = Max(0, Rhs);
38         CheckRhs(0,1);
39         CheckLhs(1,1);
40
41         if (Rhs == 0)
42         {
43                 pStVarOne = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(L"home")+1));
44                 if (pStVarOne)
45                 {
46                         wcscpy(pStVarOne, L"home");
47                 }
48         }
49         else
50         {
51                 getVarAddressFromPosition(1, &piAddressVarOne);
52
53                 if ( getVarType(piAddressVarOne) != sci_strings )
54                 {
55                         Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,1);
56                         return 0;
57                 }
58
59                 getMatrixOfWideString(piAddressVarOne,&m1,&n1,&lenStVarOne,&pStVarOne);
60                 if ( (m1 != n1) && (n1 != 1) ) 
61                 {
62                         Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
63                         return 0;
64                 }
65
66                 pStVarOne = (wchar_t*)MALLOC(sizeof(wchar_t)*(lenStVarOne + 1));
67                 if (pStVarOne == NULL)
68                 {
69                         Scierror(999,_("%s : Memory allocation error.\n"),fname);
70                         return 0;
71                 }
72
73                 getMatrixOfWideString(piAddressVarOne, &m1, &n1, &lenStVarOne, &pStVarOne);
74         }
75
76         expandedPath = expandPathVariableW(pStVarOne);
77         if (pStVarOne) {FREE(pStVarOne); pStVarOne = NULL;}
78
79         if (expandedPath)
80         {
81                 /* get value of PWD scilab variable (compatiblity scilab 4.x) */
82                 if (wcscmp(expandedPath, L"PWD") == 0)
83                 {
84                         if (getNamedVarType("PWD") == sci_strings)
85                         {
86                                 wchar_t *VARVALUE = NULL;
87                                 int VARVALUElen = 0;
88                                 int m = 0, n = 0;
89                                 if (readNamedMatrixOfWideString("PWD", &m, &n, &VARVALUElen, &VARVALUE) == 0)
90                                 {
91                                         if ( (m == 1) && (n == 1) )
92                                         {
93                                                 VARVALUE = (wchar_t*)MALLOC(sizeof(wchar_t)*(VARVALUElen + 1));
94                                                 if (VARVALUE)
95                                                 {
96                                                         readNamedMatrixOfWideString("PWD", &m, &n, &VARVALUElen, &VARVALUE);
97                                                         FREE(expandedPath);
98                                                         expandedPath = VARVALUE;
99                                                 }
100                                         }
101                                 }
102                         }
103                 }
104
105                 if (strcmp(fname, "chdir") == 0) /* chdir output boolean */
106                 {       
107                         BOOL *bOuput = (BOOL*)MALLOC(sizeof(BOOL));
108
109                         int ierr = scichdirW(expandedPath);
110
111                         if (ierr) bOuput[0] = FALSE;
112                         else bOuput[0] = TRUE; 
113
114                         createMatrixOfBoolean(Rhs + 1, 1, 1, bOuput);
115
116                         LhsVar(1) = Rhs + 1;
117                         C2F(putlhsvar)();
118                         
119                 }
120                 else /* cd output string current path */
121                 {
122                         if ( isdirW(expandedPath) || (wcscmp(expandedPath,L"/") == 0) ||
123                                  (wcscmp(expandedPath,L"\\") == 0) )
124                         {
125                                 int ierr = scichdirW(expandedPath);
126                                 wchar_t *currentDir = scigetcwdW(&ierr);
127                                 if ( (ierr == 0) && currentDir)
128                                 {
129                                         createMatrixOfWideString(Rhs + 1, 1, 1, &currentDir);
130                                 }
131                                 else
132                                 {
133                                         createMatrixOfDouble(Rhs + 1, 0, 0, NULL);
134                                 }
135
136                                 LhsVar(1) = Rhs + 1;
137                                 C2F(putlhsvar)();
138
139                                 if (currentDir) {FREE(currentDir); currentDir = NULL;}
140                         }
141                         else
142                         {
143                                 char *path = wide_string_to_UTF8(expandedPath);
144                                 if (path)
145                                 {
146                                         Scierror(998, _("%s: Cannot go to directory %s\n"), fname, path);
147                                         FREE(path);
148                                         path = NULL;
149                                 }
150                                 else
151                                 {
152                                         Scierror(998, _("%s: Cannot go to directory.\n"), fname);
153                                 }
154                         }
155                 }
156
157                 FREE(expandedPath); expandedPath = NULL;
158         }
159         else
160         {
161                 Scierror(999,_("%s : Memory allocation error.\n"),fname);
162         }
163
164         return 0;
165 }
166 /*--------------------------------------------------------------------------*/