Turn a GDI object into a system object via an explicit Wine extension
[wine/wine64.git] / programs / winepath / winepath.c
bloba5df4055567c7920c4826e414ec61bff25532181
1 /*
2 * Translate between Wine and Unix paths
4 * Copyright 2002 Mike Wetherell
5 * Copyright 2005 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <windows.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "wine/debug.h"
30 #define VERSION "0.1 (" PACKAGE_STRING ")"
32 enum {
33 SHORTFORMAT = 1,
34 LONGFORMAT = 2,
35 UNIXFORMAT = 4
38 static const char progname[] = "winepath";
40 /* Wine specific functions */
41 typedef LPSTR (*wine_get_unix_file_name_t) ( LPCWSTR dos );
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 output Unix format\n"
51 " -l, --long output Windows long format\n"
52 " -s, --short output Windows short format \n"
53 " -h, --help output this help message and exit\n"
54 " -v, --version output version information and exit\n"
55 "\n"
56 "The input paths can be in any format. If more than one option is given\n"
57 "then the input paths are output in all formats specified, in the order\n"
58 "Unix, long, short. If no option is given the default is Unix format.\n";
60 switch (shortopt) {
61 case 'h':
62 printf("Usage: %s [OPTION] [PATH]...\n", progname);
63 printf(helpmsg);
64 exit(0);
65 case 'v':
66 printf("%s version " VERSION "\n", progname);
67 exit(0);
68 case 'l':
69 return LONGFORMAT;
70 case 's':
71 return SHORTFORMAT;
72 case 'u':
73 return UNIXFORMAT;
76 fprintf(stderr, "%s: invalid option ", progname);
77 if (longopt)
78 fprintf(stderr, "%s\n", wine_dbgstr_w(longopt));
79 else
80 fprintf(stderr, "'-%c'\n", shortopt);
81 fprintf(stderr, "Try '%s --help' for help\n", progname);
82 exit(2);
86 * Parse command line options
88 static int parse_options(const WCHAR *argv[])
90 static const WCHAR longW[] = { 'l','o','n','g',0 };
91 static const WCHAR shortW[] = { 's','h','o','r','t',0 };
92 static const WCHAR unixW[] = { 'u','n','i','x',0 };
93 static const WCHAR helpW[] = { 'h','e','l','p',0 };
94 static const WCHAR versionW[] = { 'v','e','r','s','i','o','n',0 };
95 static const WCHAR nullW[] = { 0 };
96 static const WCHAR *longopts[] = { longW, shortW, unixW, helpW, versionW, nullW };
97 int outputformats = 0;
98 int done = 0;
99 int i, j;
101 for (i = 1; argv[i] && !done; )
103 if (argv[i][0] != '-') {
104 /* not an option */
105 i++;
106 continue;
109 if (argv[i][1] == '-') {
110 if (argv[i][2] == 0) {
111 /* '--' end of options */
112 done = 1;
113 } else {
114 /* long option */
115 for (j = 0; longopts[j][0]; j++)
116 if (!lstrcmpiW(argv[i]+2, longopts[j]))
117 break;
118 outputformats |= option(longopts[j][0], argv[i]);
120 } else {
121 /* short options */
122 for (j = 1; argv[i][j]; j++)
123 outputformats |= option(argv[i][j], NULL);
126 /* remove option */
127 for (j = i + 1; argv[j - 1]; j++)
128 argv[j - 1] = argv[j];
131 return outputformats;
135 * Main function
137 int wmain(int argc, const WCHAR *argv[])
139 wine_get_unix_file_name_t wine_get_unix_file_name_ptr = NULL;
140 WCHAR dos_pathW[MAX_PATH];
141 char path[MAX_PATH];
142 int outputformats;
143 int i;
145 outputformats = parse_options(argv);
146 if (outputformats == 0)
147 outputformats = UNIXFORMAT;
149 if (outputformats & UNIXFORMAT) {
150 wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
151 GetProcAddress(GetModuleHandle("KERNEL32"),
152 "wine_get_unix_file_name");
153 if (wine_get_unix_file_name_ptr == NULL) {
154 fprintf(stderr, "%s: cannot get the address of "
155 "'wine_get_unix_file_name'\n", progname);
156 exit(3);
160 for (i = 1; argv[i]; i++)
162 *path='\0';
163 if (outputformats & LONGFORMAT) {
164 if (GetFullPathNameW(argv[i], MAX_PATH, dos_pathW, NULL))
165 WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
166 printf("%s\n", path);
168 if (outputformats & SHORTFORMAT) {
169 if (GetShortPathNameW(argv[i], dos_pathW, MAX_PATH))
170 WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
171 printf("%s\n", path);
173 if (outputformats & UNIXFORMAT) {
174 char *unix_name;
176 if ((unix_name = wine_get_unix_file_name_ptr(argv[i])))
178 printf("%s\n", unix_name);
179 HeapFree( GetProcessHeap(), 0, unix_name );
181 else printf( "\n" );
185 exit(0);