[ttssh2-commit] [9504] ssh_known_hosts(ファイル名)をUnicode化

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2021年 10月 30日 (土) 00:19:06 JST


Revision: 9504
          https://osdn.net/projects/ttssh2/scm/svn/commits/9504
Author:   zmatsuo
Date:     2021-10-30 00:19:06 +0900 (Sat, 30 Oct 2021)
Log Message:
-----------
ssh_known_hosts(ファイル名)をUnicode化

- HOSTSState.file_names を char から wchar_t へ変更

Modified Paths:
--------------
    trunk/ttssh2/ttxssh/hosts.c
    trunk/ttssh2/ttxssh/hosts.h

-------------- next part --------------
Modified: trunk/ttssh2/ttxssh/hosts.c
===================================================================
--- trunk/ttssh2/ttxssh/hosts.c	2021-10-29 15:18:57 UTC (rev 9503)
+++ trunk/ttssh2/ttxssh/hosts.c	2021-10-29 15:19:06 UTC (rev 9504)
@@ -41,6 +41,7 @@
 #include "dns.h"
 #include "dlglib.h"
 #include "compat_win.h"
+#include "codeconv.h"
 
 #include <openssl/bn.h>
 #include <openssl/evp.h>
@@ -53,6 +54,7 @@
 #include <sys/stat.h>
 #include <direct.h>
 #include <memory.h>
+#include <wchar.h>
 
 #include "codeconv.h"
 #include "asprintf.h"
@@ -72,12 +74,12 @@
 void HOSTS_cancel_session_after_known_hosts(PTInstVar pvar);
 
 
