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 "unicode/utypes.h"
34 
35 #ifndef U_TOOLUTIL_IMPLEMENTATION
36 #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
37 #endif
38 
39 #if U_PLATFORM_USES_ONLY_WIN32_API
40 #   define VC_EXTRALEAN
41 #   define WIN32_LEAN_AND_MEAN
42 #   define NOUSER
43 #   define NOSERVICE
44 #   define NOIME
45 #   define NOMCX
46 #   if U_PLATFORM == U_PF_MINGW
47 #     define __NO_MINGW_LFS /* gets around missing 'off64_t' */
48 #   endif
49 #   include <windows.h>
50 #   include <direct.h>
51 #else
52 #   include <sys/stat.h>
53 #   include <sys/types.h>
54 #endif
55 
56 /* In MinGW environment, io.h needs to be included for _mkdir() */
57 #if U_PLATFORM == U_PF_MINGW
58 #include <io.h>
59 #endif
60 
61 #include <errno.h>
62 
63 #include <cstddef>
64 
65 #include "unicode/errorcode.h"
66 #include "unicode/putil.h"
67 #include "cmemory.h"
68 #include "cstring.h"
69 #include "toolutil.h"
70 #include "unicode/ucal.h"
71 
72 U_NAMESPACE_BEGIN
73 
~IcuToolErrorCode()74 IcuToolErrorCode::~IcuToolErrorCode() {
75     // Safe because our handleFailure() does not throw exceptions.
76     if(isFailure()) { handleFailure(); }
77 }
78 
handleFailure() const79 void IcuToolErrorCode::handleFailure() const {
80     fprintf(stderr, "error at %s: %s\n", location, errorName());
81     exit(errorCode);
82 }
83 
84 U_NAMESPACE_END
85 
86 static int32_t currentYear = -1;
87 
getCurrentYear()88 U_CAPI int32_t U_EXPORT2 getCurrentYear() {
89 #if !UCONFIG_NO_FORMATTING
90     UErrorCode status=U_ZERO_ERROR;
91     UCalendar *cal = NULL;
92 
93     if(currentYear == -1) {
94         cal = ucal_open(NULL, -1, NULL, UCAL_TRADITIONAL, &status);
95         ucal_setMillis(cal, ucal_getNow(), &status);
96         currentYear = ucal_get(cal, UCAL_YEAR, &status);
97         ucal_close(cal);
98     }
99 #else
100     /* No formatting- no way to set the current year. */
101 #endif
102     return currentYear;
103 }
104 
105 
106 U_CAPI const char * U_EXPORT2
getLongPathname(const char * pathname)107 getLongPathname(const char *pathname) {
108 #if U_PLATFORM_USES_ONLY_WIN32_API
109     /* anticipate problems with "short" pathnames */
110     static WIN32_FIND_DATAA info;
111     HANDLE file=FindFirstFileA(pathname, &info);
112     if(file!=INVALID_HANDLE_VALUE) {
113         if(info.cAlternateFileName[0]!=0) {
114             /* this file has a short name, get and use the long one */
115             const char *basename=findBasename(pathname);
116             if(basename!=pathname) {
117                 /* prepend the long filename with the original path */
118                 uprv_memmove(info.cFileName+(basename-pathname), info.cFileName, uprv_strlen(info.cFileName)+1);
119                 uprv_memcpy(info.cFileName, pathname, basename-pathname);
120             }
121             pathname=info.cFileName;
122         }
123         FindClose(file);
124     }
125 #endif
126     return pathname;
127 }
128 
129 U_CAPI const char * U_EXPORT2
findDirname(const char * path,char * buffer,int32_t bufLen,UErrorCode * status)130 findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status) {
131   if(U_FAILURE(*status)) return NULL;
132   const char *resultPtr = NULL;
133   int32_t resultLen = 0;
134 
135   const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR);
136 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
137   const char *basenameAlt=uprv_strrchr(path, U_FILE_ALT_SEP_CHAR);
138   if(basenameAlt && (!basename || basename<basenameAlt)) {
139     basename = basenameAlt;
140   }
141 #endif
142   if(!basename) {
143     /* no basename - return ''. */
144     resultPtr = "";
145     resultLen = 0;
146   } else {
147     resultPtr = path;
148     resultLen = static_cast<int32_t>(basename - path);
149     if(resultLen<1) {
150       resultLen = 1; /* '/' or '/a' -> '/' */
151     }
152   }
153 
154   if((resultLen+1) <= bufLen) {
155     uprv_strncpy(buffer, resultPtr, resultLen);
156     buffer[resultLen]=0;
157     return buffer;
158   } else {
159     *status = U_BUFFER_OVERFLOW_ERROR;
160     return NULL;
161   }
162 }
163 
164 U_CAPI const char * U_EXPORT2
findBasename(const char * filename)165 findBasename(const char *filename) {
166     const char *basename=uprv_strrchr(filename, U_FILE_SEP_CHAR);
167 
168 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
169     //be lenient about pathname separators on Windows, like official implementation of C++17 std::filesystem in MSVC
170     //would be convenient to merge this loop with the one above, but alas, there is no such solution in the standard library
171     const char *alt_basename=uprv_strrchr(filename, U_FILE_ALT_SEP_CHAR);
172     if(alt_basename>basename) {
173         basename=alt_basename;
174     }
175 #endif
176 
177     if(basename!=NULL) {
178         return basename+1;
179     } else {
180         return filename;
181     }
182 }
183 
184 U_CAPI void U_EXPORT2
uprv_mkdir(const char * pathname,UErrorCode * status)185 uprv_mkdir(const char *pathname, UErrorCode *status) {
186 
187     int retVal = 0;
188 #if U_PLATFORM_USES_ONLY_WIN32_API
189     retVal = _mkdir(pathname);
190 #else
191     retVal = mkdir(pathname, S_IRWXU | (S_IROTH | S_IXOTH) | (S_IROTH | S_IXOTH));
192 #endif
193     if (retVal && errno != EEXIST) {
194 #if U_PF_MINGW <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
195         /*if using Cygwin and the mkdir says it failed...check if the directory already exists..*/
196         /* if it does...don't give the error, if it does not...give the error - Brian Rower - 6/25/08 */
197         struct stat st;
198 
199         if(stat(pathname,&st) != 0)
200         {
201             *status = U_FILE_ACCESS_ERROR;
202         }
203 #else
204         *status = U_FILE_ACCESS_ERROR;
205 #endif
206     }
207 }
208 
209 #if !UCONFIG_NO_FILE_IO
210 U_CAPI UBool U_EXPORT2
uprv_fileExists(const char * file)211 uprv_fileExists(const char *file) {
212   struct stat stat_buf;
213   if (stat(file, &stat_buf) == 0) {
214     return TRUE;
215   } else {
216     return FALSE;
217   }
218 }
219 #endif
220 
221 /*U_CAPI UDate U_EXPORT2
222 uprv_getModificationDate(const char *pathname, UErrorCode *status)
223 {
224     if(U_FAILURE(*status)) {
225         return;
226     }
227     //  TODO: handle case where stat is not available
228     struct stat st;
229 
230     if(stat(pathname,&st) != 0)
231     {
232         *status = U_FILE_ACCESS_ERROR;
233     } else {
234         return st.st_mtime;
235     }
236 }
237 */
238 
239 /* tool memory helper ------------------------------------------------------- */
240 
241 struct UToolMemory {
242     char name[64];
243     int32_t capacity, maxCapacity, size, idx;
244     void *array;
245     alignas(std::max_align_t) char staticArray[1];
246 };
247 
248 U_CAPI UToolMemory * U_EXPORT2
utm_open(const char * name,int32_t initialCapacity,int32_t maxCapacity,int32_t size)249 utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size) {
250     UToolMemory *mem;
251 
252     if(maxCapacity<initialCapacity) {
253         maxCapacity=initialCapacity;
254     }
255 
256     mem=(UToolMemory *)uprv_malloc(sizeof(UToolMemory)+initialCapacity*size);
257     if(mem==NULL) {
258         fprintf(stderr, "error: %s - out of memory\n", name);
259         exit(U_MEMORY_ALLOCATION_ERROR);
260     }
261     mem->array=mem->staticArray;
262 
263     uprv_strcpy(mem->name, name);
264     mem->capacity=initialCapacity;
265     mem->maxCapacity=maxCapacity;
266     mem->size=size;
267     mem->idx=0;
268     return mem;
269 }
270 
271 U_CAPI void U_EXPORT2
utm_close(UToolMemory * mem)272 utm_close(UToolMemory *mem) {
273     if(mem!=NULL) {
274         if(mem->array!=mem->staticArray) {
275             uprv_free(mem->array);
276         }
277         uprv_free(mem);
278     }
279 }
280 
281 
282 U_CAPI void * U_EXPORT2
utm_getStart(UToolMemory * mem)283 utm_getStart(UToolMemory *mem) {
284     return (char *)mem->array;
285 }
286 
287 U_CAPI int32_t U_EXPORT2
utm_countItems(UToolMemory * mem)288 utm_countItems(UToolMemory *mem) {
289     return mem->idx;
290 }
291 
292 
293 static UBool
utm_hasCapacity(UToolMemory * mem,int32_t capacity)294 utm_hasCapacity(UToolMemory *mem, int32_t capacity) {
295     if(mem->capacity<capacity) {
296         int32_t newCapacity;
297 
298         if(mem->maxCapacity<capacity) {
299             fprintf(stderr, "error: %s - trying to use more than maxCapacity=%ld units\n",
300                     mem->name, (long)mem->maxCapacity);
301             exit(U_MEMORY_ALLOCATION_ERROR);
302         }
303 
304         /* try to allocate a larger array */
305         if(capacity>=2*mem->capacity) {
306             newCapacity=capacity;
307         } else if(mem->capacity<=mem->maxCapacity/3) {
308             newCapacity=2*mem->capacity;
309         } else {
310             newCapacity=mem->maxCapacity;
311         }
312 
313         if(mem->array==mem->staticArray) {
314             mem->array=uprv_malloc(newCapacity*mem->size);
315             if(mem->array!=NULL) {
316                 uprv_memcpy(mem->array, mem->staticArray, (size_t)mem->idx*mem->size);
317             }
318         } else {
319             mem->array=uprv_realloc(mem->array, newCapacity*mem->size);
320         }
321 
322         if(mem->array==NULL) {
323             fprintf(stderr, "error: %s - out of memory\n", mem->name);
324             exit(U_MEMORY_ALLOCATION_ERROR);
325         }
326         mem->capacity=newCapacity;
327     }
328 
329     return TRUE;
330 }
331 
332 U_CAPI void * U_EXPORT2
utm_alloc(UToolMemory * mem)333 utm_alloc(UToolMemory *mem) {
334     char *p=NULL;
335     int32_t oldIndex=mem->idx;
336     int32_t newIndex=oldIndex+1;
337     if(utm_hasCapacity(mem, newIndex)) {
338         p=(char *)mem->array+oldIndex*mem->size;
339         mem->idx=newIndex;
340         uprv_memset(p, 0, mem->size);
341     }
342     return p;
343 }
344 
345 U_CAPI void * U_EXPORT2
utm_allocN(UToolMemory * mem,int32_t n)346 utm_allocN(UToolMemory *mem, int32_t n) {
347     char *p=NULL;
348     int32_t oldIndex=mem->idx;
349     int32_t newIndex=oldIndex+n;
350     if(utm_hasCapacity(mem, newIndex)) {
351         p=(char *)mem->array+oldIndex*mem->size;
352         mem->idx=newIndex;
353         uprv_memset(p, 0, n*mem->size);
354     }
355     return p;
356 }
357