home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2003 November / VPR0311.ISO / OLS / TAR32223 / tar32223.lzh / tar32_2 / src / tarcmd.cpp < prev    next >
C/C++ Source or Header  |  2003-01-17  |  23KB  |  732 lines

  1. /*
  2.     tarcmd.cpp
  3.         Tar() API implementation class.
  4.         by Yoshioka Tsuneo(QWF00133@nifty.ne.jp)
  5. */
  6. /*    
  7.     このファイルの利用条件:
  8.         このソースファイルの利用制限は一切ありません。
  9.         ソースの一部、全部を商用、非商用など目的に
  10.         かかわりなく他のプログラムで自由に使用できます。
  11.         パブリック・ドメイン・ソフトウェアと同様に扱えます。
  12.     
  13.     プログラマ向けの要望(制限ではありません):
  14.         ソース中に改善すべき点があればお知らせください。
  15.         ソースコード中にバグを見つけた場合は報告してください。
  16.         直した部分などありましたら教えてください。
  17.         断片的な情報でも結構です。
  18.         このファイルを利用した場合はなるべく教えてください。
  19. */
  20. /*
  21.     LICENSE of this file:
  22.         There is no restriction for using this file.
  23.         You can use this file in your software for any purpose.
  24.         In other words, you can use this file as Public Domain Software.
  25.  
  26.     RECOMMENDATION for Programmer(not restriction):
  27.         If you find points to improve code, please report me.
  28.         If you find bug in source code, please report me.
  29.         If you fixed bug, please teach me.
  30.         I want any trivial information.
  31.         If you use this file, please report me.
  32. */
  33. #include "tar32api.h"
  34. #include "cmdline.h"
  35. #include "tar32.h"
  36. #include "tar32dll.h"
  37. #include "tarcmd.h"
  38. #include "util.h"
  39. #include "dlg.h"
  40. #include <mbstring.h>
  41. #include <process.h> // _beginthread
  42. #include <io.h>    // chmod
  43. #include <sys/utime.h> // utime
  44. #include <time.h> // time
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47.  
  48. #pragma warning(disable: 4786)
  49. #include <list>
  50. #include <string>
  51. #include <strstream>
  52. #include <fstream>
  53. using namespace std;
  54.  
  55. #include "fast_stl.h"
  56.  
  57. class CTar32CmdInfo
  58. {
  59. public:
  60.     struct CArgs{
  61.         CArgs(const string &f, const string &dir) : file(f), current_dir(dir){};
  62.         string file;
  63.         string current_dir;
  64.     };
  65.     CTar32CmdInfo(char *s, int len) : output(s,len), exception("",0){
  66.         hTar32StatusDialog = NULL;
  67.         b_use_directory = true;
  68.         b_display_dialog = true;
  69.         b_message_loop = true;
  70.         b_print = false;
  71.  
  72.         b_archive_tar = true;
  73.         archive_type = ARCHIVETYPE_NORMAL;
  74.         compress_level = 0;
  75.         
  76.         wm_main_thread_end = 0;
  77.         hParentWnd = NULL;
  78.         idMessageThread = 0;
  79.  
  80.     };
  81.     string arcfile;
  82.     list<CArgs> argfiles;
  83.     // list<string> files;
  84.     strstream output;
  85.     HWND hTar32StatusDialog;
  86.     CTar32Exception exception;
  87.  
  88.     bool b_use_directory;
  89.     bool b_display_dialog;
  90.     bool b_message_loop;
  91.     bool b_print;
  92.  
  93.     bool b_archive_tar;
  94.     int archive_type;
  95.     int compress_level;
  96.  
  97.     UINT wm_main_thread_end;
  98.     HWND hParentWnd;
  99.     DWORD idMessageThread;
  100.     char command;
  101. };
  102.  
  103. static void cmd_create(CTar32CmdInfo &cmdinfo);
  104. static void cmd_extract(CTar32CmdInfo &cmdinfo);
  105. static void cmd_list(CTar32CmdInfo &cmdinfo);
  106. static int tar_cmd_itr(const HWND hwnd, LPCSTR szCmdLine,LPSTR szOutput, const DWORD dwSize,CTar32CmdInfo &info);
  107. static void cmd_usage(CTar32CmdInfo &info)
  108. {
  109.     info.output 
  110.         << "usage: TAR32.DLL <command> <option> archive.{tar,tar.gz,tar.bz2} filenames...\n"
  111.         << "    command:\n"
  112.         << "       EXTRACT: -x <files.tgz> [files...] \n"
  113.         << "       CREATE:  -c <files.tgz> [files...] \n"
  114.         << "       LIST:    -l <files.tgz> [files...] \n"
  115.         << "       PRINT:   -p <files.tgz> [files...] \n"
  116.         << "    option: (default value)\n"
  117.         << "       -z[N]     compress by gzip(.tar.gz) with level N(default:6)\n"
  118.         << "       -B[N]     compress by bzip2(.tar.bz2) with level N(default:9)\n"
  119.         << "       -G        not make tar archive.(.tar/.tar.gz/.tar.bz2)\n"
  120.         << "                 make compress only archive.(.gz/.bz2)\n"
  121.         // << "       -Z[N]     compress by compress(LZW) (NOT IMPLEMENTED)\n"
  122.         << "       --use-directory=[0|1](1)  effective directory name\n"
  123.         << "       --display-dialog=[0|1](1)  display dialog box\n"
  124.         << "       --message-loop=[0|1](1)  run message loop\n"
  125.         << "       --bzip2=[N]     compress by bzip2 with level N\n"
  126.         << "    ignore option,command: a,v,V,I,i,f,e,g,S,A,b,N,U,--xxxx=xxx\n"
  127.         ;
  128. }
  129.  
  130. int tar_cmd(const HWND hwnd, LPCSTR szCmdLine,LPSTR szOutput, const DWORD dwSize, int *pWriteSize)
  131. {
  132.     CTar32CmdInfo cmdinfo(szOutput, dwSize-1);
  133.     int ret;
  134.     try{
  135.         ret =  tar_cmd_itr(hwnd, szCmdLine,szOutput,dwSize,cmdinfo);
  136.     }catch(CTar32Exception &e){
  137.         cmdinfo.output << "TAR32 Error(0x" << hex << e.m_code << "): " << e.m_str << "\n";
  138.         cmdinfo.output << "Tar((HWND)" << (unsigned)hwnd << ",(LPCSTR)" << szCmdLine << ",,(DWORD)" << (unsigned)dwSize << ")\n";
  139.         ret =  e.m_code;
  140.         cmd_usage(cmdinfo);
  141.     }
  142.     // int len = strlen(cmdinfo.output.str());
  143.     int len = cmdinfo.output.rdbuf()->pcount();
  144.     if(dwSize>0)szOutput[len] = '\0';
  145.     if(pWriteSize){*pWriteSize = len;}
  146.     return ret;
  147. }
  148. static void _cdecl tar_cmd_main_thread(LPVOID param);
  149. static int tar_cmd_itr(const HWND hwnd, LPCSTR szCmdLine,LPSTR szOutput, const DWORD dwSize,CTar32CmdInfo &cmdinfo)
  150. {
  151.     list<string> args;    // command line array
  152.     {
  153.         char **argv;
  154.         argv = split_cmdline_with_response(szCmdLine);
  155.         if(!argv){
  156.             throw CTar32Exception("commandline split error", ERROR_COMMAND_NAME);
  157.         }
  158.         char **argv2 = argv;
  159.         while(*argv2){
  160.             args.push_back(*argv2);
  161.             argv2++;
  162.         }
  163.         free(argv);
  164.     }
  165.  
  166.     char command = 0;    // main command. ('x','c','l')
  167.     string current_directory;
  168.     list<string>::iterator argi = args.begin();
  169.  
  170.     while(argi != args.end()){
  171.         string::iterator stri = (*argi).begin();
  172.         if(argi==args.begin() || (*stri == '-' && *(stri+1) != '\0')){
  173.             if(*stri == '-'){
  174.                 stri++;
  175.             }
  176.             if(*stri == '-' && *(stri+1) != '\0'){
  177.                 stri++;
  178.                 const string &long_option = (*argi).substr(stri - argi->begin());
  179.                 string key;
  180.                 string val = long_option;
  181.                 int len;
  182.                 if((len = long_option.find('=')) != string::npos){
  183.                     key = long_option.substr(0, len);
  184.                     val = long_option.substr(len + 1);
  185.                 }
  186.                 if(key == "use-directory"){
  187.                     cmdinfo.b_use_directory = (bool)atoi(val.c_str());
  188.                 }else if(key == "display-dialog"){
  189.                     cmdinfo.b_display_dialog = (bool)atoi(val.c_str());
  190.                 }else if(key == "message-loop"){
  191.                     cmdinfo.b_message_loop = (bool)atoi(val.c_str());
  192.                 }else if(key == "bzip2"){
  193.                     cmdinfo.archive_type = ARCHIVETYPE_BZ2;
  194.                     cmdinfo.compress_level = (bool)atoi(val.c_str());
  195.                 }else{
  196.                     /* igonore */;
  197.                 }
  198.                 argi++;
  199.                 continue;
  200.             }
  201.             list<string>::iterator cur_argi = argi;
  202.             while(stri != (*cur_argi).end()){
  203.                 switch(*stri){
  204.                 case 'x':
  205.                     command = 'x';break;
  206.                 case 'c':
  207.                     command = 'c';break;
  208.                 case 'l':
  209.                 case 't': // どんぞ:追加
  210.                     command = 'l';break;
  211.                 case 'p':
  212.                     command = 'x';
  213.                     cmdinfo.b_print = true;
  214.                     break;
  215.                 case 'f':
  216.                     if(++argi == args.end()){
  217.                         throw CTar32Exception("'f' follow no directory name", ERROR_COMMAND_NAME);
  218.                     }
  219.                     cmdinfo.arcfile = *argi;
  220.                     break;
  221.                 case 'o':
  222.                     if(++argi == args.end()){
  223.                         throw CTar32Exception("'o' follow no directory name", ERROR_COMMAND_NAME);
  224.                     }
  225.                     current_directory = *argi;
  226.                     // stri = argi->end()-1;
  227.                     break;
  228.                 case 'z':
  229.                     cmdinfo.archive_type = ARCHIVETYPE_GZ;
  230.                     if(isdigit(*(stri+1))){
  231.                         stri++;
  232.                         cmdinfo.compress_level = ((char)*stri) - '0';
  233.                     }else{
  234.                         cmdinfo.compress_level = 6;
  235.                     }
  236.                     break;
  237.                 case 'B':
  238.                     cmdinfo.archive_type = ARCHIVETYPE_BZ2;
  239.                     if(isdigit(*(stri+1))){
  240.                         stri++;
  241.                         cmdinfo.compress_level = ((char)*stri) - '0';
  242.                     }else{
  243.                         cmdinfo.compress_level = 9;
  244.                     }
  245.                     break;
  246.                 case 'Z':
  247.                     cmdinfo.archive_type = ARCHIVETYPE_Z;
  248.                     if(isdigit(*(stri+1))){
  249.                         stri++;
  250.                         cmdinfo.compress_level = ((char)*stri) - '0';
  251.                     }else{
  252.                         cmdinfo.compress_level = 6;
  253.                     }
  254.                     break;
  255.                 case 'G':
  256.                     cmdinfo.b_archive_tar = false;
  257.                     if(cmdinfo.archive_type == ARCHIVETYPE_NORMAL){
  258.                         cmdinfo.archive_type = ARCHIVETYPE_GZ;
  259.                         cmdinfo.compress_level = 6;
  260.                     }
  261.                     break;
  262.                 case 'a':
  263.                 case 'v':
  264.                 case 'V':
  265.                 case 'I':
  266.                 case 'i':
  267.                 case 'e':
  268.                 case 'g':
  269.                 case 'S':
  270.                 case 'R':    // added by tsuneo 2001.05.14
  271.                     /*ignore*/
  272.                     break;
  273.                 case 'A':
  274.                     stri++;
  275.                     /*ignore*/
  276.                     break;
  277.                 case 'b':
  278.                 case 'N':
  279.                     argi++;
  280.                     stri = argi->end()-1;
  281.                     /*ignore*/
  282.                     break;
  283.  
  284.                 case 'U':
  285.                     if(isdigit(*(stri+1))){
  286.                         stri++;
  287.                     }
  288.                     /*ignore*/
  289.                     break;
  290.                 default:
  291.                     throw CTar32Exception("Invalid Arguments", ERROR_COMMAND_NAME);
  292.                 }
  293.                 stri++;
  294.             }
  295.             argi++;
  296.         }else{
  297.             const char *file = (*argi).c_str();
  298.             if(*file && ((char*)_mbsrchr((const unsigned char*)file, '\\') == file + strlen(file) - 1)){
  299.                 current_directory = (char*)file;
  300.             }else if(cmdinfo.arcfile.length() == 0){
  301.                 cmdinfo.arcfile = *argi;
  302.             }else{
  303.                 string file = *argi;
  304.                 cmdinfo.argfiles.push_back(CTar32CmdInfo::CArgs(file,current_directory));
  305.             }
  306.             argi++;
  307.         }
  308.     }
  309.     if(cmdinfo.arcfile.empty()){
  310.         throw CTar32Exception("Archive File is not specified.", ERROR_NOT_ARC_FILE);
  311.     }
  312.     if(cmdinfo.argfiles.empty()){
  313.         if(command == 'x' || command == 'l'){
  314.             // If no file to extract/create is specified, I assume as all file is specified.
  315.             cmdinfo.argfiles.push_back(CTar32CmdInfo::CArgs("*",current_directory));
  316.         }else{
  317.             throw CTar32Exception("no file to archive is specified.", ERROR_NOT_ARC_FILE);
  318.         }
  319.     }
  320.     if(cmdinfo.b_archive_tar){
  321.         switch(cmdinfo.archive_type){
  322.         case ARCHIVETYPE_NORMAL:
  323.             cmdinfo.archive_type = ARCHIVETYPE_TAR;break;
  324.         case ARCHIVETYPE_GZ:
  325.             cmdinfo.archive_type = ARCHIVETYPE_TARGZ;break;
  326.         case ARCHIVETYPE_Z:
  327.             cmdinfo.archive_type = ARCHIVETYPE_TARZ;break;
  328.         case ARCHIVETYPE_BZ2:
  329.             cmdinfo.archive_type = ARCHIVETYPE_TARBZ2;break;
  330.         }
  331.     }
  332.     //string arcfile = *argi++;
  333.     //list<string> files(argi,args.end());
  334.     
  335.     //CTar32CmdInfo cmdinfo;
  336.     //cmdinfo.arcfile = arcfile;
  337.     //cmdinfo.files = files;
  338.  
  339.     /*
  340.     CTar32StatusDialog dlg;
  341.     if(cmdinfo.b_display_dialog){
  342.         cmdinfo.hTar32StatusDialog = dlg.Create(hwnd);
  343.     }
  344.     switch(command){
  345.     case 'x':
  346.         cmd_extract(cmdinfo);
  347.         break;
  348.     case 'c':
  349.         cmd_create(cmdinfo);
  350.         break;
  351.     case 'l':
  352.         cmd_list(cmdinfo);
  353.         break;
  354.     default:
  355.         throw CTar32Exception("Command not specified.", ERROR_COMMAND_NAME);
  356.     }
  357.     */
  358.     cmdinfo.command = command;
  359.     cmdinfo.hParentWnd = hwnd;
  360.     int func_ret = 0;
  361.     // extern static void _cdecl tar_cmd_main_thread(LPVOID param);
  362.     //DWORD process_id,thread_id;
  363.     //thread_id = GetWindowThreadProcessId(hwnd,&process_id);
  364.     if(cmdinfo.b_message_loop && cmdinfo.hParentWnd /*&& process_id == GetCurrentProcessId() && thread_id == GetCurrentThreadId()*/){
  365.         cmdinfo.wm_main_thread_end = RegisterWindowMessage("wm_main_thread_end");
  366.         cmdinfo.idMessageThread = GetCurrentThreadId();
  367.         MSG msg;
  368.         HANDLE hThread = (HANDLE)_beginthread(tar_cmd_main_thread,0,(void*)&cmdinfo);
  369.         while(GetMessage(&msg,NULL,0,0)){
  370.             if(msg.message == cmdinfo.wm_main_thread_end){
  371.                 unsigned long exitcode;
  372.                 while(GetExitCodeThread(hThread,&exitcode) && (exitcode==STILL_ACTIVE)){
  373.                     Sleep(1);
  374.                 }
  375.                 break;
  376.             }
  377.             if(!IsDialogMessage(cmdinfo.hTar32StatusDialog,&msg)){
  378.                 TranslateMessage(&msg);
  379.                 DispatchMessage(&msg);
  380.             }
  381.         }
  382.     }else{
  383.         /*func_ret =*/ tar_cmd_main_thread((LPVOID)&cmdinfo);
  384.     }
  385.     if(cmdinfo.exception.m_code != 0){
  386.         throw cmdinfo.exception;
  387.     }
  388.     return func_ret;
  389. }
  390. static void _cdecl tar_cmd_main_thread(LPVOID param)
  391. {
  392.     CTar32CmdInfo *pCmdInfo = (CTar32CmdInfo*)param;
  393.     CTar32CmdInfo &cmdinfo = *pCmdInfo;
  394.     CTar32StatusDialog dlg;
  395.  
  396.     try{
  397.         if(cmdinfo.b_display_dialog){
  398.             cmdinfo.hTar32StatusDialog = dlg.Create(cmdinfo.hParentWnd);
  399.         }
  400.         switch(cmdinfo.command){
  401.         case 'x':
  402.             cmd_extract(cmdinfo);
  403.             break;
  404.         case 'c':
  405.             cmd_create(cmdinfo);
  406.             break;
  407.         case 'l':
  408.             cmd_list(cmdinfo);
  409.             break;
  410.         default:
  411.             throw CTar32Exception("Command not specified.", ERROR_COMMAND_NAME);
  412.         }
  413.         // メッセージループの終了前にダイアログを閉じる!  2000/03/03 by tsuneo
  414.         dlg.Destroy();
  415.         if(pCmdInfo->wm_main_thread_end)PostThreadMessage(pCmdInfo->idMessageThread, pCmdInfo->wm_main_thread_end, 0, 0);
  416.     }catch(CTar32Exception &e){
  417.         dlg.Destroy();
  418.         if(pCmdInfo->wm_main_thread_end)PostThreadMessage(pCmdInfo->idMessageThread, pCmdInfo->wm_main_thread_end, 0, 0);
  419.         cmdinfo.exception = e;
  420.         // throw e;
  421.     }
  422.     //return 0;
  423. }
  424.  
  425. static int SendArcMessage(CTar32CmdInfo &cmdinfo, int arcmode, EXTRACTINGINFOEX *pExtractingInfoEx)
  426. {
  427.         extern UINT wm_arcextract;
  428.         int ret1,ret2,ret3;
  429.         extern HWND g_hwndOwnerWindow;
  430.         extern ARCHIVERPROC *g_pArcProc;
  431.         ret1 = ret2 = ret3 = 0;
  432.  
  433.         if(cmdinfo.hTar32StatusDialog){
  434.             ret1 = ::SendMessage(cmdinfo.hTar32StatusDialog, wm_arcextract, arcmode, (long)pExtractingInfoEx);
  435.         }
  436.         if(g_hwndOwnerWindow){
  437.             ret2 = ::SendMessage(g_hwndOwnerWindow,wm_arcextract, arcmode,(long)pExtractingInfoEx);
  438.         }
  439.         if(g_pArcProc){
  440.             ret3 = (*g_pArcProc)(g_hwndOwnerWindow, wm_arcextract, arcmode, pExtractingInfoEx);
  441.         }
  442.         return (ret1 || ret2 || ret3);
  443. }
  444.  
  445. static bool extract_file(CTar32CmdInfo &cmdinfo, CTar32 *pTarfile, const char *fname)
  446. {
  447.     CTar32FileStatus &stat = pTarfile->m_currentfile_status;
  448.     string fname2 = fname;
  449.  
  450.     EXTRACTINGINFOEX extractinfo;
  451.     {
  452.         memset(&extractinfo,0,sizeof(extractinfo));
  453.         extractinfo.exinfo.dwFileSize = stat.original_size;
  454.         extractinfo.exinfo.dwWriteSize = 0;
  455.         strncpy(extractinfo.exinfo.szSourceFileName, pTarfile->get_arc_filename().c_str(),FNAME_MAX32+1);
  456.         strncpy(extractinfo.exinfo.szDestFileName, fname2.c_str(), FNAME_MAX32+1);
  457.         extractinfo.dwCompressedSize = stat.compress_size;
  458.         extractinfo.dwCRC = stat.chksum;
  459.         extractinfo.uOSType = 0;
  460.         extractinfo.wRatio = extractinfo.exinfo.dwFileSize ? (1000 * extractinfo.dwCompressedSize / extractinfo.exinfo.dwFileSize) : 0;
  461.         extractinfo.wDate = GetARCDate(stat.mtime);
  462.         extractinfo.wTime = GetARCTime(stat.mtime);
  463.         GetARCAttribute(stat.mode, extractinfo.szAttribute,sizeof(extractinfo.szAttribute));
  464.         GetARCMethod(pTarfile->m_archive_type, extractinfo.szMode, sizeof(extractinfo.szMode));
  465.         int ret = SendArcMessage(cmdinfo, ARCEXTRACT_BEGIN, &extractinfo);
  466.         if(ret){throw CTar32Exception("Cancel button was pushed.",ERROR_USER_CANCEL);}
  467.         fname2 = extractinfo.exinfo.szDestFileName;
  468.     }
  469.  
  470.     int filesize = pTarfile->m_currentfile_status.original_size;
  471.  
  472.     CTar32InternalFile file; file.open(pTarfile);
  473.  
  474.  
  475.     // ofstream fs_w;
  476.     fast_fstream fs_w;
  477.     if(!cmdinfo.b_print){
  478.         mkdir_recursive(get_dirname(fname2.c_str()).c_str());
  479.         fs_w.open(fname2.c_str(), ios::out|ios::binary);
  480.         if(fs_w.fail()){return false;}
  481.     }
  482.  
  483.     int readsize = 0;
  484.     while(filesize ==-1 || readsize<filesize){
  485.         char buf[65536];
  486.         int nextreadsize;
  487.         if(filesize == -1){ // case ".gz",".Z",".bz2"
  488.             nextreadsize = sizeof(buf);
  489.         }else{
  490.             nextreadsize = min((int)filesize-readsize,(int)sizeof(buf));
  491.         }
  492.         int n = file.read(buf,nextreadsize);
  493.         readsize += n;
  494.         if(cmdinfo.b_print){
  495.             cmdinfo.output.write(buf,n);
  496.         }else{
  497.             fs_w.write(buf,n);
  498.             if(fs_w.fail()){return false;}
  499.         }
  500.         if(n != nextreadsize){
  501.             if(filesize == -1){ // case .gz/.Z/.bz2"
  502.                 break;
  503.             }else{
  504.                 return false;
  505.             }
  506.         }
  507.         if(cmdinfo.hTar32StatusDialog){
  508.             extractinfo.exinfo.dwWriteSize = readsize;
  509.             int ret = SendArcMessage(cmdinfo, ARCEXTRACT_INPROCESS, &extractinfo);
  510.             if(ret){throw CTar32Exception("Cancel button was pushed.",ERROR_USER_CANCEL);}
  511.         }
  512.     }
  513.     if(!cmdinfo.b_print){
  514.         fs_w.close();
  515.         struct _utimbuf ut;
  516.         ut.actime = (stat.atime ? stat.atime : time(NULL));
  517.         ut.modtime = (stat.mtime ? stat.mtime : time(NULL));
  518.         int ret;
  519.         ret = _utime(fname2.c_str(), &ut);
  520.         ret = _chmod(fname2.c_str(), stat.mode);
  521.     }
  522.     if(cmdinfo.hTar32StatusDialog){
  523.         extractinfo.exinfo.dwWriteSize = readsize;
  524.         int ret = SendArcMessage(cmdinfo, ARCEXTRACT_END, &extractinfo);
  525.         if(ret){throw CTar32Exception("Cancel button was pushed.",ERROR_USER_CANCEL);}
  526.     }
  527.     return true;
  528. }
  529. static void cmd_extract(CTar32CmdInfo &cmdinfo)
  530. {
  531.     {
  532.         EXTRACTINGINFOEX extractinfo;
  533.         memset(&extractinfo,0,sizeof(extractinfo));
  534.         strncpy(extractinfo.exinfo.szSourceFileName, cmdinfo.arcfile.c_str() ,FNAME_MAX32);
  535.         int ret = SendArcMessage(cmdinfo, ARCEXTRACT_BEGIN, &extractinfo);
  536.     }
  537.     
  538.     CTar32 tarfile;
  539.     int ret;
  540.     ret = tarfile.open(cmdinfo.arcfile.c_str(), "rb");
  541.     if(!ret){
  542.         throw CTar32Exception("can't open archive file", ERROR_ARC_FILE_OPEN);
  543.     }
  544.  
  545.     CTar32FileStatus stat;
  546.     while(true){
  547.         bool bret = tarfile.readdir(&stat);
  548.         if(!bret){break;}
  549.         
  550.         string file_internal = stat.filename;
  551.         string file_external;
  552.         {
  553.             const list<CTar32CmdInfo::CArgs> &args = cmdinfo.argfiles;
  554.             list<CTar32CmdInfo::CArgs>::const_iterator filei;
  555.             for(filei = args.begin();filei!=args.end();filei++){
  556.                 if(::is_regexp_match_dbcs(filei->file.c_str(), file_internal.c_str())){
  557.                     string file_internal2 = file_internal;
  558.                     if(!cmdinfo.b_use_directory){
  559.                         file_internal2 = get_filename(file_internal.c_str());
  560.                     }
  561.                     file_external = make_pathname(filei->current_dir.c_str(), file_internal2.c_str());
  562.                     break;
  563.                 }
  564.             }
  565.         }
  566.         if(file_external.empty()){
  567.             bret = tarfile.readskip();
  568.         }else{
  569.             bool bret2 = extract_file(cmdinfo,&tarfile,file_external.c_str());
  570.         }
  571.     }
  572.  
  573.     {
  574.         EXTRACTINGINFOEX extractinfo;
  575.         memset(&extractinfo,0,sizeof(extractinfo));
  576.         int ret = SendArcMessage(cmdinfo, ARCEXTRACT_END, &extractinfo);
  577.     }
  578. }
  579. static bool add_file(CTar32CmdInfo &cmdinfo, CTar32 *pTarfile, const char *fname)
  580. {
  581.     CTar32FileStatus &stat = pTarfile->m_currentfile_status;
  582.     string fname2 = fname;
  583.  
  584.     EXTRACTINGINFOEX extractinfo;
  585.     {
  586.         memset(&extractinfo,0,sizeof(extractinfo));
  587.         extractinfo.exinfo.dwFileSize = stat.original_size;
  588.         extractinfo.exinfo.dwWriteSize = 0;
  589.         strncpy(extractinfo.exinfo.szSourceFileName, pTarfile->get_arc_filename().c_str(),FNAME_MAX32+1);
  590.         strncpy(extractinfo.exinfo.szDestFileName, fname2.c_str(), FNAME_MAX32+1);
  591.         extractinfo.dwCompressedSize = stat.compress_size;
  592.         extractinfo.dwCRC = stat.chksum;
  593.         extractinfo.uOSType = 0;
  594.         extractinfo.wRatio = extractinfo.exinfo.dwFileSize ? (1000 * extractinfo.dwCompressedSize / extractinfo.exinfo.dwFileSize) : 0;
  595.         extractinfo.wDate = GetARCDate(stat.mtime);
  596.         extractinfo.wTime = GetARCTime(stat.mtime);
  597.         GetARCAttribute(stat.mode, extractinfo.szAttribute,sizeof(extractinfo.szAttribute));
  598.         GetARCMethod(pTarfile->m_archive_type, extractinfo.szMode, sizeof(extractinfo.szMode));
  599.         int ret = SendArcMessage(cmdinfo, ARCEXTRACT_BEGIN, &extractinfo);
  600.         if(ret){throw CTar32Exception("Cancel button was pushed.",ERROR_USER_CANCEL);}
  601.         // fname2 = extractinfo.exinfo.szDestFileName;
  602.     }
  603.  
  604.     int filesize = pTarfile->m_currentfile_status.original_size;
  605.     if(filesize == 0){return true;}
  606.     CTar32InternalFile file; file.open(pTarfile, /*write*/true);
  607.  
  608.  
  609.     // ofstream fs_w;
  610.     fast_fstream fs_r;
  611.     fs_r.open(fname2.c_str(), ios::in|ios::binary);
  612.     if(fs_r.fail()){throw CTar32Exception("can't read file", ERROR_CANNOT_READ);return false;}
  613.  
  614.     int readsize = 0;
  615.     int n;
  616.     char buf[65536];
  617.     while(fs_r.read(buf,sizeof(buf)),(n=fs_r.gcount())>0){
  618.         int m = file.write(buf, n);
  619.         if(m>0){readsize += m;}
  620.         if(n!=m){
  621.             throw CTar32Exception("can't write to arcfile", ERROR_CANNOT_WRITE);
  622.         }
  623.         if(cmdinfo.hTar32StatusDialog){
  624.             extractinfo.exinfo.dwWriteSize = readsize;
  625.             int ret = SendArcMessage(cmdinfo, ARCEXTRACT_INPROCESS, &extractinfo);
  626.             if(ret){throw CTar32Exception("Cancel button was pushed.",ERROR_USER_CANCEL);}
  627.         }
  628.     }
  629.     bool bret = file.close();
  630.     if(!bret){throw CTar32Exception("can't write to arcfile", ERROR_CANNOT_WRITE);}
  631.     if(cmdinfo.hTar32StatusDialog){
  632.         extractinfo.exinfo.dwWriteSize = readsize;
  633.         int ret = SendArcMessage(cmdinfo, ARCEXTRACT_END, &extractinfo);
  634.         if(ret){throw CTar32Exception("Cancel button was pushed.",ERROR_USER_CANCEL);}
  635.     }
  636.     return true;
  637. }
  638.  
  639. static void cmd_create(CTar32CmdInfo &cmdinfo)
  640. {
  641.     {
  642.         EXTRACTINGINFOEX extractinfo;
  643.         memset(&extractinfo,0,sizeof(extractinfo));
  644.         strncpy(extractinfo.exinfo.szSourceFileName, cmdinfo.arcfile.c_str() ,FNAME_MAX32);
  645.         int ret = SendArcMessage(cmdinfo, ARCEXTRACT_BEGIN, &extractinfo);
  646.     }
  647.  
  648.     
  649.     CTar32 tarfile;
  650.     int ret;
  651.     bool bret;
  652.     int filenum = 0;
  653.  
  654.     ret = tarfile.open(cmdinfo.arcfile.c_str(), "wb", cmdinfo.archive_type);
  655.     if(!ret){
  656.         throw CTar32Exception("can't open archive file", ERROR_ARC_FILE_OPEN);
  657.     }
  658.  
  659.     // const list<string> &files = cmdinfo.files;
  660.     const list<CTar32CmdInfo::CArgs> &args = cmdinfo.argfiles;
  661.     list<CTar32CmdInfo::CArgs>::const_iterator filei;
  662.     for(filei = args.begin();filei!=args.end();filei++){
  663.         string file_internal = (*filei).file;
  664.         string file_external = make_pathname((*filei).current_dir.c_str(), (*filei).file.c_str());
  665.  
  666.         list<string> files_internal2 = find_files(file_external.c_str());
  667.         if(_mbsrchr((const unsigned char*)file_external.c_str(),'*')==0 && files_internal2.empty()){
  668.             // fixed by tsuneo. 2001.05.15
  669.             throw CTar32Exception((string() + "can't find file [" + file_external + "]").c_str(), ERROR_FILE_OPEN);
  670.         }
  671.         list<string>::iterator files2i;
  672.         for(files2i = files_internal2.begin(); files2i != files_internal2.end(); files2i++){
  673.             string file_external2 = *files2i;
  674.             string file_internal2 = file_external2.substr((*filei).current_dir.length());
  675.             if(!cmdinfo.b_use_directory){
  676.                 file_internal2 = get_filename(file_internal2.c_str());
  677.             }
  678.             CTar32FileStatus stat;
  679.             if(!stat.SetFromFile(file_external2.c_str())){
  680.                 continue;
  681.             }
  682.             convert_yen_to_slash((char*)(file_internal2.c_str()));
  683.             {
  684.                 // if file is directory, add '/' to the tail of filename.
  685.                 struct _stat st;
  686.                 if(_stat(file_external2.c_str(), &st)!=-1 && st.st_mode & _S_IFDIR){
  687.                     char *f = (char*)file_internal2.c_str();
  688.                     if((char*)max(_mbsrchr((unsigned char*)f, '/'), _mbsrchr((unsigned char*)f,'\\')) != f+strlen(f)-1){
  689.                         file_internal2.append(1,'/');
  690.                     }
  691.                 }
  692.             }
  693.             stat.filename = file_internal2;
  694.             bret = tarfile.addheader(stat);
  695.             // bret = tarfile.addbody(file_external2.c_str());
  696.             bool bret2 = add_file(cmdinfo,&tarfile,file_external2.c_str());
  697.             filenum ++;
  698.         }
  699.     }
  700.  
  701.     {
  702.         EXTRACTINGINFOEX extractinfo;
  703.         memset(&extractinfo,0,sizeof(extractinfo));
  704.         int ret = SendArcMessage(cmdinfo, ARCEXTRACT_END, &extractinfo);
  705.     }
  706.     
  707.     if(filenum == 0){
  708.         // fixed by tsuneo. 2001.05.14
  709.         throw CTar32Exception("There is no file to archive. ", ERROR_FILE_OPEN);
  710.     }
  711. }
  712. static void cmd_list(CTar32CmdInfo &cmdinfo)
  713. {
  714.     CTar32 tarfile;
  715.     bool bret;
  716.     bret = tarfile.open(cmdinfo.arcfile.c_str(), "rb");
  717.     if(!bret){
  718.         throw CTar32Exception("can't open archive file", ERROR_ARC_FILE_OPEN);
  719.     }
  720.     CTar32FileStatus stat;
  721.     bret = true;
  722.     cmdinfo.output << "filename" << "\t" << "filesize" << "\n";
  723.     
  724.     while(1){
  725.         bret = tarfile.readdir(&stat);
  726.         if(!bret){break;}
  727.         bret = tarfile.readskip();
  728.         if(!bret){break;}
  729.         cmdinfo.output << stat.filename << "\t" << stat.original_size << "\n";
  730.     }
  731. }
  732.