OSDN Git Service

Add support for MLSD responses from some broken hosts.
[ffftp/ffftp.git] / remote.c
1 /*=============================================================================\r
2 *\r
3 *                                                       リモート側のファイル操作\r
4 *\r
5 ===============================================================================\r
6 / Copyright (C) 1997-2007 Sota. All rights reserved.\r
7 /\r
8 / Redistribution and use in source and binary forms, with or without \r
9 / modification, are permitted provided that the following conditions \r
10 / are met:\r
11 /\r
12 /  1. Redistributions of source code must retain the above copyright \r
13 /     notice, this list of conditions and the following disclaimer.\r
14 /  2. Redistributions in binary form must reproduce the above copyright \r
15 /     notice, this list of conditions and the following disclaimer in the \r
16 /     documentation and/or other materials provided with the distribution.\r
17 /\r
18 / THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR \r
19 / IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES \r
20 / OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
21 / IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, \r
22 / INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, \r
23 / BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
24 / USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
25 / ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
26 / (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF \r
27 / THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 /============================================================================*/\r
29 \r
30 /* このソースは一部、WS_FTP Version 93.12.05 のソースを参考にしました。 */\r
31 \r
32 #define STRICT\r
33 #include <stdio.h>\r
34 #include <stdlib.h>\r
35 #include <stdarg.h>\r
36 #include <string.h>\r
37 #include <mbstring.h>\r
38 #include <time.h>\r
39 // IPv6対応\r
40 //#include <winsock.h>\r
41 #include <winsock2.h>\r
42 #include <windowsx.h>\r
43 #include <commctrl.h>\r
44 \r
45 #include "common.h"\r
46 #include "resource.h"\r
47 \r
48 #define PWD_XPWD                0\r
49 #define PWD_PWD                 1\r
50 \r
51 /*===== プロトタイプ =====*/\r
52 \r
53 static int DoPWD(char *Buf);\r
54 static int ReadOneLine(SOCKET cSkt, char *Buf, int Max, int *CancelCheckWork);\r
55 static int DoDirList(HWND hWnd, SOCKET cSkt, char *AddOpt, char *Path, int Num, int *CancelCheckWork);\r
56 static void ChangeSepaLocal2Remote(char *Fname);\r
57 static void ChangeSepaRemote2Local(char *Fname);\r
58 \r
59 /*===== 外部参照 =====*/\r
60 \r
61 extern TRANSPACKET MainTransPkt;\r
62 \r
63 /* 設定値 */\r
64 extern int TimeOut;\r
65 extern int SendQuit;\r
66 \r
67 // 同時接続対応\r
68 extern int CancelFlg;\r
69 \r
70 /*===== ローカルなワーク =====*/\r
71 \r
72 static int PwdCommandType;\r
73 \r
74 // 同時接続対応\r
75 //static int CheckCancelFlg = NO;\r
76 \r
77 \r
78 \r
79 /*----- リモート側のカレントディレクトリ変更 ----------------------------------\r
80 *\r
81 *       Parameter\r
82 *               char *Path : パス名\r
83 *               int Disp : ディレクトリリストにパス名を表示するかどうか(YES/NO)\r
84 *               int ForceGet : 失敗してもカレントディレクトリを取得する\r
85 *               int ErrorBell : エラー事の音を鳴らすかどうか(YES/NO)\r
86 *\r
87 *       Return Value\r
88 *               int 応答コードの1桁目\r
89 *----------------------------------------------------------------------------*/\r
90 \r
91 int DoCWD(char *Path, int Disp, int ForceGet, int ErrorBell)\r
92 {\r
93         int Sts;\r
94         char Buf[FMAX_PATH+1];\r
95 \r
96         Sts = FTP_COMPLETE * 100;\r
97 \r
98         if(strcmp(Path, "..") == 0)\r
99                 Sts = CommandProcCmd(NULL, "CDUP");\r
100         else if(strcmp(Path, "") != 0)\r
101         {\r
102                 if((AskHostType() != HTYPE_VMS) || (strchr(Path, '[') != NULL))\r
103                         Sts = CommandProcCmd(NULL, "CWD %s", Path);\r
104                 else\r
105                         Sts = CommandProcCmd(NULL, "CWD [.%s]", Path);  /* VMS用 */\r
106         }\r
107 \r
108         if((Sts/100 >= FTP_CONTINUE) && (ErrorBell == YES))\r
109                 SoundPlay(SND_ERROR);\r
110 \r
111         if((Sts/100 == FTP_COMPLETE) ||\r
112            (ForceGet == YES))\r
113         {\r
114                 if(Disp == YES)\r
115                 {\r
116                         if(DoPWD(Buf) != FTP_COMPLETE)\r
117                         {\r
118                                 /*===== PWDが使えなかった場合 =====*/\r
119 \r
120                                 if(*Path == '/')\r
121                                         strcpy(Buf, Path);\r
122                                 else\r
123                                 {\r
124                                         AskRemoteCurDir(Buf, FMAX_PATH);\r
125                                         if(strlen(Buf) == 0)\r
126                                                 strcpy(Buf, "/");\r
127 \r
128                                         while(*Path != NUL)\r
129                                         {\r
130                                                 if(strcmp(Path, ".") == 0)\r
131                                                         Path++;\r
132                                                 else if(strncmp(Path, "./", 2) == 0)\r
133                                                         Path += 2;\r
134                                                 else if(strcmp(Path, "..") == 0)\r
135                                                 {\r
136                                                         GetUpperDir(Buf);\r
137                                                         Path += 2;\r
138                                                 }\r
139                                                 else if(strncmp(Path, "../", 2) == 0)\r
140                                                 {\r
141                                                         GetUpperDir(Buf);\r
142                                                         Path += 3;\r
143                                                 }\r
144                                                 else\r
145                                                 {\r
146                                                         SetSlashTail(Buf);\r
147                                                         strcat(Buf, Path);\r
148                                                         break;\r
149                                                 }\r
150                                         }\r
151                                 }\r
152                         }\r
153                         SetRemoteDirHist(Buf);\r
154                 }\r
155         }\r
156         return(Sts/100);\r
157 }\r
158 \r
159 \r
160 \r
161 \r
162 /*----- リモート側のカレントディレクトリ変更(その2)-------------------------\r
163 *\r
164 *       Parameter\r
165 *               char *Path : パス名\r
166 *               char *Cur : カレントディレクトリ\r
167 *\r
168 *       Return Value\r
169 *               int 応答コードの1桁目\r
170 *\r
171 *       Note\r
172 *               パス名は "xxx/yyy/zzz" の形式\r
173 *               ディレクトリ変更が失敗したら、カレントディレクトリに戻しておく\r
174 *----------------------------------------------------------------------------*/\r
175 \r
176 int DoCWDStepByStep(char *Path, char *Cur)\r
177 {\r
178         int Sts;\r
179         char *Set;\r
180         char *Set2;\r
181         char Tmp[FMAX_PATH+2];\r
182 \r
183         Sts = FTP_COMPLETE;\r
184 \r
185         memset(Tmp, NUL, FMAX_PATH+2);\r
186         strcpy(Tmp, Path);\r
187         Set = Tmp;\r
188         while(*Set != NUL)\r
189         {\r
190                 if((Set2 = strchr(Set, '/')) != NULL)\r
191                         *Set2 = NUL;\r
192                 if((Sts = DoCWD(Set, NO, NO, NO)) != FTP_COMPLETE)\r
193                         break;\r
194                 if(Set2 == NULL)\r
195                         break;\r
196                 Set = Set2 + 1;\r
197         }\r
198 \r
199         if(Sts != FTP_COMPLETE)\r
200                 DoCWD(Cur, NO, NO, NO);\r
201 \r
202         return(Sts);\r
203 }\r
204 \r
205 \r
206 /*----- リモート側のカレントディレクトリ取得 ----------------------------------\r
207 *\r
208 *       Parameter\r
209 *               char *Buf : パス名を返すバッファ\r
210 *\r
211 *       Return Value\r
212 *               int 応答コードの1桁目\r
213 *----------------------------------------------------------------------------*/\r
214 \r
215 static int DoPWD(char *Buf)\r
216 {\r
217         char *Pos;\r
218         char Tmp[1024];\r
219         int Sts;\r
220 \r
221         if(PwdCommandType == PWD_XPWD)\r
222         {\r
223                 Sts = CommandProcCmd(Tmp, "XPWD");\r
224                 if(Sts/100 != FTP_COMPLETE)\r
225                         PwdCommandType = PWD_PWD;\r
226         }\r
227         if(PwdCommandType == PWD_PWD)\r
228                 Sts = CommandProcCmd(Tmp, "PWD");\r
229 \r
230         if(Sts/100 == FTP_COMPLETE)\r
231         {\r
232                 if((Pos = strchr(Tmp, '"')) != NULL)\r
233                 {\r
234                         memmove(Tmp, Pos+1, strlen(Pos+1)+1);\r
235                         if((Pos = strchr(Tmp, '"')) != NULL)\r
236                                 *Pos = NUL;\r
237                 }\r
238                 else\r
239                         memmove(Tmp, Tmp+4, strlen(Tmp+4)+1);\r
240 \r
241                 if(strlen(Tmp) < FMAX_PATH)\r
242                 {\r
243                         strcpy(Buf, Tmp);\r
244                         ReplaceAll(Buf, '\\', '/');\r
245                         ChangeSepaRemote2Local(Buf);\r
246                 }\r
247                 else\r
248                         Sts = FTP_ERROR*100;\r
249         }\r
250         return(Sts/100);\r
251 }\r
252 \r
253 \r
254 /*----- PWDコマンドのタイプを初期化する ---------------------------------------\r
255 *\r
256 *       Parameter\r
257 *               なし\r
258 *\r
259 *       Return Value\r
260 *               なし\r
261 *----------------------------------------------------------------------------*/\r
262 \r
263 void InitPWDcommand()\r
264 {\r
265         PwdCommandType = PWD_XPWD;\r
266 }\r
267 \r
268 \r
269 /*----- リモート側のディレクトリ作成 ----------------------------------------\r
270 *\r
271 *       Parameter\r
272 *               char *Path : パス名\r
273 *\r
274 *       Return Value\r
275 *               int 応答コードの1桁目\r
276 *----------------------------------------------------------------------------*/\r
277 \r
278 int DoMKD(char *Path)\r
279 {\r
280         int Sts;\r
281 \r
282         Sts = CommandProcCmd(NULL, "MKD %s", Path);\r
283 \r
284         if(Sts/100 >= FTP_CONTINUE)\r
285                 SoundPlay(SND_ERROR);\r
286 \r
287         return(Sts/100);\r
288 }\r
289 \r
290 \r
291 /*----- リモート側のディレクトリ削除 ------------------------------------------\r
292 *\r
293 *       Parameter\r
294 *               char *Path : パス名\r
295 *\r
296 *       Return Value\r
297 *               int 応答コードの1桁目\r
298 *----------------------------------------------------------------------------*/\r
299 \r
300 int DoRMD(char *Path)\r
301 {\r
302         int Sts;\r
303 \r
304         Sts = CommandProcCmd(NULL, "RMD %s", Path);\r
305 \r
306         if(Sts/100 >= FTP_CONTINUE)\r
307                 SoundPlay(SND_ERROR);\r
308 \r
309         return(Sts/100);\r
310 }\r
311 \r
312 \r
313 /*----- リモート側のファイル削除 ----------------------------------------------\r
314 *\r
315 *       Parameter\r
316 *               char *Path : パス名\r
317 *\r
318 *       Return Value\r
319 *               int 応答コードの1桁目\r
320 *----------------------------------------------------------------------------*/\r
321 \r
322 int DoDELE(char *Path)\r
323 {\r
324         int Sts;\r
325 \r
326         Sts = CommandProcCmd(NULL, "DELE %s", Path);\r
327 \r
328         if(Sts/100 >= FTP_CONTINUE)\r
329                 SoundPlay(SND_ERROR);\r
330 \r
331         return(Sts/100);\r
332 }\r
333 \r
334 \r
335 /*----- リモート側のファイル名変更 --------------------------------------------\r
336 *\r
337 *       Parameter\r
338 *               char *Src : 元ファイル名\r
339 *               char *Dst : 変更後のファイル名\r
340 *\r
341 *       Return Value\r
342 *               int 応答コードの1桁目\r
343 *----------------------------------------------------------------------------*/\r
344 \r
345 int DoRENAME(char *Src, char *Dst)\r
346 {\r
347         int Sts;\r
348 \r
349         Sts = CommandProcCmd(NULL, "RNFR %s", Src);\r
350         if(Sts == 350)\r
351                 // 同時接続対応\r
352 //              Sts = command(AskCmdCtrlSkt(), NULL, &CheckCancelFlg, "RNTO %s", Dst);\r
353                 Sts = command(AskCmdCtrlSkt(), NULL, &CancelFlg, "RNTO %s", Dst);\r
354 \r
355         if(Sts/100 >= FTP_CONTINUE)\r
356                 SoundPlay(SND_ERROR);\r
357 \r
358         return(Sts/100);\r
359 }\r
360 \r
361 \r
362 /*----- リモート側のファイルの属性変更 ----------------------------------------\r
363 *\r
364 *       Parameter\r
365 *               char *Path : パス名\r
366 *               char *Mode : モード文字列\r
367 *\r
368 *       Return Value\r
369 *               int 応答コードの1桁目\r
370 *----------------------------------------------------------------------------*/\r
371 \r
372 int DoCHMOD(char *Path, char *Mode)\r
373 {\r
374         int Sts;\r
375 \r
376         Sts = CommandProcCmd(NULL, "%s %s %s", AskHostChmodCmd(), Mode, Path);\r
377 \r
378         if(Sts/100 >= FTP_CONTINUE)\r
379                 SoundPlay(SND_ERROR);\r
380 \r
381         return(Sts/100);\r
382 }\r
383 \r
384 \r
385 /*----- リモート側のファイルのサイズを取得(転送ソケット使用)-----------------\r
386 *\r
387 *       Parameter\r
388 *               char *Path : パス名\r
389 *               LONGLONG *Size : ファイルのサイズを返すワーク\r
390 *\r
391 *       Return Value\r
392 *               int 応答コードの1桁目\r
393 *\r
394 *       Note\r
395 *               ★★転送ソケットを使用する★★\r
396 *               サイズが選られない時は Size = -1 を返す\r
397 *----------------------------------------------------------------------------*/\r
398 \r
399 // 同時接続対応\r
400 //int DoSIZE(char *Path, LONGLONG *Size)\r
401 int DoSIZE(SOCKET cSkt, char *Path, LONGLONG *Size, int *CancelCheckWork)\r
402 {\r
403         int Sts;\r
404         char Tmp[1024];\r
405 \r
406         // 同時接続対応\r
407 //      Sts = CommandProcTrn(Tmp, "SIZE %s", Path);\r
408         Sts = CommandProcTrn(cSkt, Tmp, CancelCheckWork, "SIZE %s", Path);\r
409 \r
410         *Size = -1;\r
411         if((Sts/100 == FTP_COMPLETE) && (strlen(Tmp) > 4) && IsDigit(Tmp[4]))\r
412                 *Size = _atoi64(&Tmp[4]);\r
413 \r
414         return(Sts/100);\r
415 }\r
416 \r
417 \r
418 /*----- リモート側のファイルの日付を取得(転送ソケット使用)-------------------\r
419 *\r
420 *       Parameter\r
421 *               char *Path : パス名\r
422 *               FILETIME *Time : 日付を返すワーク\r
423 *\r
424 *       Return Value\r
425 *               int 応答コードの1桁目\r
426 *\r
427 *       Note\r
428 *               ★★転送ソケットを使用する★★\r
429 *               日付が選られない時は Time = 0 を返す\r
430 *----------------------------------------------------------------------------*/\r
431 \r
432 // 同時接続対応\r
433 //int DoMDTM(char *Path, FILETIME *Time)\r
434 int DoMDTM(SOCKET cSkt, char *Path, FILETIME *Time, int *CancelCheckWork)\r
435 {\r
436         int Sts;\r
437         char Tmp[1024];\r
438         SYSTEMTIME sTime;\r
439 \r
440     Time->dwLowDateTime = 0;\r
441     Time->dwHighDateTime = 0;\r
442 \r
443         // 同時接続対応\r
444 //      Sts = CommandProcTrn(Tmp, "MDTM %s", Path);\r
445         Sts = CommandProcTrn(cSkt, Tmp, CancelCheckWork, "MDTM %s", Path);\r
446         if(Sts/100 == FTP_COMPLETE)\r
447         {\r
448                 sTime.wMilliseconds = 0;\r
449                 if(sscanf(Tmp+4, "%04d%02d%02d%02d%02d%02d",\r
450                         &sTime.wYear, &sTime.wMonth, &sTime.wDay,\r
451                         &sTime.wHour, &sTime.wMinute, &sTime.wSecond) == 6)\r
452                 {\r
453                         SystemTimeToFileTime(&sTime, Time);\r
454                         SpecificLocalFileTime2FileTime(Time, AskHostTimeZone());\r
455 \r
456                 }\r
457         }\r
458         return(Sts/100);\r
459 }\r
460 \r
461 \r
462 /*----- リモート側のコマンドを実行 --------------------------------------------\r
463 *\r
464 *       Parameter\r
465 *               char *CmdStr : コマンド文字列\r
466 *\r
467 *       Return Value\r
468 *               int 応答コードの1桁目\r
469 *----------------------------------------------------------------------------*/\r
470 \r
471 int DoQUOTE(char *CmdStr)\r
472 {\r
473         int Sts;\r
474 \r
475         Sts = CommandProcCmd(NULL, "%s", CmdStr);\r
476 \r
477         if(Sts/100 >= FTP_CONTINUE)\r
478                 SoundPlay(SND_ERROR);\r
479 \r
480         return(Sts/100);\r
481 }\r
482 \r
483 \r
484 /*----- ソケットを閉じる ------------------------------------------------------\r
485 *\r
486 *       Parameter\r
487 *               なし\r
488 *\r
489 *       Return Value\r
490 *               SOCKET 閉じた後のソケット\r
491 *----------------------------------------------------------------------------*/\r
492 \r
493 SOCKET DoClose(SOCKET Sock)\r
494 {\r
495         if(Sock != INVALID_SOCKET)\r
496         {\r
497 //              if(WSAIsBlocking())\r
498 //              {\r
499 //                      DoPrintf("Skt=%u : Cancelled blocking call", Sock);\r
500 //                      WSACancelBlockingCall();\r
501 //              }\r
502                 do_closesocket(Sock);\r
503                 DoPrintf("Skt=%u : Socket closed.", Sock);\r
504                 Sock = INVALID_SOCKET;\r
505         }\r
506         if(Sock != INVALID_SOCKET)\r
507                 DoPrintf("Skt=%u : Failed to close socket.", Sock);\r
508 \r
509         return(Sock);\r
510 }\r
511 \r
512 \r
513 /*----- ホストからログアウトする ----------------------------------------------\r
514 *\r
515 *       Parameter\r
516 *               kSOCKET ctrl_skt : ソケット\r
517 *\r
518 *       Return Value\r
519 *               int 応答コードの1桁目\r
520 *----------------------------------------------------------------------------*/\r
521 \r
522 int DoQUIT(SOCKET ctrl_skt)\r
523 {\r
524         int Ret;\r
525 \r
526         Ret = FTP_COMPLETE;\r
527         if(SendQuit == YES)\r
528                 // 同時接続対応\r
529 //              Ret = command(ctrl_skt, NULL, &CheckCancelFlg, "QUIT") / 100;\r
530                 Ret = command(ctrl_skt, NULL, &CancelFlg, "QUIT") / 100;\r
531 \r
532         return(Ret);\r
533 }\r
534 \r
535 \r
536 /*----- リモート側のディレクトリリストを取得(コマンドコントロールソケットを使用)\r
537 *\r
538 *       Parameter\r
539 *               char *AddOpt : 追加のオプション\r
540 *               char *Path : パス名\r
541 *               int Num : ファイル名番号\r
542 *\r
543 *       Return Value\r
544 *               int 応答コードの1桁目\r
545 *----------------------------------------------------------------------------*/\r
546 \r
547 int DoDirListCmdSkt(char *AddOpt, char *Path, int Num, int *CancelCheckWork)\r
548 {\r
549         int Sts;\r
550 \r
551         if(AskTransferNow() == YES)\r
552                 SktShareProh();\r
553 \r
554 //      if((Sts = DoDirList(NULL, AskCmdCtrlSkt(), AddOpt, Path, Num)) == 429)\r
555 //      {\r
556 //              ReConnectCmdSkt();\r
557                 Sts = DoDirList(NULL, AskCmdCtrlSkt(), AddOpt, Path, Num, CancelCheckWork);\r
558 \r
559                 if(Sts/100 >= FTP_CONTINUE)\r
560                         SoundPlay(SND_ERROR);\r
561 //      }\r
562         return(Sts/100);\r
563 }\r
564 \r
565 \r
566 /*----- リモート側のディレクトリリストを取得 ----------------------------------\r
567 *\r
568 *       Parameter\r
569 *               HWND hWnd : 転送中ダイアログのウインドウハンドル\r
570 *               SOCKET cSkt : コントロールソケット\r
571 *               char *AddOpt : 追加のオプション\r
572 *               char *Path : パス名 (""=カレントディレクトリ)\r
573 *               int Num : ファイル名番号\r
574 *\r
575 *       Return Value\r
576 *               int 応答コード\r
577 *----------------------------------------------------------------------------*/\r
578 \r
579 static int DoDirList(HWND hWnd, SOCKET cSkt, char *AddOpt, char *Path, int Num, int *CancelCheckWork)\r
580 {\r
581         char Tmp[FMAX_PATH];\r
582         int Sts;\r
583 \r
584 //#pragma aaa\r
585 //DoPrintf("===== DoDirList %d = %s", Num, Path);\r
586 \r
587         MakeCacheFileName(Num, Tmp);\r
588 //      MainTransPkt.ctrl_skt = cSkt;\r
589 \r
590         if(AskListCmdMode() == NO)\r
591         {\r
592                 strcpy(MainTransPkt.Cmd, "NLST");\r
593                 if(strlen(AskHostLsName()) > 0)\r
594                 {\r
595                         strcat(MainTransPkt.Cmd, " ");\r
596                         if((AskHostType() == HTYPE_ACOS) || (AskHostType() == HTYPE_ACOS_4))\r
597                                 strcat(MainTransPkt.Cmd, "'");\r
598                         strcat(MainTransPkt.Cmd, AskHostLsName());\r
599                         if((AskHostType() == HTYPE_ACOS) || (AskHostType() == HTYPE_ACOS_4))\r
600                                 strcat(MainTransPkt.Cmd, "'");\r
601                 }\r
602                 if(strlen(AddOpt) > 0)\r
603                         strcat(MainTransPkt.Cmd, AddOpt);\r
604         }\r
605         else\r
606         {\r
607                 // MLSD対応\r
608 //              strcpy(MainTransPkt.Cmd, "LIST");\r
609                 if(AskUseMLSD() && (AskHostFeature() & FEATURE_MLSD))\r
610                         strcpy(MainTransPkt.Cmd, "MLSD");\r
611                 else\r
612                         strcpy(MainTransPkt.Cmd, "LIST");\r
613                 if(strlen(AddOpt) > 0)\r
614                 {\r
615                         strcat(MainTransPkt.Cmd, " -");\r
616                         strcat(MainTransPkt.Cmd, AddOpt);\r
617                 }\r
618         }\r
619 \r
620         if(strlen(Path) > 0)\r
621                 strcat(MainTransPkt.Cmd, " ");\r
622 \r
623         strcpy(MainTransPkt.RemoteFile, Path);\r
624         strcpy(MainTransPkt.LocalFile, Tmp);\r
625         MainTransPkt.Type = TYPE_A;\r
626         MainTransPkt.Size = -1;\r
627         /* ファイルリストの中の漢字のファイル名は、別途   */\r
628         /* ChangeFnameRemote2Local で変換する                      */\r
629         MainTransPkt.KanjiCode = KANJI_NOCNV;\r
630         MainTransPkt.KanaCnv = YES;\r
631         MainTransPkt.Mode = EXIST_OVW;\r
632         MainTransPkt.ExistSize = 0;\r
633         MainTransPkt.hWndTrans = hWnd;\r
634         MainTransPkt.Next = NULL;\r
635 \r
636         Sts = DoDownLoad(cSkt, &MainTransPkt, YES, CancelCheckWork);\r
637 \r
638 //#pragma aaa\r
639 //DoPrintf("===== DoDirList Done.");\r
640 \r
641         return(Sts);\r
642 }\r
643 \r
644 \r
645 /*----- リモート側へコマンドを送りリプライを待つ(コマンドソケット)-----------\r
646 *\r
647 *       Parameter\r
648 *               char *Reply : リプライのコピー先 (NULL=コピーしない)\r
649 *               char *fmt : フォーマット文字列\r
650 *               ... : パラメータ\r
651 *\r
652 *       Return Value\r
653 *               int 応答コード\r
654 *\r
655 *       Note\r
656 *               コマンドコントロールソケットを使う\r
657 *----------------------------------------------------------------------------*/\r
658 \r
659 int CommandProcCmd(char *Reply, char *fmt, ...)\r
660 {\r
661         va_list Args;\r
662         char Cmd[1024];\r
663         int Sts;\r
664 \r
665         va_start(Args, fmt);\r
666         wvsprintf(Cmd, fmt, Args);\r
667         va_end(Args);\r
668 \r
669         if(AskTransferNow() == YES)\r
670                 SktShareProh();\r
671 \r
672 //#pragma aaa\r
673 //DoPrintf("**CommandProcCmd : %s", Cmd);\r
674 \r
675 //      if((Sts = command(AskCmdCtrlSkt(), Reply, "%s", Cmd)) == 429)\r
676 //      {\r
677 //              if(ReConnectCmdSkt() == FFFTP_SUCCESS)\r
678 //              {\r
679                         // 同時接続対応\r
680 //                      Sts = command(AskCmdCtrlSkt(), Reply, &CheckCancelFlg, "%s", Cmd);\r
681                         Sts = command(AskCmdCtrlSkt(), Reply, &CancelFlg, "%s", Cmd);\r
682 //              }\r
683 //      }\r
684         return(Sts);\r
685 }\r
686 \r
687 \r
688 #if defined(HAVE_TANDEM)\r
689 /*----- OSS/Guardian ファイルシステムを切り替えるコマンドを送る ---------------\r
690 *\r
691 *       Parameter\r
692 *               なし\r
693 *\r
694 *       Return Value\r
695 *               なし\r
696 *----------------------------------------------------------------------------*/\r
697 \r
698 void SwitchOSSProc(void)\r
699 {\r
700         char Buf[MAX_PATH+1];\r
701 \r
702         /* DoPWD でノード名の \ を保存するために OSSフラグも変更する */\r
703         if(AskOSS() == YES) {\r
704                 DoQUOTE("GUARDIAN");\r
705                 SetOSS(NO);\r
706         } else {\r
707                 DoQUOTE("OSS");\r
708                 SetOSS(YES);\r
709         }\r
710         /* Current Dir 再取得 */\r
711         if (DoPWD(Buf) == FTP_COMPLETE)\r
712                 SetRemoteDirHist(Buf);\r
713         /* ファイルリスト再読み込み */\r
714         PostMessage(GetMainHwnd(), WM_COMMAND, MAKEWPARAM(REFRESH_REMOTE, 0), 0);\r
715 \r
716         return;\r
717 }\r
718 #endif\r
719 \r
720 \r
721 /*----- リモート側へコマンドを送りリプライを待つ(転送ソケット)---------------\r
722 *\r
723 *       Parameter\r
724 *               char *Reply : リプライのコピー先 (NULL=コピーしない)\r
725 *               char *fmt : フォーマット文字列\r
726 *               ... : パラメータ\r
727 *\r
728 *       Return Value\r
729 *               int 応答コード\r
730 *\r
731 *       Note\r
732 *               転送コントロールソケットを使う\r
733 *----------------------------------------------------------------------------*/\r
734 \r
735 // 同時接続対応\r
736 //int CommandProcTrn(char *Reply, char *fmt, ...)\r
737 int CommandProcTrn(SOCKET cSkt, char *Reply, int* CancelCheckWork, char *fmt, ...)\r
738 {\r
739         va_list Args;\r
740         char Cmd[1024];\r
741         int Sts;\r
742 \r
743         va_start(Args, fmt);\r
744         wvsprintf(Cmd, fmt, Args);\r
745         va_end(Args);\r
746 \r
747 //#pragma aaa\r
748 //DoPrintf("**CommandProcTrn : %s", Cmd);\r
749 \r
750 //      if((Sts = command(AskTrnCtrlSkt(), Reply, "%s", Cmd)) == 429)\r
751 //      {\r
752 //              if(ReConnectTrnSkt() == FFFTP_SUCCESS)\r
753 //                      Sts = command(AskTrnCtrlSkt(), Reply, &CheckCancelFlg, "%s", Cmd);\r
754                         Sts = command(cSkt, Reply, CancelCheckWork, "%s", Cmd);\r
755 //      }\r
756         return(Sts);\r
757 }\r
758 \r
759 \r
760 /*----- コマンドを送りリプライを待つ ------------------------------------------\r
761 *\r
762 *       Parameter\r
763 *               SOCKET cSkt : コントロールソケット\r
764 *               char *Reply : リプライのコピー先 (NULL=コピーしない)\r
765 *               char *fmt : フォーマット文字列\r
766 *               ... : パラメータ\r
767 *\r
768 *       Return Value\r
769 *               int 応答コード\r
770 *\r
771 *       Note\r
772 *               ホストのファイル名の漢字コードに応じて、ここで漢字コードの変換を行なう\r
773 *----------------------------------------------------------------------------*/\r
774 \r
775 //#pragma aaa\r
776 //static int cntcnt = 0;\r
777 \r
778 // SFTP対応\r
779 int ConvertFTPCommandToPuTTYSFTP(SOCKET cSkt, char *Reply, int *CancelCheckWork, char *Cmd)\r
780 {\r
781         // TODO:\r
782         // 未実装\r
783         int Sts;\r
784         char NewCmd[FMAX_PATH*2];\r
785         static char RenameFrom[FMAX_PATH+1];\r
786         Sts = 429;\r
787         Reply[0] = '\0';\r
788         if(strcmp(Cmd, "QUIT") == 0)\r
789         {\r
790                 sprintf(NewCmd, "ls\r\n");\r
791                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
792         }\r
793         if(strcmp(Cmd, "LIST") == 0)\r
794         {\r
795                 sprintf(NewCmd, "ls\r\n");\r
796                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
797         }\r
798         else if(strncmp(Cmd, "REST ", 5) == 0)\r
799         {\r
800                 SFTP_SetFilePosition(cSkt, (LONGLONG)_strtoi64(&Cmd[5], NULL, 10));\r
801         }\r
802         else if(strncmp(Cmd, "RETR ", 5) == 0)\r
803         {\r
804                 sprintf(NewCmd, "get \"%s\"\r\n", &Cmd[5]);\r
805                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
806         }\r
807         else if(strncmp(Cmd, "STOR ", 5) == 0)\r
808         {\r
809                 sprintf(NewCmd, "put \"%s\"\r\n", &Cmd[5]);\r
810                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
811         }\r
812         else if(strncmp(Cmd, "APPE ", 5) == 0)\r
813         {\r
814                 sprintf(NewCmd, "reput \"%s\"\r\n", &Cmd[5]);\r
815                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
816         }\r
817         else if(strncmp(Cmd, "DELE ", 5) == 0)\r
818         {\r
819                 sprintf(NewCmd, "rm \"%s\"\r\n", &Cmd[5]);\r
820                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
821         }\r
822         else if(strncmp(Cmd, "CWD ", 4) == 0)\r
823         {\r
824                 sprintf(NewCmd, "cd \"%s\"\r\n", &Cmd[4]);\r
825                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
826         }\r
827         else if(strcmp(Cmd, "PWD") == 0)\r
828         {\r
829                 sprintf(NewCmd, "pwd\r\n");\r
830                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
831         }\r
832         else if(strcmp(Cmd, "XPWD") == 0)\r
833         {\r
834                 sprintf(NewCmd, "pwd\r\n");\r
835                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
836         }\r
837         else if(strncmp(Cmd, "MKD ", 4) == 0)\r
838         {\r
839                 sprintf(NewCmd, "mkdir \"%s\"\r\n", &Cmd[4]);\r
840                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
841         }\r
842         else if(strncmp(Cmd, "RMD ", 4) == 0)\r
843         {\r
844                 sprintf(NewCmd, "rmdir \"%s\"\r\n", &Cmd[4]);\r
845                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
846         }\r
847         else if(strncmp(Cmd, "RNFR ", 5) == 0)\r
848         {\r
849                 strcpy(RenameFrom, &Cmd[5]);\r
850         }\r
851         else if(strncmp(Cmd, "RNTO ", 5) == 0)\r
852         {\r
853                 sprintf(NewCmd, "mv \"%s\" \"%s\"\r\n", RenameFrom, &Cmd[5]);\r
854                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
855         }\r
856         else if(strncmp(Cmd, "SITE CHMOD ", 11) == 0)\r
857         {\r
858                 Cmd[14] = '\0';\r
859                 sprintf(NewCmd, "chmod %s \"%s\"\r\n", &Cmd[11], &Cmd[15]);\r
860                 SFTP_send(cSkt, NewCmd, strlen(NewCmd), 0);\r
861         }\r
862         return Sts;\r
863 }\r
864 \r
865 int command(SOCKET cSkt, char *Reply, int *CancelCheckWork, char *fmt, ...)\r
866 {\r
867         va_list Args;\r
868         char Cmd[FMAX_PATH*2];\r
869         int Sts;\r
870         char TmpBuf[ONELINE_BUF_SIZE];\r
871 \r
872         if(cSkt != INVALID_SOCKET)\r
873         {\r
874                 va_start(Args, fmt);\r
875                 wvsprintf(Cmd, fmt, Args);\r
876                 va_end(Args);\r
877 \r
878                 // SFTP対応\r
879                 if(IsSFTPAttached(cSkt))\r
880                         return ConvertFTPCommandToPuTTYSFTP(cSkt, Reply, CancelCheckWork, Cmd);\r
881 \r
882                 if(strncmp(Cmd, "PASS ", 5) == 0)\r
883                         SetTaskMsg(">PASS [xxxxxx]");\r
884                 else if((strncmp(Cmd, "USER ", 5) == 0) ||\r
885                                 (strncmp(Cmd, "OPEN ", 5) == 0))\r
886                 {\r
887                         SetTaskMsg(">%s", Cmd);\r
888                 }\r
889                 else\r
890                 {\r
891                         ChangeSepaLocal2Remote(Cmd);\r
892                         SetTaskMsg(">%s", Cmd);\r
893                         ChangeFnameLocal2Remote(Cmd, FMAX_PATH*2);\r
894                 }\r
895 \r
896 //              DoPrintf("SEND : %s", Cmd);\r
897                 strcat(Cmd, "\x0D\x0A");\r
898 \r
899                 if(Reply != NULL)\r
900                         strcpy(Reply, "");\r
901 \r
902                 Sts = 429;\r
903                 if(SendData(cSkt, Cmd, strlen(Cmd), 0, CancelCheckWork) == FFFTP_SUCCESS)\r
904                 {\r
905                         Sts = ReadReplyMessage(cSkt, Reply, 1024, CancelCheckWork, TmpBuf);\r
906                 }\r
907 \r
908 //#pragma aaa\r
909 //if(Reply != NULL)\r
910 //      DoPrintf("%x : %x : %s : %s", cSkt, &TmpBuf, Cmd, Reply);\r
911 //else\r
912 //      DoPrintf("%x : %x : %s : NULL", cSkt, &TmpBuf, Cmd);\r
913 \r
914 //              DoPrintf("command() RET=%d", Sts);\r
915         }\r
916         else\r
917                 Sts = 429;\r
918 \r
919         return(Sts);\r
920 }\r
921 \r
922 \r
923 /*----- データを送る ----------------------------------------------------------\r
924 *\r
925 *       Parameter\r
926 *               SOCKET Skt : ソケット\r
927 *               char *Data : データ\r
928 *               int Size : 送るデータのサイズ\r
929 *               int Mode : コールモード\r
930 *\r
931 *       Return Value\r
932 *               int ステータス\r
933 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
934 *----------------------------------------------------------------------------*/\r
935 \r
936 int SendData(SOCKET Skt, char *Data, int Size, int Mode, int *CancelCheckWork)\r
937 {\r
938         int Sts;\r
939         int Tmp;\r
940 //      fd_set SendFds;\r
941 //      struct timeval Tout;\r
942 //      struct timeval *ToutPtr;\r
943         int TimeOutErr;\r
944 \r
945         Sts = FFFTP_FAIL;\r
946         if(Skt != INVALID_SOCKET)\r
947         {\r
948                 Sts = FFFTP_SUCCESS;\r
949                 while(Size > 0)\r
950                 {\r
951 //                      FD_ZERO(&SendFds);\r
952 //                      FD_SET(Skt, &SendFds);\r
953 //                      ToutPtr = NULL;\r
954 //                      if(TimeOut != 0)\r
955 //                      {\r
956 //                              Tout.tv_sec = TimeOut;\r
957 //                              Tout.tv_usec = 0;\r
958 //                              ToutPtr = &Tout;\r
959 //                      }\r
960 //                      Tmp = select(0, NULL, &SendFds, NULL, ToutPtr);\r
961 //                      if(Tmp == SOCKET_ERROR)\r
962 //                      {\r
963 //                              Sts = FFFTP_FAIL;\r
964 //                              ReportWSError("select", WSAGetLastError());\r
965 //                              break;\r
966 //                      }\r
967 //                      else if(Tmp == 0)\r
968 //                      {\r
969 //                              Sts = FFFTP_FAIL;\r
970 //                              SetTaskMsg(MSGJPN241);\r
971 //                              break;\r
972 //                      }\r
973 \r
974                         Tmp = do_send(Skt, Data, Size, Mode, &TimeOutErr, CancelCheckWork);\r
975                         if(TimeOutErr == YES)\r
976                         {\r
977                                 Sts = FFFTP_FAIL;\r
978                                 SetTaskMsg(MSGJPN241);\r
979                                 break;\r
980                         }\r
981                         else if(Tmp == SOCKET_ERROR)\r
982                         {\r
983                                 Sts = FFFTP_FAIL;\r
984                                 ReportWSError("send", WSAGetLastError());\r
985                                 break;\r
986                         }\r
987 \r
988                         Size -= Tmp;\r
989                         Data += Tmp;\r
990                 }\r
991         }\r
992         return(Sts);\r
993 }\r
994 \r
995 \r
996 /*----- 応答メッセージを受け取る ----------------------------------------------\r
997 *\r
998 *       Parameter\r
999 *               SOCKET cSkt : コントロールソケット\r
1000 *               char *Buf : メッセージを受け取るバッファ (NULL=コピーしない)\r
1001 *               int Max : バッファのサイズ\r
1002 *               int *CancelCheckWork :\r
1003 *               char *Tmp : テンポラリワーク\r
1004 *\r
1005 *       Return Value\r
1006 *               int 応答コード\r
1007 *----------------------------------------------------------------------------*/\r
1008 \r
1009 int ReadReplyMessage(SOCKET cSkt, char *Buf, int Max, int *CancelCheckWork, char *Tmp)\r
1010 {\r
1011         int iRetCode;\r
1012         int iContinue;\r
1013         int FirstCode;\r
1014         int Lines;\r
1015         int i;\r
1016 \r
1017         if(Buf != NULL)\r
1018                 memset(Buf, NUL, Max);\r
1019         Max--;\r
1020 \r
1021         FirstCode = 0;\r
1022         if(cSkt != INVALID_SOCKET)\r
1023         {\r
1024                 Lines = 0;\r
1025                 do\r
1026                 {\r
1027                         iContinue = NO;\r
1028                         iRetCode = ReadOneLine(cSkt, Tmp, ONELINE_BUF_SIZE, CancelCheckWork);\r
1029 \r
1030                         // 文字化け対策\r
1031                         ChangeFnameRemote2Local(Tmp, ONELINE_BUF_SIZE);\r
1032                         SetTaskMsg("%s", Tmp);\r
1033 \r
1034                         if(Buf != NULL)\r
1035                         {\r
1036                                 // 2行目以降の応答コードは消す\r
1037                                 if(Lines > 0)\r
1038                                 {\r
1039                                         for(i = 0; ; i++)\r
1040                                         {\r
1041                                                 if(IsDigit(Tmp[i]) == 0)\r
1042                                                         break;\r
1043                                                 Tmp[i] = ' ';\r
1044                                         }\r
1045                                 }\r
1046                                 strncat(Buf, Tmp, Max);\r
1047                                 Max = max1(0, Max-strlen(Tmp));\r
1048 \r
1049 //                              strncpy(Buf, Tmp, Max);\r
1050                         }\r
1051 \r
1052                         if((iRetCode != 421) && (iRetCode != 429))\r
1053                         {\r
1054                                 if((FirstCode == 0) &&\r
1055                                    (iRetCode >= 100) && (iRetCode <= 599))\r
1056                                 {\r
1057                                         FirstCode = iRetCode;\r
1058                                 }\r
1059 \r
1060                                 if((iRetCode < 100) || (iRetCode > 599) ||\r
1061                                    (*(Tmp + 3) == '-') ||\r
1062                                    ((FirstCode > 0) && (iRetCode != FirstCode)))\r
1063                                 {\r
1064                                         iContinue = YES;\r
1065                                 }\r
1066                         }\r
1067                         else\r
1068                                 FirstCode = iRetCode;\r
1069 \r
1070                         Lines++;\r
1071                 }\r
1072                 while(iContinue == YES);\r
1073         }\r
1074         return(FirstCode);\r
1075 }\r
1076 \r
1077 \r
1078 /*----- 1行分のデータを受け取る ----------------------------------------------\r
1079 *\r
1080 *       Parameter\r
1081 *               SOCKET cSkt : コントロールソケット\r
1082 *               char *Buf : メッセージを受け取るバッファ\r
1083 *               int Max : バッファのサイズ\r
1084 *               int *CancelCheckWork : \r
1085 *\r
1086 *       Return Value\r
1087 *               int 応答コード\r
1088 *----------------------------------------------------------------------------*/\r
1089 \r
1090 static int ReadOneLine(SOCKET cSkt, char *Buf, int Max, int *CancelCheckWork)\r
1091 {\r
1092         char *Pos;\r
1093         int SizeOnce;\r
1094         int CopySize;\r
1095         int ResCode;\r
1096         int i;\r
1097 //      fd_set ReadFds;\r
1098 //      struct timeval Tout;\r
1099 //      struct timeval *ToutPtr;\r
1100         char Tmp[1024];\r
1101         int TimeOutErr;\r
1102 \r
1103         ResCode = 0;\r
1104         if(cSkt != INVALID_SOCKET)\r
1105         {\r
1106                 memset(Buf, NUL, Max);\r
1107                 Max--;                                  /* 末尾のNULLのぶん */\r
1108                 Pos = Buf;\r
1109 \r
1110                 for(;;)\r
1111                 {\r
1112 //                      FD_ZERO(&ReadFds);\r
1113 //                      FD_SET(cSkt, &ReadFds);\r
1114 //                      ToutPtr = NULL;\r
1115 //                      if(TimeOut != 0)\r
1116 //                      {\r
1117 //                              Tout.tv_sec = TimeOut;\r
1118 //                              Tout.tv_usec = 0;\r
1119 //                              ToutPtr = &Tout;\r
1120 //                      }\r
1121 //                      i = select(0, &ReadFds, NULL, NULL, ToutPtr);\r
1122 //                      if(i == SOCKET_ERROR)\r
1123 //                      {\r
1124 //                              ReportWSError("select", WSAGetLastError());\r
1125 //                              SizeOnce = -1;\r
1126 //                              break;\r
1127 //                      }\r
1128 //                      else if(i == 0)\r
1129 //                      {\r
1130 //                              SetTaskMsg(MSGJPN242);\r
1131 //                              SizeOnce = -2;\r
1132 //                              break;\r
1133 //                      }\r
1134 \r
1135                         /* LFまでを受信するために、最初はPEEKで受信 */\r
1136                         if((SizeOnce = do_recv(cSkt, (LPSTR)Tmp, 1024, MSG_PEEK, &TimeOutErr, CancelCheckWork)) <= 0)\r
1137                         {\r
1138                                 if(TimeOutErr == YES)\r
1139                                 {\r
1140                                         SetTaskMsg(MSGJPN242);\r
1141                                         SizeOnce = -2;\r
1142                                 }\r
1143                                 else if(SizeOnce == SOCKET_ERROR)\r
1144                                 {\r
1145                                         SizeOnce = -1;\r
1146                                 }\r
1147                                 break;\r
1148                         }\r
1149 \r
1150                         /* LFを探して、あったらそこまでの長さをセット */\r
1151                         for(i = 0; i < SizeOnce ; i++)\r
1152                         {\r
1153                                 if(*(Tmp + i) == NUL || *(Tmp + i) == 0x0A)\r
1154                                 {\r
1155                                         SizeOnce = i + 1;\r
1156                                         break;\r
1157                                 }\r
1158                         }\r
1159 \r
1160                         /* 本受信 */\r
1161                         if((SizeOnce = do_recv(cSkt, Tmp, SizeOnce, 0, &TimeOutErr, CancelCheckWork)) <= 0)\r
1162                                 break;\r
1163 \r
1164                         CopySize = min1(Max, SizeOnce);\r
1165                         memcpy(Pos, Tmp, CopySize);\r
1166                         Pos += CopySize;\r
1167                         Max -= CopySize;\r
1168 \r
1169                         /* データがLFで終わっていたら1行終わり */\r
1170                         if(*(Tmp + SizeOnce - 1) == 0x0A)\r
1171                                 break;\r
1172                 }\r
1173                 *Pos = NUL;\r
1174 \r
1175                 if(SizeOnce <= 0)\r
1176                 {\r
1177                         ResCode = 429;\r
1178                         memset(Buf, 0, Max);\r
1179 \r
1180                         if((SizeOnce == -2) || (AskTransferNow() == YES))\r
1181                         // 転送中に全て中止を行うと不正なデータが得られる場合のバグ修正\r
1182 //                              DisconnectSet();\r
1183                                 cSkt = DoClose(cSkt);\r
1184                 }\r
1185                 else\r
1186                 {\r
1187                         if(IsDigit(*Buf) && IsDigit(*(Buf+1)) && IsDigit(*(Buf+2)))\r
1188                         {\r
1189                                 memset(Tmp, NUL, 4);\r
1190                                 strncpy(Tmp, Buf, 3);\r
1191                                 ResCode = atoi(Tmp);\r
1192                         }\r
1193 \r
1194                         /* 末尾の CR,LF,スペースを取り除く */\r
1195                         while((i=strlen(Buf))>2 &&\r
1196                                   (Buf[i-1]==0x0a || Buf[i-1]==0x0d || Buf[i-1]==' '))\r
1197                                 Buf[i-1]=0;\r
1198                 }\r
1199         }\r
1200         return(ResCode);\r
1201 }\r
1202 \r
1203 \r
1204 /*----- 固定長データを受け取る ------------------------------------------------\r
1205 *\r
1206 *       Parameter\r
1207 *               SOCKET cSkt : コントロールソケット\r
1208 *               char *Buf : メッセージを受け取るバッファ\r
1209 *               int Size : バイト数\r
1210 *               int *CancelCheckWork : \r
1211 *\r
1212 *       Return Value\r
1213 *               int ステータス\r
1214 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1215 *----------------------------------------------------------------------------*/\r
1216 \r
1217 int ReadNchar(SOCKET cSkt, char *Buf, int Size, int *CancelCheckWork)\r
1218 {\r
1219 //      struct timeval Tout;\r
1220 //      struct timeval *ToutPtr;\r
1221 //      fd_set ReadFds;\r
1222 //      int i;\r
1223         int SizeOnce;\r
1224         int Sts;\r
1225         int TimeOutErr;\r
1226 \r
1227         Sts = FFFTP_FAIL;\r
1228         if(cSkt != INVALID_SOCKET)\r
1229         {\r
1230                 Sts = FFFTP_SUCCESS;\r
1231                 while(Size > 0)\r
1232                 {\r
1233 //                      FD_ZERO(&ReadFds);\r
1234 //                      FD_SET(cSkt, &ReadFds);\r
1235 //                      ToutPtr = NULL;\r
1236 //                      if(TimeOut != 0)\r
1237 //                      {\r
1238 //                              Tout.tv_sec = TimeOut;\r
1239 //                              Tout.tv_usec = 0;\r
1240 //                              ToutPtr = &Tout;\r
1241 //                      }\r
1242 //                      i = select(0, &ReadFds, NULL, NULL, ToutPtr);\r
1243 //                      if(i == SOCKET_ERROR)\r
1244 //                      {\r
1245 //                              ReportWSError("select", WSAGetLastError());\r
1246 //                              Sts = FFFTP_FAIL;\r
1247 //                              break;\r
1248 //                      }\r
1249 //                      else if(i == 0)\r
1250 //                      {\r
1251 //                              SetTaskMsg(MSGJPN243);\r
1252 //                              Sts = FFFTP_FAIL;\r
1253 //                              break;\r
1254 //                      }\r
1255 \r
1256                         if((SizeOnce = do_recv(cSkt, Buf, Size, 0, &TimeOutErr, CancelCheckWork)) <= 0)\r
1257                         {\r
1258                                 if(TimeOutErr == YES)\r
1259                                         SetTaskMsg(MSGJPN243);\r
1260                                 Sts = FFFTP_FAIL;\r
1261                                 break;\r
1262                         }\r
1263 \r
1264                         Buf += SizeOnce;\r
1265                         Size -= SizeOnce;\r
1266                 }\r
1267         }\r
1268 \r
1269         if(Sts == FFFTP_FAIL)\r
1270                 SetTaskMsg(MSGJPN244);\r
1271 \r
1272         return(Sts);\r
1273 }\r
1274 \r
1275 \r
1276 /*----- エラー文字列を取得 ----------------------------------------------------\r
1277 *\r
1278 *       Parameter\r
1279 *               UINT Error : エラー番号\r
1280 *\r
1281 *       Return Value\r
1282 *               char *エラー文字列\r
1283 *----------------------------------------------------------------------------*/\r
1284 \r
1285 char *ReturnWSError(UINT Error)\r
1286 {\r
1287         static char Msg[128];\r
1288         char *Str;\r
1289 \r
1290         switch(Error)\r
1291         {\r
1292                 case WSAVERNOTSUPPORTED:\r
1293                         Str = "version of WinSock not supported";\r
1294                         break;\r
1295 \r
1296                 case WSASYSNOTREADY:\r
1297                         Str = "WinSock not present or not responding";\r
1298                         break;\r
1299 \r
1300                 case WSAEINVAL:\r
1301                         Str = "app version not supported by DLL";\r
1302                         break;\r
1303 \r
1304                 case WSAHOST_NOT_FOUND:\r
1305                         Str = "Authoritive: Host not found";\r
1306                         break;\r
1307 \r
1308                 case WSATRY_AGAIN:\r
1309                         Str = "Non-authoritive: host not found or server failure";\r
1310                         break;\r
1311 \r
1312                 case WSANO_RECOVERY:\r
1313                         Str = "Non-recoverable: refused or not implemented";\r
1314                         break;\r
1315 \r
1316                 case WSANO_DATA:\r
1317                         Str = "Valid name, no data record for type";\r
1318                         break;\r
1319 \r
1320 #if 0\r
1321                 case WSANO_ADDRESS:\r
1322                         Str = "Valid name, no MX record";\r
1323                         break;\r
1324 #endif\r
1325 \r
1326                 case WSANOTINITIALISED:\r
1327                         Str = "WSA Startup not initialized";\r
1328                         break;\r
1329 \r
1330                 case WSAENETDOWN:\r
1331                         Str = "Network subsystem failed";\r
1332                         break;\r
1333 \r
1334                 case WSAEINPROGRESS:\r
1335                         Str = "Blocking operation in progress";\r
1336                         break;\r
1337 \r
1338                 case WSAEINTR:\r
1339                         Str = "Blocking call cancelled";\r
1340                         break;\r
1341 \r
1342                 case WSAEAFNOSUPPORT:\r
1343                         Str = "address family not supported";\r
1344                         break;\r
1345 \r
1346                 case WSAEMFILE:\r
1347                         Str = "no file descriptors available";\r
1348                         break;\r
1349 \r
1350                 case WSAENOBUFS:\r
1351                         Str = "no buffer space available";\r
1352                         break;\r
1353 \r
1354                 case WSAEPROTONOSUPPORT:\r
1355                         Str = "specified protocol not supported";\r
1356                         break;\r
1357 \r
1358                 case WSAEPROTOTYPE:\r
1359                         Str = "protocol wrong type for this socket";\r
1360                         break;\r
1361 \r
1362                 case WSAESOCKTNOSUPPORT:\r
1363                         Str = "socket type not supported for address family";\r
1364                         break;\r
1365 \r
1366                 case WSAENOTSOCK:\r
1367                         Str = "descriptor is not a socket";\r
1368                         break;\r
1369 \r
1370                 case WSAEWOULDBLOCK:\r
1371                         Str = "socket marked as non-blocking and SO_LINGER set not 0";\r
1372                         break;\r
1373 \r
1374                 case WSAEADDRINUSE:\r
1375                         Str = "address already in use";\r
1376                         break;\r
1377 \r
1378                 case WSAECONNABORTED:\r
1379                         Str = "connection aborted";\r
1380                         break;\r
1381 \r
1382                 case WSAECONNRESET:\r
1383                         Str = "connection reset";\r
1384                         break;\r
1385 \r
1386                 case WSAENOTCONN:\r
1387                         Str = "not connected";\r
1388                         break;\r
1389 \r
1390                 case WSAETIMEDOUT:\r
1391                         Str = "connection timed out";\r
1392                         break;\r
1393 \r
1394                 case WSAECONNREFUSED:\r
1395                         Str = "connection refused";\r
1396                         break;\r
1397 \r
1398                 case WSAEHOSTDOWN:\r
1399                         Str = "host down";\r
1400                         break;\r
1401 \r
1402                 case WSAEHOSTUNREACH:\r
1403                         Str = "host unreachable";\r
1404                         break;\r
1405 \r
1406                 case WSAEADDRNOTAVAIL:\r
1407                         Str = "address not available";\r
1408                         break;\r
1409 \r
1410                 default:\r
1411                         sprintf(Msg, "error %u", Error);\r
1412                         return(Msg);\r
1413         }\r
1414         return(Str);\r
1415 }\r
1416 \r
1417 \r
1418 /*----- デバッグコンソールにエラーを表示 --------------------------------------\r
1419 *\r
1420 *       Parameter\r
1421 *               char *Msg : エラーの前に表示するメッセージ\r
1422 *               UINT Error : エラー番号\r
1423 *\r
1424 *       Return Value\r
1425 *               なし\r
1426 *----------------------------------------------------------------------------*/\r
1427 \r
1428 void ReportWSError(char *Msg, UINT Error)\r
1429 {\r
1430         if(Msg != NULL)\r
1431                 DoPrintf("[[%s : %s]]", Msg, ReturnWSError(Error));\r
1432         else\r
1433                 DoPrintf("[[%s]]", ReturnWSError(Error));\r
1434 }\r
1435 \r
1436 \r
1437 /*----- ファイル名をローカル側で扱えるように変換する --------------------------\r
1438 *\r
1439 *       Parameter\r
1440 *               char *Fname : ファイル名\r
1441 *               int Max : 最大長\r
1442 *\r
1443 *       Return Value\r
1444 *               int ステータス\r
1445 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1446 *----------------------------------------------------------------------------*/\r
1447 \r
1448 int ChangeFnameRemote2Local(char *Fname, int Max)\r
1449 {\r
1450         int Sts;\r
1451         char *Buf;\r
1452         char *Pos;\r
1453         CODECONVINFO cInfo;\r
1454         // バッファ上書きバグ対策\r
1455         char *Buf2;\r
1456 \r
1457         Sts = FFFTP_FAIL;\r
1458         if((Buf = malloc(Max)) != NULL)\r
1459         {\r
1460         // バッファ上書きバグ対策\r
1461         if((Buf2 = malloc(strlen(Fname) + 1)) != NULL)\r
1462         {\r
1463                 InitCodeConvInfo(&cInfo);\r
1464                 cInfo.KanaCnv = NO;                     //AskHostNameKana();\r
1465                 // バッファ上書きバグ対策\r
1466 //              cInfo.Str = Fname;\r
1467                 strcpy(Buf2, Fname);\r
1468                 cInfo.Str = Buf2;\r
1469                 cInfo.StrLen = strlen(Fname);\r
1470                 cInfo.Buf = Buf;\r
1471                 cInfo.BufSize = Max - 1;\r
1472 \r
1473                 // ここで全てUTF-8へ変換する\r
1474                 // TODO: SJIS以外も直接UTF-8へ変換\r
1475                 switch(AskHostNameKanji())\r
1476                 {\r
1477                         case KANJI_SJIS :\r
1478                                 ConvSJIStoUTF8N(&cInfo);\r
1479                                 *(Buf + cInfo.OutLen) = NUL;\r
1480                                 strcpy(Fname, Buf);\r
1481                                 Pos = strchr(Fname, NUL);\r
1482                                 FlushRestData(&cInfo);\r
1483                                 *(Buf + cInfo.OutLen) = NUL;\r
1484                                 strcpy(Pos, Buf);\r
1485                                 break;\r
1486 \r
1487                         case KANJI_JIS :\r
1488                                 ConvJIStoSJIS(&cInfo);\r
1489                                 *(Buf + cInfo.OutLen) = NUL;\r
1490                                 strcpy(Fname, Buf);\r
1491                                 Pos = strchr(Fname, NUL);\r
1492                                 FlushRestData(&cInfo);\r
1493                                 *(Buf + cInfo.OutLen) = NUL;\r
1494                                 strcpy(Pos, Buf);\r
1495                                 // TODO\r
1496                                 InitCodeConvInfo(&cInfo);\r
1497                                 cInfo.KanaCnv = NO;\r
1498                                 cInfo.Str = Fname;\r
1499                                 cInfo.StrLen = strlen(Fname);\r
1500                                 cInfo.Buf = Buf;\r
1501                                 cInfo.BufSize = Max - 1;\r
1502                                 ConvSJIStoUTF8N(&cInfo);\r
1503                                 *(Buf + cInfo.OutLen) = NUL;\r
1504                                 strcpy(Fname, Buf);\r
1505                                 Pos = strchr(Fname, NUL);\r
1506                                 FlushRestData(&cInfo);\r
1507                                 *(Buf + cInfo.OutLen) = NUL;\r
1508                                 strcpy(Pos, Buf);\r
1509                                 break;\r
1510 \r
1511                         case KANJI_EUC :\r
1512                                 ConvEUCtoSJIS(&cInfo);\r
1513                                 *(Buf + cInfo.OutLen) = NUL;\r
1514                                 strcpy(Fname, Buf);\r
1515                                 Pos = strchr(Fname, NUL);\r
1516                                 FlushRestData(&cInfo);\r
1517                                 *(Buf + cInfo.OutLen) = NUL;\r
1518                                 strcpy(Pos, Buf);\r
1519                                 // TODO\r
1520                                 InitCodeConvInfo(&cInfo);\r
1521                                 cInfo.KanaCnv = NO;\r
1522                                 cInfo.Str = Fname;\r
1523                                 cInfo.StrLen = strlen(Fname);\r
1524                                 cInfo.Buf = Buf;\r
1525                                 cInfo.BufSize = Max - 1;\r
1526                                 ConvSJIStoUTF8N(&cInfo);\r
1527                                 *(Buf + cInfo.OutLen) = NUL;\r
1528                                 strcpy(Fname, Buf);\r
1529                                 Pos = strchr(Fname, NUL);\r
1530                                 FlushRestData(&cInfo);\r
1531                                 *(Buf + cInfo.OutLen) = NUL;\r
1532                                 strcpy(Pos, Buf);\r
1533                                 break;\r
1534 \r
1535                         case KANJI_SMB_HEX :\r
1536                         case KANJI_SMB_CAP :\r
1537                                 ConvSMBtoSJIS(&cInfo);\r
1538                                 *(Buf + cInfo.OutLen) = NUL;\r
1539                                 strcpy(Fname, Buf);\r
1540                                 Pos = strchr(Fname, NUL);\r
1541                                 FlushRestData(&cInfo);\r
1542                                 *(Buf + cInfo.OutLen) = NUL;\r
1543                                 strcpy(Pos, Buf);\r
1544                                 // TODO\r
1545                                 InitCodeConvInfo(&cInfo);\r
1546                                 cInfo.KanaCnv = NO;\r
1547                                 cInfo.Str = Fname;\r
1548                                 cInfo.StrLen = strlen(Fname);\r
1549                                 cInfo.Buf = Buf;\r
1550                                 cInfo.BufSize = Max - 1;\r
1551                                 ConvSJIStoUTF8N(&cInfo);\r
1552                                 *(Buf + cInfo.OutLen) = NUL;\r
1553                                 strcpy(Fname, Buf);\r
1554                                 Pos = strchr(Fname, NUL);\r
1555                                 FlushRestData(&cInfo);\r
1556                                 *(Buf + cInfo.OutLen) = NUL;\r
1557                                 strcpy(Pos, Buf);\r
1558                                 break;\r
1559 \r
1560 //                      case KANJI_UTF8N :\r
1561 //                              ConvUTF8NtoSJIS(&cInfo);\r
1562 //                              *(Buf + cInfo.OutLen) = NUL;\r
1563 //                              strcpy(Fname, Buf);\r
1564 //                              Pos = strchr(Fname, NUL);\r
1565 //                              FlushRestData(&cInfo);\r
1566 //                              *(Buf + cInfo.OutLen) = NUL;\r
1567 //                              strcpy(Pos, Buf);\r
1568 //                              break;\r
1569                 }\r
1570                 // バッファ上書きバグ対策\r
1571                 free(Buf2);\r
1572                 Sts = FFFTP_SUCCESS;\r
1573                 }\r
1574                 free(Buf);\r
1575                 // バッファ上書きバグ対策\r
1576 //              Sts = FFFTP_SUCCESS;\r
1577         }\r
1578         return(Sts);\r
1579 }\r
1580 \r
1581 \r
1582 /*----- ファイル名をリモート側で扱えるように変換する --------------------------\r
1583 *\r
1584 *       Parameter\r
1585 *               char *Fname : ファイル名\r
1586 *               int Max : 最大長\r
1587 *\r
1588 *       Return Value\r
1589 *               int ステータス\r
1590 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1591 *----------------------------------------------------------------------------*/\r
1592 \r
1593 int ChangeFnameLocal2Remote(char *Fname, int Max)\r
1594 {\r
1595         int Sts;\r
1596         char *Buf;\r
1597         char *Pos;\r
1598         CODECONVINFO cInfo;\r
1599         // バッファ上書きバグ対策\r
1600         char *Buf2;\r
1601 \r
1602         Sts = FFFTP_FAIL;\r
1603         if((Buf = malloc(Max)) != NULL)\r
1604         {\r
1605         // バッファ上書きバグ対策\r
1606         if((Buf2 = malloc(strlen(Fname) + 1)) != NULL)\r
1607         {\r
1608                 InitCodeConvInfo(&cInfo);\r
1609                 cInfo.KanaCnv = AskHostNameKana();\r
1610                 // バッファ上書きバグ対策\r
1611 //              cInfo.Str = Fname;\r
1612                 strcpy(Buf2, Fname);\r
1613                 cInfo.Str = Buf2;\r
1614                 cInfo.StrLen = strlen(Fname);\r
1615                 cInfo.Buf = Buf;\r
1616                 cInfo.BufSize = Max - 1;\r
1617 \r
1618                 // ここで全てUTF-8から変換する\r
1619                 // TODO: SJIS以外も直接UTF-8から変換\r
1620                 switch(AskHostNameKanji())\r
1621                 {\r
1622                         case KANJI_SJIS :\r
1623                                 ConvUTF8NtoSJIS(&cInfo);\r
1624                                 *(Buf + cInfo.OutLen) = NUL;\r
1625                                 strcpy(Fname, Buf);\r
1626                                 Pos = strchr(Fname, NUL);\r
1627                                 FlushRestData(&cInfo);\r
1628                                 *(Buf + cInfo.OutLen) = NUL;\r
1629                                 strcpy(Pos, Buf);\r
1630                                 break;\r
1631 \r
1632                         case KANJI_JIS :\r
1633                                 ConvUTF8NtoSJIS(&cInfo);\r
1634                                 *(Buf + cInfo.OutLen) = NUL;\r
1635                                 strcpy(Fname, Buf);\r
1636                                 Pos = strchr(Fname, NUL);\r
1637                                 FlushRestData(&cInfo);\r
1638                                 *(Buf + cInfo.OutLen) = NUL;\r
1639                                 strcpy(Pos, Buf);\r
1640                                 // TODO\r
1641                                 InitCodeConvInfo(&cInfo);\r
1642                                 cInfo.KanaCnv = NO;\r
1643                                 cInfo.Str = Fname;\r
1644                                 cInfo.StrLen = strlen(Fname);\r
1645                                 cInfo.Buf = Buf;\r
1646                                 cInfo.BufSize = Max - 1;\r
1647                                 ConvSJIStoJIS(&cInfo);\r
1648                                 *(Buf + cInfo.OutLen) = NUL;\r
1649                                 strcpy(Fname, Buf);\r
1650                                 Pos = strchr(Fname, NUL);\r
1651                                 FlushRestData(&cInfo);\r
1652                                 *(Buf + cInfo.OutLen) = NUL;\r
1653                                 strcpy(Pos, Buf);\r
1654                                 break;\r
1655 \r
1656                         case KANJI_EUC :\r
1657                                 ConvUTF8NtoSJIS(&cInfo);\r
1658                                 *(Buf + cInfo.OutLen) = NUL;\r
1659                                 strcpy(Fname, Buf);\r
1660                                 Pos = strchr(Fname, NUL);\r
1661                                 FlushRestData(&cInfo);\r
1662                                 *(Buf + cInfo.OutLen) = NUL;\r
1663                                 strcpy(Pos, Buf);\r
1664                                 // TODO\r
1665                                 InitCodeConvInfo(&cInfo);\r
1666                                 cInfo.KanaCnv = NO;\r
1667                                 cInfo.Str = Fname;\r
1668                                 cInfo.StrLen = strlen(Fname);\r
1669                                 cInfo.Buf = Buf;\r
1670                                 cInfo.BufSize = Max - 1;\r
1671                                 ConvSJIStoEUC(&cInfo);\r
1672                                 *(Buf + cInfo.OutLen) = NUL;\r
1673                                 strcpy(Fname, Buf);\r
1674                                 Pos = strchr(Fname, NUL);\r
1675                                 FlushRestData(&cInfo);\r
1676                                 *(Buf + cInfo.OutLen) = NUL;\r
1677                                 strcpy(Pos, Buf);\r
1678                                 break;\r
1679 \r
1680                         case KANJI_SMB_HEX :\r
1681                                 ConvUTF8NtoSJIS(&cInfo);\r
1682                                 *(Buf + cInfo.OutLen) = NUL;\r
1683                                 strcpy(Fname, Buf);\r
1684                                 Pos = strchr(Fname, NUL);\r
1685                                 FlushRestData(&cInfo);\r
1686                                 *(Buf + cInfo.OutLen) = NUL;\r
1687                                 strcpy(Pos, Buf);\r
1688                                 // TODO\r
1689                                 InitCodeConvInfo(&cInfo);\r
1690                                 cInfo.KanaCnv = NO;\r
1691                                 cInfo.Str = Fname;\r
1692                                 cInfo.StrLen = strlen(Fname);\r
1693                                 cInfo.Buf = Buf;\r
1694                                 cInfo.BufSize = Max - 1;\r
1695                                 ConvSJIStoSMB_HEX(&cInfo);\r
1696                                 *(Buf + cInfo.OutLen) = NUL;\r
1697                                 strcpy(Fname, Buf);\r
1698                                 Pos = strchr(Fname, NUL);\r
1699                                 FlushRestData(&cInfo);\r
1700                                 *(Buf + cInfo.OutLen) = NUL;\r
1701                                 strcpy(Pos, Buf);\r
1702                                 break;\r
1703 \r
1704                         case KANJI_SMB_CAP :\r
1705                                 ConvUTF8NtoSJIS(&cInfo);\r
1706                                 *(Buf + cInfo.OutLen) = NUL;\r
1707                                 strcpy(Fname, Buf);\r
1708                                 Pos = strchr(Fname, NUL);\r
1709                                 FlushRestData(&cInfo);\r
1710                                 *(Buf + cInfo.OutLen) = NUL;\r
1711                                 strcpy(Pos, Buf);\r
1712                                 // TODO\r
1713                                 InitCodeConvInfo(&cInfo);\r
1714                                 cInfo.KanaCnv = NO;\r
1715                                 cInfo.Str = Fname;\r
1716                                 cInfo.StrLen = strlen(Fname);\r
1717                                 cInfo.Buf = Buf;\r
1718                                 cInfo.BufSize = Max - 1;\r
1719                                 ConvSJIStoSMB_CAP(&cInfo);\r
1720                                 *(Buf + cInfo.OutLen) = NUL;\r
1721                                 strcpy(Fname, Buf);\r
1722                                 Pos = strchr(Fname, NUL);\r
1723                                 FlushRestData(&cInfo);\r
1724                                 *(Buf + cInfo.OutLen) = NUL;\r
1725                                 strcpy(Pos, Buf);\r
1726                                 break;\r
1727 \r
1728 //                      case KANJI_UTF8N :\r
1729 //                              ConvSJIStoUTF8N(&cInfo);\r
1730 //                              *(Buf + cInfo.OutLen) = NUL;\r
1731 //                              strcpy(Fname, Buf);\r
1732 //                              Pos = strchr(Fname, NUL);\r
1733 //                              FlushRestData(&cInfo);\r
1734 //                              *(Buf + cInfo.OutLen) = NUL;\r
1735 //                              strcpy(Pos, Buf);\r
1736 //                              break;\r
1737                 }\r
1738                 // バッファ上書きバグ対策\r
1739                 free(Buf2);\r
1740                 Sts = FFFTP_SUCCESS;\r
1741                 }\r
1742                 free(Buf);\r
1743                 // バッファ上書きバグ対策\r
1744 //              Sts = FFFTP_SUCCESS;\r
1745         }\r
1746         return(Sts);\r
1747 }\r
1748 \r
1749 \r
1750 /*----- パスの区切り文字をホストに合わせて変更する ----------------------------\r
1751 *\r
1752 *       Parameter\r
1753 *               char *Fname : ファイル名\r
1754 *\r
1755 *       Return Value\r
1756 *               なし\r
1757 *----------------------------------------------------------------------------*/\r
1758 static void ChangeSepaLocal2Remote(char *Fname)\r
1759 {\r
1760         if(AskHostType() == HTYPE_STRATUS)\r
1761         {\r
1762                 ReplaceAll(Fname, '/', '>');\r
1763         }\r
1764         return;\r
1765 }\r
1766 \r
1767 \r
1768 /*----- パスの区切り文字をローカルに合わせて変更する --------------------------\r
1769 *\r
1770 *       Parameter\r
1771 *               char *Fname : ファイル名\r
1772 *\r
1773 *       Return Value\r
1774 *               なし\r
1775 *----------------------------------------------------------------------------*/\r
1776 static void ChangeSepaRemote2Local(char *Fname)\r
1777 {\r
1778         if(AskHostType() == HTYPE_STRATUS)\r
1779         {\r
1780                 ReplaceAll(Fname, '>', '/');\r
1781         }\r
1782         return;\r
1783 }\r
1784 \r
1785 \r
1786 \r
1787 \r
1788 \r
1789 \r
1790 \r