Set the winepath version to be the same as the global package
[wine] / programs / winepath / winepath.c
1 /*
2  * Translate between Wine and Unix paths
3  *
4  * Copyright 2002 Mike Wetherell
5  * Copyright 2005 Dmitry Timoshkov
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23
24 #include <windows.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "wine/debug.h"
29
30 enum {
31     SHORTFORMAT = 1,
32     LONGFORMAT  = 2,
33     UNIXFORMAT  = 4
34 };
35
36 static const char progname[] = "winepath";
37
38 /* Wine specific functions */
39 typedef LPSTR (*wine_get_unix_file_name_t) ( LPCWSTR dos );
40 /*
41  * handle an option
42  */
43 static int option(int shortopt, const WCHAR *longopt)
44 {
45     static const char helpmsg[] =
46     "Convert PATH(s) to Unix or Windows long or short paths.\n"
47     "\n"
48     "  -u, --unix    output Unix format\n"
49     "  -l, --long    output Windows long format\n"
50     "  -s, --short   output Windows short format \n"
51     "  -h, --help    output this help message and exit\n"
52     "  -v, --version output version information and exit\n"
53     "\n"
54     "The input paths can be in any format. If more than one option is given\n"
55     "then the input paths are output in all formats specified, in the order\n"
56     "Unix, long, short. If no option is given the default is Unix format.\n";
57
58     switch (shortopt) {
59         case 'h':
60             printf("Usage: %s [OPTION] [PATH]...\n", progname);
61             printf(helpmsg);
62             exit(0);
63         case 'v':
64             printf("%s version " PACKAGE_VERSION "\n", progname);
65             exit(0);
66         case 'l':
67             return LONGFORMAT;
68         case 's':
69             return SHORTFORMAT;
70         case 'u':
71             return UNIXFORMAT;
72     }
73
74     fprintf(stderr, "%s: invalid option ", progname);
75     if (longopt)
76         fprintf(stderr, "%s\n", wine_dbgstr_w(longopt));
77     else
78         fprintf(stderr, "'-%c'\n", shortopt);
79     fprintf(stderr, "Try '%s --help' for help\n", progname);
80     exit(2);
81 }
82
83 /*
84  * Parse command line options
85  */
86 static int parse_options(const WCHAR *argv[])
87 {
88     static const WCHAR longW[] = { 'l','o','n','g',0 };
89     static const WCHAR shortW[] = { 's','h','o','r','t',0 };
90     static const WCHAR unixW[] = { 'u','n','i','x',0 };
91     static const WCHAR helpW[] = { 'h','e','l','p',0 };
92     static const WCHAR versionW[] = { 'v','e','r','s','i','o','n',0 };
93     static const WCHAR nullW[] = { 0 };
94     static const WCHAR *longopts[] = { longW, shortW, unixW, helpW, versionW, nullW };
95     int outputformats = 0;
96     int done = 0;
97     int i, j;
98
99     for (i = 1; argv[i] && !done; )
100     {
101         if (argv[i][0] != '-') {
102             /* not an option */
103             i++;
104             continue;
105         }
106
107         if (argv[i][1] == '-') {
108             if (argv[i][2] == 0) {
109                 /* '--' end of options */
110                 done = 1;
111             } else {
112                 /* long option */
113                 for (j = 0; longopts[j][0]; j++)
114                     if (!lstrcmpiW(argv[i]+2, longopts[j]))
115                         break;
116                 outputformats |= option(longopts[j][0], argv[i]);
117             }
118         } else {
119             /* short options */
120             for (j = 1; argv[i][j]; j++)
121                 outputformats |= option(argv[i][j], NULL);
122         }
123
124         /* remove option */
125         for (j = i + 1; argv[j - 1]; j++)
126             argv[j - 1] = argv[j];
127     }
128
129     return outputformats;
130 }
131
132 /*
133  * Main function
134  */
135 int wmain(int argc, const WCHAR *argv[])
136 {
137     wine_get_unix_file_name_t wine_get_unix_file_name_ptr = NULL;
138     WCHAR dos_pathW[MAX_PATH];
139     char path[MAX_PATH];
140     int outputformats;
141     int i;
142
143     outputformats = parse_options(argv);
144     if (outputformats == 0)
145         outputformats = UNIXFORMAT;
146
147     if (outputformats & UNIXFORMAT) {
148         wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
149             GetProcAddress(GetModuleHandle("KERNEL32"),
150                            "wine_get_unix_file_name");
151         if (wine_get_unix_file_name_ptr == NULL) {
152             fprintf(stderr, "%s: cannot get the address of "
153                             "'wine_get_unix_file_name'\n", progname);
154             exit(3);
155         }
156     }
157
158     for (i = 1; argv[i]; i++)
159     {
160         *path='\0';
161         if (outputformats & LONGFORMAT) {
162             if (GetFullPathNameW(argv[i], MAX_PATH, dos_pathW, NULL))
163                 WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
164             printf("%s\n", path);
165         }
166         if (outputformats & SHORTFORMAT) {
167             if (GetShortPathNameW(argv[i], dos_pathW, MAX_PATH))
168                 WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
169             printf("%s\n", path);
170         }
171         if (outputformats & UNIXFORMAT) {
172             char *unix_name;
173
174             if ((unix_name = wine_get_unix_file_name_ptr(argv[i])))
175             {
176                 printf("%s\n", unix_name);
177                 HeapFree( GetProcessHeap(), 0, unix_name );
178             }
179             else printf( "\n" );
180         }
181     }
182
183     exit(0);
184 }