summaryrefslogtreecommitdiff
path: root/legacy/evil/src/lib/evil_stdio.c
blob: 78ddc17a562934a1b1958ce34562246c09e30a68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include "Evil.h"
#include "evil_private.h"

#undef fopen


#if defined (_WIN32_WCE) && ! defined (__CEGCC__)

/*
 * Error related functions
 *
 */

void perror (const char *s __UNUSED__)
{
   fprintf(stderr, "[Windows CE] error\n");
}

/*
 * Stream related functions
 *
 */

FILE *evil_fopen(const char *path, const char *mode)
{
   FILE *f;
   char *filename;
   char *tmp;

   if (!path || !*path)
     return NULL;

   if ((*path != '\\') && (*path != '/'))
     {
        char buf[PATH_MAX];
        int   l1;
        int   l2;

        if (!evil_getcwd(buf, PATH_MAX))
          return NULL;

        l1 = strlen(buf);
        l2 = strlen(path);
        filename = (char *)malloc(l1 + 1 + l2 + 1);
        if (!filename)
          return NULL;
        memcpy(filename, buf, l1);
        filename[l1] = '\\';
        memcpy(filename + l1 + 1, path, l2);
        filename[l1 + 1 + l2] = '\0';
     }
   else
     filename = _strdup(path);

   tmp = filename;
   while (*tmp)
     {
        if (*tmp == '/')
          *tmp = '\\';
        tmp++;
     }

   printf ("fopen : %s\n", filename);

   f = fopen(filename, mode);
   free(filename);

   return f;
}

void evil_rewind(FILE *stream)
{
   fseek(stream, 0, SEEK_SET);
}


#endif /* _WIN32_WCE && ! __CEGCC__ */


#ifdef _WIN32_WCE

FILE *evil_fopen_native(const char *path, const char *mode)
{
   HANDLE   handle;
   char    *filename;
   char    *tmp;
   wchar_t *wfilename;
   DWORD    access = GENERIC_READ;
   DWORD    creation;

   if (!path || !*path || !mode || !*mode)
     return NULL;

   if ((*path != '\\') && (*path != '/'))
     {
        char buf[PATH_MAX];
        int   l1;
        int   l2;

        if (!evil_getcwd(buf, PATH_MAX))
          return NULL;

        l1 = strlen(buf);
        l2 = strlen(path);
        filename = (char *)malloc(l1 + 1 + l2 + 1);
        if (!filename)
          return NULL;
        memcpy(filename, buf, l1);
        filename[l1] = '\\';
        memcpy(filename + l1 + 1, path, l2);
        filename[l1 + 1 + l2] = '\0';
     }
   else
     filename = _strdup(path);

   tmp = filename;
   while (*tmp)
     {
        if (*tmp == '/')
          *tmp = '\\';
        tmp++;
     }
   printf ("fopen native : %s\n", filename);

   wfilename = evil_char_to_wchar(filename);
   free(filename);

   if (!wfilename)
     return NULL;

   if (*mode == 'r')
     {
        access = GENERIC_READ;
        creation = OPEN_EXISTING;
     }
   if (*mode == 'w')
     {
        access = GENERIC_WRITE;
        creation = CREATE_ALWAYS;
     }

   handle = CreateFile(wfilename,
                       access,
                       0, NULL,
                       creation,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);
   free(wfilename);

   if (handle == INVALID_HANDLE_VALUE)
     {
        _evil_last_error_display(__FUNCTION__);
        return NULL;
     }

   return (FILE *)handle;
}

size_t evil_fread_native(void* buffer, size_t size, size_t count, FILE* stream)
{
   HANDLE handle;
   DWORD  bytes_read;
   BOOL   res;

   if ((size == 0) || (count == 0))
     return 0;

   handle = (HANDLE)stream;
   res = ReadFile(handle, buffer, size * count, &bytes_read, NULL);
   if (!res)
     {
        _evil_last_error_display(__FUNCTION__);
        return 0;
     }

   return (bytes_read != size * count) ? 0 : 1;
}

int evil_fclose_native(FILE *stream)
{
   if (!CloseHandle((HANDLE)stream))
     {
        _evil_last_error_display(__FUNCTION__);
        return -1;
     }

   return 0;
}

#endif /* _WIN32_WCE */