winepath.c 8.74 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 37
    SHORTFORMAT   = 1,
    LONGFORMAT    = 2,
    UNIXFORMAT    = 4,
    WINDOWSFORMAT = 8
38 39
};

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

/*
 * handle an option
 */
45
static int option(int shortopt, const WCHAR *longopt)
46
{
47
    static const char helpmsg[] =
48 49
    "Convert PATH(s) to Unix or Windows long or short paths.\n"
    "\n"
50 51
    "  -u, --unix    converts a Windows path to a Unix path\n"
    "  -w, --windows converts a Unix path to a long Windows path\n"
52 53 54 55
    "  -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"
56 57 58
    "  -h, --help    output this help message and exit\n"
    "  -v, --version output version information and exit\n"
    "\n"
59 60 61
    "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";
62 63 64 65 66 67 68

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

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

/*
 * Parse command line options
 */
93
static int parse_options(const WCHAR *argv[])
94
{
95 96 97
    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 };
98
    static const WCHAR windowsW[] = { 'w','i','n','d','o','w','s',0 };
99 100 101
    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 };
102
    static const WCHAR *longopts[] = { longW, shortW, unixW, windowsW, helpW, versionW, nullW };
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    int outputformats = 0;
    int done = 0;
    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 */
                done = 1;
            } else {
                /* long option */
                for (j = 0; longopts[j][0]; j++)
122
                    if (!lstrcmpiW(argv[i]+2, longopts[j]))
123 124 125 126 127 128
                        break;
                outputformats |= option(longopts[j][0], argv[i]);
            }
        } else {
            /* short options */
            for (j = 1; argv[i][j]; j++)
129
                outputformats |= option(argv[i][j], NULL);
130 131 132 133 134 135 136 137 138 139 140 141 142
        }

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

    return outputformats;
}

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

    outputformats = parse_options(argv);
    if (outputformats == 0)
        outputformats = UNIXFORMAT;

    if (outputformats & UNIXFORMAT) {
157
        wine_get_unix_file_name_ptr = (void*)
158
            GetProcAddress(GetModuleHandleA("KERNEL32"),
159 160 161 162 163 164 165 166
                           "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);
        }
    }

167 168
    if (outputformats & WINDOWSFORMAT) {
        wine_get_dos_file_name_ptr = (void*)
169
            GetProcAddress(GetModuleHandleA("KERNEL32"),
170 171 172 173 174 175 176 177
                           "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);
        }
    }

178 179
    for (i = 1; argv[i]; i++)
    {
180
        *path='\0';
181
        if (outputformats & LONGFORMAT) {
182
            if (GetLongPathNameW(argv[i], dos_pathW, MAX_PATH))
183
                WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
184 185 186
            printf("%s\n", path);
        }
        if (outputformats & SHORTFORMAT) {
187 188
            if (GetShortPathNameW(argv[i], dos_pathW, MAX_PATH))
                WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
189 190 191
            printf("%s\n", path);
        }
        if (outputformats & UNIXFORMAT) {
192 193 194 195 196 197
            WCHAR *ntpath, *tail;
            int ntpathlen=lstrlenW(argv[i]);
            ntpath=HeapAlloc(GetProcessHeap(), 0, sizeof(*ntpath)*(ntpathlen+1));
            lstrcpyW(ntpath, argv[i]);
            tail=NULL;
            while (1)
198
            {
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
                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);
                        printf("%s/%s\n", unix_name, path);
                    }
                    else
                    {
                        printf("%s\n", unix_name);
                    }
                    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 == "".
                     */
                    printf("\n");
                    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.
                     */
                    printf("\n");
                    break;
                }
                if (tail)
                    *tail='/';
                tail=slash;
                *tail='\0';
245
            }
246
            HeapFree(GetProcessHeap(), 0, ntpath);
247
        }
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        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);
                printf("%s\n", path);
                HeapFree( GetProcessHeap(), 0, windows_name );
            }
            else printf( "\n" );
            HeapFree( GetProcessHeap(), 0, unix_name );
        }
266 267 268 269
    }

    exit(0);
}