Index: EcnlProtoTool/trunk/ntshell/src/io_stub.c
===================================================================
--- EcnlProtoTool/trunk/ntshell/src/io_stub.c	(revision 434)
+++ EcnlProtoTool/trunk/ntshell/src/io_stub.c	(revision 441)
@@ -1,7 +1,7 @@
 /*
  *  TOPPERS PROJECT Home Network Working Group Software
- * 
+ *
  *  Copyright (C) 2017-2019 Cores Co., Ltd. Japan
- * 
+ *
  *  上記著作権者は，以下の(1)～(4)の条件を満たす場合に限り，本ソフトウェ
  *  ア（本ソフトウェアを改変したものを含む．以下同じ）を使用・複製・改
@@ -26,5 +26,5 @@
  *      由に基づく請求からも，上記著作権者およびTOPPERSプロジェクトを
  *      免責すること．
- * 
+ *
  *  本ソフトウェアは，無保証で提供されているものである．上記著作権者お
  *  よびTOPPERSプロジェクトは，本ソフトウェアに関して，特定の使用目的
@@ -32,5 +32,5 @@
  *  アの利用により直接的または間接的に生じたいかなる損害に関しても，そ
  *  の責任を負わない．
- * 
+ *
  *  @(#) $Id$
  */
@@ -42,24 +42,18 @@
 #include <t_stdlib.h>
 #include <sil.h>
-//#include <string.h>
-#include <setjmp.h>
+#include <string.h>
+#include "syssvc/serial.h"
 #include "syssvc/syslog.h"
-#include <tinet_config.h>
-#include <netinet/in.h>
-#include <netinet/in_itron.h>
-#include <tinet_nic_defs.h>
-#include <tinet_cfg.h>
-#include <netinet/in_var.h>
-#include <net/ethernet.h>
-#include <net/if6_var.h>
-#include <net/net.h>
-#include <net/if_var.h>
-#include <netinet/udp_var.h>
-//#include <ethernet_api.h>
 #include "fdtable.h"
-//#include <sys/stat.h>
 #include "util/ntstdio.h"
 #include "usrcmd.h"
 #include "core/ntlibc.h"
+#include "kernel_cfg.h"
+#include "target_syssvc.h"
+
+struct SHELL_DIR {
+	FATFS_DIR dir;
+	struct dirent dirent;
+};
 
 int fresult2errno(FRESULT res)
@@ -90,17 +84,41 @@
 }
 
-int shell_open(const char * path, int flags, void *arg)
-{
-	FRESULT res;
-	struct _IO_FILE *fp;
+static int file_close(struct SHELL_FILE *fp);
+static size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
+static size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
+static off_t file_seek(struct SHELL_FILE *fp, off_t ofs, int org);
+static int file_ioctl(struct SHELL_FILE *fp, int req, void *arg);
+static bool_t file_readable(struct SHELL_FILE *fp);
+static bool_t file_writable(struct SHELL_FILE *fp);
+static void file_delete(struct SHELL_FILE *fp);
+
+static int dir_close(struct SHELL_FILE *fp);
+static size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
+static size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
+static off_t dir_seek(struct SHELL_FILE *fp, off_t ofs, int org);
+static int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg);
+static bool_t dir_readable(struct SHELL_FILE *fp);
+static bool_t dir_writable(struct SHELL_FILE *fp);
+static void dir_delete(struct SHELL_FILE *fp);
+
+IO_TYPE IO_TYPE_FILE = { file_close, file_read, file_write, file_seek, file_ioctl, file_readable, file_writable, file_delete };
+IO_TYPE IO_TYPE_DIR = { dir_close, dir_read, dir_write, dir_seek, dir_ioctl, dir_readable, dir_writable, dir_delete };
+
+int shell_open(const char *path, int flags, void *arg)
+{
+	FRESULT res;
+	struct SHELL_FILE *fp;
 
 	if (flags & O_DIRECTORY) {
-		fp = new_dir_fd(0);
+		fp = new_fp(&IO_TYPE_DIR, 0, 0);
 		if (fp == NULL)
 			return -ENOMEM;
 
-		FATFS_DIR *dir = &fp->dir;
-		FRESULT res;
+		fp->exinf = malloc(sizeof(struct SHELL_DIR));
+		memset(fp->exinf, 0, sizeof(struct SHELL_DIR));
+
+		FATFS_DIR *dir = &((struct SHELL_DIR *)fp->exinf)->dir;
 		if ((res = f_opendir(dir, path)) != FR_OK) {
+			delete_fp(fp);
 			return fresult2errno(res);
 		}
@@ -108,7 +126,10 @@
 	}
 
-	fp = new_file_fd(0);
+	fp = new_fp(&IO_TYPE_FILE, 0, 1);
 	if (fp == NULL)
 		return -ENOMEM;
+
+	fp->exinf = malloc(sizeof(FIL));
+	memset(fp->exinf, 0, sizeof(FIL));
 
 	BYTE fmd = 0;
@@ -146,17 +167,18 @@
 	}
 
