@@ -118,10 +118,7 @@ static inline int fcntl(int fd, int cmd, ...)
118
118
* simple adaptors
119
119
*/
120
120
121
- static inline int mingw_mkdir (const char * path , int mode )
122
- {
123
- return mkdir (path );
124
- }
121
+ int mingw_mkdir (const char * path , int mode );
125
122
#define mkdir mingw_mkdir
126
123
127
124
#define WNOHANG 1
@@ -192,11 +189,27 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);
192
189
int mingw_fflush (FILE * stream );
193
190
#define fflush mingw_fflush
194
191
192
+ int mingw_access (const char * filename , int mode );
193
+ #undef access
194
+ #define access mingw_access
195
+
196
+ int mingw_chdir (const char * dirname );
197
+ #define chdir mingw_chdir
198
+
199
+ int mingw_chmod (const char * filename , int mode );
200
+ #define chmod mingw_chmod
201
+
202
+ char * mingw_mktemp (char * template );
203
+ #define mktemp mingw_mktemp
204
+
195
205
char * mingw_getcwd (char * pointer , int len );
196
206
#define getcwd mingw_getcwd
197
207
198
208
char * mingw_getenv (const char * name );
199
209
#define getenv mingw_getenv
210
+ int mingw_putenv (const char * namevalue );
211
+ #define putenv mingw_putenv
212
+ #define unsetenv mingw_putenv
200
213
201
214
int mingw_gethostname (char * host , int namelen );
202
215
#define gethostname mingw_gethostname
@@ -317,12 +330,8 @@ int mingw_raise(int sig);
317
330
* ANSI emulation wrappers
318
331
*/
319
332
320
- int winansi_fputs (const char * str , FILE * stream );
321
- int winansi_printf (const char * format , ...) __attribute__((format (printf , 1 , 2 )));
322
- int winansi_fprintf (FILE * stream , const char * format , ...) __attribute__((format (printf , 2 , 3 )));
323
- #define fputs winansi_fputs
324
- #define printf (...) winansi_printf(__VA_ARGS__)
325
- #define fprintf (...) winansi_fprintf(__VA_ARGS__)
333
+ void winansi_init (void );
334
+ HANDLE winansi_get_osfhandle (int fd );
326
335
327
336
/*
328
337
* git specific compatibility
@@ -346,12 +355,112 @@ static inline char *mingw_find_last_dir_sep(const char *path)
346
355
void mingw_open_html (const char * path );
347
356
#define open_html mingw_open_html
348
357
349
- /*
350
- * helpers
358
+ void mingw_mark_as_git_dir (const char * dir );
359
+ #define mark_as_git_dir mingw_mark_as_git_dir
360
+
361
+ /**
362
+ * Converts UTF-8 encoded string to UTF-16LE.
363
+ *
364
+ * To support repositories with legacy-encoded file names, invalid UTF-8 bytes
365
+ * 0xa0 - 0xff are converted to corresponding printable Unicode chars \u00a0 -
366
+ * \u00ff, and invalid UTF-8 bytes 0x80 - 0x9f (which would make non-printable
367
+ * Unicode) are converted to hex-code.
368
+ *
369
+ * Lead-bytes not followed by an appropriate number of trail-bytes, over-long
370
+ * encodings and 4-byte encodings > \u10ffff are detected as invalid UTF-8.
371
+ *
372
+ * Maximum space requirement for the target buffer is two wide chars per UTF-8
373
+ * char (((strlen(utf) * 2) + 1) [* sizeof(wchar_t)]).
374
+ *
375
+ * The maximum space is needed only if the entire input string consists of
376
+ * invalid UTF-8 bytes in range 0x80-0x9f, as per the following table:
377
+ *
378
+ * | | UTF-8 | UTF-16 |
379
+ * Code point | UTF-8 sequence | bytes | words | ratio
380
+ * --------------+-------------------+-------+--------+-------
381
+ * 000000-00007f | 0-7f | 1 | 1 | 1
382
+ * 000080-0007ff | c2-df + 80-bf | 2 | 1 | 0.5
383
+ * 000800-00ffff | e0-ef + 2 * 80-bf | 3 | 1 | 0.33
384
+ * 010000-10ffff | f0-f4 + 3 * 80-bf | 4 | 2 (a) | 0.5
385
+ * invalid | 80-9f | 1 | 2 (b) | 2
386
+ * invalid | a0-ff | 1 | 1 | 1
387
+ *
388
+ * (a) encoded as UTF-16 surrogate pair
389
+ * (b) encoded as two hex digits
390
+ *
391
+ * Note that, while the UTF-8 encoding scheme can be extended to 5-byte, 6-byte
392
+ * or even indefinite-byte sequences, the largest valid code point \u10ffff
393
+ * encodes as only 4 UTF-8 bytes.
394
+ *
395
+ * Parameters:
396
+ * wcs: wide char target buffer
397
+ * utf: string to convert
398
+ * wcslen: size of target buffer (in wchar_t's)
399
+ * utflen: size of string to convert, or -1 if 0-terminated
400
+ *
401
+ * Returns:
402
+ * length of converted string (_wcslen(wcs)), or -1 on failure
403
+ *
404
+ * Errors:
405
+ * EINVAL: one of the input parameters is invalid (e.g. NULL)
406
+ * ERANGE: the output buffer is too small
407
+ */
408
+ int xutftowcsn (wchar_t * wcs , const char * utf , size_t wcslen , int utflen );
409
+
410
+ /**
411
+ * Simplified variant of xutftowcsn, assumes input string is \0-terminated.
412
+ */
413
+ static inline int xutftowcs (wchar_t * wcs , const char * utf , size_t wcslen )
414
+ {
415
+ return xutftowcsn (wcs , utf , wcslen , -1 );
416
+ }
417
+
418
+ /**
419
+ * Simplified file system specific variant of xutftowcsn, assumes output
420
+ * buffer size is MAX_PATH wide chars and input string is \0-terminated,
421
+ * fails with ENAMETOOLONG if input string is too long.
351
422
*/
423
+ static inline int xutftowcs_path (wchar_t * wcs , const char * utf )
424
+ {
425
+ int result = xutftowcsn (wcs , utf , MAX_PATH , -1 );
426
+ if (result < 0 && errno == ERANGE )
427
+ errno = ENAMETOOLONG ;
428
+ return result ;
429
+ }
352
430
353
- char * * make_augmented_environ (const char * const * vars );
354
- void free_environ (char * * env );
431
+ /**
432
+ * Converts UTF-16LE encoded string to UTF-8.
433
+ *
434
+ * Maximum space requirement for the target buffer is three UTF-8 chars per
435
+ * wide char ((_wcslen(wcs) * 3) + 1).
436
+ *
437
+ * The maximum space is needed only if the entire input string consists of
438
+ * UTF-16 words in range 0x0800-0xd7ff or 0xe000-0xffff (i.e. \u0800-\uffff
439
+ * modulo surrogate pairs), as per the following table:
440
+ *
441
+ * | | UTF-16 | UTF-8 |
442
+ * Code point | UTF-16 sequence | words | bytes | ratio
443
+ * --------------+-----------------------+--------+-------+-------
444
+ * 000000-00007f | 0000-007f | 1 | 1 | 1
445
+ * 000080-0007ff | 0080-07ff | 1 | 2 | 2
446
+ * 000800-00ffff | 0800-d7ff / e000-ffff | 1 | 3 | 3
447
+ * 010000-10ffff | d800-dbff + dc00-dfff | 2 | 4 | 2
448
+ *
449
+ * Note that invalid code points > 10ffff cannot be represented in UTF-16.
450
+ *
451
+ * Parameters:
452
+ * utf: target buffer
453
+ * wcs: wide string to convert
454
+ * utflen: size of target buffer
455
+ *
456
+ * Returns:
457
+ * length of converted string, or -1 on failure
458
+ *
459
+ * Errors:
460
+ * EINVAL: one of the input parameters is invalid (e.g. NULL)
461
+ * ERANGE: the output buffer is too small
462
+ */
463
+ int xwcstoutf (char * utf , const wchar_t * wcs , size_t utflen );
355
464
356
465
/*
357
466
* A critical section used in the implementation of the spawn
@@ -361,22 +470,16 @@ void free_environ(char **env);
361
470
extern CRITICAL_SECTION pinfo_cs ;
362
471
363
472
/*
364
- * A replacement of main() that ensures that argv[0] has a path
365
- * and that default fmode and std(in|out|err) are in binary mode
473
+ * A replacement of main() that adds win32 specific initialization.
366
474
*/
367
475
476
+ void mingw_startup ();
368
477
#define main (c ,v ) dummy_decl_mingw_main(); \
369
478
static int mingw_main(c,v); \
370
- int main(int argc, char **argv ) \
479
+ int main(c,v ) \
371
480
{ \
372
- extern CRITICAL_SECTION pinfo_cs; \
373
- _fmode = _O_BINARY; \
374
- _setmode(_fileno(stdin), _O_BINARY); \
375
- _setmode(_fileno(stdout), _O_BINARY); \
376
- _setmode(_fileno(stderr), _O_BINARY); \
377
- argv[0] = xstrdup(_pgmptr); \
378
- InitializeCriticalSection(&pinfo_cs); \
379
- return mingw_main(argc, argv); \
481
+ mingw_startup(); \
482
+ return mingw_main(__argc, __argv); \
380
483
} \
381
484
static int mingw_main(c,v)
382
485
0 commit comments