summaryrefslogtreecommitdiff
path: root/wince/io_wce.c
blob: 613934aa66df2ef1b89a88bc1315c96b75b3ef21 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/***************************************************************
  io.c

  author : uema2
  date   : Nov 30, 2002

  You can freely use, copy, modify, and redistribute
  the whole contents.
***************************************************************/

#include <windows.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include "wince.h" /* for wce_mbtowc */

extern int _errno;


int _rename(const char *oldname, const char *newname)
{
	wchar_t *wold, *wnew;
	BOOL rc;

	wold = wce_mbtowc(oldname);
	wnew = wce_mbtowc(newname);

	/* replace with MoveFile. */
	rc = MoveFileW(wold, wnew);

	free(wold);
	free(wnew);

	return rc==TRUE ? 0 : -1;
}

int _unlink(const char *file)
{
	wchar_t *wfile;
	BOOL rc;

	/* replace with DeleteFile. */
	wfile = wce_mbtowc(file);
	rc = DeleteFileW(wfile);
	free(wfile);

	return rc==TRUE ? 0 : -1;
}

/* replace "open" with "CreateFile", etc. */
int _open(const char *file, int mode, va_list arg)
{
	wchar_t *wfile;
	DWORD access=0, share=0, create=0;
	HANDLE h;

	if( (mode&_O_RDWR) != 0 )
		access = GENERIC_READ|GENERIC_WRITE;
	else if( (mode&_O_RDONLY) != 0 )
		access = GENERIC_READ;
	else if( (mode&_O_WRONLY) != 0 )
		access = GENERIC_WRITE;

	if( (mode&_O_CREAT) != 0 )
		create = CREATE_ALWAYS;
	else
		create = OPEN_ALWAYS;

	wfile = wce_mbtowc(file);

	h = CreateFileW(wfile, access, share, NULL,
			create, 0, NULL );

	free(wfile);
	return (int)h;
}

int close(int fd)
{
	CloseHandle( (HANDLE)fd );
	return 0;
}

int _read(int fd, void *buffer, int length)
{
	DWORD dw;
	ReadFile( (HANDLE)fd, buffer, length, &dw, NULL );
	return (int)dw;
}

int _write(int fd, const void *buffer, unsigned count)
{
	DWORD dw;
	WriteFile( (HANDLE)fd, buffer, count, &dw, NULL );
	return (int)dw;
}

long _lseek(int handle, long offset, int origin)
{
	DWORD flag, ret;

	switch(origin)
	{
	case SEEK_SET: flag = FILE_BEGIN;   break;
	case SEEK_CUR: flag = FILE_CURRENT; break;
	case SEEK_END: flag = FILE_END;     break;
	default:       flag = FILE_CURRENT; break;
	}

	ret = SetFilePointer( (HANDLE)handle, offset, NULL, flag );
	return ret==0xFFFFFFFF ? -1 : 0;
}

/* _findfirst, _findnext, _findclose. */
/* replace them with FindFirstFile, etc. */
long _findfirst( char *file, struct _finddata_t *fi )
{
	HANDLE h;
	WIN32_FIND_DATAA fda;

	h = FindFirstFileA( file, &fda );
	if( h==NULL )
	{
		errno = EINVAL; return -1;
	}

	fi->attrib      = fda.dwFileAttributes;
	fi->time_create = wce_FILETIME2time_t( &fda.ftCreationTime );
	fi->time_access = wce_FILETIME2time_t( &fda.ftLastAccessTime );
	fi->time_write  = wce_FILETIME2time_t( &fda.ftLastWriteTime );
	fi->size        = fda.nFileSizeLow + (fda.nFileSizeHigh<<32);
	strcpy( fi->name, fda.cFileName );

	return (long)h;
}

int _findnext( long handle, struct _finddata_t *fi )
{
	WIN32_FIND_DATAA fda;
	BOOL b;

	b = FindNextFileA( (HANDLE)handle, &fda );

	if( b==FALSE )
	{
		errno = ENOENT; return -1;
	}

	fi->attrib      = fda.dwFileAttributes;
	fi->time_create = wce_FILETIME2time_t( &fda.ftCreationTime );
	fi->time_access = wce_FILETIME2time_t( &fda.ftLastAccessTime );
	fi->time_write  = wce_FILETIME2time_t( &fda.ftLastWriteTime );
	fi->size        = fda.nFileSizeLow + (fda.nFileSizeHigh<<32);
	strcpy( fi->name, fda.cFileName );

	return 0;
}

int _findclose( long handle )
{
	BOOL b;
	b = FindClose( (HANDLE)handle );
	return b==FALSE ? -1 : 0;
}

/* below functions unsupported... */
/* I have no idea how to replace... */
int _chsize(int handle, long size)
{
	errno = EACCES;
	return -1;
}

int _umask(int cmask)
{
	return 0;
}

int _chmod(const char *path, int mode)
{
	return 0;
}

/* WinCE doesn't have dup and dup2.  */
/* so, we cannot use missing/dup2.c. */
int dup( int handle )
{
	errno = EBADF;
	return -1;
}
/*
int dup2( int handle1, int handle2 )
{
	errno = EBADF;
	return -1;
}
*/
int _isatty(int fd)
{
	if( fd==(int)_fileno(stdin) || 
		fd==(int)_fileno(stdout)||
		fd==(int)_fileno(stderr) )
		return 1;
	else
		return 0;
}

int _pipe(int *phandles, unsigned int psize, int textmode)
{
	return -1;
}

int _access(const char *filename, int flags)
{
	return 0;
}

int _open_osfhandle( long osfhandle, int flags)
{
/*	return 0; */
	return (int)osfhandle;
}

long _get_osfhandle( int filehandle )
{
/*	return 0; */
	return (long)filehandle;
}