1 /*
2  * Copyright (c) 2017, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 /* clang-format off */
19 
20 /*	getfileinfoqq3f.c - Implements DFLIB getfileinfoqq subprogram.  */
21 #if defined(WIN64) || defined(WIN32)
22 #include <windows.h>
23 #endif
24 #include <string.h>
25 #include <stdlib.h>
26 /* must include ent3f.h AFTER io3f.h */
27 #include "io3f.h"
28 #include "ent3f.h"
29 
30 #define FILE$FIRST -1
31 #define FILE$LAST -2
32 #define FILE$ERROR -3
33 
34 #if defined(WIN64) || defined(WIN32)
35 extern char *__fstr2cstr();
36 extern void __GetTimeToSecondsSince1970(ULARGE_INTEGER *fileTime,
37                                         unsigned int *out);
ENT3F(GETFILEINFOQQI8,getfileinfoqqi8)38 int ENT3F(GETFILEINFOQQI8, getfileinfoqqi8)(DCHAR(ffiles), char *buffer,
39                                             int *handle DCLEN(ffiles))
40 {
41   char *files;
42   int rslt = 0, i;
43   WIN32_FIND_DATA FindFileData;
44   HANDLE hFind;
45   ULARGE_INTEGER fileTime;
46   char *name;
47   struct FILE$INFO8 {
48     int creation;
49     int lastWrite;
50     int lastAccess;
51     long long length;
52     int permit;
53     char name[256];
54   } fileInfo;
55 
56   files = __fstr2cstr(CADR(ffiles), CLEN(ffiles));
57   if (!files || !buffer || !handle) {
58     __io_errno();
59     return 0;
60   }
61   if (*handle <= 0) {
62     hFind = FindFirstFile(files, &FindFileData);
63     *handle = hFind;
64   } else {
65     hFind = (HANDLE)*handle;
66     if (FindNextFile(hFind, &FindFileData) == 0) {
67       *handle = FILE$LAST;
68       FindClose(hFind);
69       goto rtn;
70     }
71   }
72   if (hFind == INVALID_HANDLE_VALUE) {
73     *handle = FILE$ERROR;
74     goto rtn;
75   }
76 
77   fileTime.u.LowPart = FindFileData.ftCreationTime.dwLowDateTime;
78   fileTime.u.HighPart = FindFileData.ftCreationTime.dwHighDateTime;
79   __GetTimeToSecondsSince1970(&fileTime, &(fileInfo.creation));
80 
81   fileTime.u.LowPart = FindFileData.ftLastWriteTime.dwLowDateTime;
82   fileTime.u.HighPart = FindFileData.ftLastWriteTime.dwHighDateTime;
83   __GetTimeToSecondsSince1970(&fileTime, &(fileInfo.lastWrite));
84 
85   fileTime.u.LowPart = FindFileData.ftLastAccessTime.dwLowDateTime;
86   fileTime.u.HighPart = FindFileData.ftLastAccessTime.dwHighDateTime;
87   __GetTimeToSecondsSince1970(&fileTime, &(fileInfo.lastAccess));
88 
89   fileInfo.length = (int)((FindFileData.nFileSizeHigh * (MAXDWORD + 1)) +
90                           FindFileData.nFileSizeLow);
91   fileInfo.permit = FindFileData.dwFileAttributes;
92   name = FindFileData.cFileName;
93   rslt = strlen(name);
94 
95   __fcp_cstr(fileInfo.name, 255, name);
96   memcpy(buffer, &fileInfo, sizeof(fileInfo));
97 rtn:
98   __cstr_free(files);
99   return rslt;
100 }
101 #else
ENT3F(GETFILEINFOQQI8,getfileinfoqqi8)102 int ENT3F(GETFILEINFOQQI8, getfileinfoqqi8)(DCHAR(ffiles), int *buffer,
103                                             int *handle DCLEN(ffiles))
104 {
105   fprintf(__io_stderr(),
106           "getfileinfoqqi8() not implemented on this target\n");
107   return 0;
108 }
109 
110 #endif
111