-static char **parse_multi_path(char *buf)
+static wchar_t **parse_multi_path(wchar_t *buf)
 {
 	int i;
-	int ch;
+	wchar_t ch;
 	int num_paths = 1;
-	char ** result;
+	wchar_t ** result;
 	int last_path_index;
 
 	for (i = 0; (ch = buf[i]) != 0; i++) {
@@ -87,7 +89,7 @@
 	}
 
 	result =
-		(char **) malloc(sizeof(char *) * (num_paths + 1));
+		(wchar_t **) malloc(sizeof(wchar_t *) * (num_paths + 1));
 
 	last_path_index = 0;
 	num_paths = 0;
@@ -94,7 +96,7 @@
 	for (i = 0; (ch = buf[i]) != 0; i++) {
 		if (ch == ';') {
 			buf[i] = 0;
-			result[num_paths] = _strdup(buf + last_path_index);
+			result[num_paths] = _wcsdup(buf + last_path_index);
 			num_paths++;
 			buf[i] = ch;
 			last_path_index = i + 1;
@@ -101,7 +103,7 @@
 		}
 	}
 	if (i > last_path_index) {
-		result[num_paths] = _strdup(buf + last_path_index);
+		result[num_paths] = _wcsdup(buf + last_path_index);
 		num_paths++;
 	}
 	result[num_paths] = NULL;
@@ -123,23 +125,26 @@
 
 void HOSTS_open(PTInstVar pvar)
 {
+	wchar_t *known_hosts_filesW = ToWcharA(pvar->session_settings.KnownHostsFiles);
 	pvar->hosts_state.file_names =
-		parse_multi_path(pvar->session_settings.KnownHostsFiles);
+		parse_multi_path(known_hosts_filesW);
+	free(known_hosts_filesW);
 }
 
 //
 // known_hosts\x83t\x83@\x83C\x83\x8B\x82̓\xE0\x97e\x82\xF0\x82\xB7\x82ׂ\xC4 pvar->hosts_state.file_data \x82֓ǂݍ\x9E\x82\xDE
 //
-static int begin_read_file(PTInstVar pvar, char *name,
+static int begin_read_file(PTInstVar pvar, wchar_t *name,
                            int suppress_errors)
 {
 	int fd;
 	int length;
 	int amount_read;
-	char buf[2048];
+	wchar_t *bufW;
 
-	get_teraterm_dir_relative_name(buf, sizeof(buf), name);
-	fd = _open(buf, _O_RDONLY | _O_SEQUENTIAL | _O_BINARY);
+	bufW = get_teraterm_dir_relative_nameW(name);
+	fd = _wopen(bufW, _O_RDONLY | _O_SEQUENTIAL | _O_BINARY);
+	free(bufW);
 	if (fd == -1) {
 		if (!suppress_errors) {
 			if (errno == ENOENT) {
@@ -560,7 +565,7 @@
 	do {
 		if (pvar->hosts_state.file_data == NULL
 		 || pvar->hosts_state.file_data[pvar->hosts_state.file_data_index] == 0) {
-			char *filename;
+			wchar_t *filename;
 			int keep_going = 1;
 
 			if (pvar->hosts_state.file_data != NULL) {
@@ -797,7 +802,7 @@
 	int success = 0;
 	int suppress_errors = 1;
 	unsigned short tcpport;
-	char *filename;
+	wchar_t *filename;
 	char *hostname;
 	Key *key;
 
@@ -1238,7 +1243,7 @@
 
 static void add_host_key(PTInstVar pvar)
 {
-	char *name = NULL;
+	wchar_t *name = NULL;
 
 	if ( pvar->hosts_state.file_names != NULL)
 		name = pvar->hosts_state.file_names[0];
@@ -1254,12 +1259,13 @@
 		int fd;
 		int amount_written;
 		int close_result;
-		char buf[FILENAME_MAX];
+		wchar_t *buf;
 
-		get_teraterm_dir_relative_name(buf, sizeof(buf), name);
-		fd = _open(buf,
+		buf = get_teraterm_dir_relative_nameW(name);
+		fd = _wopen(buf,
 		          _O_APPEND | _O_CREAT | _O_WRONLY | _O_SEQUENTIAL | _O_BINARY,
 		          _S_IREAD | _S_IWRITE);
+		free(buf);
 		if (fd == -1) {
 			if (errno == EACCES) {
 				UTIL_get_lang_msg("MSG_HOSTS_WRITE_EACCES_ERROR", pvar,
@@ -1291,7 +1297,7 @@
 // \x8Ew\x92肵\x82\xBD\x83L\x81[\x82\xF0 known_hosts \x82ɒlj\xC1\x82\xB7\x82\xE9\x81B
 void HOSTS_add_host_key(PTInstVar pvar, Key *key)
 {
-	char *name = NULL;
+	wchar_t *name = NULL;
 	char *hostname;
 	unsigned short tcpport;
 
@@ -1313,12 +1319,13 @@
 		int fd;
 		int amount_written;
 		int close_result;
-		char buf[FILENAME_MAX];
+		wchar_t *buf;
 
-		get_teraterm_dir_relative_name(buf, sizeof(buf), name);
-		fd = _open(buf,
+		buf = get_teraterm_dir_relative_nameW(name);
+		fd = _wopen(buf,
 			_O_APPEND | _O_CREAT | _O_WRONLY | _O_SEQUENTIAL | _O_BINARY,
 			_S_IREAD | _S_IWRITE);
+		free(buf);
 		if (fd == -1) {
 			if (errno == EACCES) {
 				UTIL_get_lang_msg("MSG_HOSTS_WRITE_EACCES_ERROR", pvar,
@@ -1354,7 +1361,7 @@
 //
 static void delete_different_key(PTInstVar pvar)
 {
-	char *name = pvar->hosts_state.file_names[0];
+	wchar_t *name = pvar->hosts_state.file_names[0];
 
 	if (name == NULL || name[0] == 0) {
 		UTIL_get_lang_msg("MSG_HOSTS_FILE_UNSPECIFY_ERROR", pvar,
@@ -1373,7 +1380,8 @@
 		int amount_written = 0;
 		int close_result;
 		int data_index = 0;
-		char buf[FILENAME_MAX];
+		wchar_t *buf;
+		wchar_t *filenameW;
 
 		// \x8F\x91\x82\xAB\x8D\x9E\x82݈ꎞ\x83t\x83@\x83C\x83\x8B\x82\xF0\x8AJ\x82\xAD
 #if _MSC_VER < 1900 // less than VSC2015(VC14.0)
@@ -1527,9 +1535,12 @@
 		}
 
 		// \x8F\x91\x82\xAB\x8D\x9E\x82݈ꎞ\x83t\x83@\x83C\x83\x8B\x82\xA9\x82烊\x83l\x81[\x83\x80
-		get_teraterm_dir_relative_name(buf, sizeof(buf), name);
-		_unlink(buf);
-		rename(filename, buf);
+		buf = get_teraterm_dir_relative_nameW(name);
+		_wunlink(buf);
+		filenameW = ToWcharA(filename);
+		_wrename(filenameW, buf);
+		free(buf);
+		free(filenameW);
 
 error2:
 		_unlink(filename);
@@ -1544,7 +1555,7 @@
 
 void HOSTS_delete_all_hostkeys(PTInstVar pvar)
 {
-	char *name = pvar->hosts_state.file_names[0];
+	wchar_t *name = pvar->hosts_state.file_names[0];
 	char *hostname;
 	unsigned short tcpport;
 
@@ -1568,7 +1579,8 @@
 		int amount_written = 0;
 		int close_result;
 		int data_index = 0;
-		char buf[FILENAME_MAX];
+		wchar_t *buf;
+		wchar_t *filenameW;
 
 		// \x8F\x91\x82\xAB\x8D\x9E\x82݈ꎞ\x83t\x83@\x83C\x83\x8B\x82\xF0\x8AJ\x82\xAD
 #if _MSC_VER < 1900 // less than VSC2015(VC14.0)
@@ -1720,9 +1732,12 @@
 		}
 
 		// \x8F\x91\x82\xAB\x8D\x9E\x82݈ꎞ\x83t\x83@\x83C\x83\x8B\x82\xA9\x82烊\x83l\x81[\x83\x80
-		get_teraterm_dir_relative_name(buf, sizeof(buf), name);
-		_unlink(buf);
-		rename(filename, buf);
+		buf = get_teraterm_dir_relative_nameW(name);
+		_wunlink(buf);
+		filenameW = ToWcharA(filename);
+		_wrename(filenameW, buf);
+		free(filenameW);
+		free(buf);
 
 	error2:
 		_unlink(filename);

Modified: trunk/ttssh2/ttxssh/hosts.h
===================================================================
--- trunk/ttssh2/ttxssh/hosts.h	2021-10-29 15:18:57 UTC (rev 9503)
+++ trunk/ttssh2/ttxssh/hosts.h	2021-10-29 15:19:06 UTC (rev 9504)
@@ -49,7 +49,7 @@
 #endif
 
   int file_num;
-  char **file_names;
+  wchar_t **file_names;
   int file_data_index;
   char *file_data;  // known_hosts\x83t\x83@\x83C\x83\x8B\x82̓\xE0\x97e\x82\xAA\x82\xB7\x82ׂĊi\x94[\x82\xB3\x82\xEA\x82\xE9
 


ttssh2-commit メーリングリストの案内
Back to archive index