@@ -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
@@ -348,12 +357,112 @@ int mingw_offset_1st_component(const char *path);
348
357
void mingw_open_html (const char * path );
349
358
#define open_html mingw_open_html
350
359
351
- /*
352
- * helpers
360
+ void mingw_mark_as_git_dir (const char * dir );
361
+ #define mark_as_git_dir mingw_mark_as_git_dir
362
+
363
+ /**
364
+ * Converts UTF-8 encoded string to UTF-16LE.
365
+ *
366
+ * To support repositories with legacy-encoded file names, invalid UTF-8 bytes
367
+ * 0xa0 - 0xff are converted to corresponding printable Unicode chars \u00a0 -
368
+ * \u00ff, and invalid UTF-8 bytes 0x80 - 0x9f (which would make non-printable
369
+ * Unicode) are converted to hex-code.
370
+ *
371
+ * Lead-bytes not followed by an appropriate number of trail-bytes, over-long
372
+ * encodings and 4-byte encodings > \u10ffff are detected as invalid UTF-8.
373
+ *
374
+ * Maximum space requirement for the target buffer is two wide chars per UTF-8
375
+ * char (((strlen(utf) * 2) + 1) [* sizeof(wchar_t)]).
376
+ *
377
+ * The maximum space is needed only if the entire input string consists of
378
+ * invalid UTF-8 bytes in range 0x80-0x9f, as per the following table:
379
+ *
380
+ * | | UTF-8 | UTF-16 |
381
+ * Code point | UTF-8 sequence | bytes | words | ratio
382
+ * --------------+-------------------+-------+--------+-------
383
+ * 000000-00007f | 0-7f | 1 | 1 | 1
384
+ * 000080-0007ff | c2-df + 80-bf | 2 | 1 | 0.5
385
+ * 000800-00ffff | e0-ef + 2 * 80-bf | 3 | 1 | 0.33
386
+ * 010000-10ffff | f0-f4 + 3 * 80-bf | 4 | 2 (a) | 0.5
387
+ * invalid | 80-9f | 1 | 2 (b) | 2
388
+ * invalid | a0-ff | 1 | 1 | 1
389
+ *
390
+ * (a) encoded as UTF-16 surrogate pair
391
+ * (b) encoded as two hex digits
392
+ *
393
+ * Note that, while the UTF-8 encoding scheme can be extended to 5-byte, 6-byte
394
+ * or even indefinite-byte sequences, the largest valid code point \u10ffff
395
+ * encodes as only 4 UTF-8 bytes.
396
+ *
397
+ * Parameters:
398
+ * wcs: wide char target buffer
399
+ * utf: string to convert
400
+ * wcslen: size of target buffer (in wchar_t's)
401
+ * utflen: size of string to convert, or -1 if 0-terminated
402
+ *
403
+ * Returns:
404
+ * length of converted string (_wcslen(wcs)), or -1 on failure
405
+ *
406
+ * Errors:
407
+ * EINVAL: one of the input parameters is invalid (e.g. NULL)
408
+ * ERANGE: the output buffer is too small
409
+ */
410
+ int xutftowcsn (wchar_t * wcs , const char * utf , size_t wcslen , int utflen );
411
+
412
+ /**
413
+ * Simplified variant of xutftowcsn, assumes input string is \0-terminated.
414
+ */
415
+ static inline int xutftowcs (wchar_t * wcs , const char * utf , size_t wcslen )
416
+ {
417
+ return xutftowcsn (wcs , utf , wcslen , -1 );
418
+ }
419
+
420
+ /**
421
+ * Simplified file system specific variant of xutftowcsn, assumes output
422
+ * buffer size is MAX_PATH wide chars and input string is \0-terminated,
423
+ * fails with ENAMETOOLONG if input string is too long.
353
424
*/
425
+ static inline int xutftowcs_path (wchar_t * wcs , const char * utf )
426
+ {
427
+ int result = xutftowcsn (wcs , utf , MAX_PATH , -1 );
428
+ if (result < 0 && errno == ERANGE )
429
+ errno = ENAMETOOLONG ;
430
+ return result ;
431
+ }
354
432
355
- char * * make_augmented_environ (const char * const * vars );
356
- void free_environ (char * * env );
433
+ /**
434
+ * Converts UTF-16LE encoded string to UTF-8.
435
+ *
436
+ * Maximum space requirement for the target buffer is three UTF-8 chars per
437
+ * wide char ((_wcslen(wcs) * 3) + 1).
438
+ *
439
+ * The maximum space is needed only if the entire input string consists of
440
+ * UTF-16 words in range 0x0800-0xd7ff or 0xe000-0xffff (i.e. \u0800-\uffff
441
+ * modulo surrogate pairs), as per the following table:
442
+ *
443
+ * | | UTF-16 | UTF-8 |
444
+ * Code point | UTF-16 sequence | words | bytes | ratio
445
+ * --------------+-----------------------+--------+-------+-------
446
+ * 000000-00007f | 0000-007f | 1 | 1 | 1
447
+ * 000080-0007ff | 0080-07ff | 1 | 2 | 2
448
+ * 000800-00ffff | 0800-d7ff / e000-ffff | 1 | 3 | 3
449
+ * 010000-10ffff | d800-dbff + dc00-dfff | 2 | 4 | 2
450
+ *
451
+ * Note that invalid code points > 10ffff cannot be represented in UTF-16.
452
+ *
453
+ * Parameters:
454
+ * utf: target buffer
455
+ * wcs: wide string to convert
456
+ * utflen: size of target buffer
457
+ *
458
+ * Returns:
459
+ * length of converted string, or -1 on failure
460
+ *
461
+ * Errors:
462
+ * EINVAL: one of the input parameters is invalid (e.g. NULL)
463
+ * ERANGE: the output buffer is too small
464
+ */
465
+ int xwcstoutf (char * utf , const wchar_t * wcs , size_t utflen );
357
466
358
467
/*
359
468
* A critical section used in the implementation of the spawn
@@ -363,22 +472,16 @@ void free_environ(char **env);
363
472
extern CRITICAL_SECTION pinfo_cs ;
364
473
365
474
/*
366
- * A replacement of main() that ensures that argv[0] has a path
367
- * and that default fmode and std(in|out|err) are in binary mode
475
+ * A replacement of main() that adds win32 specific initialization.
368
476
*/
369
477
478
+ void mingw_startup ();
370
479
#define main (c ,v ) dummy_decl_mingw_main(); \
371
480
static int mingw_main(c,v); \
372
- int main(int argc, char **argv ) \
481
+ int main(c,v ) \
373
482
{ \
374
- extern CRITICAL_SECTION pinfo_cs; \
375
- _fmode = _O_BINARY; \
376
- _setmode(_fileno(stdin), _O_BINARY); \
377
- _setmode(_fileno(stdout), _O_BINARY); \
378
- _setmode(_fileno(stderr), _O_BINARY); \
379
- argv[0] = xstrdup(_pgmptr); \
380
- InitializeCriticalSection(&pinfo_cs); \
381
- return mingw_main(argc, argv); \
483
+ mingw_startup(); \
484
+ return mingw_main(__argc, __argv); \
382
485
} \
383
486
static int mingw_main(c,v)
384
487
0 commit comments