source: EcnlProtoTool/trunk/ntshell/src/io_stub.c@ 279

Last change on this file since 279 was 279, checked in by coas-nagasima, 7 years ago

ファイルを追加、更新。

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 8.1 KB
Line 
1#include <stdint.h>
2#include <stdio.h>
3#include <sys/unistd.h>
4#include <limits.h>
5#include <fcntl.h>
6#include "ff.h"
7#include <kernel.h>
8#include <t_syslog.h>
9#include <t_stdlib.h>
10#include <sil.h>
11#include <stdlib.h>
12#include <string.h>
13#include <stdio.h>
14#include <setjmp.h>
15#include "syssvc/syslog.h"
16#include <tinet_config.h>
17#include <netinet/in.h>
18#include <netinet/in_itron.h>
19#include <tinet_nic_defs.h>
20#include <tinet_cfg.h>
21#include <netinet/in_var.h>
22#include <net/ethernet.h>
23#include <net/if6_var.h>
24#include <net/net.h>
25#include <net/if_var.h>
26#include <netinet/udp_var.h>
27#include <ethernet_api.h>
28#include "ff.h"
29#include "socket_stub.h"
30#include "../../../musl-1.1.12/include/_dirent.h"
31#include "../../../musl-1.1.12/include/_termios.h"
32#include "ntstdio.h"
33
34int shell_open(const char * path, int flags)
35{
36 FRESULT res;
37
38 struct _IO_FILE *fp = new_file_fd(0);
39 if (fp == NULL)
40 return -1;
41
42 BYTE fmd = 0;
43 switch (flags & O_ACCMODE) {
44 case O_RDONLY:
45 fmd = FA_READ;
46 break;
47 case O_WRONLY:
48 fmd = FA_WRITE;
49 break;
50 default:
51 fmd = FA_READ | FA_WRITE;
52 break;
53 }
54 /* ファイルを作成 */
55 if (flags & O_CREAT) {
56 /* 既存の内
57容は消す */
58 if (flags & O_TRUNC) {
59 fmd |= FA_CREATE_ALWAYS;
60 }
61 /* 新規作成の保障 */
62 else if (flags & O_EXCL) {
63 fmd |= FA_CREATE_NEW;
64 }
65 else {
66 fmd |= FA_OPEN_ALWAYS;
67 }
68 }
69 /* ある場合は開く */
70 else {
71 /* 既存の内
72容は消す */
73 if (flags & O_TRUNC) {
74 fmd |= FA_CREATE_ALWAYS;
75 }
76 }
77
78 if ((res = f_open(&fp->file, path, fmd)) == FR_OK) {
79 fp->handle = fp->fd;
80 return fp->fd;
81 }
82
83 return -1;
84}
85
86int file_close(struct _IO_FILE *fp)
87{
88 FRESULT res;
89
90 if ((res = f_close(&fp->file)) == FR_OK) {
91 return 0;
92 }
93
94 return -1;
95}
96
97size_t file_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
98{
99 unsigned int ret = 0;
100 FRESULT res;
101
102 if ((res = f_read(&fp->file, data, len, &ret)) != FR_OK)
103 return -1;
104
105 return ret;
106}
107
108size_t file_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
109{
110 unsigned int ret = 0;
111 FRESULT res;
112
113 if ((res = f_write(&fp->file, data, len, &ret)) != FR_OK)
114 return -1;
115
116 return ret;
117}
118
119int shell_close(int fd)
120{
121 struct _IO_FILE *fp = fd_to_fp(fd);
122 if (fp == NULL)
123 return -1;
124
125 return fp->close(fp);
126}
127
128int shell_read(int fd, char *data, int len)
129{
130 struct _IO_FILE *fp = fd_to_fp(fd);
131 if (fp == NULL)
132 return -1;
133
134 return fp->read(fp, (unsigned char *)data, len);
135}
136
137int shell_write(int fd, char *data, int len)
138{
139 struct _IO_FILE *fp = fd_to_fp(fd);
140 if (fp == NULL)
141 return -1;
142
143 return fp->write(fp, (unsigned char *)data, len);
144}
145
146int shell_lseek(int fd, int ptr, int dir)
147{
148 struct _IO_FILE *fp = fd_to_fp(fd);
149 if (fp == NULL)
150 return -1;
151
152 FRESULT res;
153 if ((res = f_seek(&fp->file, ptr, dir)) != FR_OK)
154 return -1;
155
156 return fp->file.fptr;
157}
158
159int shell_fstat(int fd, struct stat * st)
160{
161 struct _IO_FILE *fp = fd_to_fp(fd);
162 if (fp == NULL)
163 return -1;
164
165 memset(st, 0, sizeof(*st));
166 st->st_mode = S_IFCHR;
167 //st->st_blksize = 1024;
168 return 0;
169}
170
171int fsync(int fd)
172{
173 struct _IO_FILE *fp = fd_to_fp(fd);
174 if (fp == NULL)
175 return -1;
176 return -1;
177}
178
179int ftruncate(int fd, off_t length)
180{
181 struct _IO_FILE *fp = fd_to_fp(fd);
182 if (fp == NULL)
183 return -1;
184
185 FRESULT res;
186 if ((res = f_truncate(&fp->file)) != FR_OK)
187 return -1;
188
189 return 0;
190}
191
192int ioctl(int fd, int request, va_list ap)
193{
194 struct _IO_FILE *fp = fd_to_fp(fd);
195 if (fp == NULL)
196 return -1;
197 return -1;
198}
199
200int tcgetattr(int fd, struct termios *termios)
201{
202 extern ntstdio_t ntstdio;
203
204 if (fd == STDIN_FILENO) {
205 memset(termios, 0, sizeof(*termios));
206
207 if (ntstdio.option & NTSTDIO_OPTION_LINE_ECHO) {
208 termios->c_lflag |= ECHO;
209 }
210 else {
211 termios->c_lflag &= ~ECHO;
212 }
213 if (ntstdio.option & NTSTDIO_OPTION_CANON) {
214 termios->c_lflag |= ICANON;
215 }
216 else {
217 termios->c_lflag &= ~ICANON;
218 }
219 if (ntstdio.option & NTSTDIO_OPTION_LF_CR) {
220 termios->c_iflag |= INLCR;
221 }
222 else {
223 termios->c_iflag &= ~INLCR;
224 }
225 if (ntstdio.option & NTSTDIO_OPTION_LF_CRLF) {
226 termios->c_oflag |= ONLCR;
227 }
228 else {
229 termios->c_oflag &= ~ONLCR;
230 }
231 return 0;
232 }
233 abort();
234 return 0;
235}
236
237int tcsetattr(int fd, int optional_actions, const struct termios *termios)
238{
239 extern ntstdio_t ntstdio;
240
241 if ((fd == STDIN_FILENO) && (optional_actions == TCSANOW)) {
242 if (termios->c_lflag & ECHO) {
243 ntstdio.option |= NTSTDIO_OPTION_LINE_ECHO;
244 }
245 else {
246 ntstdio.option &= ~NTSTDIO_OPTION_LINE_ECHO;
247 }
248 if (termios->c_lflag & ICANON) {
249 ntstdio.option |= NTSTDIO_OPTION_CANON;
250 }
251 else {
252 ntstdio.option &= ~NTSTDIO_OPTION_CANON;
253 }
254 if (termios->c_iflag & INLCR) {
255 ntstdio.option |= NTSTDIO_OPTION_LF_CR;
256 }
257 else{
258 ntstdio.option &= ~NTSTDIO_OPTION_LF_CR;
259 }
260 if (termios->c_oflag & ONLCR) {
261 ntstdio.option |= NTSTDIO_OPTION_LF_CRLF;
262 }
263 else {
264 ntstdio.option &= ~NTSTDIO_OPTION_LF_CRLF;
265 }
266 return 0;
267 }
268 abort();
269 return 0;
270}
271
272int shell_stat(const char *path, struct stat *st)
273{
274 FILINFO fi;
275 FRESULT ret;
276#if _USE_LFN
277 static char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */
278 fi.lfname = lfn;
279 fi.lfsize = sizeof lfn;
280#endif
281 if (strcmp(path, ".") == 0) {
282 char cwd[_MAX_LFN];
283 if ((ret = f_getcwd(cwd, sizeof(cwd))) != FR_OK) {
284 return -1;
285 }
286 int l = strlen(cwd);
287 // ルートディレクトリの場合
288 if (cwd[l - 2] == ':' && cwd[l - 1] == '/') {
289 st->st_size = 0;
290 st->st_mtime = 0;
291 st->st_mode = S_IFDIR;
292 return 0;
293 }
294 if ((ret = f_stat(cwd, &fi)) != FR_OK) {
295 return -1;
296 }
297 }
298 else if ((ret = f_stat(path, &fi)) != FR_OK) {
299 return -1;
300 }
301
302 st->st_size = fi.fsize;
303 st->st_mtime = fi.fdate + fi.ftime;
304 st->st_mode = (S_IRUSR | S_IRGRP | S_IROTH);
305 st->st_mode |= (fi.fattrib & AM_RDO) ? 0 : (S_IWUSR | S_IWGRP | S_IWOTH);
306 st->st_mode |= (fi.fattrib & (AM_DIR | AM_VOL)) ? S_IFDIR : S_IFREG;
307
308 return 0;
309}
310
311int shell_link(void)
312{
313 return -1;
314}
315
316int shell_unlink(const char *path)
317{
318 FRESULT res;
319
320 if ((res = f_unlink(path)) != FR_OK)
321 return -1;
322
323 return 0;
324}
325
326int rmdir(const char *path)
327{
328 FRESULT res;
329
330 if ((res = f_unlink(path)) != FR_OK)
331 return -1;
332
333 return 0;
334}
335
336int shell_rename(const char *oldpath, const char *newpath)
337{
338 FRESULT res;
339
340 if ((res = f_rename(oldpath, newpath)) != FR_OK)
341 return -1;
342 return 0;
343}
344
345int mkdir(const char *path, mode_t mode)
346{
347 FRESULT res;
348
349 if ((res = f_mkdir(path)) != FR_OK)
350 return -1;
351
352 BYTE attr = 0;
353 BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID
354
355 if(mode & S_IREAD) {
356 if((mode & S_IWRITE) == 0) {
357 attr |= AM_RDO;
358 }
359 }
360 else {
361 attr |= AM_SYS;
362 }
363
364 if((res = f_chmod(path, attr, mask)) != FR_OK) {
365 return -1;
366 }
367
368 return 0;
369}
370
371int chmod(const char *path, mode_t mode)
372{
373 FRESULT ret;
374 BYTE attr = 0;
375 BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID
376
377 if(mode & S_IREAD) {
378 if((mode & S_IWRITE) == 0) {
379 attr |= AM_RDO;
380 }
381 }
382 else {
383 attr |= AM_SYS;
384 }
385
386 if((ret = f_chmod(path, attr, mask)) != FR_OK) {
387 return -1;
388 }
389
390 return 0;
391}
392
393char *getcwd(char *buf, size_t size)
394{
395 FRESULT ret;
396 if((ret = f_getcwd(buf, size)) != FR_OK) {
397 return NULL;
398 }
399
400 return buf;
401}
402
403int chdir(const char *path)
404{
405 FRESULT ret;
406 if ((ret = f_chdir(path)) != FR_OK) {
407 return -1;
408 }
409
410 return 0;
411}
412
413int chroot(const char *path)
414{
415 abort();
416 return -1;
417}
418
419DIR *opendir(const char *path)
420{
421 DIR *dir = malloc(sizeof(DIR) + sizeof(struct dirent));
422 FRESULT ret;
423 if ((ret = f_opendir(dir, path)) != FR_OK) {
424 free(dir);
425 return NULL;
426 }
427
428 dir->dirent = &dir[1];
429 return dir;
430}
431
432int closedir(DIR *dir)
433{
434 FRESULT ret;
435 if ((ret = f_closedir(dir)) != FR_OK) {
436 free(dir);
437 return -1;
438 }
439
440 free(dir);
441 return 0;
442}
443
444struct dirent *readdir(DIR *dir)
445{
446 struct dirent *de = dir->dirent;
447 FILINFO fno;
448#if _USE_LFN
449 static char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */
450 fno.lfname = lfn;
451 fno.lfsize = sizeof lfn;
452#endif
453 FRESULT ret;
454 if ((ret = f_readdir(dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
455 return NULL;
456 }
457
458 memset(de, 0, sizeof(*de));
459#if _USE_LFN
460 strlcpy(de->d_name, *fno.lfname ? fno.lfname : fno.fname, sizeof(de->d_name));
461#else
462 strlcpy(de->d_name, fno.fname, sizeof(de->d_name));
463#endif
464
465 return de;
466}
467
468void rewinddir(DIR *dir)
469{
470 FRESULT ret;
471 if ((ret = f_rewinddir(dir)) != FR_OK) {
472 return;
473 }
474}
475
476void seekdir(DIR *dir, long pos)
477{
478 abort();
479}
480
481long telldir(DIR *dir)
482{
483 abort();
484 return 0;
485}
486
487int shell_getpid(int n)
488{
489 return 1;
490}
Note: See TracBrowser for help on using the repository browser.