winepath.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. #define WIN32_LEAN_AND_MEAN
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <fcntl.h>
  27. #include <io.h>
  28. #include "wine/debug.h"
  29. enum {
  30. SHORTFORMAT = 1,
  31. LONGFORMAT = 2,
  32. UNIXFORMAT = 4,
  33. WINDOWSFORMAT = 8,
  34. PRINT0 = 16,
  35. };
  36. static const char progname[] = "winepath";
  37. /*
  38. * handle an option
  39. */
  40. static int option(int shortopt, const WCHAR *longopt)
  41. {
  42. static const char helpmsg[] =
  43. "Convert PATH(s) to Unix or Windows long or short paths.\n"
  44. "\n"
  45. " -u, --unix converts a Windows path to a Unix path\n"
  46. " -w, --windows converts a Unix path to a long Windows path\n"
  47. " -l, --long converts the short Windows path of an existing file or\n"
  48. " directory to the long format\n"
  49. " -s, --short converts the long Windows path of an existing file or\n"
  50. " directory to the short format\n"
  51. " -0 separate output with \\0 character, instead of a newline\n"
  52. " -h, --help output this help message and exit\n"
  53. "\n"
  54. "If more than one option is given then the input paths are output in\n"
  55. "all formats specified, in the order long, short, Unix, Windows.\n"
  56. "If no option is given the default is Unix format.\n";
  57. switch (shortopt) {
  58. case 'h':
  59. printf("Usage: %s [OPTION] [PATH]...\n", progname);
  60. printf(helpmsg);
  61. exit(0);
  62. case 'l':
  63. return LONGFORMAT;
  64. case 's':
  65. return SHORTFORMAT;
  66. case 'u':
  67. return UNIXFORMAT;
  68. case 'w':
  69. return WINDOWSFORMAT;
  70. case '0':
  71. return PRINT0;
  72. }
  73. fprintf(stderr, "%s: invalid option ", progname);
  74. if (longopt)
  75. fprintf(stderr, "%s\n", wine_dbgstr_w(longopt));
  76. else
  77. fprintf(stderr, "'-%c'\n", shortopt);
  78. fprintf(stderr, "Try '%s --help' for help\n", progname);
  79. exit(2);
  80. }
  81. /*
  82. * Parse command line options
  83. */
  84. static int parse_options(WCHAR *argv[])
  85. {
  86. static const WCHAR *longopts[] = { L"long", L"short", L"unix", L"windows", L"help", NULL };
  87. int outputformats = 0;
  88. BOOL done = FALSE;
  89. int i, j;
  90. for (i = 1; argv[i] && !done; )
  91. {
  92. if (argv[i][0] != '-') {
  93. /* not an option */
  94. i++;
  95. continue;
  96. }
  97. if (argv[i][1] == '-') {
  98. if (argv[i][2] == 0) {
  99. /* '--' end of options */
  100. done = TRUE;
  101. } else {
  102. /* long option */
  103. for (j = 0; longopts[j]; j++)
  104. if (!lstrcmpiW(argv[i]+2, longopts[j]))
  105. break;
  106. if (longopts[j]) outputformats |= option(longopts[j][0], argv[i]);
  107. }
  108. } else {
  109. /* short options */
  110. for (j = 1; argv[i][j]; j++)
  111. outputformats |= option(argv[i][j], NULL);
  112. }
  113. /* remove option */
  114. for (j = i + 1; argv[j - 1]; j++)
  115. argv[j - 1] = argv[j];
  116. }
  117. return outputformats;
  118. }
  119. /*
  120. * Main function
  121. */
  122. int __cdecl wmain(int argc, WCHAR *argv[])
  123. {
  124. LPSTR (*CDECL wine_get_unix_file_name_ptr)(LPCWSTR) = NULL;
  125. LPWSTR (*CDECL wine_get_dos_file_name_ptr)(LPCSTR) = NULL;
  126. WCHAR dos_pathW[MAX_PATH];
  127. char path[MAX_PATH];
  128. int outputformats;
  129. int i;
  130. int separator;
  131. setmode( fileno(stdout), O_BINARY ); /* avoid crlf */
  132. outputformats = parse_options(argv);
  133. if (outputformats & PRINT0)
  134. {
  135. separator = '\0';
  136. outputformats ^= PRINT0;
  137. }
  138. else
  139. separator = '\n';
  140. if (outputformats == 0)
  141. outputformats = UNIXFORMAT;
  142. if (outputformats & UNIXFORMAT) {
  143. wine_get_unix_file_name_ptr = (void*)
  144. GetProcAddress(GetModuleHandleA("KERNEL32"),
  145. "wine_get_unix_file_name");
  146. if (wine_get_unix_file_name_ptr == NULL) {
  147. fprintf(stderr, "%s: cannot get the address of "
  148. "'wine_get_unix_file_name'\n", progname);
  149. exit(3);
  150. }
  151. }
  152. if (outputformats & WINDOWSFORMAT) {
  153. wine_get_dos_file_name_ptr = (void*)
  154. GetProcAddress(GetModuleHandleA("KERNEL32"),
  155. "wine_get_dos_file_name");
  156. if (wine_get_dos_file_name_ptr == NULL) {
  157. fprintf(stderr, "%s: cannot get the address of "
  158. "'wine_get_dos_file_name'\n", progname);
  159. exit(3);
  160. }
  161. }
  162. for (i = 1; argv[i]; i++)
  163. {
  164. *path='\0';
  165. if (outputformats & LONGFORMAT) {
  166. if (GetLongPathNameW(argv[i], dos_pathW, MAX_PATH))
  167. WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
  168. printf("%s%c", path, separator);
  169. }
  170. if (outputformats & SHORTFORMAT) {
  171. if (GetShortPathNameW(argv[i], dos_pathW, MAX_PATH))
  172. WideCharToMultiByte(CP_UNIXCP, 0, dos_pathW, -1, path, MAX_PATH, NULL, NULL);
  173. printf("%s%c", path, separator);
  174. }
  175. if (outputformats & UNIXFORMAT) {
  176. WCHAR *ntpath, *tail;
  177. int ntpathlen=lstrlenW(argv[i]);
  178. ntpath=HeapAlloc(GetProcessHeap(), 0, sizeof(*ntpath)*(ntpathlen+1));
  179. lstrcpyW(ntpath, argv[i]);
  180. tail=NULL;
  181. while (1)
  182. {
  183. char *unix_name;
  184. WCHAR *slash, *c;
  185. unix_name = wine_get_unix_file_name_ptr(ntpath);
  186. if (unix_name)
  187. {
  188. if (tail)
  189. {
  190. WideCharToMultiByte(CP_UNIXCP, 0, tail+1, -1, path, MAX_PATH, NULL, NULL);
  191. printf("%s/%s%c", unix_name, path, separator);
  192. }
  193. else
  194. {
  195. printf("%s%c", unix_name, separator);
  196. }
  197. HeapFree( GetProcessHeap(), 0, unix_name );
  198. break;
  199. }
  200. slash=(tail ? tail : ntpath+ntpathlen);
  201. while (slash != ntpath && *slash != '/' && *slash != '\\')
  202. slash--;
  203. if (slash == ntpath)
  204. {
  205. /* This is a complete path conversion failure.
  206. * It would typically happen if ntpath == "".
  207. */
  208. printf("%c", separator);
  209. break;
  210. }
  211. c=slash+1;
  212. while (*c != '\0' && *c != '*' && *c != '?' &&
  213. *c != '<' && *c != '>' && *c != '|' && *c != '"')
  214. c++;
  215. if (*c != '\0')
  216. {
  217. /* If this is not a valid NT path to start with,
  218. * then obviously we cannot convert it.
  219. */
  220. printf("%c", separator);
  221. break;
  222. }
  223. if (tail)
  224. *tail='/';
  225. tail=slash;
  226. *tail='\0';
  227. }
  228. HeapFree(GetProcessHeap(), 0, ntpath);
  229. }
  230. if (outputformats & WINDOWSFORMAT) {
  231. WCHAR* windows_name;
  232. char* unix_name;
  233. DWORD size;
  234. size=WideCharToMultiByte(CP_UNIXCP, 0, argv[i], -1, NULL, 0, NULL, NULL);
  235. unix_name=HeapAlloc(GetProcessHeap(), 0, size);
  236. WideCharToMultiByte(CP_UNIXCP, 0, argv[i], -1, unix_name, size, NULL, NULL);
  237. if ((windows_name = wine_get_dos_file_name_ptr(unix_name)))
  238. {
  239. WideCharToMultiByte(CP_UNIXCP, 0, windows_name, -1, path, MAX_PATH, NULL, NULL);
  240. printf("%s%c", path, separator);
  241. HeapFree( GetProcessHeap(), 0, windows_name );
  242. }
  243. else printf("%c", separator);
  244. HeapFree( GetProcessHeap(), 0, unix_name );
  245. }
  246. }
  247. exit(0);
  248. }