-	if ((res = f_open(&fp->file, path, fmd)) == FR_OK) {
+	if ((res = f_open((FIL *)fp->exinf, path, fmd)) == FR_OK) {
 		fp->handle = fp->fd;
 		return fp->fd;
 	}
 
+	delete_fp(fp);
 	return fresult2errno(res);
 }
 
-int file_close(struct _IO_FILE *fp)
-{
-	FRESULT res;
-
-	if ((res = f_close(&fp->file)) == FR_OK) {
+int file_close(struct SHELL_FILE *fp)
+{
+	FRESULT res;
+
+	if ((res = f_close((FIL *)fp->exinf)) == FR_OK) {
 		return 0;
 	}
@@ -165,10 +187,10 @@
 }
 
-size_t file_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
+size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
 {
 	unsigned int ret = 0;
 	FRESULT res;
 
-	if ((res = f_read(&fp->file, data, len, &ret)) != FR_OK)
+	if ((res = f_read((FIL *)fp->exinf, data, len, &ret)) != FR_OK)
 		return -EIO;
 
@@ -176,10 +198,10 @@
 }
 
-size_t file_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
+size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
 {
 	unsigned int ret = 0;
 	FRESULT res;
 
-	if ((res = f_write(&fp->file, data, len, &ret)) != FR_OK)
+	if ((res = f_write((FIL *)fp->exinf, data, len, &ret)) != FR_OK)
 		return -EIO;
 
@@ -187,5 +209,5 @@
 }
 
-off_t file_seek(struct _IO_FILE *fp, off_t ptr, int dir)
+off_t file_seek(struct SHELL_FILE *fp, off_t ptr, int dir)
 {
 	switch (dir) {
@@ -204,15 +226,15 @@
 
 	FRESULT res;
-	if ((res = f_seek(&fp->file, ptr, dir)) != FR_OK)
+	if ((res = f_seek((FIL *)fp->exinf, ptr, dir)) != FR_OK)
 		return -EIO;
 
-	return fp->file.fptr;
-}
-
-int file_ioctl(struct _IO_FILE *fp, int req, void *arg)
+	return ((FIL *)fp->exinf)->fptr;
+}
+
+int file_ioctl(struct SHELL_FILE *fp, int req, void *arg)
 {
 	DRESULT res;
 
-	if ((res = disk_ioctl(fp->file.fs->drv, req, arg) != RES_OK))
+	if ((res = disk_ioctl(((FIL *)fp->exinf)->fs->drv, req, arg) != RES_OK))
 		return -EINVAL;
 
@@ -220,78 +242,23 @@
 }
 
-int shell_close(int fd)
-{
-	struct _IO_FILE *fp = fd_to_fp(fd);
-	if (fp == NULL)
-		return -EBADF;
-
-	return fp->close(fp);
-}
-
-ssize_t shell_read(int fd, void *data, size_t len)
-{
-	struct _IO_FILE *fp = fd_to_fp(fd);
-	if (fp == NULL)
-		return -EBADF;
-
-	return fp->read(fp, (unsigned char *)data, len);
-}
-
-int shell_readv(int fd, const struct iovec *iov, int iovcnt)
-{
-	int result = 0;
-	struct _IO_FILE *fp = fd_to_fp(fd);
-	if (fp == NULL)
-		return -EBADF;
-
-	const struct iovec *end = &iov[iovcnt];
-	for (; iov < end; iov++) {
-		result += fp->read(fp, (unsigned char *)iov->iov_base, iov->iov_len);
-	}
-
-	return result;
-}
-
-ssize_t shell_write(int fd, const void *data, size_t len)
-{
-	struct _IO_FILE *fp = fd_to_fp(fd);
-	if (fp == NULL)
-		return -EBADF;
-
-	return fp->write(fp, (unsigned char *)data, len);
-}
-
-int shell_writev(int fd, const struct iovec *iov, int iovcnt)
-{
-	int result = 0;
-	struct _IO_FILE *fp = fd_to_fp(fd);
-	if (fp == NULL)
-		return -EBADF;
-
-	const struct iovec *end = &iov[iovcnt];
-	for (; iov < end; iov++) {
-		result += fp->write(fp, (unsigned char *)iov->iov_base, iov->iov_len);
-	}
-
-	return result;
-}
-
-int shell_llseek(int fd, off_t ptr, off_t *result, int dir)
-{
-	struct _IO_FILE *fp = fd_to_fp(fd);
-	if (fp == NULL)
-		return -EBADF;
-
-	off_t ret = fp->seek(fp, ptr, dir);
-	if (ret < 0)
-		return ret;
-
-	*result = ret;
-	return 0;
+bool_t file_readable(struct SHELL_FILE *fp)
+{
+	return fp->readevt_w != fp->readevt_r;
+}
+
+bool_t file_writable(struct SHELL_FILE *fp)
+{
+	return fp->writable && (fp->writeevt_w == fp->writeevt_r);
+}
+
+void file_delete(struct SHELL_FILE *fp)
+{
+	free((FIL *)fp->exinf);
+	fp->exinf = NULL;
 }
 
 int shell_fstat(int fd, struct stat * st)
 {
-	struct _IO_FILE *fp = fd_to_fp(fd);
+	struct SHELL_FILE *fp = fd_to_fp(fd);
 	if (fp == NULL)
 		return -EBADF;
@@ -305,5 +272,5 @@
 int shell_fsync(int fd)
 {
-	struct _IO_FILE *fp = fd_to_fp(fd);
+	struct SHELL_FILE *fp = fd_to_fp(fd);
 	if (fp == NULL)
 		return -EBADF;
@@ -313,10 +280,10 @@
 int shell_ftruncate(int fd, off_t length)
 {
-	struct _IO_FILE *fp = fd_to_fp(fd);
+	struct SHELL_FILE *fp = fd_to_fp(fd);
 	if (fp == NULL)
 		return -EBADF;
 
 	FRESULT res;
-	if ((res = f_truncate(&fp->file)) != FR_OK)
+	if ((res = f_truncate((FIL *)fp->exinf)) != FR_OK)
 		return fresult2errno(res);
 
@@ -329,77 +296,5 @@
 }
 
-int sio_tcgetattr(int fd, struct termios *termios)
-{
-	extern ntstdio_t ntstdio;
-
-	if (fd == STDIN_FILENO) {
-		memset(termios, 0, sizeof(*termios));
-
-		if (ntstdio.option & NTSTDIO_OPTION_LINE_ECHO) {
-			termios->c_lflag |= ECHO;
-		}
-		else {
-			termios->c_lflag &= ~ECHO;
-		}
-		if (ntstdio.option & NTSTDIO_OPTION_CANON) {
-			termios->c_lflag |= ICANON;
-		}
-		else {
-			termios->c_lflag &= ~ICANON;
-		}
-		if (ntstdio.option & NTSTDIO_OPTION_LF_CR) {
-			termios->c_iflag |= INLCR;
-		}
-		else {
-			termios->c_iflag &= ~INLCR;
-		}
-		if (ntstdio.option & NTSTDIO_OPTION_LF_CRLF) {
-			termios->c_oflag |= ONLCR;
-		}
-		else {
-			termios->c_oflag &= ~ONLCR;
-		}
-		return 0;
-	}
-	shell_abort();
-	return 0;
-}
-
-int sio_tcsetattr(int fd, int optional_actions, const struct termios *termios)
-{
-	extern ntstdio_t ntstdio;
-
-	if ((fd == STDIN_FILENO) && (optional_actions == TCSANOW)) {
-		if (termios->c_lflag & ECHO) {
-			ntstdio.option |= NTSTDIO_OPTION_LINE_ECHO;
-		}
-		else {
-			ntstdio.option &= ~NTSTDIO_OPTION_LINE_ECHO;
-		}
-		if (termios->c_lflag & ICANON) {
-			ntstdio.option |= NTSTDIO_OPTION_CANON;
-		}
-		else {
-			ntstdio.option &= ~NTSTDIO_OPTION_CANON;
-		}
-		if (termios->c_iflag & INLCR) {
-			ntstdio.option |= NTSTDIO_OPTION_LF_CR;
-		}
-		else{
-			ntstdio.option &= ~NTSTDIO_OPTION_LF_CR;
-		}
-		if (termios->c_oflag & ONLCR) {
-			ntstdio.option |= NTSTDIO_OPTION_LF_CRLF;
-		}
-		else {
-			ntstdio.option &= ~NTSTDIO_OPTION_LF_CRLF;
-		}
-		return 0;
-	}
-	shell_abort();
-	return 0;
-}
-
-int shell_stat(const char *__restrict path, struct stat *__restrict st)
+int shell_stat(const char *restrict path, struct stat *restrict st)
 {
 	FILINFO fi;
@@ -434,6 +329,6 @@
 	st->st_size = fi.fsize;
 	st->st_mtim.tv_nsec = 0;
-	st->st_mtim.tv_sec = fi.fdate + fi.ftime;
-	st->st_mode  = (S_IRUSR | S_IRGRP | S_IROTH);
+	st->st_mtim.tv_sec = (time_t)fi.fdate + (time_t)fi.ftime;
+	st->st_mode = (S_IRUSR | S_IRGRP | S_IROTH);
 	st->st_mode |= (fi.fattrib & AM_RDO) ? 0 : (S_IWUSR | S_IWGRP | S_IWOTH);
 	st->st_mode |= (fi.fattrib & (AM_DIR | AM_VOL)) ? S_IFDIR : S_IFREG;
@@ -442,5 +337,5 @@
 }
 
-int shell_lstat(const char *__restrict path, struct stat *__restrict st)
+int shell_lstat(const char *restrict path, struct stat *restrict st)
 {
 	return shell_stat(path, st);
@@ -494,6 +389,6 @@
 	BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID
 
-	if(mode & S_IREAD) {
-		if((mode & S_IWRITE) == 0) {
+	if (mode & S_IREAD) {
+		if ((mode & S_IWRITE) == 0) {
 			attr |= AM_RDO;
 		}
@@ -503,5 +398,5 @@
 	}
 
-	if((res = f_chmod(path, attr, mask)) != FR_OK) {
+	if ((res = f_chmod(path, attr, mask)) != FR_OK) {
 		return fresult2errno(res);
 	}
@@ -516,6 +411,6 @@
 	BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID
 
-	if(mode & S_IREAD) {
-		if((mode & S_IWRITE) == 0) {
+	if (mode & S_IREAD) {
+		if ((mode & S_IWRITE) == 0) {
 			attr |= AM_RDO;
 		}
@@ -525,5 +420,5 @@
 	}
 
-	if((res = f_chmod(path, attr, mask)) != FR_OK) {
+	if ((res = f_chmod(path, attr, mask)) != FR_OK) {
 		return fresult2errno(res);
 	}
@@ -535,5 +430,5 @@
 {
 	FRESULT ret;
-	if((ret = f_getcwd(buf, size)) != FR_OK) {
+	if ((ret = f_getcwd(buf, size)) != FR_OK) {
 		return NULL;
 	}
@@ -558,8 +453,8 @@
 }
 
-int dir_close(struct _IO_FILE *fp)
-{
-	FRESULT res;
-	if ((res = f_closedir(&fp->dir)) != FR_OK) {
+int dir_close(struct SHELL_FILE *fp)
+{
+	FRESULT res;
+	if ((res = f_closedir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) {
 		return fresult2errno(res);
 	}
@@ -573,5 +468,5 @@
 		return -EINVAL;
 
-	struct _IO_FILE *fp = fd_to_fp(fd);
+	struct SHELL_FILE *fp = fd_to_fp(fd);
 	if (fp == NULL)
 		return -EBADF;
@@ -584,5 +479,5 @@
 #endif
 	FRESULT res;
-	if ((res = f_readdir(&fp->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
+	if ((res = f_readdir(&((struct SHELL_DIR *)fp->exinf)->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
 		return fresult2errno(res);
 	}
@@ -598,15 +493,15 @@
 }
 
-size_t dir_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
+size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
 {
 	return -EPERM;
 }
 
-size_t dir_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
+size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
 {
 	return -EPERM;
 }
 
-off_t dir_seek(struct _IO_FILE *fp, off_t ptr, int dir)
+off_t dir_seek(struct SHELL_FILE *fp, off_t ptr, int dir)
 {
 	FRESULT res;
@@ -616,5 +511,5 @@
 
 	if (ptr == 0) {
-		if ((res = f_rewinddir(&fp->dir)) != FR_OK) {
+		if ((res = f_rewinddir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) {
 			return fresult2errno(res);
 		}
@@ -627,10 +522,10 @@
 		fno.lfsize = sizeof lfn;
 #endif
-		if ((res = f_rewinddir(&fp->dir)) != FR_OK) {
+		if ((res = f_rewinddir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) {
 			return fresult2errno(res);
 		}
 
 		for (int i = 0; i < ptr; i++) {
-			if ((res = f_readdir(&fp->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
+			if ((res = f_readdir(&((struct SHELL_DIR *)fp->exinf)->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
 				return fresult2errno(res);
 			}
@@ -641,7 +536,23 @@
 }
 
-int dir_ioctl(struct _IO_FILE *fp, int req, void *arg)
+int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg)
 {
 	return -EINVAL;
+}
+
+bool_t dir_readable(struct SHELL_FILE *fp)
+{
+	return fp->readevt_w != fp->readevt_r;
+}
+
+bool_t dir_writable(struct SHELL_FILE *fp)
+{
+	return false;
+}
+
+void dir_delete(struct SHELL_FILE *fp)
+{
+	free((struct SHELL_DIR *)fp->exinf);
+	fp->exinf = NULL;
 }
 
@@ -662,38 +573,2 @@
 	return 0;
 }
-
-//#include "../musl-1.1.18/include/bits/limits.h"
-#define PAGE_SIZE 4096
-
-uint32_t  __CmdBase;
-uint32_t  __CmdLimit;
-
-void *shell_brk(void *addr)
-{
-	if (addr == 0) {
-		return (void *)((intptr_t)&__CmdBase + 0x20000);
-	}
-	if ((addr >= (intptr_t)&__CmdBase + 0x20000) && (addr < &__CmdLimit)) {
-		return addr;
-	}
-	return (void *)-1;
-}
-
-void *shell_mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
-{
-	if (fd != -1)
-		return -EINVAL;
-
-	if ((length >= 0) && (length <= (intptr_t)&__CmdLimit - (intptr_t)&__CmdBase - 0x20000)) {
-		return &__CmdBase + 0x20000;
-	}
-	return (void *)-1;
-}
-
-int shell_mprotect(void *addr, size_t len, int prot)
-{
-	//if ((addr >= (intptr_t)&__CmdBase + 0x20000) && ((intptr_t)addr + len < &__CmdLimit)) {
-		return 0;
-	//}
-	//return -1;
-}
