Localization (fileio)
[scilab.git] / scilab / modules / fileio / sci_gateway / c / sci_isdir.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Sylvestre LEDRU
4  * ...
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 /*
15  * Provides is_dir to scilab
16  *
17  */
18 /*--------------------------------------------------------------------------*/ 
19 #include <stdio.h>
20 #include "gw_fileio.h"
21 #include "isdir.h"
22 #include "stack-c.h"
23 #include "MALLOC.h"
24 #include "cluni0.h"
25 #include "Scierror.h"
26 #include "localization.h"
27 /*--------------------------------------------------------------------------*/
28 int C2F(sci_isdir)(char *fname,unsigned long fname_len)
29 {
30         int l1 = 0,n1 = 0,m1 = 0;
31
32         CheckRhs(1,1);
33         CheckLhs(1,1);
34         
35         if (! (GetType(1) == sci_strings))
36         {
37                 Scierror(999,_("%s: Wrong type for input argument: String expected.\n"),fname);
38                 return 0;
39         }
40         else
41         {
42                 char *path = NULL, *myPath = NULL;
43                 char filename[FILENAME_MAX];
44                 long int lout;
45                 int out_n;
46                 BOOL result = FALSE;
47
48                 GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
49                 if ( n1==1 )
50                 {
51                         path=cstk(l1);
52                 }
53                 else
54                 {
55                         Scierror(999,_("%s: Wrong size for input argument: Single string expected.\n"),fname);
56                         return 0;
57                 }
58
59                 /* make sure the names are not too long */
60                 
61                 if( strlen(path) > PATH_MAX )
62                 {
63                   Scierror(999,_("%s: Wrong size for input argument: Must be less than %d characters.\n"),fname,PATH_MAX);
64                 }
65                 
66                 /* Crappy workaround because a / was added after SCI & ~ into 
67                  * the Scilab macros
68                  * cluni0 waits for SCI/ or ~/. It doesn't detect isdir("SCI")
69                  */
70                 if(strcmp(path,"SCI") == 0)
71                 {
72                         myPath = (char*)MALLOC((m1+2)*sizeof(char)); /* +2 because the / added + \0 */
73                         strcpy(myPath,"SCI/");
74                 }
75                 if(strcmp(path,"~") == 0)
76                 {
77                         myPath = (char*)MALLOC((m1+2)*sizeof(char)); /* +2 because the / added + \0 */
78                         strcpy(myPath,"~/");
79                 }
80                 /* End of the crappy workaround */
81
82                 lout = FILENAME_MAX;
83                 if(myPath == NULL)
84                 {
85                         /* Replaces SCI, ~, HOME by the real path */
86                         C2F(cluni0)(path, filename, &out_n,m1*n1,lout);
87                 }
88                 else
89                 {
90                         /* Replaces SCI, ~, HOME by the real path */
91                         C2F(cluni0)(myPath, filename, &out_n,m1*n1,lout);
92                 }
93
94                 result = isdir(filename);
95                 m1 = 1;
96                 n1 = 1;
97                 CreateVar(Rhs+1,MATRIX_OF_BOOLEAN_DATATYPE, &m1, &n1 ,&l1); /* Create the space in the stack for result */
98                 *istk(l1) = result; /* Copy result into the stack */
99                 
100                 LhsVar(1) = Rhs+1;
101                 C2F(putlhsvar)();
102                 if (myPath) {FREE(myPath);myPath = NULL;}
103         }
104         return 0;
105         
106 }
107 /*--------------------------------------------------------------------------*/