set windows environment for ilib_for_link, add getenv, setenv, dos, winqueryreg gateways
[scilab.git] / scilab / modules / io / sci_gateway / c / sci_getenv.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 <stdio.h>
14 #include "gw_io.h"
15 #include "MALLOC.h"
16 #include "freeArrayOfString.h"
17 #include "localization.h"
18 #include "Scierror.h"
19 #include "getenvc.h"
20 #include "PATH_MAX.h"
21 #include "stack-c.h"
22 #include "api_scilab.h"
23 #include "api_oldstack.h"
24 /*--------------------------------------------------------------------------*/
25 int sci_getenv(char *fname, int* _piKey)
26 {
27         SciErr sciErr;
28         int ierr = 0;
29         char *default_env_value = NULL;
30         char *env_value = NULL;
31         int length_env = 0;
32         char *env_name = NULL;
33
34         int m1 = 0, n1 = 0;
35         int *piAddressVarOne = NULL;
36         int iType1      = 0;
37         char *pStVarOne = NULL;
38         int lenStVarOne = 0;
39
40         int m2 = 0, n2 = 0;
41         int *piAddressVarTwo = NULL;
42         int iType2      = 0;
43         char *pStVarTwo = NULL;
44         int lenStVarTwo = 0;
45
46         CheckRhs(1,2);
47         CheckLhs(1,1);
48
49         if(Rhs == 2)
50         {//second parameter
51                 sciErr = getVarAddressFromPosition(_piKey, 2, &piAddressVarTwo);
52                 if(sciErr.iErr)
53                 {
54                         printError(&sciErr, 0);
55                         return 1;
56                 }
57
58         if(isStringType(_piKey, piAddressVarTwo) == FALSE || isScalar(_piKey, piAddressVarTwo) == FALSE)
59                 {
60                         Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,2);
61                         return 1;
62                 }
63         
64         if(getAllocatedSingleString(_piKey, piAddressVarTwo, &pStVarTwo))
65         {
66             return 1;
67         }
68         }
69
70     //first parameter
71         sciErr = getVarAddressFromPosition(_piKey, 1, &piAddressVarOne);
72         if(sciErr.iErr)
73         {
74                 printError(&sciErr, 0);
75                 return 1;
76         }
77
78     if(isStringType(_piKey, piAddressVarOne) == FALSE || isScalar(_piKey, piAddressVarOne) == FALSE)
79     {
80         Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
81         return 1;
82     }
83
84     if(getAllocatedSingleString(_piKey, piAddressVarOne, &pStVarOne))
85     {
86         return 1;
87     }
88
89         #ifdef _MSC_VER
90         length_env = _MAX_ENV;
91         #else
92         length_env = bsiz;
93         #endif
94
95         default_env_value =  pStVarTwo;
96         env_name = pStVarOne;
97
98         env_value = (char*)MALLOC( (length_env + 1) *sizeof(char) );
99
100         if(env_value == NULL)
101         {
102                 if(default_env_value) {FREE(default_env_value); default_env_value = NULL;}
103                 if(env_name) {FREE(env_name); env_name = NULL;}
104
105                 Scierror(999,_("%s: No more memory.\n"), fname);
106                 return 1;
107         }
108         else
109         {
110                 int m_out = 1, n_out = 1;
111                 int iflag = 0;
112
113                 getenvc(&ierr, env_name, env_value, &length_env, &iflag);
114
115                 if(ierr == 0)
116                 {
117                         sciErr = createMatrixOfString(_piKey, Rhs + 1, m_out, n_out, &env_value);
118                         if(sciErr.iErr)
119                         {
120                                 printError(&sciErr, 0);
121                                 return 1;
122                         }
123
124                         LhsVar(1) = Rhs + 1;
125             PutLhsVar();        
126                 }
127                 else
128                 {
129                         if(default_env_value)
130                         {
131                                 sciErr = createMatrixOfString(_piKey, Rhs + 1, m_out, n_out, &default_env_value);
132                                 if(sciErr.iErr)
133                                 {
134                                         printError(&sciErr, 0);
135                                         return 1;
136                                 }
137
138                                 LhsVar(1) = Rhs + 1;
139                 PutLhsVar();    
140                         }
141                         else
142                         {
143                                 Scierror(999,_("%s: Undefined environment variable %s.\n"), fname, env_name);
144                         }
145                 }
146
147                 if(default_env_value) {FREE(default_env_value); default_env_value = NULL;}
148                 if(env_name) {FREE(env_name); env_name = NULL;}
149                 if(env_value) {FREE(env_value); env_value = NULL;}
150
151         }
152         return 0;
153 }
154 /*--------------------------------------------------------------------------*/