ncrypt/tests: Test for symmetric keys support.
[wine.git] / programs / winepath / winepath.c
blob17cfdc1d2eb5c4ecdb86aaa7aa52fb430ca74f22
1 /*
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
24 #include <windows.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <io.h>
30 #include "wine/debug.h"
32 enum {
33 SHORTFORMAT = 1,
34 LONGFORMAT = 2,
35 UNIXFORMAT = 4,
36 WINDOWSFORMAT = 8,
37 PRINT0 = 16,
40 static const char progname[] = "winepath";
43 * handle an option
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"
49 "\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"
58 "\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";
63 switch (shortopt) {
64 case 'h':
65 printf("Usage: %s [OPTION] [PATH]...\n", progname);
66 printf(helpmsg);
67 exit(0);
68 case 'l':
69 return LONGFORMAT;
70 case 's':
71 return SHORTFORMAT;
72 case 'u':
73 return UNIXFORMAT;
74 case 'w':
75 return WINDOWSFORMAT;
76 case '0':
77 return PRINT0;
80 fprintf(stderr, "%s: invalid option ", progname);
81 if (longopt)
82 fprintf(stderr, "%s\n", wine_dbgstr_w(longopt));
83 else
84 fprintf(stderr, "'-%c'\n", shortopt);
85 fprintf(stderr, "Try '%s --help' for help\n", progname);
86 exit(2);
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;
96 BOOL done = FALSE;
97 int i, j;
99 for (i = 1; argv[i] && !done; )
101 if (argv[i][0] != '-') {
102 /* not an option */
103 i++;
104 continue;
107 if (argv[i][1] == '-') {
108 if (argv[i][2] == 0) {
109 /* '--' end of options */
110 done = TRUE;
111 } else {
112 /* long option */
113 for (j = 0; longopts[j]; j++)
114 if (!lstrcmpiW(argv[i]+2, longopts[j]))
115 break;
116 if (longopts[j]) outputformats |= option(longopts[j][0], argv[i]);
118 } else {
119 /* short options */
120 for (j = 1; argv[i][j]; j++)
121 outputformats |= option(argv[i][j], NULL);
124 /* remove option */
125 for (j = i + 1; argv[j - 1]; j++)
126 argv[j - 1] = argv[j];
129 return outputformats;
133 * Main function
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];
140 char path[MAX_PATH];
141 int outputformats;
142 int i;
143 int separator;
145 setmode( fileno(stdout), O_BINARY ); /* avoid crlf */
147 outputformats = parse_options(argv);
149 if (outputformats & PRINT0)
151 separator = '\0';
152 outputformats ^= PRINT0;
154 else
155 separator = '\n';
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);
167 exit(3);
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);
178 exit(3);
182 for (i = 1; argv[i]; i++)
184 *path='\0';
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=HeapAlloc(GetProcessHeap(), 0, sizeof(*ntpath)*(ntpathlen+1));
199 lstrcpyW(ntpath, argv[i]);
200 tail=NULL;
201 while (1)
203 char *unix_name;
204 WCHAR *slash, *c;
206 unix_name = wine_get_unix_file_name_ptr(ntpath);
207 if (unix_name)
209 if (tail)
211 WideCharToMultiByte(CP_UNIXCP, 0, tail+1, -1, path, MAX_PATH, NULL, NULL);
212 printf("%s/%s%c", unix_name, path, separator);
214 else
216 printf("%s%c", unix_name, separator);
218 HeapFree( GetProcessHeap(), 0, unix_name );
219 break;
222 slash=(tail ? tail : ntpath+ntpathlen);
223 while (slash != ntpath && *slash != '/' && *slash != '\\')
224 slash--;
225 if (slash == ntpath)
227 /* This is a complete path conversion failure.
228 * It would typically happen if ntpath == "".
230 printf("%c", separator);
231 break;
233 c=slash+1;
234 while (*c != '\0' && *c != '*' && *c != '?' &&
235 *c != '<' && *c != '>' && *c != '|' && *c != '"')
236 c++;
237 if (*c != '\0')
239 /* If this is not a valid NT path to start with,
240 * then obviously we cannot convert it.
242 printf("%c", separator);
243 break;
245 if (tail)
246 *tail='/';
247 tail=slash;
248 *tail='\0';
250 HeapFree(GetProcessHeap(), 0, ntpath);
252 if (outputformats & WINDOWSFORMAT) {
253 WCHAR* windows_name;
254 char* unix_name;
255 DWORD size;
257 size=WideCharToMultiByte(CP_UNIXCP, 0, argv[i], -1, NULL, 0, NULL, NULL);
258 unix_name=HeapAlloc(GetProcessHeap(), 0, 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 HeapFree( GetProcessHeap(), 0, windows_name );
267 else printf("%c", separator);
268 HeapFree( GetProcessHeap(), 0, unix_name );
272 exit(0);