3293c80c33ecbd296ff512f8163abec49733a23c
[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  *
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 <string.h>
14 #ifdef _MSC_VER
15         #include <windows.h>
16         #include <direct.h>
17         #include <errno.h>
18 #else
19         #include <unistd.h>
20         #define GETCWD(x,y) getcwd(x,y)
21 #endif
22 #include "sciprint.h"
23 #include "scicurdir.h"
24 #include "MALLOC.h"
25 #include "localization.h"
26 #include "charEncoding.h"
27 #include "warningmode.h"
28 #include "PATH_MAX.h"
29 static char cur_dir[PATH_MAX];
30 /*--------------------------------------------------------------------------*/
31 int scichdir(char *path,int *err)
32 {
33         *err=0;
34         if (path == (char*) NULL)
35         {
36                 *cur_dir = '\0';
37                 return (0);
38         }
39
40 #ifndef _MSC_VER
41         if (chdir(path) == -1)
42         {
43                 if ( getWarningMode() ) sciprint(_("Can't go to directory %s.\n"), path);
44                 *err=1;
45         }
46 #else
47         /**/
48         {
49                 wchar_t *pwTemp = to_wide_string(path);
50
51                 if ( _wchdir(pwTemp) )
52                 {
53                         switch (errno)
54                         {
55                         case ENOENT:
56                                 if ( getWarningMode() ) sciprint(_("Can't go to directory %s.\n"), path);
57                                 break;
58                         case EINVAL:
59                                 if ( getWarningMode() ) sciprint(_("Invalid buffer.\n"));
60                                 break;
61                         default:
62                                 if ( getWarningMode() ) sciprint(_("Unknown error.\n"));
63                         }
64                         *err = 1;
65                 }
66                 FREE(pwTemp);
67         }
68 #endif
69         return 0;
70 }
71 /*--------------------------------------------------------------------------*/
72 int scigetcwd(char **path,int *lpath,int *err)
73 {
74 #ifndef _MSC_VER
75         if (GETCWD(cur_dir, PATH_MAX) == (char*) 0)
76 #else
77         wchar_t wcdir[PATH_MAX];
78         if ( _wgetcwd(wcdir, PATH_MAX) == (wchar_t*) 0 )
79 #endif
80         {
81                 /* get current working dir */
82                 if ( getWarningMode() ) sciprint(_("Can't get current directory.\n"));
83                 *cur_dir = '\0';
84                 *lpath = 0;
85                 *err = 1;
86         }
87   else
88         {
89 #ifdef _MSC_VER
90                 char *tmpcdir = wide_string_to_UTF8(wcdir);
91                 if (tmpcdir)
92                 {
93                         strcpy(cur_dir,tmpcdir);
94                         FREE(tmpcdir);
95                         tmpcdir = NULL;
96                         *path = cur_dir;
97                         *lpath =(int)strlen(cur_dir);
98                         *err = 0;
99                 }
100                 else
101                 {
102                         *cur_dir = '\0';
103                         *lpath=0;
104                         *err = 1;
105                 }
106 #else
107         *path = cur_dir;
108         *lpath =(int)strlen(cur_dir);
109         *err = 0;
110 #endif
111         }
112         return 0;
113 }
114 /*--------------------------------------------------------------------------*/