1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 *   Copyright (C) 1999-2014, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 *******************************************************************************
10 *   file name:  toolutil.c
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   created on: 1999nov19
16 *   created by: Markus W. Scherer
17 *
18 *	6/25/08 - Added Cygwin specific code in uprv_mkdir - Brian Rower
19 *
20 *   This file contains utility functions for ICU tools like genccode.
21 */
22 
23 #include "unicode/platform.h"
24 #if U_PLATFORM == U_PF_MINGW
25 // *cough* - for struct stat
26 #ifdef __STRICT_ANSI__
27 #undef __STRICT_ANSI__
28 #endif
29 #endif
30 
31 #include <stdio.h>
32 #include <sys/stat.h>
33 #include <fstream>
34 #include <time.h>
35 #include "unicode/utypes.h"
36 
37 #ifndef U_TOOLUTIL_IMPLEMENTATION
38 #error U_TOOLUTIL_IMPLEMENTATION not set - must be set for all ICU source files in common/ - see https://unicode-org.github.io/icu/userguide/howtouseicu
39 #endif
40 
41 #if U_PLATFORM_USES_ONLY_WIN32_API
42 #   define VC_EXTRALEAN
43 #   define WIN32_LEAN_AND_MEAN
44 #   define NOUSER
45 #   define NOSERVICE
46 #   define NOIME
47 #   define NOMCX
48 #   if U_PLATFORM == U_PF_MINGW
49 #     define __NO_MINGW_LFS /* gets around missing 'off64_t' */
50 #   endif
51 #   include <windows.h>
52 #   include <direct.h>
53 #else
54 #   include <sys/stat.h>
55 #   include <sys/types.h>
56 #endif
57 
58 /* In MinGW environment, io.h needs to be included for _mkdir() */
59 #if U_PLATFORM == U_PF_MINGW
60 #include <io.h>
61 #endif
62 
63 #include <errno.h>
64 
65 #include <cstddef>
66 
67 #include "unicode/errorcode.h"
68 #include "unicode/putil.h"
69 #include "cmemory.h"
70 #include "cstring.h"
71 #include "toolutil.h"
72 
73 U_NAMESPACE_BEGIN
74 
~IcuToolErrorCode()75 IcuToolErrorCode::~IcuToolErrorCode() {
76     // Safe because our handleFailure() does not throw exceptions.
77     if(isFailure()) { handleFailure(); }
78 }
79 
handleFailure() const80 void IcuToolErrorCode::handleFailure() const {
81     fprintf(stderr, "error at %s: %s\n", location, errorName());
82     exit(errorCode);
83 }
84 
85 U_NAMESPACE_END
86 
87 static int32_t currentYear = -1;
88 
getCurrentYear()89 U_CAPI int32_t U_EXPORT2 getCurrentYear() {
90     if(currentYear == -1) {
91         time_t now = time(nullptr);
92         tm *fields = gmtime(&now);
93         currentYear = 1900 + fields->tm_year;
94     }
95     return currentYear;
96 }
97 
98 
99 U_CAPI const char * U_EXPORT2
getLongPathname(const char * pathname)100 getLongPathname(const char *pathname) {
101 #if U_PLATFORM_USES_ONLY_WIN32_API
102     /* anticipate problems with "short" pathnames */
103     static WIN32_FIND_DATAA info;
104     HANDLE file=FindFirstFileA(pathname, &info);
105     if(file!=INVALID_HANDLE_VALUE) {
106         if(info.cAlternateFileName[0]!=0) {
107             /* this file has a short name, get and use the long one */
108             const char *basename=findBasename(pathname);
109             if(basename!=pathname) {
110                 /* prepend the long filename with the original path */
111                 uprv_memmove(info.cFileName+(basename-pathname), info.cFileName, uprv_strlen(info.cFileName)+1);
112                 uprv_memcpy(info.cFileName, pathname, basename-pathname);
113             }
114             pathname=info.cFileName;
115         }
116         FindClose(file);
117     }
118 #endif
119     return pathname;
120 }
121 
122 U_CAPI const char * U_EXPORT2
findDirname(const char * path,char * buffer,int32_t bufLen,UErrorCode * status)123 findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status) {
124   if(U_FAILURE(*status)) return NULL;
125   const char *resultPtr = NULL;
126   int32_t resultLen = 0;
127 
128   const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR);
129 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
130   const char *basenameAlt=uprv_strrchr(path, U_FILE_ALT_SEP_CHAR);
131   if(basenameAlt && (!basename || basename<basenameAlt)) {
132     basename = basenameAlt;
133   }
134 #endif
135   if(!basename) {
136     /* no basename - return ''. */
137     resultPtr = "";
138     resultLen = 0;
139   } else {
140     resultPtr = path;
141     resultLen = static_cast<int32_t>(basename - path);
142     if(resultLen<1) {
143       resultLen = 1; /* '/' or '/a' -> '/' */
144     }
145   }
146 
147   if((resultLen+1) <= bufLen) {
148     uprv_strncpy(buffer, resultPtr, resultLen);
149     buffer[resultLen]=0;
150     return buffer;
151   } else {
152     *status = U_BUFFER_OVERFLOW_ERROR;
153     return NULL;
154   }
155 }
156 
157 U_CAPI const char * U_EXPORT2
findBasename(const char * filename)158 findBasename(const char *filename) {
159     const char *basename=uprv_strrchr(filename, U_FILE_SEP_CHAR);
160 
161 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
162     //be lenient about pathname separators on Windows, like official implementation of C++17 std::filesystem in MSVC
163     //would be convenient to merge this loop with the one above, but alas, there is no such solution in the standard library
164     const char *alt_basename=uprv_strrchr(filename, U_FILE_ALT_SEP_CHAR);
165     if(alt_basename>basename) {
166         basename=alt_basename;
167     }
168 #endif
169 
170     if(basename!=NULL) {
171         return basename+1;
172     } else {
173         return filename;
174     }
175 }
176 
177 U_CAPI void U_EXPORT2
uprv_mkdir(const char * pathname,UErrorCode * status)178 uprv_mkdir(const char *pathname, UErrorCode *status) {
179 
180     int retVal = 0;
181 #if U_PLATFORM_USES_ONLY_WIN32_API
182     retVal = _mkdir(pathname);
183 #else
184     retVal = mkdir(pathname, S_IRWXU | (S_IROTH | S_IXOTH) | (S_IROTH | S_IXOTH));
185 #endif
186     if (retVal && errno != EEXIST) {
187 #if U_PF_MINGW <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
188         /*if using Cygwin and the mkdir says it failed...check if the directory already exists..*/
189         /* if it does...don't give the error, if it does not...give the error - Brian Rower - 6/25/08 */
190         struct stat st;
191 
192         if(stat(pathname,&st) != 0)
193         {
194             *status = U_FILE_ACCESS_ERROR;
195         }
196 #else
197         *status = U_FILE_ACCESS_ERROR;
198 #endif
199     }
200 }
201 
202 #if !UCONFIG_NO_FILE_IO
203 U_CAPI UBool U_EXPORT2
uprv_fileExists(const char * file)204 uprv_fileExists(const char *file) {
205   struct stat stat_buf;
206   if (stat(file, &stat_buf) == 0) {
207     return TRUE;
208   } else {
209     return FALSE;
210   }
211 }
212 #endif
213 
214 U_CAPI int32_t U_EXPORT2
uprv_compareGoldenFiles(const char * buffer,int32_t bufferLen,const char * goldenFilePath,bool overwrite)215 uprv_compareGoldenFiles(
216         const char* buffer, int32_t bufferLen,
217         const char* goldenFilePath,
218         bool overwrite) {
219 
220     if (overwrite) {
221         std::ofstream ofs;
222         ofs.open(goldenFilePath);
223         ofs.write(buffer, bufferLen);
224         ofs.close();
225         return -1;
226     }
227 
228     std::ifstream ifs(goldenFilePath, std::ifstream::in);
229     int32_t pos = 0;
230     char c;
231     while ((c = ifs.get()) != std::char_traits<char>::eof() && pos < bufferLen) {
232         if (c != buffer[pos]) {
233             // Files differ at this position
234             return pos;
235         }
236         pos++;
237     }
238     if (pos < bufferLen || c != std::char_traits<char>::eof()) {
239         // Files are different lengths
240         return pos;
241     }
242     return -1;
243 }
244 
245 /*U_CAPI UDate U_EXPORT2
246 uprv_getModificationDate(const char *pathname, UErrorCode *status)
247 {
248     if(U_FAILURE(*status)) {
249         return;
250     }
251     //  TODO: handle case where stat is not available
252     struct stat st;
253 
254     if(stat(pathname,&st) != 0)
255     {
256         *status = U_FILE_ACCESS_ERROR;
257     } else {
258         return st.st_mtime;
259     }
260 }
261 */
262 
263 /* tool memory helper ------------------------------------------------------- */
264 
265 struct UToolMemory {
266     char name[64];
267     int32_t capacity, maxCapacity, size, idx;
268     void *array;
269     alignas(std::max_align_t) char staticArray[1];
270 };
271 
272 U_CAPI UToolMemory * U_EXPORT2
utm_open(const char * name,int32_t initialCapacity,int32_t maxCapacity,int32_t size)273 utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size) {
274     UToolMemory *mem;
275 
276     if(maxCapacity<initialCapacity) {
277         maxCapacity=initialCapacity;
278     }
279 
280     mem=(UToolMemory *)uprv_malloc(sizeof(UToolMemory)+initialCapacity*size);
281     if(mem==NULL) {
282         fprintf(stderr, "error: %s - out of memory\n", name);
283         exit(U_MEMORY_ALLOCATION_ERROR);
284     }
285     mem->array=mem->staticArray;
286 
287     uprv_strcpy(mem->name, name);
288     mem->capacity=initialCapacity;
289     mem->maxCapacity=maxCapacity;
290     mem->size=size;
291     mem->idx=0;
292     return mem;
293 }
294 
295 U_CAPI void U_EXPORT2
utm_close(UToolMemory * mem)296 utm_close(UToolMemory *mem) {
297     if(mem!=NULL) {
298         if(mem->array!=mem->staticArray) {
299             uprv_free(mem->array);
300         }
301         uprv_free(mem);
302     }
303 }
304 
305 
306 U_CAPI void * U_EXPORT2
utm_getStart(UToolMemory * mem)307 utm_getStart(UToolMemory *mem) {
308     return (char *)mem->array;
309 }
310 
311 U_CAPI int32_t U_EXPORT2
utm_countItems(UToolMemory * mem)312 utm_countItems(UToolMemory *mem) {
313     return mem->idx;
314 }
315 
316 
317 static UBool
utm_hasCapacity(UToolMemory * mem,int32_t capacity)318 utm_hasCapacity(UToolMemory *mem, int32_t capacity) {
319     if(mem->capacity<capacity) {
320         int32_t newCapacity;
321 
322         if(mem->maxCapacity<capacity) {
323             fprintf(stderr, "error: %s - trying to use more than maxCapacity=%ld units\n",
324                     mem->name, (long)mem->maxCapacity);
325             exit(U_MEMORY_ALLOCATION_ERROR);
326         }
327 
328         /* try to allocate a larger array */
329         if(capacity>=2*mem->capacity) {
330             newCapacity=capacity;
331         } else if(mem->capacity<=mem->maxCapacity/3) {
332             newCapacity=2*mem->capacity;
333         } else {
334             newCapacity=mem->maxCapacity;
335         }
336 
337         if(mem->array==mem->staticArray) {
338             mem->array=uprv_malloc(newCapacity*mem->size);
339             if(mem->array!=NULL) {
340                 uprv_memcpy(mem->array, mem->staticArray, (size_t)mem->idx*mem->size);
341             }
342         } else {
343             mem->array=uprv_realloc(mem->array, newCapacity*mem->size);
344         }
345 
346         if(mem->array==NULL) {
347             fprintf(stderr, "error: %s - out of memory\n", mem->name);
348             exit(U_MEMORY_ALLOCATION_ERROR);
349         }
350         mem->capacity=newCapacity;
351     }
352 
353     return TRUE;
354 }
355 
356 U_CAPI void * U_EXPORT2
utm_alloc(UToolMemory * mem)357 utm_alloc(UToolMemory *mem) {
358     char *p=NULL;
359     int32_t oldIndex=mem->idx;
360     int32_t newIndex=oldIndex+1;
361     if(utm_hasCapacity(mem, newIndex)) {
362         p=(char *)mem->array+oldIndex*mem->size;
363         mem->idx=newIndex;
364         uprv_memset(p, 0, mem->size);
365     }
366     return p;
367 }
368 
369 U_CAPI void * U_EXPORT2
utm_allocN(UToolMemory * mem,int32_t n)370 utm_allocN(UToolMemory *mem, int32_t n) {
371     char *p=NULL;
372     int32_t oldIndex=mem->idx;
373     int32_t newIndex=oldIndex+n;
374     if(utm_hasCapacity(mem, newIndex)) {
375         p=(char *)mem->array+oldIndex*mem->size;
376         mem->idx=newIndex;
377         uprv_memset(p, 0, n*mem->size);
378     }
379     return p;
380 }
381