2 * Translate between Windows and Unix paths formats
4 * Copyright 2002 Mike Wetherell
5 * Copyright 2005 Dmitry Timoshkov
6 * Copyright 2005 Francois Gouget
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define WIN32_LEAN_AND_MEAN
30 #include "wine/debug.h"
40 static const char progname
[] = "winepath";
45 static int option(int shortopt
, const WCHAR
*longopt
)
47 static const char helpmsg
[] =
48 "Convert PATH(s) to Unix or Windows long or short paths.\n"
50 " -u, --unix converts a Windows path to a Unix path\n"
51 " -w, --windows converts a Unix path to a long Windows path\n"
52 " -l, --long converts the short Windows path of an existing file or\n"
53 " directory to the long format\n"
54 " -s, --short converts the long Windows path of an existing file or\n"
55 " directory to the short format\n"
56 " -0 separate output with \\0 character, instead of a newline\n"
57 " -h, --help output this help message and exit\n"
59 "If more than one option is given then the input paths are output in\n"
60 "all formats specified, in the order long, short, Unix, Windows.\n"
61 "If no option is given the default is Unix format.\n";
65 printf("Usage: %s [OPTION] [PATH]...\n", progname
);
80 fprintf(stderr
, "%s: invalid option ", progname
);
82 fprintf(stderr
, "%s\n", wine_dbgstr_w(longopt
));
84 fprintf(stderr
, "'-%c'\n", shortopt
);
85 fprintf(stderr
, "Try '%s --help' for help\n", progname
);
90 * Parse command line options
92 static int parse_options(WCHAR
*argv
[])
94 static const WCHAR
*longopts
[] = { L
"long", L
"short", L
"unix", L
"windows", L
"help", NULL
};
95 int outputformats
= 0;
99 for (i
= 1; argv
[i
] && !done
; )
101 if (argv
[i
][0] != '-') {
107 if (argv
[i
][1] == '-') {
108 if (argv
[i
][2] == 0) {
109 /* '--' end of options */
113 for (j
= 0; longopts
[j
]; j
++)
114 if (!lstrcmpiW(argv
[i
]+2, longopts
[j
]))
116 if (longopts
[j
]) outputformats
|= option(longopts
[j
][0], argv
[i
]);
120 for (j
= 1; argv
[i
][j
]; j
++)
121 outputformats
|= option(argv
[i
][j
], NULL
);
125 for (j
= i
+ 1; argv
[j
- 1]; j
++)
126 argv
[j
- 1] = argv
[j
];
129 return outputformats
;
135 int __cdecl
wmain(int argc
, WCHAR
*argv
[])
137 LPSTR (*CDECL wine_get_unix_file_name_ptr
)(LPCWSTR
) = NULL
;
138 LPWSTR (*CDECL wine_get_dos_file_name_ptr
)(LPCSTR
) = NULL
;
139 WCHAR dos_pathW
[MAX_PATH
];
145 setmode( fileno(stdout
), O_BINARY
); /* avoid crlf */
147 outputformats
= parse_options(argv
);
149 if (outputformats
& PRINT0
)
152 outputformats
^= PRINT0
;
157 if (outputformats
== 0)
158 outputformats
= UNIXFORMAT
;
160 if (outputformats
& UNIXFORMAT
) {
161 wine_get_unix_file_name_ptr
= (void*)
162 GetProcAddress(GetModuleHandleA("KERNEL32"),
163 "wine_get_unix_file_name");
164 if (wine_get_unix_file_name_ptr
== NULL
) {
165 fprintf(stderr
, "%s: cannot get the address of "
166 "'wine_get_unix_file_name'\n", progname
);
171 if (outputformats
& WINDOWSFORMAT
) {
172 wine_get_dos_file_name_ptr
= (void*)
173 GetProcAddress(GetModuleHandleA("KERNEL32"),
174 "wine_get_dos_file_name");
175 if (wine_get_dos_file_name_ptr
== NULL
) {
176 fprintf(stderr
, "%s: cannot get the address of "
177 "'wine_get_dos_file_name'\n", progname
);
182 for (i
= 1; argv
[i
]; i
++)
185 if (outputformats
& LONGFORMAT
) {
186 if (GetLongPathNameW(argv
[i
], dos_pathW
, MAX_PATH
))
187 WideCharToMultiByte(CP_UNIXCP
, 0, dos_pathW
, -1, path
, MAX_PATH
, NULL
, NULL
);
188 printf("%s%c", path
, separator
);
190 if (outputformats
& SHORTFORMAT
) {
191 if (GetShortPathNameW(argv
[i
], dos_pathW
, MAX_PATH
))
192 WideCharToMultiByte(CP_UNIXCP
, 0, dos_pathW
, -1, path
, MAX_PATH
, NULL
, NULL
);
193 printf("%s%c", path
, separator
);
195 if (outputformats
& UNIXFORMAT
) {
196 WCHAR
*ntpath
, *tail
;
197 int ntpathlen
=lstrlenW(argv
[i
]);
198 ntpath
= malloc(sizeof(*ntpath
)*(ntpathlen
+1));
199 lstrcpyW(ntpath
, argv
[i
]);
206 unix_name
= wine_get_unix_file_name_ptr(ntpath
);
211 WideCharToMultiByte(CP_UNIXCP
, 0, tail
+1, -1, path
, MAX_PATH
, NULL
, NULL
);
212 printf("%s/%s%c", unix_name
, path
, separator
);
216 printf("%s%c", unix_name
, separator
);
222 slash
=(tail
? tail
: ntpath
+ntpathlen
);
223 while (slash
!= ntpath
&& *slash
!= '/' && *slash
!= '\\')
227 /* This is a complete path conversion failure.
228 * It would typically happen if ntpath == "".
230 printf("%c", separator
);
234 while (*c
!= '\0' && *c
!= '*' && *c
!= '?' &&
235 *c
!= '<' && *c
!= '>' && *c
!= '|' && *c
!= '"')
239 /* If this is not a valid NT path to start with,
240 * then obviously we cannot convert it.
242 printf("%c", separator
);
252 if (outputformats
& WINDOWSFORMAT
) {
257 size
=WideCharToMultiByte(CP_UNIXCP
, 0, argv
[i
], -1, NULL
, 0, NULL
, NULL
);
258 unix_name
= malloc(size
);
259 WideCharToMultiByte(CP_UNIXCP
, 0, argv
[i
], -1, unix_name
, size
, NULL
, NULL
);
261 if ((windows_name
= wine_get_dos_file_name_ptr(unix_name
)))
263 WideCharToMultiByte(CP_UNIXCP
, 0, windows_name
, -1, path
, MAX_PATH
, NULL
, NULL
);
264 printf("%s%c", path
, separator
);
265 free( windows_name
);
267 else printf("%c", separator
);