update for [a,b] = getversion()
[scilab.git] / scilab / modules / io / src / c / getenvc.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2006 - INRIA
4  * Copyright (C) 2008 - INRIA - 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
14 #include <stdlib.h> 
15 #include <string.h> /* strlen */
16 #ifdef _MSC_VER
17         #include <Windows.h> /* GetEnvironmentVariable */
18         #include "strdup_windows.h"
19 #endif
20 #include "MALLOC.h"
21 #include "getenvc.h"
22 #include "localization.h"
23 #include "sciprint.h"
24 #include "FileExist.h"
25 /*--------------------------------------------------------------------------*/
26 #ifndef _MSC_VER
27 static void searchenv_others(const char *filename, const char *varname,
28                             char *pathname);
29 #endif
30 /*--------------------------------------------------------------------------*/
31 void C2F(getenvc)(int *ierr,char *var,char *buf,int *buflen,int *iflag)
32 {
33         #ifdef _MSC_VER
34         if (GetEnvironmentVariable(var,buf,(DWORD)*buflen) == 0)
35         {
36                 if ( *iflag == 1 ) sciprint(_("Undefined environment variable %s.\n"),var);
37                 *ierr=1;
38         }
39         else
40         {
41                 *buflen = (int)strlen(buf);
42                 *ierr=0;
43         }
44         #else
45         char *local;
46         if ( (local=getenv(var)) == 0)
47         {
48                 if ( *iflag == 1 ) sciprint(_("Undefined environment variable %s.\n"),var);
49                 *ierr=1;
50         }
51         else
52         {
53                 strncpy(buf,local,*buflen);
54                 *buflen = strlen(buf);
55                 *ierr=0;
56         }
57         #endif
58 }
59 /*--------------------------------------------------------------------------*/
60 #ifndef _MSC_VER
61 static void searchenv_others(const char *filename, 
62                             const char *varname,
63                             char *pathname)
64 {
65         char *cp = NULL;
66
67         *pathname = '\0';
68
69         if( filename[0] == DIR_SEPARATOR[0])
70         {
71                 strcpy(pathname, filename);
72                 return;
73         }
74         
75         cp = getenv(varname);
76         if(cp == NULL)
77         {
78                 /* environment Variable not defined. */
79                 return;
80         }
81
82         while(*cp)
83         {
84                 char *concat = NULL;
85                 *pathname = '\0';
86         concat = pathname;
87                 /* skip PATH_SEPARATOR[0] and empty entries */
88                 while( (*cp) && (*cp == PATH_SEPARATOR[0]) )
89                 {
90                         cp++;
91                 }
92
93                 /* copy path */
94                 while( (*cp) && (*cp != PATH_SEPARATOR[0]) )
95                 {
96                         *concat = *cp;
97                         cp++;
98                         concat++;
99                 }
100                 
101                 if ( concat == pathname )
102                 {
103                         /* filename not found */
104                         *pathname = '\0';
105                         return;
106                 }
107                 
108                 if( *(concat-1) != DIR_SEPARATOR[0] )
109                 {
110                         /* add directory separator */
111                         *concat = DIR_SEPARATOR[0];
112                         concat++;
113                 }
114
115                 /* concatate path & filename */
116                 strcpy(concat, filename);
117
118                 /* file exists ? */
119                 if(FileExist(pathname))
120                 {
121                         // file found
122                         return;
123                 }
124         }
125
126         /* file not found */
127         *pathname = '\0';
128 }
129 #endif
130 /*--------------------------------------------------------------------------*/
131 char *searchEnv(const char *name,const char *env_var)
132 {
133         char *buffer = NULL;
134         char fullpath[PATH_MAX];
135
136         strcpy(fullpath,"");
137
138         #if _MSC_VER 
139                 _searchenv(name,env_var,fullpath);
140         #else
141                 searchenv_others(name,env_var,fullpath);
142         #endif
143
144         if (strlen(fullpath) > 0) buffer = strdup(fullpath);
145         return buffer;
146 }
147 /*--------------------------------------------------------------------------*/
148