filename.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Implementation of Filename for Windows.
  3. */
  4. #include "putty.h"
  5. #ifdef MPEXT
  6. #include <assert.h>
  7. #endif
  8. Filename *filename_from_str(const char *str)
  9. {
  10. Filename *ret = snew(Filename);
  11. ret->path = dupstr(str);
  12. return ret;
  13. }
  14. Filename *filename_copy(const Filename *fn)
  15. {
  16. return filename_from_str(fn->path);
  17. }
  18. #ifdef WINSCP
  19. const char* in_memory_key_data(const Filename *fn)
  20. {
  21. const char* result = fn->path;
  22. if (result[0] != '@')
  23. {
  24. result = NULL;
  25. }
  26. else
  27. {
  28. int len;
  29. result++;
  30. len = strlen(result);
  31. if (((len % 2) != 0) ||
  32. ((len / 2) < MAX_PATH))
  33. {
  34. result = NULL;
  35. }
  36. else
  37. {
  38. int i;
  39. for (i = 0; (result != NULL) && (i < len); i++)
  40. {
  41. if (!isxdigit(result[i]))
  42. {
  43. result = NULL;
  44. }
  45. }
  46. }
  47. }
  48. return result;
  49. }
  50. #endif
  51. const char *filename_to_str(const Filename *fn)
  52. {
  53. #ifdef WINSCP
  54. if (in_memory_key_data(fn) != NULL) return "in-memory";
  55. #endif
  56. return fn->path;
  57. }
  58. bool filename_equal(const Filename *f1, const Filename *f2)
  59. {
  60. return !strcmp(f1->path, f2->path);
  61. }
  62. bool filename_is_null(const Filename *fn)
  63. {
  64. return !*fn->path;
  65. }
  66. void filename_free(Filename *fn)
  67. {
  68. sfree(fn->path);
  69. sfree(fn);
  70. }
  71. void filename_serialise(BinarySink *bs, const Filename *f)
  72. {
  73. put_asciz(bs, f->path);
  74. }
  75. Filename *filename_deserialise(BinarySource *src)
  76. {
  77. return filename_from_str(get_asciz(src));
  78. }
  79. char filename_char_sanitise(char c)
  80. {
  81. if (strchr("<>:\"/\\|?*", c))
  82. return '.';
  83. return c;
  84. }
  85. #ifdef WINSCP
  86. FILE * mp_wfopen(const char *filename, const char *mode)
  87. {
  88. size_t len = strlen(filename);
  89. wchar_t * wfilename = snewn(len * 10, wchar_t);
  90. size_t wlen = MultiByteToWideChar(CP_UTF8, 0, filename, -1, wfilename, len * 10);
  91. FILE * file;
  92. if (wlen <= 0)
  93. {
  94. file = NULL;
  95. }
  96. else
  97. {
  98. wchar_t wmode[3];
  99. memset(wmode, 0, sizeof(wmode));
  100. wmode[0] = (wchar_t)mode[0];
  101. if (mode[0] != '\0')
  102. {
  103. wmode[1] = (wchar_t)mode[1];
  104. if (mode[1] != '\0')
  105. {
  106. assert(mode[2] == '\0');
  107. }
  108. }
  109. file = _wfopen(wfilename, wmode);
  110. }
  111. sfree(wfilename);
  112. return file;
  113. }
  114. #endif