winepath.c 9.14 KB
Newer Older
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 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
21 22
 */

23 24
#define WIN32_LEAN_AND_MEAN

25 26 27 28 29 30
#include "config.h"

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

31 32
#include "wine/debug.h"

33
enum {
34 35 36
    SHORTFORMAT   = 1,
    LONGFORMAT    = 2,
    UNIXFORMAT    = 4,
37 38
    WINDOWSFORMAT = 8,
    PRINT0        = 16,
39 40
};

41
static const char progname[] = "winepath";
42 43 44 45

/*
 * handle an option
 */
46
static int option(int shortopt, const WCHAR *longopt)
47
{
48
    static const char helpmsg[] =
49 50
    "Convert PATH(s) to Unix or Windows long or short paths.\n"
    "\n"
51 52
    "  -u, --unix    converts a Windows path to a Unix path\n"
    "  -w, --windows converts a Unix path to a long Windows path\n"
53 54 55 56
    "  -l, --long    converts the short Windows path of an existing file or\n"
    "                directory to the long format\n"
    "  -s, --short   converts the long Windows path of an existing file or\n"
    "                directory to the short format\n"
57
    "  -0            separate output with \\0 character, instead of a newline\n"
58 59 60
    "  -h, --help    output this help message and exit\n"
    "  -v, --version output version information and exit\n"
    "\n"
61 62 63
    "If more than one option is given then the input paths are output in\n"
    "all formats specified, in the order long, short, Unix, Windows.\n"
    "If no option is given the default is Unix format.\n";
64 65 66 67 68 69 70

    switch (shortopt) {
        case 'h':
            printf("Usage: %s [OPTION] [PATH]...\n", progname);
            printf(helpmsg);
            exit(0);
        case 'v':
71
            printf("%s version " PACKAGE_VERSION "\n", progname);
72 73 74 75 76 77 78
            exit(0);
        case 'l':
            return LONGFORMAT;
        case 's':
            return SHORTFORMAT;
        case 'u':
            return UNIXFORMAT;
79 80
        case 'w':
            return WINDOWSFORMAT;
81 82
        case '0':
            return PRINT0;
83
    }
84 85 86

    fprintf(stderr, "%s: invalid option ", progname);
    if (longopt)
87
        fprintf(stderr, "%s\n", wine_dbgstr_w(longopt));
88
    else
89
        fprintf(stderr, "'-%c'\n", shortopt);
90 91 92 93 94 95 96
    fprintf(stderr, "Try '%s --help' for help\n", progname);
    exit(2);
}

/*
 * Parse command line options
 */
97
static int parse_options(WCHAR *argv[])
98
{
99 100 101
    static const WCHAR longW[] = { 'l','o','n','g',0 };
    static const WCHAR shortW[] = { 's','h','o','r','t',0 };
    static const WCHAR unixW[] = { 'u','n','i','x',0 };
102
    static const WCHAR windowsW[] = { 'w','i','n','d','o','w','s',0 };
103 104 105
    static const WCHAR helpW[] = { 'h','e','l','p',0 };
    static const WCHAR versionW[] = { 'v','e','r','s','i','o','n',0 };
    static const WCHAR nullW[] = { 0 };
106
    static const WCHAR *longopts[] = { longW, shortW, unixW, windowsW, helpW, versionW, nullW };
107
    int outputformats = 0;
108
    BOOL done = FALSE;
109 110 111 112 113 114 115 116 117 118 119 120 121
    int i, j;

    for (i = 1; argv[i] && !done; )
    {
        if (argv[i][0] != '-') {
            /* not an option */
            i++;
            continue;
        }

        if (argv[i][1] == '-') {
            if (argv[i][2] == 0) {
                /* '--' end of options */
122
                done = TRUE;
123 124 125
            } else {
                /* long option */
                for (j = 0; longopts[j][0]; j++)
126
                    if (!lstrcmpiW(argv[i]+2, longopts[j]))
127 128 129 130 131 132
                        break;
                outputformats |= option(longopts[j][0], argv[i]);
            }
        } else {
            /* short options */
            for (j = 1; argv[i][j]; j++)
133
                outputformats |= option(argv[i][j], NULL);
134 135 136 137 138 139 140 141 142 143 144 145 146
        }

        /* remove option */
        for (j = i + 1; argv[j - 1]; j++)
            argv[j - 1] = argv[j];
    }

    return outputformats;
}

/*
 * Main function
 */
