Fabcoin Core  0.16.2
P2P Digital Currency
io_win32.c
Go to the documentation of this file.
1 /*
2  This file is part of ethash.
3 
4  ethash is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  ethash is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with ethash. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "io.h"
23 #include <direct.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <shlobj.h>
29 
30 FILE* ethash_fopen(char const* file_name, char const* mode)
31 {
32  FILE* f;
33  return fopen_s(&f, file_name, mode) == 0 ? f : NULL;
34 }
35 
36 char* ethash_strncat(char* dest, size_t dest_size, char const* src, size_t count)
37 {
38  return strncat_s(dest, dest_size, src, count) == 0 ? dest : NULL;
39 }
40 
41 bool ethash_mkdir(char const* dirname)
42 {
43  int rc = _mkdir(dirname);
44  return rc != -1 || errno == EEXIST;
45 }
46 
47 int ethash_fileno(FILE* f)
48 {
49  return _fileno(f);
50 }
51 
53  char const* dirname,
54  char const* filename,
55  size_t filename_length
56 )
57 {
58  size_t dirlen = strlen(dirname);
59  size_t dest_size = dirlen + filename_length + 1;
60  if (dirname[dirlen] != '\\' || dirname[dirlen] != '/') {
61  dest_size += 1;
62  }
63  char* name = malloc(dest_size);
64  if (!name) {
65  return NULL;
66  }
67 
68  name[0] = '\0';
69  ethash_strncat(name, dest_size, dirname, dirlen);
70  if (dirname[dirlen] != '\\' || dirname[dirlen] != '/') {
71  ethash_strncat(name, dest_size, "\\", 1);
72  }
73  ethash_strncat(name, dest_size, filename, filename_length);
74  return name;
75 }
76 
77 bool ethash_file_size(FILE* f, size_t* ret_size)
78 {
79  struct _stat st;
80  int fd;
81  if ((fd = _fileno(f)) == -1 || _fstat(fd, &st) != 0) {
82  return false;
83  }
84  *ret_size = st.st_size;
85  return true;
86 }
87 
88 bool ethash_get_default_dirname(char* strbuf, size_t buffsize)
89 {
90  static const char dir_suffix[] = "Ethash\\";
91  strbuf[0] = '\0';
92  if (!SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, (CHAR*)strbuf))) {
93  return false;
94  }
95  if (!ethash_strncat(strbuf, buffsize, "\\", 1)) {
96  return false;
97  }
98 
99  return ethash_strncat(strbuf, buffsize, dir_suffix, sizeof(dir_suffix));
100 }
size_t count
Definition: ExecStats.cpp:37
evm_mode mode
Definition: SmartVM.cpp:47
bool ethash_file_size(FILE *f, size_t *ret_size)
Get a file&#39;s size.
Definition: io_win32.c:77
int ethash_fileno(FILE *f)
Get a file descriptor number from a FILE stream.
Definition: io_win32.c:47
char * ethash_io_create_filename(char const *dirname, char const *filename, size_t filename_length)
Create the filename for the DAG.
Definition: io_win32.c:52
const char * name
Definition: rest.cpp:36
FILE * ethash_fopen(char const *file_name, char const *mode)
An fopen wrapper for no-warnings crossplatform fopen.
Definition: io_win32.c:30
bool ethash_mkdir(char const *dirname)
A cross-platform mkdir wrapper to create a directory or assert it&#39;s there.
Definition: io_win32.c:41
char * ethash_strncat(char *dest, size_t dest_size, char const *src, size_t count)
An strncat wrapper for no-warnings crossplatform strncat.
Definition: io_win32.c:36
#define f(x)
Definition: gost.cpp:57
#define fd(x)
Definition: rijndael.cpp:172
bool ethash_get_default_dirname(char *strbuf, size_t buffsize)
Gets the default directory name for the DAG depending on the system.
Definition: io_win32.c:88