filename.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Implementation of Filename for Windows.
  3. */
  4. #include <wchar.h>
  5. #include "putty.h"
  6. #ifdef MPEXT
  7. #include <assert.h>
  8. #endif
  9. Filename *filename_from_str(const char *str)
  10. {
  11. Filename *fn = snew(Filename);
  12. fn->cpath = dupstr(str);
  13. fn->wpath = dup_mb_to_wc(DEFAULT_CODEPAGE, fn->cpath);
  14. fn->utf8path = encode_wide_string_as_utf8(fn->wpath);
  15. return fn;
  16. }
  17. Filename *filename_from_wstr(const wchar_t *str)
  18. {
  19. Filename *fn = snew(Filename);
  20. fn->wpath = dupwcs(str);
  21. fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, fn->wpath, "?");
  22. fn->utf8path = encode_wide_string_as_utf8(fn->wpath);
  23. return fn;
  24. }
  25. Filename *filename_from_utf8(const char *ustr)
  26. {
  27. Filename *fn = snew(Filename);
  28. fn->utf8path = dupstr(ustr);
  29. fn->wpath = decode_utf8_to_wide_string(fn->utf8path);
  30. fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, fn->wpath, "?");
  31. return fn;
  32. }
  33. Filename *filename_copy(const Filename *fn)
  34. {
  35. Filename *newfn = snew(Filename);
  36. newfn->cpath = dupstr(fn->cpath);
  37. newfn->wpath = dupwcs(fn->wpath);
  38. newfn->utf8path = dupstr(fn->utf8path);
  39. return newfn;
  40. }
  41. #ifdef WINSCP
  42. const char* in_memory_key_data(const Filename *fn)
  43. {
  44. const char* result = fn->cpath;
  45. if (result[0] != '@')
  46. {
  47. result = NULL;
  48. }
  49. else
  50. {
  51. int len;
  52. result++;
  53. len = strlen(result);
  54. if (((len % 2) != 0) ||
  55. ((len / 2) < MAX_PATH))
  56. {
  57. result = NULL;
  58. }
  59. else
  60. {
  61. int i;
  62. for (i = 0; (result != NULL) && (i < len); i++)
  63. {
  64. if (!isxdigit(result[i]))
  65. {
  66. result = NULL;
  67. }
  68. }
  69. }
  70. }
  71. return result;
  72. }
  73. #endif
  74. const char *filename_to_str(const Filename *fn)
  75. {
  76. #ifdef WINSCP
  77. if (in_memory_key_data(fn) != NULL) return "in-memory";
  78. #endif
  79. // WINSCP
  80. return fn->utf8path; /* FIXME */
  81. }
  82. const wchar_t *filename_to_wstr(const Filename *fn)
  83. {
  84. return fn->wpath;
  85. }
  86. bool filename_equal(const Filename *f1, const Filename *f2)
  87. {
  88. /* wpath is primary: two filenames refer to the same file if they
  89. * have the same wpath */
  90. return !wcscmp(f1->wpath, f2->wpath);
  91. }
  92. bool filename_is_null(const Filename *fn)
  93. {
  94. return !*fn->wpath;
  95. }
  96. void filename_free(Filename *fn)
  97. {
  98. sfree(fn->wpath);
  99. sfree(fn->cpath);
  100. sfree(fn->utf8path);
  101. sfree(fn);
  102. }
  103. void filename_serialise(BinarySink *bs, const Filename *f)
  104. {
  105. put_asciz(bs, f->utf8path);
  106. }
  107. Filename *filename_deserialise(BinarySource *src)
  108. {
  109. const char *utf8 = get_asciz(src);
  110. return filename_from_utf8(utf8);
  111. }
  112. char filename_char_sanitise(char c)
  113. {
  114. if (strchr("<>:\"/\\|?*", c))
  115. return '.';
  116. return c;
  117. }
  118. FILE *f_open(const Filename *fn, const char *mode, bool isprivate)
  119. {
  120. #ifdef LEGACY_WINDOWS
  121. /* Fallback for legacy pre-NT windows, where as far as I can see
  122. * _wfopen just doesn't work at all */
  123. init_winver();
  124. if (osPlatformId == VER_PLATFORM_WIN32_WINDOWS ||
  125. osPlatformId == VER_PLATFORM_WIN32s)
  126. return fopen(fn->cpath, mode);
  127. #endif
  128. wchar_t *wmode = dup_mb_to_wc(DEFAULT_CODEPAGE, mode);
  129. FILE *fp = _wfopen(fn->wpath, wmode);
  130. sfree(wmode);
  131. return fp;
  132. }