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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "wine/debug.h"
38 static const char progname[] = "winepath";
43 static int option(int shortopt, const WCHAR *longopt)
45 static const char helpmsg[] =
46 "Convert PATH(s) to Unix or Windows long or short paths.\n"
48 " -u, --unix converts a Windows path to a Unix path\n"
49 " -w, --windows converts a Unix path to a long Windows path\n"
50 " -l, --long converts a short Windows path to the long format\n"
51 " -s, --short converts a long Windows path to the short format\n"
52 " -h, --help output this help message and exit\n"
53 " -v, --version output version information and exit\n"
55 "If more than one option is given then the input paths are output in\n"
56 "all formats specified, in the order long, short, Unix, Windows.\n"
57 "If no option is given the default is Unix format.\n";
61 printf("Usage: %s [OPTION] [PATH]...\n", progname);
65 printf("%s version " PACKAGE_VERSION "\n", progname);
77 fprintf(stderr, "%s: invalid option ", progname);
79 fprintf(stderr, "%s\n", wine_dbgstr_w(longopt));
81 fprintf(stderr, "'-%c'\n", shortopt);
82 fprintf(stderr, "Try '%s --help' for help\n", progname);
87 * Parse command line options
89 static int parse_options(const WCHAR *argv[])
91 static const WCHAR longW[] = { 'l','o','n','g',0 };
92 static const WCHAR shortW[] = { 's','h','o','r','t',0 };
93 static const WCHAR unixW[] = { 'u','n','i','x',0 };
94 static const WCHAR windowsW[] = { 'w','i','n','d','o','w','s',0 };
95 static const WCHAR helpW[] = { 'h','e','l','p',0 };
96 static const WCHAR versionW[] = { 'v','e','r','s','i','o','n',0 };
97 static const WCHAR nullW[] = { 0 };
98 static const WCHAR *longopts[] = { longW, shortW, unixW, windowsW, helpW, versionW, nullW };
99 int outputformats = 0;
103 for (i = 1; argv[i] && !done; )
105 if (argv[i][0] != '-') {
111 if (argv[i][1] == '-') {
112 if (argv[i][2] == 0) {
113 /* '--' end of options */
117 for (j = 0; longopts[j][0]; j++)
118 if (!lstrcmpiW(argv[i]+2, longopts[j]))
120 outputformats |= option(longopts[j][0], argv[i]);
124 for (j = 1; argv[i][j]; j++)
125 outputformats |= option(argv[i][j], NULL);
129 for (j = i + 1; argv[j - 1]; j++)
130 argv[j - 1] = argv[j];
133 return outputformats;
139 int wmain(int argc, const WCHAR *argv[])
141 LPSTR (*wine_get_unix_file_name_ptr)(LPCWSTR) = NULL;
142 LPWSTR (*wine_get_dos_file_name_ptr)(LPCSTR) = NULL;
143 WCHAR dos_pathW[MAX_PATH];
148 outputformats = parse_options(argv);
149 if (outputformats == 0)
150 outputformats = UNIXFORMAT;
152 if (outputformats & UNIXFORMAT) {
153 wine_get_unix_file_name_ptr = (void*)
154 GetProcAddress(GetModuleHandle("KERNEL32"),
155 "wine_get_unix_file_name");
156 if (wine_get_unix_file_name_ptr == NULL) {
157 fprintf(stderr, "%s: cannot get the address of "
158 "'wine_get_unix_file_name'\n", progname);
163 if (outputformats & WINDOWSFORMAT) {
164 wine_get_dos_file_name_ptr = (void*)
165 GetProcAddress(GetModuleHandle("KERNEL32"),
166 "wine_get_dos_file_name");
167 if (wine_get_dos_file_name_ptr == NULL) {
168 fprintf(stderr, "%s: cannot get the address of "
169 "'wine_get_dos_file_name'\n", progname);
174 for (i = 1; argv[i]; i++)
177 if (outputformats & LONGFORMAT) {
178 if (GetFullPathNameW(argv[i], MAX_PATH, dos_pathW, NULL))
179 WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
180 printf("%s\n", path);
182 if (outputformats & SHORTFORMAT) {
183 if (GetShortPathNameW(argv[i], dos_pathW, MAX_PATH))
184 WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
185 printf("%s\n", path);
187 if (outputformats & UNIXFORMAT) {
190 if ((unix_name = wine_get_unix_file_name_ptr(argv[i])))
192 printf("%s\n", unix_name);
193 HeapFree( GetProcessHeap(), 0, unix_name );
197 if (outputformats & WINDOWSFORMAT) {
202 size=WideCharToMultiByte(CP_UNIXCP, 0, argv[i], -1, NULL, 0, NULL, NULL);
203 unix_name=HeapAlloc(GetProcessHeap(), 0, size);
204 WideCharToMultiByte(CP_UNIXCP, 0, argv[i], -1, unix_name, size, NULL, NULL);
206 if ((windows_name = wine_get_dos_file_name_ptr(unix_name)))
208 WideCharToMultiByte(CP_UNIXCP, 0, windows_name, -1, path, MAX_PATH, NULL, NULL);
209 printf("%s\n", path);
210 HeapFree( GetProcessHeap(), 0, windows_name );
213 HeapFree( GetProcessHeap(), 0, unix_name );