1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Translate between Windows and Unix paths formats
*
* Copyright 2002 Mike Wetherell
* Copyright 2005 Dmitry Timoshkov
* Copyright 2005 Francois Gouget
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#define WIN32_LEAN_AND_MEAN
#include "config.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "wine/debug.h"
enum {
SHORTFORMAT = 1,
LONGFORMAT = 2,
UNIXFORMAT = 4,
WINDOWSFORMAT = 8,
PRINT0 = 16,
};
static const char progname[] = "winepath";
/*
* handle an option
*/
static int option(int shortopt, const WCHAR *longopt)
{
static const char helpmsg[] =
"Convert PATH(s) to Unix or Windows long or short paths.\n"
"\n"
" -u, --unix converts a Windows path to a Unix path\n"
" -w, --windows converts a Unix path to a long Windows path\n"
" -l, --long converts the short Windows path of an existing file or\n"
" directory to the long format\n"
" -s, --short converts the long Windows path of an existing file or\n"
" directory to the short format\n"
" -0 separate output with \\0 character, instead of a newline\n"
" -h, --help output this help message and exit\n"
" -v, --version output version information and exit\n"
"\n"
"If more than one option is given then the input paths are output in\n"
"all formats specified, in the order long, short, Unix, Windows.\n"
"If no option is given the default is Unix format.\n";
switch (shortopt) {
case 'h':
printf("Usage: %s [OPTION] [PATH]...\n", progname);
printf(helpmsg);
exit(0);
case 'v':
printf("%s version " PACKAGE_VERSION "\n", progname);
exit(0);
case 'l':
return LONGFORMAT;
case 's':
return SHORTFORMAT;
case 'u':
return UNIXFORMAT;
case 'w':
return WINDOWSFORMAT;
case '0':
return PRINT0;
}
fprintf(stderr, "%s: invalid option ", progname);
if (longopt)
fprintf(stderr, "%s\n", wine_dbgstr_w(longopt));
else
fprintf(stderr, "'-%c'\n", shortopt);
fprintf(stderr, "Try '%s --help' for help\n", progname);
exit(2);
}
/*
* Parse command line options
*/
static int parse_options(WCHAR *argv[])
{
static const WCHAR longW[] = { 'l','o','n','g',0 };
static const WCHAR shortW[] = { 's','h','o','r','t',0 };
static const WCHAR unixW[] = { 'u','n','i','x',0 };
static const WCHAR windowsW[] = { 'w','i','n','d','o','w','s',0 };
static const WCHAR helpW[] = { 'h','e','l','p',0 };
static const WCHAR versionW[] = { 'v','e','r','s','i','o','n',0 };
static const WCHAR nullW[] = { 0 };
static const WCHAR *longopts[] = { longW, shortW, unixW, windowsW, helpW, versionW, nullW };
int outputformats = 0;
int done = 0;
int i, j;
for (i = 1; argv[i] && !done; )
{
if (argv[i][0] != '-') {
/* not an option */
i++;
continue;
}
if (argv[i][1] == '-') {
if (argv[i][2] == 0) {
/* '--' end of options */
done = 1;
} else {
/* long option */
for (j = 0; longopts[j][0]; j++)
if (!lstrcmpiW(argv[i]+2, longopts[j]))
break;
outputformats |= option(longopts[j][0], argv[i]);
}
} else {
/* short options */
for (j = 1; argv[i][j]; j++)
outputformats |= option(argv[i][j], NULL);
}
/* remove option */
for (j = i + 1; argv[j - 1]; j++)
argv[j - 1] = argv[j];
}
return outputformats;
}
/*
* Main function
*/
int wmain(int argc, WCHAR *argv[])
{
LPSTR (*CDECL wine_get_unix_file_name_ptr)(LPCWSTR) = NULL;
LPWSTR (*CDECL wine_get_dos_file_name_ptr)(LPCSTR) = NULL;
WCHAR dos_pathW[MAX_PATH];
char path[MAX_PATH];
int outputformats;
int i;
int separator;
outputformats = parse_options(argv);
if (outputformats & PRINT0)
{
separator = '\0';
outputformats ^= PRINT0;
}
else
separator = '\n';
if (outputformats == 0)
outputformats = UNIXFORMAT;
if (outputformats & UNIXFORMAT) {
wine_get_unix_file_name_ptr = (void*)
GetProcAddress(GetModuleHandleA("KERNEL32"),
"wine_get_unix_file_name");
if (wine_get_unix_file_name_ptr == NULL) {
fprintf(stderr, "%s: cannot get the address of "
"'wine_get_unix_file_name'\n", progname);
exit(3);
}
}
if (outputformats & WINDOWSFORMAT) {
wine_get_dos_file_name_ptr = (void*)
GetProcAddress(GetModuleHandleA("KERNEL32"),
"wine_get_dos_file_name");
if (wine_get_dos_file_name_ptr == NULL) {
fprintf(stderr, "%s: cannot get the address of "
"'wine_get_dos_file_name'\n", progname);
exit(3);
}
}
for (i = 1; argv[i]; i++)
{
*path='\0';
if (outputformats & LONGFORMAT) {
if (GetLongPathNameW(argv[i], dos_pathW, MAX_PATH))
WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
printf("%s%c", path, separator);
}
if (outputformats & SHORTFORMAT) {
if (GetShortPathNameW(argv[i], dos_pathW, MAX_PATH))
WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
printf("%s%c", path, separator);
}
if (outputformats & UNIXFORMAT) {
WCHAR *ntpath, *tail;
int ntpathlen=lstrlenW(argv[i]);
ntpath=HeapAlloc(GetProcessHeap(), 0, sizeof(*ntpath)*(ntpathlen+1));
lstrcpyW(ntpath, argv[i]);
tail=NULL;
while (1)
{
char *unix_name;
WCHAR *slash, *c;
unix_name = wine_get_unix_file_name_ptr(ntpath);
if (unix_name)
{
if (tail)
{
WideCharToMultiByte(CP_UNIXCP, 0, tail+1, -1, path, MAX_PATH, NULL, NULL);
printf("%s/%s%c", unix_name, path, separator);
}
else
{
printf("%s%c", unix_name, separator);
}
HeapFree( GetProcessHeap(), 0, unix_name );
break;
}
slash=(tail ? tail : ntpath+ntpathlen);
while (slash != ntpath && *slash != '/' && *slash != '\\')
slash--;
if (slash == ntpath)
{
/* This is a complete path conversion failure.
* It would typically happen if ntpath == "".
*/
printf("%c", separator);
break;
}
c=slash+1;
while (*c != '\0' && *c != '*' && *c != '?' &&
*c != '<' && *c != '>' && *c != '|' && *c != '"')
c++;
if (*c != '\0')
{
/* If this is not a valid NT path to start with,
* then obviously we cannot convert it.
*/
printf("%c", separator);
break;
}
if (tail)
*tail='/';
tail=slash;
*tail='\0';
}
HeapFree(GetProcessHeap(), 0, ntpath);
}
if (outputformats & WINDOWSFORMAT) {
WCHAR* windows_name;
char* unix_name;
DWORD size;
size=WideCharToMultiByte(CP_UNIXCP, 0, argv[i], -1, NULL, 0, NULL, NULL);
unix_name=HeapAlloc(GetProcessHeap(), 0, size);
WideCharToMultiByte(CP_UNIXCP, 0, argv[i], -1, unix_name, size, NULL, NULL);
if ((windows_name = wine_get_dos_file_name_ptr(unix_name)))
{
WideCharToMultiByte(CP_UNIXCP, 0, windows_name, -1, path, MAX_PATH, NULL, NULL);
printf("%s%c", path, separator);
HeapFree( GetProcessHeap(), 0, windows_name );
}
else printf("%c", separator);
HeapFree( GetProcessHeap(), 0, unix_name );
}
}
exit(0);
}