udpate rdoc.
[ruby-svn.git] / wince / io_wce.c
blob613934aa66df2ef1b89a88bc1315c96b75b3ef21
1 /***************************************************************
2 io.c
4 author : uema2
5 date : Nov 30, 2002
7 You can freely use, copy, modify, and redistribute
8 the whole contents.
9 ***************************************************************/
11 #include <windows.h>
12 #include <stdlib.h>
13 #include <io.h>
14 #include <fcntl.h>
15 #include <time.h>
16 #include <errno.h>
17 #include "wince.h" /* for wce_mbtowc */
19 extern int _errno;
22 int _rename(const char *oldname, const char *newname)
24 wchar_t *wold, *wnew;
25 BOOL rc;
27 wold = wce_mbtowc(oldname);
28 wnew = wce_mbtowc(newname);
30 /* replace with MoveFile. */
31 rc = MoveFileW(wold, wnew);
33 free(wold);
34 free(wnew);
36 return rc==TRUE ? 0 : -1;
39 int _unlink(const char *file)
41 wchar_t *wfile;
42 BOOL rc;
44 /* replace with DeleteFile. */
45 wfile = wce_mbtowc(file);
46 rc = DeleteFileW(wfile);
47 free(wfile);
49 return rc==TRUE ? 0 : -1;
52 /* replace "open" with "CreateFile", etc. */
53 int _open(const char *file, int mode, va_list arg)
55 wchar_t *wfile;
56 DWORD access=0, share=0, create=0;
57 HANDLE h;
59 if( (mode&_O_RDWR) != 0 )
60 access = GENERIC_READ|GENERIC_WRITE;
61 else if( (mode&_O_RDONLY) != 0 )
62 access = GENERIC_READ;
63 else if( (mode&_O_WRONLY) != 0 )
64 access = GENERIC_WRITE;
66 if( (mode&_O_CREAT) != 0 )
67 create = CREATE_ALWAYS;
68 else
69 create = OPEN_ALWAYS;
71 wfile = wce_mbtowc(file);
73 h = CreateFileW(wfile, access, share, NULL,
74 create, 0, NULL );
76 free(wfile);
77 return (int)h;
80 int close(int fd)
82 CloseHandle( (HANDLE)fd );
83 return 0;
86 int _read(int fd, void *buffer, int length)
88 DWORD dw;
89 ReadFile( (HANDLE)fd, buffer, length, &dw, NULL );
90 return (int)dw;
93 int _write(int fd, const void *buffer, unsigned count)
95 DWORD dw;
96 WriteFile( (HANDLE)fd, buffer, count, &dw, NULL );
97 return (int)dw;
100 long _lseek(int handle, long offset, int origin)
102 DWORD flag, ret;
104 switch(origin)
106 case SEEK_SET: flag = FILE_BEGIN; break;
107 case SEEK_CUR: flag = FILE_CURRENT; break;
108 case SEEK_END: flag = FILE_END; break;
109 default: flag = FILE_CURRENT; break;
112 ret = SetFilePointer( (HANDLE)handle, offset, NULL, flag );
113 return ret==0xFFFFFFFF ? -1 : 0;
116 /* _findfirst, _findnext, _findclose. */
117 /* replace them with FindFirstFile, etc. */
118 long _findfirst( char *file, struct _finddata_t *fi )
120 HANDLE h;
121 WIN32_FIND_DATAA fda;
123 h = FindFirstFileA( file, &fda );
124 if( h==NULL )
126 errno = EINVAL; return -1;
129 fi->attrib = fda.dwFileAttributes;
130 fi->time_create = wce_FILETIME2time_t( &fda.ftCreationTime );
131 fi->time_access = wce_FILETIME2time_t( &fda.ftLastAccessTime );
132 fi->time_write = wce_FILETIME2time_t( &fda.ftLastWriteTime );
133 fi->size = fda.nFileSizeLow + (fda.nFileSizeHigh<<32);
134 strcpy( fi->name, fda.cFileName );
136 return (long)h;
139 int _findnext( long handle, struct _finddata_t *fi )
141 WIN32_FIND_DATAA fda;
142 BOOL b;
144 b = FindNextFileA( (HANDLE)handle, &fda );
146 if( b==FALSE )
148 errno = ENOENT; return -1;
151 fi->attrib = fda.dwFileAttributes;
152 fi->time_create = wce_FILETIME2time_t( &fda.ftCreationTime );
153 fi->time_access = wce_FILETIME2time_t( &fda.ftLastAccessTime );
154 fi->time_write = wce_FILETIME2time_t( &fda.ftLastWriteTime );
155 fi->size = fda.nFileSizeLow + (fda.nFileSizeHigh<<32);
156 strcpy( fi->name, fda.cFileName );
158 return 0;
161 int _findclose( long handle )
163 BOOL b;
164 b = FindClose( (HANDLE)handle );
165 return b==FALSE ? -1 : 0;
168 /* below functions unsupported... */
169 /* I have no idea how to replace... */
170 int _chsize(int handle, long size)
172 errno = EACCES;
173 return -1;
176 int _umask(int cmask)
178 return 0;
181 int _chmod(const char *path, int mode)
183 return 0;
186 /* WinCE doesn't have dup and dup2. */
187 /* so, we cannot use missing/dup2.c. */
188 int dup( int handle )
190 errno = EBADF;
191 return -1;
194 int dup2( int handle1, int handle2 )
196 errno = EBADF;
197 return -1;
200 int _isatty(int fd)
202 if( fd==(int)_fileno(stdin) ||
203 fd==(int)_fileno(stdout)||
204 fd==(int)_fileno(stderr) )
205 return 1;
206 else
207 return 0;
210 int _pipe(int *phandles, unsigned int psize, int textmode)
212 return -1;
215 int _access(const char *filename, int flags)
217 return 0;
220 int _open_osfhandle( long osfhandle, int flags)
222 /* return 0; */
223 return (int)osfhandle;
226 long _get_osfhandle( int filehandle )
228 /* return 0; */
229 return (long)filehandle;