io_win32.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. This file is part of ethash.
  3. ethash is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. ethash is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with ethash. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file io_win32.c
  15. * @author Lefteris Karapetsas <lefteris@ethdev.com>
  16. * @date 2015
  17. */
  18. #include "io.h"
  19. #include <direct.h>
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <shlobj.h>
  25. FILE* ethash_fopen(char const* file_name, char const* mode)
  26. {
  27. FILE* f;
  28. return fopen_s(&f, file_name, mode) == 0 ? f : NULL;
  29. }
  30. char* ethash_strncat(char* dest, size_t dest_size, char const* src, size_t count)
  31. {
  32. return strncat_s(dest, dest_size, src, count) == 0 ? dest : NULL;
  33. }
  34. bool ethash_mkdir(char const* dirname)
  35. {
  36. int rc = _mkdir(dirname);
  37. return rc != -1 || errno == EEXIST;
  38. }
  39. int ethash_fileno(FILE* f)
  40. {
  41. return _fileno(f);
  42. }
  43. char* ethash_io_create_filename(
  44. char const* dirname,
  45. char const* filename,
  46. size_t filename_length
  47. )
  48. {
  49. size_t dirlen = strlen(dirname);
  50. size_t dest_size = dirlen + filename_length + 1;
  51. if (dirname[dirlen] != '\\' || dirname[dirlen] != '/') {
  52. dest_size += 1;
  53. }
  54. char* name = malloc(dest_size);
  55. if (!name) {
  56. return NULL;
  57. }
  58. name[0] = '\0';
  59. ethash_strncat(name, dest_size, dirname, dirlen);
  60. if (dirname[dirlen] != '\\' || dirname[dirlen] != '/') {
  61. ethash_strncat(name, dest_size, "\\", 1);
  62. }
  63. ethash_strncat(name, dest_size, filename, filename_length);
  64. return name;
  65. }
  66. bool ethash_file_size(FILE* f, size_t* ret_size)
  67. {
  68. struct _stat st;
  69. int fd;
  70. if ((fd = _fileno(f)) == -1 || _fstat(fd, &st) != 0) {
  71. return false;
  72. }
  73. *ret_size = st.st_size;
  74. return true;
  75. }
  76. bool ethash_get_default_dirname(char* strbuf, size_t buffsize)
  77. {
  78. static const char dir_suffix[] = "Ethash\\";
  79. strbuf[0] = '\0';
  80. if (!SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, (CHAR*)strbuf))) {
  81. return false;
  82. }
  83. if (!ethash_strncat(strbuf, buffsize, "\\", 1)) {
  84. return false;
  85. }
  86. return ethash_strncat(strbuf, buffsize, dir_suffix, sizeof(dir_suffix));
  87. }