programs: Use WIN32_LEAN_AND_MEAN in some more places.
[wine] / programs / winepath / winepath.c
1 /*
2  * Translate between Windows and Unix paths formats
3  *
4  * Copyright 2002 Mike Wetherell
5  * Copyright 2005 Dmitry Timoshkov
6  * Copyright 2005 Francois Gouget
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #define WIN32_LEAN_AND_MEAN
24
25 #include "config.h"
26
27 #include <windows.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "wine/debug.h"
32
33 enum {
34     SHORTFORMAT   = 1,
35     LONGFORMAT    = 2,
36     UNIXFORMAT    = 4,
37     WINDOWSFORMAT = 8
38 };
39
40 static const char progname[] = "winepath";
41
42 /*
43  * handle an option
44  */
45 static int option(int shortopt, const WCHAR *longopt)
46 {
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 a short Windows path to the long format\n"
53     "  -s, --short   converts a long Windows path to the short format\n"
54     "  -h, --help    output this help message and exit\n"
55     "  -v, --version output version information and exit\n"
56     "\n"
57     "If more than one option is given then the input paths are output in\n"
58     "all formats specified, in the order long, short, Unix, Windows.\n"
59     "If no option is given the default is Unix format.\n";
60
61     switch (shortopt) {
62         case 'h':
63             printf("Usage: %s [OPTION] [PATH]...\n", progname);
64             printf(helpmsg);
65             exit(0);
66         case 'v':
67             printf("%s version " PACKAGE_VERSION "\n", progname);
68             exit(0);
69         case 'l':
70             return LONGFORMAT;
71         case 's':
72             return SHORTFORMAT;
73         case 'u':
74             return UNIXFORMAT;
75         case 'w':
76             return WINDOWSFORMAT;
77     }
78
79     fprintf(stderr, "%s: invalid option ", progname);
80     if (longopt)
81         fprintf(stderr, "%s\n", wine_dbgstr_w(longopt));
82     else
83         fprintf(stderr, "'-%c'\n", shortopt);
84     fprintf(stderr, "Try '%s --help' for help\n", progname);
85     exit(2);
86 }
87
88 /*
89  * Parse command line options
90  */
91 static int parse_options(const WCHAR *argv[])
92 {
93     static const WCHAR longW[] = { 'l','o','n','g',0 };
94     static const WCHAR shortW[] = { 's','h','o','r','t',0 };
95     static const WCHAR unixW[] = { 'u','n','i','x',0 };
96     static const WCHAR windowsW[] = { 'w','i','n','d','o','w','s',0 };
97     static const WCHAR helpW[] = { 'h','e','l','p',0 };
98     static const WCHAR versionW[] = { 'v','e','r','s','i','o','n',0 };
99     static const WCHAR nullW[] = { 0 };
100     static const WCHAR *longopts[] = { longW, shortW, unixW, windowsW, helpW, versionW, nullW };
101     int outputformats = 0;
102     int done = 0;
103     int i, j;
104
105     for (i = 1; argv[i] && !done; )
106     {
107         if (argv[i][0] != '-') {
108             /* not an option */
109             i++;
110             continue;
111         }
112
113         if (argv[i][1] == '-') {
114             if (argv[i][2] == 0) {
115                 /* '--' end of options */
116                 done = 1;
117             } else {
118                 /* long option */
119                 for (j = 0; longopts[j][0]; j++)
120                     if (!lstrcmpiW(argv[i]+2, longopts[j]))
121                         break;
122                 outputformats |= option(longopts[j][0], argv[i]);
123             }
124         } else {
125             /* short options */
126             for (j = 1; argv[i][j]; j++)
127                 outputformats |= option(argv[i][j], NULL);
128         }
129
130         /* remove option */
131         for (j = i + 1; argv[j - 1]; j++)
132             argv[j - 1] = argv[j];
133     }
134
135     return outputformats;
136 }
137
138 /*
139  * Main function
140  */
141 int wmain(int argc, const WCHAR *argv[])
142 {
143     LPSTR (*wine_get_unix_file_name_ptr)(LPCWSTR) = NULL;
144     LPWSTR (*wine_get_dos_file_name_ptr)(LPCSTR) = NULL;
145     WCHAR dos_pathW[MAX_PATH];
146     char path[MAX_PATH];
147     int outputformats;
148     int i;
149
150     outputformats = parse_options(argv);
151     if (outputformats == 0)
152         outputformats = UNIXFORMAT;
153
154     if (outputformats & UNIXFORMAT) {
155         wine_get_unix_file_name_ptr = (void*)
156             GetProcAddress(GetModuleHandle("KERNEL32"),
157                            "wine_get_unix_file_name");
158         if (wine_get_unix_file_name_ptr == NULL) {
159             fprintf(stderr, "%s: cannot get the address of "
160                             "'wine_get_unix_file_name'\n", progname);
161             exit(3);
162         }
163     }
164
165     if (outputformats & WINDOWSFORMAT) {
166         wine_get_dos_file_name_ptr = (void*)
167             GetProcAddress(GetModuleHandle("KERNEL32"),
168                            "wine_get_dos_file_name");
169         if (wine_get_dos_file_name_ptr == NULL) {
170             fprintf(stderr, "%s: cannot get the address of "
171                             "'wine_get_dos_file_name'\n", progname);
172             exit(3);
173         }
174     }
175
176     for (i = 1; argv[i]; i++)
177     {
178         *path='\0';
179         if (outputformats & LONGFORMAT) {
180             if (GetFullPathNameW(argv[i], MAX_PATH, dos_pathW, NULL))
181                 WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
182             printf("%s\n", path);
183         }
184         if (outputformats & SHORTFORMAT) {
185             if (GetShortPathNameW(argv[i], dos_pathW, MAX_PATH))
186                 WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
187             printf("%s\n", path);
188         }
189         if (outputformats & UNIXFORMAT) {
190             char *unix_name;
191
192             if ((unix_name = wine_get_unix_file_name_ptr(argv[i])))
193             {
194                 printf("%s\n", unix_name);
195                 HeapFree( GetProcessHeap(), 0, unix_name );
196             }
197             else printf( "\n" );
198         }
199         if (outputformats & WINDOWSFORMAT) {
200             WCHAR* windows_name;
201             char* unix_name;
202             DWORD size;
203
204             size=WideCharToMultiByte(CP_UNIXCP, 0, argv[i], -1, NULL, 0, NULL, NULL);
205             unix_name=HeapAlloc(GetProcessHeap(), 0, size);
206             WideCharToMultiByte(CP_UNIXCP, 0, argv[i], -1, unix_name, size, NULL, NULL);
207
208             if ((windows_name = wine_get_dos_file_name_ptr(unix_name)))
209             {
210                 WideCharToMultiByte(CP_UNIXCP, 0, windows_name, -1, path, MAX_PATH, NULL, NULL);
211                 printf("%s\n", path);
212                 HeapFree( GetProcessHeap(), 0, windows_name );
213             }
214             else printf( "\n" );
215             HeapFree( GetProcessHeap(), 0, unix_name );
216         }
217     }
218
219     exit(0);
220 }