1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Cedric DELAMARRE
4 * Copyright (C) 2006 - INRIA - Allan CORNET
5 *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14 *
15 */
16 #include <sys/stat.h>
17 #include <string.h>
18 #include <stdio.h>
19 
20 #include "filemanager.hxx"
21 
22 extern "C"
23 {
24 #include "sci_malloc.h"
25 #include "expandPathVariable.h"
26 #include "charEncoding.h"
27 #include "newest.h"
28 }
29 
newest(wchar_t ** _pwcsFilesString,int _iNbrOfFileString)30 int newest(wchar_t** _pwcsFilesString, int _iNbrOfFileString)
31 {
32 #ifdef _MSC_VER
33     struct _stat buf;
34 #else
35     struct stat buf;
36 #endif
37 
38     int i = 0;
39     int RetVal = 0;
40 
41     int RetIndex = 1;
42     long int MaxTime = 0;
43 
44     for (i = 0; i < _iNbrOfFileString ; i++)
45     {
46 
47         int resultstat = 0;
48         wchar_t *FileName = NULL;
49 
50         FileName = expandPathVariableW(_pwcsFilesString[i]);
51 
52 #ifdef _MSC_VER
53         if (FileName)
54         {
55             if ( (FileName[wcslen(FileName) - 1] == L'/') || (FileName[wcslen(FileName) - 1] == L'\\') )
56             {
57                 FileName[wcslen(FileName) - 1] = L'\0';
58             }
59 
60         }
61         resultstat = _wstat(FileName, &buf );
62 #else
63         char* temp = wide_string_to_UTF8(FileName);
64         resultstat = stat(temp, &buf );
65         FREE(temp);
66 #endif
67         if (resultstat == 0)
68         {
69             if ((long int)buf.st_mtime > MaxTime)
70             {
71                 MaxTime = (long int)buf.st_mtime;
72                 RetIndex = i + 1;
73             }
74         }
75 
76         FREE(FileName);
77         FileName = NULL;
78     }
79 
80     RetVal = RetIndex;
81     return RetVal;
82 }
83