147
int wmain(int argc, WCHAR *argv[])
148
{
149 150
    LPSTR (*CDECL wine_get_unix_file_name_ptr)(LPCWSTR) = NULL;
    LPWSTR (*CDECL wine_get_dos_file_name_ptr)(LPCSTR) = NULL;
151 152
    WCHAR dos_pathW[MAX_PATH];
    char path[MAX_PATH];
153 154
    int outputformats;
    int i;
155
    int separator;
156 157

    outputformats = parse_options(argv);
158 159 160 161 162 163 164 165 166

    if (outputformats & PRINT0)
    {
        separator = '\0';
        outputformats ^= PRINT0;
    }
    else
        separator = '\n';

167 168 169 170
    if (outputformats == 0)
        outputformats = UNIXFORMAT;

    if (outputformats & UNIXFORMAT) {
171
        wine_get_unix_file_name_ptr = (void*)
172
            GetProcAddress(GetModuleHandleA("KERNEL32"),
173 174 175 176 177 178 179 180
                           "wine_get_unix_file_name");
        if (wine_get_unix_file_name_ptr == NULL) {
            fprintf(stderr, "%s: cannot get the address of "
                            "'wine_get_unix_file_name'\n", progname);
            exit(3);
        }
    }

181 182
    if (outputformats & WINDOWSFORMAT) {
        wine_get_dos_file_name_ptr = (void*)
183
            GetProcAddress(GetModuleHandleA("KERNEL32"),
184 185 186 187 188 189 190 191
                           "wine_get_dos_file_name");
        if (wine_get_dos_file_name_ptr == NULL) {
            fprintf(stderr, "%s: cannot get the address of "
                            "'wine_get_dos_file_name'\n", progname);
            exit(3);
        }
    }

192 193
    for (i = 1; argv[i]; i++)
    {
194
        *path='\0';
195
        if (outputformats & LONGFORMAT) {
196
            if (GetLongPathNameW(argv[i], dos_pathW, MAX_PATH))
197
                WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
198
            printf("%s%c", path, separator);
199 200
        }
        if (outputformats & SHORTFORMAT) {
201 202
            if (GetShortPathNameW(argv[i], dos_pathW, MAX_PATH))
                WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
203
            printf("%s%c", path, separator);
204 205
        }
        if (outputformats & UNIXFORMAT) {
206 207 208 209 210 211
            WCHAR *ntpath, *tail;
            int ntpathlen=lstrlenW(argv[i]);
            ntpath=HeapAlloc(GetProcessHeap(), 0, sizeof(*ntpath)*(ntpathlen+1));
            lstrcpyW(ntpath, argv[i]);
            tail=NULL;
            while (1)
212
            {
213 214 215 216 217 218 219 220 221
                char *unix_name;
                WCHAR *slash, *c;

                unix_name = wine_get_unix_file_name_ptr(ntpath);
                if (unix_name)
                {
                    if (tail)
                    {
                        WideCharToMultiByte(CP_UNIXCP, 0, tail+1, -1, path, MAX_PATH, NULL, NULL);
222
                        printf("%s/%s%c", unix_name, path, separator);
223 224 225
                    }
                    else
                    {
226
                        printf("%s%c", unix_name, separator);
227 228 229 230 231 232 233 234 235 236 237 238 239
                    }
                    HeapFree( GetProcessHeap(), 0, unix_name );
                    break;
                }

                slash=(tail ? tail : ntpath+ntpathlen);
                while (slash != ntpath && *slash != '/' && *slash != '\\')
                    slash--;
                if (slash == ntpath)
                {
                    /* This is a complete path conversion failure.
                     * It would typically happen if ntpath == "".
                     */
240
                    printf("%c", separator);
241 242 243 244 245 246 247 248 249 250 251
                    break;
                }
                c=slash+1;
                while (*c != '\0' && *c != '*' && *c != '?' &&
                       *c != '<' && *c != '>' && *c != '|' && *c != '"')
                    c++;
                if (*c != '\0')
                {
                    /* If this is not a valid NT path to start with,
                     * then obviously we cannot convert it.
                     */
252
                    printf("%c", separator);
253 254 255 256 257 258
                    break;
                }
                if (tail)
                    *tail='/';
                tail=slash;
                *tail='\0';
259
            }
260
            HeapFree(GetProcessHeap(), 0, ntpath);
261
        }
262 263 264 265 266 267 268 269 270 271 272 273
        if (outputformats & WINDOWSFORMAT) {
            WCHAR* windows_name;
            char* unix_name;
            DWORD size;

            size=WideCharToMultiByte(CP_UNIXCP, 0, argv[i], -1, NULL, 0, NULL, NULL);
            unix_name=HeapAlloc(GetProcessHeap(), 0, size);
            WideCharToMultiByte(CP_UNIXCP, 0, argv[i], -1, unix_name, size, NULL, NULL);

            if ((windows_name = wine_get_dos_file_name_ptr(unix_name)))
            {
                WideCharToMultiByte(CP_UNIXCP, 0, windows_name, -1, path, MAX_PATH, NULL, NULL);
274
                printf("%s%c", path, separator);
275 276
                HeapFree( GetProcessHeap(), 0, windows_name );
            }
277
            else printf("%c", separator);
278 279
            HeapFree( GetProcessHeap(), 0, unix_name );
        }
280 281 282 283
    }

    exit(0);
}