Mercurial > vim
comparison src/filepath.c @ 18479:cbea1392c393 v8.1.2234
patch 8.1.2234: get_short_pathname() fails depending on encoding
Commit: https://github.com/vim/vim/commit/3f39697b73f661d6900c7cf5430d967a129660d7
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Oct 30 04:10:06 2019 +0100
patch 8.1.2234: get_short_pathname() fails depending on encoding
Problem: get_short_pathname() fails depending on encoding.
Solution: Use the wide version of the library function. (closes https://github.com/vim/vim/issues/5129)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 30 Oct 2019 04:15:04 +0100 |
parents | 8d09b7f53c71 |
children | 9676dc5fd705 |
comparison
equal
deleted
inserted
replaced
18478:94223687df0e | 18479:cbea1392c393 |
---|---|
28 */ | 28 */ |
29 static int | 29 static int |
30 get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen) | 30 get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen) |
31 { | 31 { |
32 int l, len; | 32 int l, len; |
33 char_u *newbuf; | 33 WCHAR *newbuf; |
34 | 34 WCHAR *wfname; |
35 len = *fnamelen; | 35 |
36 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len); | 36 len = MAXPATHL; |
37 newbuf = malloc(len * sizeof(*newbuf)); | |
38 if (newbuf == NULL) | |
39 return FAIL; | |
40 | |
41 wfname = enc_to_utf16(*fnamep, NULL); | |
42 if (wfname == NULL) | |
43 { | |
44 vim_free(newbuf); | |
45 return FAIL; | |
46 } | |
47 | |
48 l = GetShortPathNameW(wfname, newbuf, len); | |
37 if (l > len - 1) | 49 if (l > len - 1) |
38 { | 50 { |
39 // If that doesn't work (not enough space), then save the string | 51 // If that doesn't work (not enough space), then save the string |
40 // and try again with a new buffer big enough. | 52 // and try again with a new buffer big enough. |
41 newbuf = vim_strnsave(*fnamep, l); | 53 WCHAR *newbuf_t = newbuf; |
54 newbuf = vim_realloc(newbuf, (l + 1) * sizeof(*newbuf)); | |
42 if (newbuf == NULL) | 55 if (newbuf == NULL) |
56 { | |
57 vim_free(wfname); | |
58 vim_free(newbuf_t); | |
43 return FAIL; | 59 return FAIL; |
44 | 60 } |
45 vim_free(*bufp); | |
46 *fnamep = *bufp = newbuf; | |
47 | |
48 // Really should always succeed, as the buffer is big enough. | 61 // Really should always succeed, as the buffer is big enough. |
49 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1); | 62 l = GetShortPathNameW(wfname, newbuf, l+1); |
50 } | 63 } |
51 | 64 if (l != 0) |
52 *fnamelen = l; | 65 { |
66 char_u *p = utf16_to_enc(newbuf, NULL); | |
67 if (p != NULL) | |
68 { | |
69 vim_free(*bufp); | |
70 *fnamep = *bufp = p; | |
71 } | |
72 else | |
73 { | |
74 vim_free(wfname); | |
75 vim_free(newbuf); | |
76 return FAIL; | |
77 } | |
78 } | |
79 vim_free(wfname); | |
80 vim_free(newbuf); | |
81 | |
82 *fnamelen = l == 0 ? l : STRLEN(*bufp); | |
53 return OK; | 83 return OK; |
54 } | 84 } |
55 | 85 |
56 /* | 86 /* |
57 * Get the short path (8.3) for the filename in "fname". The converted | 87 * Get the short path (8.3) for the filename in "fname". The converted |