2002-05-05 22:31:54 +02:00
|
|
|
/*
|
2005-10-10 12:24:53 +02:00
|
|
|
* Translate between Windows and Unix paths formats
|
2002-05-05 22:31:54 +02:00
|
|
|
*
|
|
|
|
* Copyright 2002 Mike Wetherell
|
2005-01-25 17:41:50 +01:00
|
|
|
* Copyright 2005 Dmitry Timoshkov
|
2005-10-10 12:24:53 +02:00
|
|
|
* Copyright 2005 Francois Gouget
|
2002-05-05 22:31:54 +02:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-05-05 22:31:54 +02:00
|
|
|
*/
|
|
|
|
|
2006-01-16 20:41:34 +01:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2002-05-05 22:31:54 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2005-01-25 17:41:50 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
enum {
|
2005-10-10 12:24:53 +02:00
|
|
|
SHORTFORMAT = 1,
|
|
|
|
LONGFORMAT = 2,
|
|
|
|
UNIXFORMAT = 4,
|
2010-08-23 16:32:29 +02:00
|
|
|
WINDOWSFORMAT = 8,
|
|
|
|
PRINT0 = 16,
|
2002-05-05 22:31:54 +02:00
|
|
|
};
|
|
|
|
|
2005-01-25 17:41:50 +01:00
|
|
|
static const char progname[] = "winepath";
|
2002-05-05 22:31:54 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* handle an option
|
|
|
|
*/
|
2005-01-25 17:41:50 +01:00
|
|
|
static int option(int shortopt, const WCHAR *longopt)
|
2002-05-05 22:31:54 +02:00
|
|
|
{
|
2005-01-25 17:41:50 +01:00
|
|
|
static const char helpmsg[] =
|
2002-05-05 22:31:54 +02:00
|
|
|
"Convert PATH(s) to Unix or Windows long or short paths.\n"
|
|
|
|
"\n"
|
2005-10-10 12:24:53 +02:00
|
|
|
" -u, --unix converts a Windows path to a Unix path\n"
|
|
|
|
" -w, --windows converts a Unix path to a long Windows path\n"
|
2006-11-09 15:36:12 +01:00
|
|
|
" -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"
|
2010-08-23 16:32:29 +02:00
|
|
|
" -0 separate output with \\0 character, instead of a newline\n"
|
2002-05-05 22:31:54 +02:00
|
|
|
" -h, --help output this help message and exit\n"
|
|
|
|
"\n"
|
2005-10-10 12:24:53 +02:00
|
|
|
"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";
|
2002-05-05 22:31:54 +02:00
|
|
|
|
|
|
|
switch (shortopt) {
|
|
|
|
case 'h':
|
|
|
|
printf("Usage: %s [OPTION] [PATH]...\n", progname);
|
|
|
|
printf(helpmsg);
|
|
|
|
exit(0);
|
|
|
|
case 'l':
|
|
|
|
return LONGFORMAT;
|
|
|
|
case 's':
|
|
|
|
return SHORTFORMAT;
|
|
|
|
case 'u':
|
|
|
|
return UNIXFORMAT;
|
2005-10-10 12:24:53 +02:00
|
|
|
case 'w':
|
|
|
|
return WINDOWSFORMAT;
|
2010-08-23 16:32:29 +02:00
|
|
|
case '0':
|
|
|
|
return PRINT0;
|
2002-06-01 01:06:46 +02:00
|
|
|
}
|
2002-05-05 22:31:54 +02:00
|
|
|
|
|
|
|
fprintf(stderr, "%s: invalid option ", progname);
|
|
|
|
if (longopt)
|
2005-01-25 17:41:50 +01:00
|
|
|
fprintf(stderr, "%s\n", wine_dbgstr_w(longopt));
|
2002-05-05 22:31:54 +02:00
|
|
|
else
|
2002-06-01 01:06:46 +02:00
|
|
|
fprintf(stderr, "'-%c'\n", shortopt);
|
2002-05-05 22:31:54 +02:00
|
|
|
fprintf(stderr, "Try '%s --help' for help\n", progname);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse command line options
|
|
|
|
*/
|
2009-02-06 23:20:27 +01:00
|
|
|
static int parse_options(WCHAR *argv[])
|
2002-05-05 22:31:54 +02:00
|
|
|
{
|
2020-04-09 09:43:30 +02:00
|
|
|
static const WCHAR *longopts[] = { L"long", L"short", L"unix", L"windows", L"help", NULL };
|
2002-05-05 22:31:54 +02:00
|
|
|
int outputformats = 0;
|
2013-10-11 22:43:03 +02:00
|
|
|
BOOL done = FALSE;
|
2002-05-05 22:31:54 +02:00
|
|
|
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 */
|
2013-10-11 22:43:03 +02:00
|
|
|
done = TRUE;
|
2002-05-05 22:31:54 +02:00
|
|
|
} else {
|
|
|
|
/* long option */
|
2020-04-09 09:43:30 +02:00
|
|
|
for (j = 0; longopts[j]; j++)
|
2005-01-25 17:41:50 +01:00
|
|
|
if (!lstrcmpiW(argv[i]+2, longopts[j]))
|
2002-05-05 22:31:54 +02:00
|
|
|
break;
|
2020-04-09 09:43:30 +02:00
|
|
|
if (longopts[j]) outputformats |= option(longopts[j][0], argv[i]);
|
2002-05-05 22:31:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* short options */
|
|
|
|
for (j = 1; argv[i][j]; j++)
|
2002-06-01 01:06:46 +02:00
|
|
|
outputformats |= option(argv[i][j], NULL);
|
2002-05-05 22:31:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remove option */
|
|
|
|
for (j = i + 1; argv[j - 1]; j++)
|
|
|
|
argv[j - 1] = argv[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputformats;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main function
|
|
|
|
*/
|
2019-05-30 10:24:19 +02:00
|
|
|
int __cdecl wmain(int argc, WCHAR *argv[])
|
2002-05-05 22:31:54 +02:00
|
|
|
{
|
2008-12-16 16:16:10 +01:00
|
|
|
LPSTR (*CDECL wine_get_unix_file_name_ptr)(LPCWSTR) = NULL;
|
|
|
|
LPWSTR (*CDECL wine_get_dos_file_name_ptr)(LPCSTR) = NULL;
|
2005-01-25 17:41:50 +01:00
|
|
|
WCHAR dos_pathW[MAX_PATH];
|
|
|
|
char path[MAX_PATH];
|
2002-05-05 22:31:54 +02:00
|
|
|
int outputformats;
|
|
|
|
int i;
|
2010-08-23 16:32:29 +02:00
|
|
|
int separator;
|
2002-05-05 22:31:54 +02:00
|
|
|
|
|
|
|
outputformats = parse_options(argv);
|
2010-08-23 16:32:29 +02:00
|
|
|
|
|
|
|
if (outputformats & PRINT0)
|
|
|
|
{
|
|
|
|
separator = '\0';
|
|
|
|
outputformats ^= PRINT0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
separator = '\n';
|
|
|
|
|
2002-05-05 22:31:54 +02:00
|
|
|
if (outputformats == 0)
|
|
|
|
outputformats = UNIXFORMAT;
|
|
|
|
|
|
|
|
if (outputformats & UNIXFORMAT) {
|
2005-10-10 12:24:53 +02:00
|
|
|
wine_get_unix_file_name_ptr = (void*)
|
2008-09-03 11:30:44 +02:00
|
|
|
GetProcAddress(GetModuleHandleA("KERNEL32"),
|
2002-05-05 22:31:54 +02:00
|
|
|
"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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-10 12:24:53 +02:00
|
|
|
if (outputformats & WINDOWSFORMAT) {
|
|
|
|
wine_get_dos_file_name_ptr = (void*)
|
2008-09-03 11:30:44 +02:00
|
|
|
GetProcAddress(GetModuleHandleA("KERNEL32"),
|
2005-10-10 12:24:53 +02:00
|
|
|
"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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-05 22:31:54 +02:00
|
|
|
for (i = 1; argv[i]; i++)
|
|
|
|
{
|
2003-05-11 04:47:28 +02:00
|
|
|
*path='\0';
|
2002-05-05 22:31:54 +02:00
|
|
|
if (outputformats & LONGFORMAT) {
|
2006-11-09 15:36:12 +01:00
|
|
|
if (GetLongPathNameW(argv[i], dos_pathW, MAX_PATH))
|
2005-01-25 17:41:50 +01:00
|
|
|
WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
|
2010-08-23 16:32:29 +02:00
|
|
|
printf("%s%c", path, separator);
|
2002-05-05 22:31:54 +02:00
|
|
|
}
|
|
|
|
if (outputformats & SHORTFORMAT) {
|
2005-01-25 17:41:50 +01:00
|
|
|
if (GetShortPathNameW(argv[i], dos_pathW, MAX_PATH))
|
|
|
|
WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
|
2010-08-23 16:32:29 +02:00
|
|
|
printf("%s%c", path, separator);
|
2002-05-05 22:31:54 +02:00
|
|
|
}
|
|
|
|
if (outputformats & UNIXFORMAT) {
|
2006-11-10 15:40:59 +01:00
|
|
|
WCHAR *ntpath, *tail;
|
|
|
|
int ntpathlen=lstrlenW(argv[i]);
|
|
|
|
ntpath=HeapAlloc(GetProcessHeap(), 0, sizeof(*ntpath)*(ntpathlen+1));
|
|
|
|
lstrcpyW(ntpath, argv[i]);
|
|
|
|
tail=NULL;
|
|
|
|
while (1)
|
2004-04-14 21:40:53 +02:00
|
|
|
{
|
2006-11-10 15:40:59 +01:00
|
|
|
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);
|
2010-08-23 16:32:29 +02:00
|
|
|
printf("%s/%s%c", unix_name, path, separator);
|
2006-11-10 15:40:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-23 16:32:29 +02:00
|
|
|
printf("%s%c", unix_name, separator);
|
2006-11-10 15:40:59 +01:00
|
|
|
}
|
|
|
|
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 == "".
|
|
|
|
*/
|
2010-08-27 07:54:52 +02:00
|
|
|
printf("%c", separator);
|
2006-11-10 15:40:59 +01:00
|
|
|
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.
|
|
|
|
*/
|
2010-08-27 07:54:52 +02:00
|
|
|
printf("%c", separator);
|
2006-11-10 15:40:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tail)
|
|
|
|
*tail='/';
|
|
|
|
tail=slash;
|
|
|
|
*tail='\0';
|
2004-04-14 21:40:53 +02:00
|
|
|
}
|
2006-11-10 15:40:59 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, ntpath);
|
2002-05-05 22:31:54 +02:00
|
|
|
}
|
2005-10-10 12:24:53 +02:00
|
|
|
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);
|
2010-08-23 16:32:29 +02:00
|
|
|
printf("%s%c", path, separator);
|
2005-10-10 12:24:53 +02:00
|
|
|
HeapFree( GetProcessHeap(), 0, windows_name );
|
|
|
|
}
|
2010-08-27 07:54:52 +02:00
|
|
|
else printf("%c", separator);
|
2005-10-10 12:24:53 +02:00
|
|
|
HeapFree( GetProcessHeap(), 0, unix_name );
|
|
|
|
}
|
2002-05-05 22:31:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|