home *** CD-ROM | disk | FTP | other *** search
/ PC Open 48 / pcopen48.iso / Internet / HtTrack / DATA1.CAB / Sources / src / htsmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-21  |  62.4 KB  |  1,727 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche, Yann Philippot
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. This project has been developed by Xavier Roche and Yann Philippot,
  29. from the company Serianet at Caen, France (http://www.serianet.com)
  30.  
  31. Please visit our Website: http://www.httrack.com
  32. */
  33.  
  34.  
  35. /* ------------------------------------------------------------ */
  36. /* File: httrack.c subroutines:                                 */
  37. /*       main routine (first called)                            */
  38. /* Author: Xavier Roche                                         */
  39. /* ------------------------------------------------------------ */
  40.  
  41. #include "htsmain.h"
  42.  
  43. #include "htsglobal.h"
  44. #include "httrack.h"
  45. #include "htsdefines.h"
  46. #include "htsalias.h"
  47. #include <ctype.h>
  48. #if HTS_WIN
  49. #else
  50. #ifndef HTS_DO_NOT_USE_UID
  51. /* setuid */
  52. #include <pwd.h>
  53. #include <unistd.h>
  54. #endif
  55. #endif
  56.  
  57. extern int exit_xh;          // sortir prΘmaturΘment
  58.  
  59. // Add a command in the argc/argv
  60. #define cmdl_add(token,argc,argv,buff,ptr) \
  61.   argv[argc]=(buff+ptr); \
  62.   strcpy(argv[argc],token); \
  63.   ptr += (strlen(argv[argc])+1); \
  64.   argc++
  65.  
  66. // Insert a command in the argc/argv
  67. #define cmdl_ins(token,argc,argv,buff,ptr) \
  68.   { \
  69.   int i; \
  70.   for(i=argc;i>0;i--)\
  71.   argv[i]=argv[i-1];\
  72.   } \
  73.   argv[0]=(buff+ptr); \
  74.   strcpy(argv[0],token); \
  75.   ptr += (strlen(argv[0])+1); \
  76.   argc++
  77.  
  78.  
  79. // Main, rΘcupΦre les paramΦtres et appelle le robot
  80. #if HTS_ANALYSTE!=2
  81. int main(int argc, char **argv) {
  82. #else
  83. int hts_main(int argc, char **argv) {
  84. #endif
  85.   char* x_argv[999];      // Patch pour argv et argc: en cas de rΘcupΘration de ligne de commande
  86.   char* x_argvblk=NULL;   // (reprise ou update)
  87.   int   x_ptr=0;          // offset
  88.   /*
  89.   char* x_argv2[999];     // Patch pour config
  90.   char* x_argvblk2=NULL;
  91.   */
  92.   //
  93.   int argv_url=-1;         // ==0 : utiliser cache et doit.log
  94.   char url[65536];         // URLS sΘparΘes par un espace
  95.   int httrack_level=9999;  // mirror total par dΘfaut
  96.   // the parametres
  97.   httrackp httrack;
  98.   int httrack_logmode=3;   // ONE log file
  99.   int recuperer=0;       // rΘcupΘrer un plantage (n'arrive jamais, α supprimer)
  100. #if HTS_WIN
  101. #if HTS_ANALYSTE!=2
  102.   WORD   wVersionRequested; /* requested version WinSock API */ 
  103.   WSADATA wsadata;        /* Windows Sockets API data */
  104. #endif
  105. #else
  106. #ifndef HTS_DO_NOT_USE_UID
  107.   int switch_uid=-1,switch_gid=-1;      /* setuid/setgid */
  108. #endif
  109.   int switch_chroot=0;                  /* chroot ? */
  110. #endif
  111.   //
  112.   url[0]='\0';
  113.   //
  114.  
  115.   // options par dΘfaut
  116.   bzero((char *)&httrack, sizeof(httrackp));
  117.   httrack.wizard=2;   // wizard automatique
  118.   httrack.quiet=0;     // questions
  119.   //  
  120.   httrack.travel=0;   // mΩme adresse
  121.   httrack.seeker=1;   // down 
  122.   httrack.urlmode=2;  // relatif par dΘfaut
  123.   httrack.debug=0;    // pas de dΘbug en plus
  124.   httrack.getmode=3;  // linear scan
  125.   httrack.maxsite=-1; // taille max site (aucune)
  126.   httrack.maxfile_nonhtml=-1; // taille max fichier non html
  127.   httrack.maxfile_html=-1;    // idem pour html
  128.   httrack.maxsoc=8;     // nbre socket max
  129.   httrack.fragment=-1;  // pas de fragmentation
  130.   httrack.nearlink=0;   // ne pas prendre les liens non-html "adjacents"
  131.   httrack.makeindex=1;  // faire un index
  132.   httrack.delete_old=1; // effacer anciens fichiers
  133.   httrack.makestat=0;  // pas de fichier de stats
  134.   httrack.maketrack=0; // ni de tracking
  135.   httrack.timeout=120; // timeout par dΘfaut (2 minutes)
  136.   httrack.cache=1;     // cache prioritaire
  137.   httrack.shell=0;     // pas de shell par defaut
  138.   httrack.proxy.active=0;    // pas de proxy
  139.   httrack.user_agent_send=1; // envoyer un user-agent
  140.   strcpy(httrack.user_agent,"Mozilla/4.5 (compatible; HTTrack 3.0x; Windows 98)");
  141.   httrack.savename_83=0;     // noms longs par dΘfaut
  142.   httrack.savename_type=0;   // avec structure originale
  143.   httrack.parsejava=1;       // parser classes
  144.   httrack.hostcontrol=0;     // PAS de control host pour timeout et traffic jammer
  145.   httrack.retry=2;           // 2 retry par dΘfaut
  146.   httrack.errpage=1;         // copier ou gΘnΘrer une page d'erreur en cas d'erreur (404 etc.)
  147.   httrack.check_type=1;      // vΘrifier type si inconnu (cgi,asp..) SAUF / considΘrΘ comme html
  148.   httrack.all_in_cache=0;    // ne pas tout stocker en cache
  149.   httrack.robots=2;          // traiter les robots.txt
  150.   httrack.external=0;        // liens externes normaux
  151.   httrack.mirror_first_page=0;  // pas mode mirror links
  152.   httrack.accept_cookie=1;   // gΘrer les cookies
  153.   httrack.cookie=NULL;
  154.   httrack.http10=0;          // laisser http/1.1
  155.   httrack.tolerant=0;        // ne pas accepter content-length incorrect
  156.   httrack.parseall=1;        // tout parser (tags inconnus, par exemple)
  157.   httrack.norecatch=0;       // ne pas reprendre les fichiers effacΘs par l'utilisateur
  158.   httrack.verbosedisplay=0;  // pas d'animation texte
  159.   strcpy(httrack.footer,HTS_DEFAULT_FOOTER);
  160.   httrack.ftp_proxy=1;       // proxy http pour ftp
  161.   strcpy(httrack.filelist,"");
  162.   //
  163.   httrack.log=stdout;
  164.   httrack.errlog=stderr;
  165.   httrack.flush=1;           // flush sur les fichiers log
  166.   httrack.aff_progress=0;
  167.   httrack.keyboard=0;
  168.   //
  169.   strcpy(httrack.path_html,"");
  170.   strcpy(httrack.path_log,"");
  171.   //
  172.   httrack.maxlink=100000;    // 100,000 liens max par dΘfaut (400Kb)
  173.   httrack.maxfilter=500;     // 500 filtres max par dΘfaut (400Kb)
  174.   httrack.maxcache=1048576;   // a peu prΦs 1Mo en cache max -- OPTION NON PARAMETRABLE POUR L'INSTANT --
  175.   httrack.maxcache_anticipate=256;  // maximum de liens α anticiper
  176.   httrack.maxtime=-1;        // temps max en secondes
  177.   httrack.maxrate=-1;        // pas de taux maxi
  178.   httrack.maxconn=10;        // nombre connexions/s
  179.   httrack.waittime=-1;      // wait until.. hh*3600+mm*60+ss
  180.   //
  181.   httrack.exec=argv[0];
  182.   //
  183.   _DEBUG_HEAD=0;            // pas de debuggage en tΩtes
  184.   
  185. #if HTS_WIN
  186. #if HTS_ANALYSTE!=2
  187.   {
  188.     int stat;
  189.     wVersionRequested = 0x0101;
  190.     stat = WSAStartup( wVersionRequested, &wsadata );
  191.     if (stat != 0) {
  192.       HTS_PANIC_PRINTF("Winsock not found!\n");
  193.       return -1;
  194.     } else if (LOBYTE(wsadata.wVersion) != 1  && HIBYTE(wsadata.wVersion) != 1) {
  195.       HTS_PANIC_PRINTF("WINSOCK.DLL does not support version 1.1\n");
  196.       WSACleanup();
  197.       return -1;
  198.     }
  199.   }
  200. #endif
  201. #endif
  202.  
  203.  
  204.   /* First test: if -#R then only launch ftp */
  205.   if (argc > 2) {
  206.     if (strcmp(argv[1],"-#R")==0) {
  207.       if (argc==6) {
  208.         lien_back r;
  209.         char* path;
  210.         FILE* fp;
  211.         strcpy(r.url_adr,argv[2]);
  212.         strcpy(r.url_fil,argv[3]);
  213.         strcpy(r.url_sav,argv[4]);
  214.         path=argv[5];
  215.         r.status=1000;
  216.         run_launch_ftp(&r);
  217.         fp=fopen(fconv(path),"wb");
  218.         if (fp) {
  219.           fprintf(fp,"%d %s",r.r.statuscode,r.r.msg);
  220.           fclose(fp); fp=NULL;
  221.           rename(fconv(path),fconcat(path,".ok"));
  222.         } else remove(fconv(path));
  223.       } else {
  224.         printf("htsftp error, wrong parameter number (%d)\n",argc);
  225.       }
  226.       exit(0);   // pas _exit()
  227.     }
  228.   }
  229.  
  230.   // ok, non ftp, continuer
  231.  
  232.  
  233.   /* filter CR, LF, TAB.. */
  234.   {
  235.     int na;
  236.     for(na=1;na<argc;na++) {
  237.       char* a;
  238.       while( (a=strchr(argv[na],'\x0d')) ) *a=' ';
  239.       while( (a=strchr(argv[na],'\x0a')) ) *a=' ';
  240.       while( (a=strchr(argv[na],9)) )      *a=' ';
  241.       /* equivalent to "empty parameter" */
  242.       if ((strcmp(argv[na],HTS_NOPARAM)==0) || (strcmp(argv[na],HTS_NOPARAM2)==0))        // (none)
  243.         strcpy(argv[na],"\"\"");
  244.       if (strncmp(argv[na],"-&",2)==0)
  245.         argv[na][1]='%';
  246.     }
  247.   }
  248.  
  249.  
  250.  
  251.   /* create x_argvblk buffer for transformed command line */
  252.   {
  253.     int current_size=0;
  254.     int size;
  255.     int na;
  256.     for(na=0;na<argc;na++)
  257.       current_size += (strlen(argv[na]) + 1);
  258.     if ((size=fsize("config"))>0)
  259.       current_size += size;
  260.     x_argvblk=(char*) malloct(current_size+32768);
  261.     if (x_argvblk == NULL) {
  262.       HTS_PANIC_PRINTF("Error, not enough memory");
  263.       return -1;
  264.     }
  265.     x_argvblk[0]='\0';
  266.     x_ptr=0;
  267.   }
  268.  
  269.   /* Create new argc/argv, replace alias, count URLs, treat -h, -q, -i */
  270.   {
  271.     char _tmp_argv[2][HTS_CDLMAXSIZE];
  272.     char* tmp_argv[2];
  273.     char tmp_error[HTS_CDLMAXSIZE];
  274.     int tmp_argc;
  275.     int x_argc=0;
  276.     int na;
  277.     tmp_argv[0]=_tmp_argv[0];
  278.     tmp_argv[1]=_tmp_argv[1];
  279.     //
  280.     argv_url=0;       /* pour comptage */
  281.     //
  282.     cmdl_add(argv[0],x_argc,x_argv,x_argvblk,x_ptr);
  283.     na=1;             /* commencer aprΦs nom_prg */
  284.     while(na<argc) {
  285.       int result=1;
  286.       tmp_argv[0][0]=tmp_argv[1][0]='\0';
  287.  
  288.       /* VΘrifier argv[] non vide */
  289.       if (strnotempty(argv[na])) {
  290.         
  291.         /* VΘrifier Commande (alias) */
  292.         result=optalias_check(argc,(const char * const *)argv,na,
  293.           &tmp_argc,(char**)tmp_argv,tmp_error);
  294.         if (!result) {
  295.           HTS_PANIC_PRINTF(tmp_error);
  296.           return -1;
  297.         }
  298.         
  299.         /* Copier */
  300.         cmdl_add(tmp_argv[0],x_argc,x_argv,x_argvblk,x_ptr);
  301.         if (tmp_argc > 1) {
  302.           cmdl_add(tmp_argv[1],x_argc,x_argv,x_argvblk,x_ptr);
  303.         }
  304.         
  305.         /* Compter URLs et dΘtecter -i,-q.. */
  306.         if (tmp_argc == 1) {           /* pas -P & co */
  307.           if (!cmdl_opt(tmp_argv[0])) {   /* pas -c0 & co */
  308.             if (argv_url>=0)
  309.               argv_url++;
  310.           } else {
  311.             if (strcmp(tmp_argv[0],"-h")==0) {
  312.               help(argv[0],!httrack.quiet);
  313.               return 0;
  314.             } else {
  315.               if (strncmp(tmp_argv[0],"--",2)) {   /* pas */
  316.                 if ((strchr(tmp_argv[0],'q')!=NULL))
  317.                   httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  318.                 if ((strchr(tmp_argv[0],'i')!=NULL)) {  // doit.log!
  319.                   argv_url=-1;        /* forcer */
  320.                   httrack.quiet=1;
  321.                 }
  322.               }
  323.             }
  324.           }
  325.         } else if (tmp_argc == 2) {
  326.           if ((strcmp(tmp_argv[0],"-%L")==0)) {  // liste d'URLs
  327.             if (argv_url>=0)
  328.               argv_url++;        /* forcer */
  329.           }
  330.         }
  331.       }
  332.  
  333.       na+=result;
  334.     }
  335.     if (argv_url<0)
  336.       argv_url=0;
  337.  
  338.     /* Nouveaux argc et argv */
  339.     argv=x_argv;
  340.     argc=x_argc;
  341.   }
  342.  
  343.  
  344.  
  345.  
  346.   // Ici on ajoute les arguments de config
  347. /*
  348.   if (fexist("config")) {    // configuration        
  349.     x_argvblk2=(char*) calloct(32768,1);
  350.  
  351.     if (x_argvblk2!=NULL) {
  352.       FILE* fp;
  353.       int x_argc2;
  354.           
  355.       //strcpy(x_argvblk2,"httrack ");
  356.       fp=fopen("config","rb");
  357.       if (fp) {
  358.         linput(fp,x_argvblk2+strlen(x_argvblk2),32000);
  359.         fclose(fp); fp=NULL;
  360.     
  361.         // calculer arguments selon derniers arguments
  362.         x_argv2[0]=argv[0];
  363.         x_argc2=1;
  364.         {
  365.           char* p=x_argvblk2;
  366.           do {
  367.             x_argv2[x_argc2++]=p;
  368.             p=strchr(p,' ');
  369.             if (p) {
  370.               *p=0;    // octet nul (tableau)
  371.               p++;
  372.             }            
  373.           } while(p!=NULL);
  374.         }
  375.         // recopier arguments actuels (pointeurs uniquement)
  376.         {
  377.          int na;
  378.           for(na=1;na<argc;na++) {
  379.             x_argv2[x_argc2++]=argv[na];
  380.           }
  381.         }
  382.         argc=x_argc2;      // nouvel argc
  383.         argv=x_argv2;      // nouvel argv
  384.       }
  385.     }
  386.   }
  387. */
  388.  
  389.  
  390.   // Traiter l'option -O en premier!
  391.   { 
  392.     char* com;
  393.     int na;
  394.     
  395.     for(na=1;na<argc;na++) {
  396.       
  397.       if (argv[na][0]=='"') {
  398.         char tempo[HTS_CDLMAXSIZE];
  399.         strcpy(tempo,argv[na]+1);
  400.         if (tempo[strlen(tempo)-1]!='"') {
  401.           char s[HTS_CDLMAXSIZE];
  402.           sprintf(s,"Missing quote in %s",argv[na]);
  403.           HTS_PANIC_PRINTF(s);
  404.           return -1;
  405.         }
  406.         tempo[strlen(tempo)-1]='\0';
  407.         strcpy(argv[na],tempo);
  408.       }
  409.       
  410.       if (cmdl_opt(argv[na])) { // option
  411.         com=argv[na]+1;
  412.         
  413.         while(*com) {
  414.           switch(*com) {
  415.           case 'O':    // output path
  416.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  417.               HTS_PANIC_PRINTF("Option O needs to be followed by a blank space, and a path (or path,path)");
  418.               printf("Example: -O /binary/\n");
  419.               printf("Example: -O /binary/,/log/\n");
  420.               return -1;
  421.             } else {
  422.               char* a;
  423.               na++;
  424.               strcpy(httrack.path_html,"");
  425.               strcpy(httrack.path_log,"");
  426.               a=strstr(argv[na],"\",\"");  // rechercher en premier, au cas ou -O "c:\pipo,test","c:\test"
  427.               if (!a)
  428.                 a=strchr(argv[na],',');  // 2 path
  429.               else
  430.                 a++;  // position ,
  431.               if (a) {
  432.                 strncat(httrack.path_html,argv[na],(int) a-(int) argv[na]);
  433.                 strcat(httrack.path_log,a+1);
  434.               } else {
  435.                 strcpy(httrack.path_log,argv[na]);
  436.                 strcpy(httrack.path_html,argv[na]);
  437.               }
  438.               // Eliminer les cas comme -O "C:\mirror\"
  439.               if (httrack.path_log[0]=='"') {  // Guillemets
  440.                 char tmp[256];
  441.                 strcpy(tmp,httrack.path_log+1);
  442.                 if (tmp[strlen(tmp)-1]=='"')
  443.                   tmp[strlen(tmp)-1]='\0';
  444.                 strcpy(httrack.path_log,tmp);
  445.               }
  446.               if (httrack.path_html[0]=='"') {
  447.                 char tmp[256];
  448.                 strcpy(tmp,httrack.path_html+1);
  449.                 if (tmp[strlen(tmp)-1]=='"')
  450.                   tmp[strlen(tmp)-1]='\0';
  451.                 strcpy(httrack.path_html,tmp);
  452.               }
  453.               check_path(httrack.path_log);
  454.               check_path(httrack.path_html);
  455.               
  456.               //printf("-->%s\n%s\n",httrack.path_html,httrack.path_log);
  457.               
  458.             }
  459.             break;
  460.           }  // switch
  461.           com++;    
  462.         }  // while
  463.         
  464.       }  // arg
  465.       
  466.     }  // for
  467.   }  // traiter -O
  468.   
  469.   /* if doit.log exists, or if new URL(s) defined, 
  470.      then DO NOT load standard config files */
  471.   /* (config files are added in doit.log) */
  472. #if DEBUG_STEPS
  473.   printf("Loading httrackrc/doit.log\n");
  474. #endif
  475.   /* recreate a doit.log (no old doit.log or new URLs (and parameters)) */
  476.   if ( (!fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) || (argv_url>0) ) {
  477.     if (!optinclude_file(fconcat(httrack.path_log,HTS_HTTRACKRC),&argc,argv,x_argvblk,&x_ptr))
  478.       if (!optinclude_file(HTS_HTTRACKRC,&argc,argv,x_argvblk,&x_ptr)) {
  479.         if (!optinclude_file(fconcat(hts_gethome(),"/"HTS_HTTRACKRC),&argc,argv,x_argvblk,&x_ptr)) {
  480. #ifdef HTS_HTTRACKCNF
  481.           optinclude_file(HTS_HTTRACKCNF,&argc,argv,x_argvblk,&x_ptr);
  482. #endif
  483.         }
  484.       }
  485.   } 
  486.   /* load doit.log and insert in current command line */
  487.   else {
  488.     FILE* fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"rb");
  489.     if (fp) {
  490.       int insert_after=1;     /* insΘrer aprΦs nom au dΘbut */
  491.       //
  492.       char buff[8192];
  493.       char *p,*lastp;
  494.       linput(fp,buff,8000);
  495.       fclose(fp); fp=NULL;
  496.       p=buff;
  497.       do {
  498.         int insert_after_argc;
  499.         // read next
  500.         lastp=p;
  501.         if (p) {
  502.           p=next_token(p,1);
  503.           if (p) {
  504.             *p=0;    // null
  505.             p++;
  506.           }
  507.         }
  508.  
  509.         /* Insert parameters BUT so that they can be in the same order */
  510.         if (lastp) {
  511.           if (strnotempty(lastp)) {
  512.             insert_after_argc=argc-insert_after;
  513.             cmdl_ins(lastp,insert_after_argc,(argv+insert_after),x_argvblk,x_ptr);
  514.             argc=insert_after_argc+insert_after;
  515.             insert_after++;
  516.           }
  517.         }
  518.       } while(lastp!=NULL);
  519.       //fclose(fp);
  520.     }
  521.   }
  522.  
  523.  
  524.   // Existence d'un cache - pas de new mais un old.. renommer
  525. #if DEBUG_STEPS
  526.   printf("Checking cache\n");
  527. #endif
  528.   if ( (!fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) || (!fexist(fconcat(httrack.path_log,"hts-cache/new.ndx"))) ) {
  529.     if ( (fexist(fconcat(httrack.path_log,"hts-cache/old.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) ) {
  530.       remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  531.       remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  532.       rename(fconcat(httrack.path_log,"hts-cache/old.dat"),fconcat(httrack.path_log,"hts-cache/new.dat"));
  533.       rename(fconcat(httrack.path_log,"hts-cache/old.ndx"),fconcat(httrack.path_log,"hts-cache/new.ndx"));
  534.     }
  535.   }
  536.  
  537.   /* Interrupted mirror detected */
  538.   if (!httrack.quiet) {
  539.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {
  540.       /* Old cache */
  541.       if ( (fexist(fconcat(httrack.path_log,"hts-cache/old.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) ) {
  542.         fprintf(httrack.log,"Warning!\n");
  543.         fprintf(httrack.log,"An aborted mirror has been detected!\nThe current temporary cache is required for any update operation and only contains data downloaded during the last aborted session.\nThe former cache might contain more complete information; if you do not want to lose that information, you have to restore it and delete the current cache.\nThis can easily be done here by erasing the hts-cache/new.* files\n");
  544.         fprintf(httrack.log,"Please restart HTTrack with --quiet (-q) option to override this message!\n");
  545.         exit(0);
  546.       }
  547.     }
  548.   }
  549.     
  550.   // remplacer "macros" comme --spider
  551.   // permet de lancer httrack sans a avoir α se rappeler de syntaxes comme p0C0I0Qc32 ..
  552. #if DEBUG_STEPS
  553.   printf("Checking last macros\n");
  554. #endif
  555.   {
  556.     int i;
  557.     for(i=0;i<argc;i++) {
  558. #if DEBUG_STEPS
  559.       printf("Checking #%d:\n",argv[i]);
  560.       printf("%s\n",argv[i]);
  561. #endif
  562.       if (argv[i][0]=='-') {
  563.         if (argv[i][1]=='-') {  // --xxx
  564.           // Note: on fait attention α ne pas Θcrire plus qu'il n'y avait avant..
  565.           /*
  566.           if (strfield2(argv[i]+2,"spider"))       // mode spider (scan+log)
  567.             strcpy(argv[i]+1,"p0C0I0t");
  568.           else if (strfield2(argv[i]+2,"wide-spider"))   // spider
  569.             strcpy(argv[i]+1,"p0C0I0c32t");
  570.           else if (strfield2(argv[i]+2,"tiny-spider"))   // i am a little grasshoper
  571.             strcpy(argv[i]+1,"p0C0I0c1t");
  572.           else if (strfield2(argv[i]+2,"testsite"))       // mode test site (scan+log)
  573.             strcpy(argv[i]+1,"p0C0I0t");
  574.           else if (strfield2(argv[i]+2,"wide-testsite"))
  575.             strcpy(argv[i]+1,"p0C0I0c32t");
  576.           else if (strfield2(argv[i]+2,"tiny-testsite"))
  577.             strcpy(argv[i]+1,"p0C0I0c1t");
  578.           //
  579.           else if (strfield2(argv[i]+2,"testlinks"))       // mode test bookmark (scan+log)
  580.             strcpy(argv[i]+1,"r1p0C0I0t");
  581.           else if (strfield2(argv[i]+2,"wide-testlinks"))
  582.             strcpy(argv[i]+1,"r1p0C0I0c32t");
  583.           else if (strfield2(argv[i]+2,"tiny-testlinks"))
  584.             strcpy(argv[i]+1,"r1p0C0I0c1t");
  585.           else if ( (strfield2(argv[i]+2,"testlink")) || (strfield2(argv[i]+2,"bookmark"))) {
  586.             HTS_PANIC_PRINTF("Please use --testlinks to check links in a page");
  587.             return -1;                
  588.           }
  589.           //
  590.           else if (strfield2(argv[i]+2,"mirrorlinks"))       // mode test bookmark (scan+log)
  591.             strcpy(argv[i]+1,"Y");
  592.           else if (strfield2(argv[i]+2,"wide-mirrorlinks"))       // mode test bookmark (scan+log)
  593.             strcpy(argv[i]+1,"Yc32");
  594.           else if (strfield2(argv[i]+2,"tiny-mirrorlinks"))       // mode test bookmark (scan+log)
  595.             strcpy(argv[i]+1,"Yc1");
  596.           //
  597.           else if (strfield2(argv[i]+2,"mirror"))  // miroir (mode par dΘfaut)
  598.             strcpy(argv[i]+1,"");
  599.           else if (strfield2(argv[i]+2,"wide-mirror"))
  600.             strcpy(argv[i]+1,"c32");
  601.           else if (strfield2(argv[i]+2,"tiny-mirror"))
  602.             strcpy(argv[i]+1,"c1");
  603.           //
  604.           else if (strfield2(argv[i]+2,"testscan"))  // scan sans log
  605.             strcpy(argv[i]+1,"p0C0I0Q");
  606.           else if (strfield2(argv[i]+2,"wide-testscan"))
  607.             strcpy(argv[i]+1,"p0C0I0c32Q");
  608.           else if (strfield2(argv[i]+2,"tiny-testscan"))
  609.             strcpy(argv[i]+1,"p0C0I0c1Q");
  610.           else if ((strfield2(argv[i]+2,"scan")) || (strfield2(argv[i]+2,"test")) || (strfield2(argv[i]+2,"check"))) {
  611.             HTS_PANIC_PRINTF("Please use --spider to check all links in a site");
  612.             return -1;    
  613.           }
  614.           //
  615.           else if (strfield2(argv[i]+2,"skeleton"))    // rΘcupΘrer squelette (html)
  616.             strcpy(argv[i]+1,"p1");
  617.           else if (strfield2(argv[i]+2,"wide-skeleton"))
  618.             strcpy(argv[i]+1,"p1c32");
  619.           else if (strfield2(argv[i]+2,"tiny-skeleton"))
  620.             strcpy(argv[i]+1,"p1c1");
  621.           //
  622.           else if (strfield2(argv[i]+2,"get"))     // rΘcupΘrer fichiers isolΘs
  623.             strcpy(argv[i]+1,"qg");
  624.           else if (strfield2(argv[i]+2,"update"))        // update: pas de question
  625.             strcpy(argv[i]+1,"iC2");
  626.           else if (strfield2(argv[i]+2,"continue"))      // continue, cache prio
  627.             strcpy(argv[i]+1,"iC1");
  628.           else if (strfield2(argv[i]+2,"restart"))       // idem
  629.             strcpy(argv[i]+1,"iC1");
  630.           //
  631.           else if (strfield2(argv[i]+2,"sucker"))        // web sucker...toi aussi suce le web!
  632.             strcpy(argv[i]+1,"r99");
  633.     //
  634.           else if (strfield2(argv[i]+2,"help"))          // un peu d'aide
  635.             strcpy(argv[i]+1,"h");
  636.           else if (strfield2(argv[i]+2,"documentation"))
  637.             strcpy(argv[i]+1,"h");
  638.           else if (strfield2(argv[i]+2,"doc"))
  639.             strcpy(argv[i]+1,"h");
  640.           //
  641.           else if (strfield2(argv[i]+2,"wide"))
  642.             strcpy(argv[i]+1,"c32");
  643.           else if (strfield2(argv[i]+2,"tiny"))
  644.             strcpy(argv[i]+1,"c1");
  645.           else if (strfield2(argv[i]+2,"ultrawide"))  // oulαα..
  646.             strcpy(argv[i]+1,"c48");
  647.           //
  648.           else if (strfield2(argv[i]+2,"http10"))     // use if possible HTTP/1.0
  649.             strcpy(argv[i]+1,"%h");
  650.           //
  651.           else if ( (strfield2(argv[i]+2,"filelist"))     // file list
  652.                   || (strfield2(argv[i]+2,"list")) )
  653.             strcpy(argv[i]+1,"%L");
  654.  
  655.           else */
  656.           if ((strfield2(argv[i]+2,"clean")) || (strfield2(argv[i]+2,"tide"))) {  // nettoyer
  657.             strcpy(argv[i]+1,"");
  658.             if (fexist(fconcat(httrack.path_log,"hts-log.txt")))
  659.               remove(fconcat(httrack.path_log,"hts-log.txt"));
  660.             if (fexist(fconcat(httrack.path_log,"hts-err.txt")))
  661.               remove(fconcat(httrack.path_log,"hts-err.txt"));
  662.             if (fexist(fconcat(httrack.path_html,"index.html")))
  663.               remove(fconcat(httrack.path_html,"index.html"));
  664.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.dat")))
  665.               remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  666.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))
  667.               remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  668.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.dat")))
  669.               remove(fconcat(httrack.path_log,"hts-cache/old.dat"));
  670.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx")))
  671.               remove(fconcat(httrack.path_log,"hts-cache/old.ndx"));
  672.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.lst")))
  673.               remove(fconcat(httrack.path_log,"hts-cache/new.lst"));
  674.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.lst")))
  675.               remove(fconcat(httrack.path_log,"hts-cache/old.lst"));
  676.             if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log")))
  677.               remove(fconcat(httrack.path_log,"hts-cache/doit.log"));
  678.             if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock")))
  679.               remove(fconcat(httrack.path_log,"hts-in_progress.lock"));
  680.             rmdir(fconcat(httrack.path_log,"hts-cache"));
  681.             //
  682.           } else if (strfield2(argv[i]+2,"catchurl")) {      // capture d'URL via proxy temporaire!
  683.             argv_url=1;     // forcer a passer les parametres
  684.             strcpy(argv[i]+1,"#P");
  685.             //
  686.           } else if (strfield2(argv[i]+2,"updatehttrack")) {
  687. #ifdef _WIN32
  688.             char s[HTS_CDLMAXSIZE];
  689.             sprintf(s,"%s not available in this version",argv[i]);
  690.             HTS_PANIC_PRINTF(s);
  691.             return -1;
  692. #else
  693. #if 0
  694.             char _args[8][256];
  695.             char *args[8];
  696.             
  697.             printf("Cheking for updates...\n");
  698.             strcpy(_args[0],argv[0]);
  699.             strcpy(_args[1],"--get");
  700.             sprintf(_args[2],HTS_UPDATE_WEBSITE,HTS_PLATFORM,"");
  701.             strcpy(_args[3],"--quickinfo");
  702.             args[0]=_args[0];
  703.             args[1]=_args[1];
  704.             args[2]=_args[2];
  705.             args[3]=_args[3];
  706.             args[4]=NULL;
  707.             if (execvp(args[0],args)==-1) {
  708.             }
  709. #endif
  710. #endif
  711.           }
  712.           //
  713.           else {
  714.             char s[HTS_CDLMAXSIZE];
  715.             sprintf(s,"%s not recognized",argv[i]);
  716.             HTS_PANIC_PRINTF(s);
  717.             return -1;
  718.           }
  719.  
  720.         } 
  721.       }
  722.     }
  723.   }
  724.  
  725.   // Compter urls/jokers
  726.   /*
  727.   if (argv_url<=0) { 
  728.     int na;
  729.     argv_url=0;
  730.     for(na=1;na<argc;na++) {
  731.       if ( (strcmp(argv[na],"-P")==0) || (strcmp(argv[na],"-N")==0) || (strcmp(argv[na],"-F")==0) || (strcmp(argv[na],"-O")==0) || (strcmp(argv[na],"-V")==0) ) {
  732.         na++;    // sauter nom de proxy
  733.       } else if (!cmdl_opt(argv[na])) { 
  734.         argv_url++;   // un de plus       
  735.       } else if (strcmp(argv[na],"-h")==0) {
  736.         help(argv[0],!httrack.quiet);
  737.         return 0;
  738.       } else {
  739.         if ((strchr(argv[na],'q')!=NULL))
  740.           httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  741.         if ((strchr(argv[na],'i')!=NULL)) {  // doit.log!
  742.           argv_url=0;
  743.           na=argc;
  744.         }
  745.       }
  746.     }
  747.   }  
  748.   */
  749.  
  750.   // Ici on ajoute les arguments qui ont ΘtΘ appelΘs avant au cas o∙ on rΘcupΦre une session
  751.   // Exemple: httrack www.truc.fr -L0 puis ^C puis httrack sans URL : ajouter URL prΘcΘdente
  752.   /*
  753.   if (argv_url==0) {
  754.     //if ((fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))) {  // il existe dΘja un cache prΘcΘdent.. renommer
  755.     if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) {    // un cache est prΘsent
  756.       
  757.       x_argvblk=(char*) calloct(32768,1);
  758.       
  759.       if (x_argvblk!=NULL) {
  760.         FILE* fp;
  761.         int x_argc;
  762.         
  763.         //strcpy(x_argvblk,"httrack ");
  764.         fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"rb");
  765.         if (fp) {
  766.           linput(fp,x_argvblk+strlen(x_argvblk),8192);
  767.           fclose(fp); fp=NULL;
  768.         }
  769.         
  770.         // calculer arguments selon derniers arguments
  771.         x_argv[0]=argv[0];
  772.         x_argc=1;
  773.         {
  774.           char* p=x_argvblk;
  775.           do {
  776.             x_argv[x_argc++]=p;
  777.             //p=strstr(p," ");
  778.             // exemple de chaine: "echo \"test\"" c:\a "\$0"
  779.             p=next_token(p,1);    // prochain token
  780.             if (p) {
  781.               *p=0;    // octet nul (tableau)
  782.               p++;
  783.             }            
  784.           } while(p!=NULL);
  785.         }
  786.         // recopier arguments actuels (pointeurs uniquement)
  787.         {
  788.           int na;
  789.           for(na=1;na<argc;na++) {
  790.             if (strcmp(argv[na],"-O") != 0)    // SAUF le path!
  791.               x_argv[x_argc++]=argv[na];
  792.             else
  793.               na++;
  794.           }
  795.         }
  796.         argc=x_argc;      // nouvel argc
  797.         argv=x_argv;      // nouvel argv
  798.       }
  799.       
  800.       
  801.     }
  802.     //}
  803.   }
  804.   */
  805.   
  806.   // VΘrifier quiet
  807.   /*
  808.   { 
  809.     int na;    
  810.     for(na=1;na<argc;na++) {
  811.       if (!cmdl_opt(argv[na])) { 
  812.         if ((strcmp(argv[na],"-P")==0) || (strcmp(argv[na],"-N")==0) || (strcmp(argv[na],"-F")==0) || (strcmp(argv[na],"-O")==0) || (strcmp(argv[na],"-V")==0))
  813.           na++;    // sauter nom de proxy
  814.       } else {
  815.         if ((strchr(argv[na],'q')!=NULL) || (strchr(argv[na],'i')!=NULL))
  816.           httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  817.       }
  818.     }
  819.   }
  820.   */
  821.  
  822.   // Pas d'URL
  823. #if DEBUG_STEPS
  824.   printf("Checking URLs\n");
  825. #endif
  826.   if (argv_url==0) {
  827.     // PrΘsence d'un cache, que faire?..
  828.     if ((fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))) {  // il existe dΘja un cache prΘcΘdent.. renommer
  829.       if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) {    // un cache est prΘsent
  830.         if (x_argvblk!=NULL) {
  831.           int m;        
  832.           // Θtablir mode - mode cache: 1 (cache valide) 2 (cache α vΘrifier)
  833.           if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {    // cache prioritaire
  834.             m=1;
  835.             recuperer=1;
  836.           } else {
  837.             m=2;
  838.           }
  839.           httrack.cache=m;
  840.           
  841.           if (httrack.quiet==0) {  // sinon on continue automatiquement
  842.             HT_REQUEST_START;
  843.             HT_PRINT("A cache has been found"LF);
  844.             if (m==1) {
  845.               HT_PRINT("That means that a transfer has been aborted"LF);
  846.               HT_PRINT("OK to Continue ");
  847.             } else {
  848.               HT_PRINT("That means you can update faster the remote site(s)"LF);
  849.               HT_PRINT("OK to Update ");
  850.             }
  851.             HT_PRINT("httrack "); HT_PRINT(x_argvblk); HT_PRINT("?"LF);
  852.             HT_REQUEST_END;
  853.             if (!ask_continue()) return 0;
  854.           }
  855.           
  856.         } else {
  857.           HTS_PANIC_PRINTF("Error, not enough memory");
  858.           return -1;
  859.         }
  860.       } else { // log existe pas
  861.         HTS_PANIC_PRINTF("A cache has been found, but no command line");
  862.         printf("Please launch httrack with proper parameters to reuse the cache\n");
  863.         return -1;
  864.       }
  865.       
  866.     } else {    // aucune URL dΘfinie et pas de cache
  867. #if HTS_ANALYSTE!=2
  868.       if (httrack.quiet) {
  869. #endif
  870.         help(argv[0],!httrack.quiet);
  871.         return -1;
  872. #if HTS_ANALYSTE!=2
  873.       } else {
  874.         help_wizard();
  875.         return -1;
  876.       }
  877. #endif
  878.       return 0;
  879.     }
  880.   } else {   // plus de 2 paramΦtres
  881.     // un fichier log existe?
  882.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {  // fichier lock?
  883.       //char s[32];
  884.       
  885.       httrack.cache=1;    // cache prioritaire
  886.       if (httrack.quiet==0) {
  887.         if ((fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))) {  // il existe dΘja un cache prΘcΘdent.. renommer
  888.           HT_REQUEST_START;
  889.           HT_PRINT("There is a lock-file in the current directory"LF);
  890.           HT_PRINT("That means that a mirror has not been terminated"LF);
  891.           HT_PRINT("Be sure you call httrack with proper parameters"LF);
  892.           HT_PRINT("(The cache allows you to restart faster the transfer)"LF);
  893.           HT_REQUEST_END;
  894.           if (!ask_continue()) return 0;
  895.         /*} else {
  896.           HT_PRINT("There is a lock-file in the current directory"LF);
  897.           HT_PRINT("That means that a mirror has not been terminated"LF);
  898.           HT_PRINT("There is no cache, HTtrack will not be able to use formerly loaded files"LF);*/
  899.         }
  900.       }
  901.     } else if (fexist(fconcat(httrack.path_html,"index.html"))) {
  902.       //char s[32];
  903.       httrack.cache=2;  // cache vient aprΦs test de validitΘ
  904.       if (httrack.quiet==0) {
  905.         if ((fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))) {  // il existe dΘja un cache prΘcΘdent.. renommer
  906.           HT_REQUEST_START;
  907.           HT_PRINT("There is an index.html in the current directory, and a cache"LF);
  908.           HT_PRINT("A site may have been mirrored here, that could mean that you want to update it"LF);
  909.           HT_PRINT("Be sure parameters are ok"LF);
  910.           HT_REQUEST_END;
  911.           if (!ask_continue()) return 0;
  912.         } else {
  913.           HT_REQUEST_START;
  914.           HT_PRINT("There is an index.html in the current directory, but no cache"LF);
  915.           HT_PRINT("A site may have been mirrored here, and erased.."LF);
  916.           HT_PRINT("Be sure parameters are ok"LF);
  917.           HT_REQUEST_END;
  918.           if (!ask_continue()) return 0;
  919.         }
  920.       }
  921.     }
  922.   }
  923.   
  924.   
  925.   // Treat parameters
  926.   // Traiter les paramΦtres
  927. #if DEBUG_STEPS
  928.   printf("Analyze parameters\n");
  929. #endif
  930.   { 
  931.     char* com;
  932.     int na;
  933.     
  934.     for(na=1;na<argc;na++) {
  935.  
  936.       if (argv[na][0]=='"') {
  937.         char tempo[HTS_CDLMAXSIZE];
  938.         strcpy(tempo,argv[na]+1);
  939.         if (tempo[strlen(tempo)-1]!='"') {
  940.           char s[HTS_CDLMAXSIZE];
  941.           sprintf(s,"Missing quote in %s",argv[na]);
  942.           HTS_PANIC_PRINTF(s);
  943.           return -1;
  944.         }
  945.         tempo[strlen(tempo)-1]='\0';
  946.         strcpy(argv[na],tempo);
  947.       }
  948.  
  949.       if (cmdl_opt(argv[na])) { // option
  950.         com=argv[na]+1;
  951.         
  952.         while(*com) {
  953.           switch(*com) {
  954.           case ' ': case 9: case '-': case '\0': break;
  955.             //
  956.           case 'h': help(argv[0],0); return 0;   // dΘja fait normalement
  957.             //
  958.           case 'g':    // rΘcupΘrer un (ou plusieurs) fichiers isolΘs
  959.             httrack.wizard=2;             // le wizard on peut plus s'en passer..
  960.             //httrack.wizard=0;             // pas de wizard
  961.             httrack.cache=0;              // ni de cache
  962.             httrack.makeindex=0;          // ni d'index
  963.             httrack_logmode=1;            // erreurs α l'Θcran
  964.             httrack.savename_type=1003;   // mettre dans le rΘpertoire courant
  965.             httrack_level=0;              // ne pas explorer la page
  966.             break;
  967.           case 'w': httrack.wizard=2;    // wizard 'soft' (ne pose pas de questions)
  968.             httrack.travel=0;
  969.             httrack.seeker=1;
  970.             break;
  971.           case 'W': httrack.wizard=1;    // Wizard-Help (pose des questions)
  972.             httrack.travel=0;
  973.             httrack.seeker=1;
  974.             break;
  975.           case 'r':                      // n'est plus le recurse get bestial mais wizard itou!
  976.             if (isdigit((unsigned char)*(com+1))) {
  977.               sscanf(com+1,"%d",&httrack_level);
  978.               while(isdigit((unsigned char)*(com+1))) com++;
  979.             } else httrack_level=3;
  980.             break;
  981. /*
  982.           case 'r': httrack.wizard=0;
  983.             if (isdigit((unsigned char)*(com+1))) {
  984.               sscanf(com+1,"%d",&httrack_level);
  985.               while(isdigit((unsigned char)*(com+1))) com++;
  986.             } else httrack_level=3;
  987.             break;
  988. */
  989.             //
  990.             // note: les tests httrack_level sont pour Θviter de faire
  991.             // un miroir du web (:-O) accidentelement ;-)
  992.           case 'a': /*if (httrack_level==9999) httrack_level=3;*/
  993.             httrack.travel=0+(httrack.travel&256); break;
  994.           case 'd': /*if (httrack_level==9999) httrack_level=3;*/
  995.             httrack.travel=1+(httrack.travel&256); break;
  996.           case 'l': /*if (httrack_level==9999) httrack_level=3;*/
  997.             httrack.travel=2+(httrack.travel&256); break;
  998.           case 'e': /*if (httrack_level==9999) httrack_level=3;*/
  999.             httrack.travel=7+(httrack.travel&256); break;
  1000.           case 't': httrack.travel|=256; break;
  1001.           case 'n': httrack.nearlink=1; break;
  1002.           case 'x': httrack.external=1; break;
  1003.             //
  1004.           case 'U': httrack.seeker=2; break;
  1005.           case 'D': httrack.seeker=1; break;
  1006.           case 'S': httrack.seeker=0; break;
  1007.           case 'B': httrack.seeker=3; break;
  1008.             //
  1009.           case 'Y': httrack.mirror_first_page=1; break;
  1010.             //
  1011.           case 'q': case 'i': httrack.quiet=1; break;
  1012.             //
  1013.           case 'Q': httrack_logmode=0; break;
  1014.           case 'v': httrack_logmode=1; break;
  1015.           case 'f': httrack_logmode=2; if (*(com+1)=='2') httrack_logmode=3; while(isdigit((unsigned char)*(com+1))) com++; break;
  1016.             //
  1017.           //case 'A': httrack.urlmode=1; break;
  1018.           //case 'R': httrack.urlmode=2; break;
  1019.           case 'K': httrack.urlmode=0; if (*(com+1)=='0') { httrack.urlmode=2; com++; } break;
  1020.             //
  1021.           case 'c':
  1022.             if (isdigit((unsigned char)*(com+1))) {
  1023.               sscanf(com+1,"%d",&httrack.maxsoc);
  1024.               while(isdigit((unsigned char)*(com+1))) com++;
  1025.               httrack.maxsoc=max(httrack.maxsoc,1);     // FORCER A 1
  1026.             } else httrack.maxsoc=8;
  1027.             
  1028.             break;
  1029.             //
  1030.           case 'p': sscanf(com+1,"%d",&httrack.getmode); while(isdigit((unsigned char)*(com+1))) com++; break;
  1031.             //        
  1032.           case 'G': sscanf(com+1,"%d",&httrack.fragment); while(isdigit((unsigned char)*(com+1))) com++; break;
  1033.           case 'M': sscanf(com+1,"%d",&httrack.maxsite); while(isdigit((unsigned char)*(com+1))) com++; break;
  1034.           case 'm': sscanf(com+1,"%d",&httrack.maxfile_nonhtml); while(isdigit((unsigned char)*(com+1))) com++; 
  1035.             if (*(com+1)==',') {
  1036.               com++;
  1037.               sscanf(com+1,"%d",&httrack.maxfile_html); while(isdigit((unsigned char)*(com+1))) com++;
  1038.             } else httrack.maxfile_html=-1;
  1039.             break;
  1040.             //
  1041.           case 'T': sscanf(com+1,"%d",&httrack.timeout); while(isdigit((unsigned char)*(com+1))) com++; break;
  1042.           case 'J': sscanf(com+1,"%d",&httrack.rateout); while(isdigit((unsigned char)*(com+1))) com++; break;
  1043.           case 'R': sscanf(com+1,"%d",&httrack.retry); while(isdigit((unsigned char)*(com+1))) com++; break;
  1044.           case 'E': sscanf(com+1,"%d",&httrack.maxtime); while(isdigit((unsigned char)*(com+1))) com++; break;
  1045.           case 'H': sscanf(com+1,"%d",&httrack.hostcontrol); while(isdigit((unsigned char)*(com+1))) com++; break;
  1046.           case 'A': sscanf(com+1,"%d",&httrack.maxrate); while(isdigit((unsigned char)*(com+1))) com++; break;
  1047.  
  1048.           case 'j': httrack.parsejava=1; if (*(com+1)=='0') { httrack.parsejava=0; com++; } break;
  1049.             //
  1050.           case 'I': httrack.makeindex=1; if (*(com+1)=='0') { httrack.makeindex=0; com++; } break;
  1051.             //
  1052.           case 'X': httrack.delete_old=1; if (*(com+1)=='0') { httrack.delete_old=0; com++; } break;
  1053.             //
  1054.           case 'b': sscanf(com+1,"%d",&httrack.accept_cookie); while(isdigit((unsigned char)*(com+1))) com++; break;
  1055.             //
  1056.           case 'N':
  1057.             if (strcmp(argv[na],"-N")==0) {    // Tout seul
  1058.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {  // erreur
  1059.                 HTS_PANIC_PRINTF("Option N needs a number, or needs to be followed by a blank space, and a string");
  1060.                 printf("Example: -N4\n");
  1061.                 return -1;
  1062.               } else {
  1063.                 na++;
  1064.                 if (strlen(argv[na])>=127) {
  1065.                   HTS_PANIC_PRINTF("Userdef structure string too long");
  1066.                   return -1;
  1067.                 }
  1068.                 strcpy(httrack.savename_userdef,argv[na]);
  1069.                 if (strnotempty(httrack.savename_userdef))
  1070.                   httrack.savename_type = -1;    // userdef!
  1071.                 else
  1072.                   httrack.savename_type = 0;    // -N "" : par dΘfaut
  1073.               }
  1074.             } else {
  1075.               sscanf(com+1,"%d",&httrack.savename_type); while(isdigit((unsigned char)*(com+1))) com++;
  1076.             }
  1077.             break;
  1078.           case 'L': sscanf(com+1,"%d",&httrack.savename_83); httrack.savename_83=!httrack.savename_83; while(isdigit((unsigned char)*(com+1))) com++; break;
  1079.           case 's': 
  1080.             if (isdigit((unsigned char)*(com+1))) {
  1081.               sscanf(com+1,"%d",&httrack.robots);
  1082.               while(isdigit((unsigned char)*(com+1))) com++;
  1083.             } else httrack.robots=1;
  1084. #if DEBUG_ROBOTS
  1085.             printf("robots.txt mode set to %d\n",httrack.robots);
  1086. #endif
  1087.             break;
  1088.           case 'o': sscanf(com+1,"%d",&httrack.errpage); while(isdigit((unsigned char)*(com+1))) com++; break;
  1089.           case 'u': sscanf(com+1,"%d",&httrack.check_type); while(isdigit((unsigned char)*(com+1))) com++; break;
  1090.             //
  1091.           case 'C': 
  1092.             if (isdigit((unsigned char)*(com+1))) {
  1093.               sscanf(com+1,"%d",&httrack.cache);
  1094.               while(isdigit((unsigned char)*(com+1))) com++;
  1095.             } else httrack.cache=1;
  1096.             break;
  1097.           case 'k': httrack.all_in_cache=1; break;
  1098.             //
  1099.           case 'z': httrack.debug=1; break;  // petit debug
  1100.           case 'Z': httrack.debug=2; break;  // GROS debug
  1101.             //
  1102.           case '&': case '%': {    // deuxiΦme jeu d'options
  1103.             com++;
  1104.             switch(*com) {
  1105.             case 'c': sscanf(com+1,"%d",&httrack.maxconn); while(isdigit((unsigned char)*(com+1))) com++; break;
  1106.             case 'B': httrack.tolerant=1; if (*(com+1)=='0') { httrack.tolerant=0; com++; } break;   // HTTP/1.0 notamment
  1107.             case 'h': httrack.http10=1; if (*(com+1)=='0') { httrack.http10=0; com++; } break;   // HTTP/1.0
  1108.             case 'f': httrack.ftp_proxy=1; if (*(com+1)=='0') { httrack.ftp_proxy=0; com++; } break;   // proxy http pour ftp
  1109.             case 'P': httrack.parseall=1; if (*(com+1)=='0') { httrack.parseall=0; com++; } break;   // tout parser
  1110.             case 'n': httrack.norecatch=1; if (*(com+1)=='0') { httrack.norecatch=0; com++; } break;   // ne pas reprendre fichiers effacΘs localement
  1111.             case 'v': httrack.verbosedisplay=1; if (*(com+1)=='0') { httrack.verbosedisplay=0; com++; } break;
  1112.             case 'L':    // URL list
  1113.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1114.                 HTS_PANIC_PRINTF("Option %L needs to be followed by a blank space, and a text filename");
  1115.                 printf("Example: -%%L \"mylist.txt\"\n");
  1116.                 return -1;
  1117.               } else{
  1118.                 na++;
  1119.                 if (strlen(argv[na])>=254) {
  1120.                   HTS_PANIC_PRINTF("File list string too long");
  1121.                   return -1;
  1122.                 }
  1123.                 strcpy(httrack.filelist,argv[na]);
  1124.               }
  1125.               break;
  1126.               //
  1127.             case 'F':     // file list
  1128.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1129.                 HTS_PANIC_PRINTF("Option %F needs to be followed by a blank space, and a footer string");
  1130.                 printf("Example: -%%F \"<!-- Mirrored from %%s by HTTrack Website Copier/"HTTRACK_AFF_VERSION" "HTTRACK_AFF_AUTHORS" -->\"\n");
  1131.                 return -1;
  1132.               } else{
  1133.                 na++;
  1134.                 if (strlen(argv[na])>=254) {
  1135.                   HTS_PANIC_PRINTF("Footer string too long");
  1136.                   return -1;
  1137.                 }
  1138.                 strcpy(httrack.footer,argv[na]);
  1139.               }
  1140.               break;
  1141.             case 'H':                 // debug headers
  1142.               _DEBUG_HEAD=1;
  1143.               break;
  1144.             case 'O':
  1145. #if HTS_WIN
  1146.               printf("Warning option -%%O has no effect in this system (chroot)\n");
  1147. #else
  1148.               switch_chroot=1;
  1149. #endif
  1150.               break;
  1151.             case 'U':                 // setuid
  1152.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1153.                 HTS_PANIC_PRINTF("Option %U needs to be followed by a blank space, and a username");
  1154.                 printf("Example: -%%U smith\n");
  1155.                 return -1;
  1156.               } else {
  1157.                 na++;
  1158. #if HTS_WIN
  1159.                 printf("Warning option -%%U has no effect on this system (setuid)\n");
  1160. #else
  1161. #ifndef HTS_DO_NOT_USE_UID
  1162.                 /* Change the user id and gid */
  1163.                 {
  1164.                   struct passwd* userdef=getpwnam((const char*)argv[na]);
  1165.                   if (userdef) {    /* we'll have to switch the user id */
  1166.                     switch_gid=userdef->pw_gid;
  1167.                     switch_uid=userdef->pw_uid;
  1168.                   }
  1169.                 }
  1170. #else
  1171.                 printf("Warning option -%%U has no effect with this compiled version (setuid)\n");
  1172. #endif
  1173. #endif
  1174.               }
  1175.               break;
  1176.             }
  1177.                     }
  1178.             break;
  1179.             //
  1180.           case '#':  { // non documentΘ (appel de l'interface)
  1181.             com++;
  1182.             switch(*com) {
  1183.             case 'f': httrack.flush=1; break;
  1184.             case 'h':
  1185.               printf("HTTrack version "HTTRACK_VERSION"\n");
  1186.               break;
  1187.             case 'p': httrack.aff_progress=1; break;
  1188.             case 'S': httrack.shell=1; break;  // stdin sur un shell
  1189.             case 'K': httrack.keyboard=1; break;  // vΘrifier stdin
  1190.               //
  1191.             case 'L': sscanf(com+1,"%d",&httrack.maxlink); while(isdigit((unsigned char)*(com+1))) com++; break;
  1192.             case 'F': sscanf(com+1,"%d",&httrack.maxfilter); while(isdigit((unsigned char)*(com+1))) com++; break;
  1193.             case 'Z': httrack.makestat=1; break;
  1194.             case 'T': httrack.maketrack=1; break;
  1195.             case 'u': sscanf(com+1,"%d",&httrack.waittime); while(isdigit((unsigned char)*(com+1))) com++; break;
  1196.               
  1197.             case 'R':    // ohh ftp, catch->ftpget
  1198.               HTS_PANIC_PRINTF("Unexpected internal error with -#R command");
  1199.               return -1;        
  1200.               break;
  1201.             case 'P': {     // catchurl
  1202.               help_catchurl(httrack.path_log);
  1203.               return 0;
  1204.                       }
  1205.               break;
  1206.           
  1207.             case '0':   /* test #0 : filters */
  1208.               if (na+2>=argc) {
  1209.                 HTS_PANIC_PRINTF("Option #0 needs to be followed by a filter string and a string");
  1210.                 printf("Example: '-#0' '*.gif' 'foo.gif'\n");
  1211.                 return -1;
  1212.               } else {
  1213.                 if (strjoker(argv[na+2],argv[na+1],NULL,NULL))
  1214.                   printf("%s does match %s\n",argv[na+2],argv[na+1]);
  1215.                 else
  1216.                   printf("%s does NOT match %s\n",argv[na+2],argv[na+1]);
  1217.                 return 0;
  1218.               }
  1219.               break;
  1220.  
  1221.             default: printf("Internal option %c not recognized\n",*com); break;
  1222.             }
  1223.                      }
  1224.             break; 
  1225.           case 'O':    // output path
  1226.             na++;     // sauter, dΘja traitΘ
  1227.             break;
  1228.           case 'P':    // proxy
  1229.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1230.               HTS_PANIC_PRINTF("Option P needs to be followed by a blank space, and a proxy proxy:port or user:id@proxy:port");
  1231.               printf("Example: -P proxy.myhost.com:8080\n");
  1232.               return -1;
  1233.             } else {
  1234.               char* a;
  1235.               na++;
  1236.               httrack.proxy.active=1;
  1237.               // Rechercher MAIS en partant de la fin α cause de user:pass@proxy:port
  1238.               a = argv[na] + strlen(argv[na]) -1;
  1239.               // a=strstr(argv[na],":");  // port
  1240.               while( ((int) a > (int) argv[na]) && (*a != ':') && (*a != '@') ) a--;
  1241.               if (*a == ':') {  // un port est prΘsent, <proxy>:port
  1242.                 sscanf(a+1,"%d",&httrack.proxy.port);
  1243.                 httrack.proxy.name[0]='\0';
  1244.                 strncat(httrack.proxy.name,argv[na],(int) a-(int) argv[na]);
  1245.               } else {  // <proxy>
  1246.                 httrack.proxy.port=8080;
  1247.                 strcpy(httrack.proxy.name,argv[na]);
  1248.               }
  1249.             }
  1250.             break;
  1251.           case 'F':    // user-agent field
  1252.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1253.               HTS_PANIC_PRINTF("Option F needs to be followed by a blank space, and a user-agent name");
  1254.               printf("Example: -F \"my_user_agent/1.0\"\n");
  1255.               return -1;
  1256.             } else{
  1257.               na++;
  1258.               if (strlen(argv[na])>=62) {
  1259.                 HTS_PANIC_PRINTF("User-agent length too long");
  1260.                 return -1;
  1261.               }
  1262.               strcpy(httrack.user_agent,argv[na]);
  1263.               if (strnotempty(httrack.user_agent))
  1264.                 httrack.user_agent_send=1;
  1265.               else
  1266.                 httrack.user_agent_send=0;    // -F "" dΘsactive l'option
  1267.             }
  1268.             break;
  1269.             //
  1270.           case 'V':    // execute command
  1271.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1272.               HTS_PANIC_PRINTF("Option V needs to be followed by a system-command string");
  1273.               printf("Example: -V \"tar uvf some.tar \\$0\"\n");
  1274.               return -1;
  1275.             } else{
  1276.               na++;
  1277.               if (strlen(argv[na])>=2048) {
  1278.                 HTS_PANIC_PRINTF("System-command length too long");
  1279.                 return -1;
  1280.               }
  1281.               strcpy(httrack.sys_com,argv[na]);
  1282.               if (strnotempty(httrack.sys_com))
  1283.                 httrack.sys_com_exec=1;
  1284.               else
  1285.                 httrack.sys_com_exec=0;    // -V "" dΘsactive l'option
  1286.             }
  1287.             break;
  1288.             //
  1289.           default: {
  1290.             char s[HTS_CDLMAXSIZE];
  1291.             sprintf(s,"invalid option %c\n",*com);
  1292.             HTS_PANIC_PRINTF(s);
  1293.             return -1;
  1294.                    }
  1295.             break;
  1296.           }  // switch
  1297.           com++;    
  1298.         }  // while
  1299.         
  1300.       }  else {  // URL/filters
  1301.         char* a=argv[na];
  1302.         
  1303.         while(is_space(*a)) a++;
  1304.         
  1305.         // ajouter URLs et filters[]
  1306.         while (*a) {
  1307.           char c[2];
  1308.           c[1]='\0';
  1309.           while(is_space(*a)) a++;
  1310.           if (*a) {
  1311.             if (strnotempty(url)) strcat(url," ");  // espace de sΘparation
  1312.             while((!is_space(*a)) && (*a!=0)) { c[0]=*a; strcat(url,c); a++;}
  1313.           }
  1314.         }    
  1315.         
  1316.         
  1317.       }  // if argv=- etc. 
  1318.       
  1319.     }  // for
  1320.   }
  1321.   
  1322. #if BDEBUG==3  
  1323.   printf("URLs/filters=%s\n",url);
  1324. #endif
  1325.  
  1326. #if DEBUG_STEPS
  1327.   printf("Analyzing parameters done\n");
  1328. #endif
  1329.  
  1330.  
  1331. #if HTS_WIN
  1332. #else
  1333. #ifndef HTS_DO_NOT_USE_UID
  1334.   /* Chroot */
  1335.   if (switch_chroot) {
  1336.     uid_t userid=getuid();
  1337.     //struct passwd* userdef=getpwuid(userid);
  1338.     //if (userdef) {
  1339.     if (!userid) {
  1340.       //if (strcmp(userdef->pw_name,"root")==0) {
  1341.       char rpath[1024];
  1342.       //printf("html=%s log=%s\n",httrack.path_html,httrack.path_log);    // xxc
  1343.       if ((httrack.path_html[0]) && (httrack.path_log[0])) {
  1344.         char *a=httrack.path_html,*b=httrack.path_log,*c=NULL,*d=NULL;
  1345.         c=a; d=b;
  1346.         while ((*a) && (*a == *b)) {
  1347.           if (*a=='/') { c=a; d=b; }
  1348.           a++;
  1349.           b++;
  1350.         }
  1351.         /*printf("phtml=%s plog=%s\n",a,b);
  1352.         printf("ctml=%s clog=%s\n",c,d);   // xxc
  1353.         */
  1354.         rpath[0]='\0';
  1355.         if (c != httrack.path_html) {
  1356.           if (httrack.path_html[0]!='/')
  1357.             strcat(rpath,"./");
  1358.           strncat(rpath,httrack.path_html,(int) c - (int) httrack.path_html);
  1359.         }
  1360.         {
  1361.           char tmp[1024];
  1362.           strcpy(tmp,c); strcpy(httrack.path_html,tmp);
  1363.           strcpy(tmp,d); strcpy(httrack.path_log,tmp);
  1364.         }
  1365.       } else {
  1366.         strcpy(rpath,"./");
  1367.         strcpy(httrack.path_html,"/");
  1368.         strcpy(httrack.path_log,"/");
  1369.       }
  1370.       if (rpath[0]) {
  1371.         printf("[changing root path to %s (path_data=%s,path_log=%s)]\n",rpath,httrack.path_html,httrack.path_log);
  1372.         if (chroot(rpath)) {
  1373.           printf("ERROR! Can not chroot to %s!\n",rpath);
  1374.           exit(0);
  1375.         }
  1376.         if (chdir("/")) {     /* new root */
  1377.           printf("ERROR! Can not chdir to %s!\n",rpath);
  1378.           exit(0);
  1379.         }
  1380.       } else
  1381.         printf("WARNING: chroot not possible with these paths\n");
  1382.     }
  1383.     //}
  1384.   }
  1385.  
  1386.   /* Setuid */
  1387.   if (switch_uid>=0) {
  1388.     printf("[setting user/group to %d/%d]\n",switch_uid,switch_gid);
  1389.     if (setgid(switch_gid))
  1390.       printf("WARNING! Can not setgid to %d!\n",switch_gid);
  1391.     if (setuid(switch_uid))
  1392.       printf("WARNING! Can not setuid to %d!\n",switch_uid);
  1393.   }
  1394.  
  1395.   /* Final check */
  1396.   {
  1397.     uid_t userid=getuid();
  1398.     if (!userid) {              /* running as r00t */
  1399.       printf("WARNING! You are running this program as root!\n");
  1400.       printf("It might be a good idea to use the -%%U option to change the userid:\n");
  1401.       printf("Example: -%%U smith\n\n");
  1402.     }
  1403.   }
  1404. #endif
  1405. #endif
  1406.   
  1407.   //printf("WARNING! This is *only* a beta-release of HTTrack\n");
  1408.   io_flush;
  1409.   
  1410. #if DEBUG_STEPS
  1411.   printf("Cache & log settings\n");
  1412. #endif
  1413.   
  1414.   // on utilise le cache..
  1415.   // en cas de prΘsence des deux versions, garder la version la plus avancΘe,
  1416.   // cad la version contenant le plus de fichiers  
  1417.   if (httrack.cache) {
  1418.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {   // problemes..
  1419.       if (fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/new.ndx"))) { 
  1420.         if (fexist(fconcat(httrack.path_log,"hts-cache/old.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) {
  1421.           // switcher si new<32Ko et old>65Ko (tailles arbitraires) ?
  1422.           // ce cas est peut Ωtre une erreur ou un crash d'un miroir ancien, prendre
  1423.           // alors l'ancien cache
  1424.           if (fsize(fconcat(httrack.path_log,"hts-cache/new.dat"))<32768) {
  1425.             if (fsize(fconcat(httrack.path_log,"hts-cache/old.dat"))>65536) {
  1426.               if (fsize(fconcat(httrack.path_log,"hts-cache/old.dat")) > fsize(fconcat(httrack.path_log,"hts-cache/new.dat"))) {
  1427.                 remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  1428.                 remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  1429.                 rename(fconcat(httrack.path_log,"hts-cache/old.dat"),fconcat(httrack.path_log,"hts-cache/new.dat"));
  1430.                 rename(fconcat(httrack.path_log,"hts-cache/old.ndx"),fconcat(httrack.path_log,"hts-cache/new.ndx"));  
  1431.                 //} else {  // ne rien faire
  1432.                 //  remove("hts-cache/old.dat");
  1433.                 //  remove("hts-cache/old.ndx");
  1434.               }
  1435.             }
  1436.           }
  1437.         }
  1438.       }
  1439.     }
  1440.   }
  1441.  
  1442.   // DΘbuggage des en tΩtes
  1443.   if (_DEBUG_HEAD) {
  1444.     ioinfo=fopen(fconcat(httrack.path_log,"hts-ioinfo.txt"),"wb");
  1445.   }
  1446.   
  1447.   {
  1448.     char n_lock[256];
  1449.     // on peut pas avoir un affichage ET un fichier log
  1450.     // ca sera pour la version 2
  1451.     if (httrack_logmode==1) {
  1452.       httrack.log=stdout;
  1453.       httrack.errlog=stderr;
  1454.     } else if (httrack_logmode>=2) {
  1455.       // deux fichiers log
  1456.       structcheck(httrack.path_log);
  1457.       if (fexist(fconcat(httrack.path_log,"hts-log.txt")))
  1458.         remove(fconcat(httrack.path_log,"hts-log.txt"));
  1459.       if (fexist(fconcat(httrack.path_log,"hts-err.txt")))
  1460.         remove(fconcat(httrack.path_log,"hts-err.txt"));
  1461.  
  1462.       httrack.log=fopen(fconcat(httrack.path_log,"hts-log.txt"),"w");
  1463.       if (httrack_logmode==2)
  1464.         httrack.errlog=fopen(fconcat(httrack.path_log,"hts-err.txt"),"w");
  1465.       else
  1466.         httrack.errlog=httrack.log;
  1467.       if (httrack.log==NULL) {
  1468.         char s[HTS_CDLMAXSIZE];
  1469.         sprintf(s,"Unable to create log file %s",fconcat(httrack.path_log,"hts-log.txt"));
  1470.         HTS_PANIC_PRINTF(s);
  1471.         return -1;
  1472.       } else if (httrack.errlog==NULL) {
  1473.         char s[HTS_CDLMAXSIZE];
  1474.         sprintf(s,"Unable to create log file %s",fconcat(httrack.path_log,"hts-err.txt"));
  1475.         HTS_PANIC_PRINTF(s);
  1476.         return -1;
  1477.       }
  1478.  
  1479.     } else {
  1480.       httrack.log=NULL;
  1481.       httrack.errlog=NULL;
  1482.     }
  1483.     
  1484.     // un petit lock-file pour indiquer un miroir en cours, ainsi qu'un Θventuel fichier log
  1485.     {
  1486.       FILE* fp=NULL;
  1487.       //int n=0;
  1488.       char t[256];
  1489.       time_local_rfc822(t);    // faut bien que ca serve quelque part l'heure RFC1945 arf'
  1490.       
  1491.       /* readme for information purpose */
  1492.       {
  1493.         FILE* fp=fopen(fconcat(httrack.path_log,"hts-cache/readme.txt"),"wb");
  1494.         if (fp) {
  1495.           fprintf(fp,"What's in this folder?"LF);
  1496.           fprintf(fp,""LF);
  1497.           fprintf(fp,"This folder (hts-cache) has been generated by WinHTTrack "HTTRACK_VERSION""LF);
  1498.           fprintf(fp,"and is used for updating this website."LF);
  1499.           fprintf(fp,"(The HTML website structure is stored here to allow fast updates)"LF""LF);
  1500.           fprintf(fp,"DO NOT delete this folder unless you do not want to update the mirror in the future!!"LF);
  1501.           fprintf(fp,"(you can safely delete old.dat, old.ndx and old.lst files, however)"LF);
  1502.           fclose(fp);
  1503.         }
  1504.       }
  1505.  
  1506.       sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"));
  1507.       //sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"),n);
  1508.       /*do {
  1509.         if (!n)
  1510.           sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"),n);
  1511.         else
  1512.           sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress%d.lock"),n);
  1513.         n++;
  1514.       } while((fexist(n_lock)) && httrack.quiet);      
  1515.       if (fexist(n_lock)) {
  1516.         if (!recuperer) {
  1517.           remove(n_lock);
  1518.         }
  1519.       }*/
  1520.  
  1521.       // vΘrifier existence de la structure
  1522.       structcheck(httrack.path_html);
  1523.       structcheck(httrack.path_log);
  1524.      
  1525.       // reprise/update
  1526.       if (httrack.cache) {
  1527.         FILE* fp;
  1528.         int i;
  1529. #if HTS_WIN
  1530.         mkdir(fconcat(httrack.path_log,"hts-cache"));
  1531. #else
  1532.         mkdir(fconcat(httrack.path_log,"hts-cache"),HTS_PROTECT_FOLDER);
  1533. #endif
  1534.         fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"wb");
  1535.         if (fp) {
  1536.           for(i=0+1;i<argc;i++) {
  1537.             if ( ((strchr(argv[i],' ')!=NULL) || (strchr(argv[i],'"')!=NULL) || (strchr(argv[i],'\\')!=NULL)) && (argv[i][0]!='"')  ) {
  1538.               int j;
  1539.               fprintf(fp,"\"");
  1540.               for(j=0;j<(int) strlen(argv[i]);j++) {
  1541.                 if (argv[i][j]==34)
  1542.                   fprintf(fp,"\\\"");
  1543.                 else if (argv[i][j]=='\\')
  1544.                   fprintf(fp,"\\\\");
  1545.                 else
  1546.                   fprintf(fp,"%c",argv[i][j]);
  1547.               }
  1548.               fprintf(fp,"\"");
  1549.             } else if (strnotempty(argv[i])==0) {   // ""
  1550.               fprintf(fp,"\"\"");
  1551.             } else {   // non critique
  1552.               fprintf(fp,"%s",argv[i]);
  1553.             }
  1554.             if (i<argc-1)
  1555.               fprintf(fp," ");
  1556.           }
  1557.           fprintf(fp,LF);
  1558.           fprintf(fp,"File generated automatically on %s, do NOT edit"LF,t);
  1559.           fprintf(fp,LF);
  1560.           fprintf(fp,"To update a mirror, just launch httrack without any parameters"LF);
  1561.           fprintf(fp,"The existing cache will be used (and modified)"LF);
  1562.           fprintf(fp,"To have other options, retype all parameters and launch HTTrack"LF);
  1563.           fprintf(fp,"To continue an interrupted mirror, just launch httrack without any parameters"LF);
  1564.           fprintf(fp,LF);
  1565.           fclose(fp); fp=NULL;
  1566.         //} else if (httrack.debug>1) {
  1567.         //  printf("! FileOpen error, \"%s\"\n",strerror(errno));
  1568.         }
  1569.       }
  1570.       
  1571.       // petit message dans le lock
  1572.       if ( (fp=fopen(n_lock,"wb"))!=NULL) {
  1573.         int i;
  1574.         fprintf(fp,"Mirror in progress since %s .. please wait!"LF,t);
  1575.         for(i=0;i<argc;i++) {
  1576.           if (strchr(argv[i],' ')==NULL)
  1577.             fprintf(fp,"%s ",argv[i]);
  1578.           else    // entre ""
  1579.             fprintf(fp,"\"%s\" ",argv[i]);
  1580.         }
  1581.         fprintf(fp,LF);
  1582.         fclose(fp); fp=NULL;
  1583.       }
  1584.       
  1585.       // fichier log        
  1586.       if (httrack.log)     {
  1587.         int i;
  1588.         fprintf(httrack.log,"HTTrack"HTTRACK_VERSION" launched on %s at %s"LF,t,url);
  1589.         fprintf(httrack.log,"(");
  1590.         for(i=0;i<argc;i++) {
  1591.           if ((strchr(argv[i],' ')==NULL) || (strchr(argv[i],'\"')))
  1592.             fprintf(httrack.log,"%s ",argv[i]);
  1593.           else    // entre "" (si espace(s) et pas dΘja de ")
  1594.             fprintf(httrack.log,"\"%s\" ",argv[i]);
  1595.         }
  1596.         fprintf(httrack.log,")"LF);
  1597.         fprintf(httrack.log,LF);
  1598.         fprintf(httrack.log,"Information, Warnings and Errors reported for this mirror:"LF);
  1599.       }
  1600.  
  1601.       if (httrack_logmode)     {
  1602.         printf("Mirror launched on %s by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS""LF,t);
  1603.         if (httrack.wizard==0) {
  1604.           printf("mirroring %s with %d levels, %d sockets,t=%d,s=%d,logm=%d,lnk=%d,mdg=%d\n",url,httrack_level,httrack.maxsoc,httrack.travel,httrack.seeker,httrack_logmode,httrack.urlmode,httrack.getmode);
  1605.         } else {    // the magic wizard
  1606.           printf("mirroring %s with the wizard help..\n",url);
  1607.         }
  1608.       }
  1609.     }
  1610.     
  1611.     io_flush;
  1612.  
  1613. #if HTS_ANALYSTE
  1614.   hts_htmlcheck_init();
  1615. #endif
  1616.  
  1617.   // dΘtourner SIGHUP etc.
  1618. #if HTS_WIN
  1619.   signal( SIGINT  , sig_ask    );   // ^C
  1620.   signal( SIGTERM , sig_finish );   // kill <process>
  1621. #else
  1622.   signal( SIGHUP  , sig_back   );   // close window
  1623.   signal( SIGTSTP , sig_back   );   // ^Z
  1624.   signal( SIGTERM , sig_finish );   // kill <process>
  1625.   signal( SIGINT  , sig_ask    );   // ^C
  1626. /*
  1627. deprecated - see SIGCHLD
  1628. #ifndef HTS_DO_NOT_SIGCLD
  1629.   signal( SIGCLD  , sig_ignore );   // child change status
  1630. #endif
  1631. */
  1632.   signal( SIGCHLD , sig_ignore );   // child change status
  1633. #endif
  1634. #if DEBUG_STEPS
  1635.   printf("Launching the mirror\n");
  1636. #endif
  1637.   
  1638.  
  1639.     // Lancement du miroir
  1640.     // ------------------------------------------------------------
  1641.     if (httpmirror(url,httrack_level,httrack)==0) {
  1642.       printf("Error during operation (see log file), site has not been successfully mirrored\n");
  1643.     } else {
  1644.       if  (httrack.shell) {
  1645.         HTT_REQUEST_START;
  1646.         HT_PRINT("TRANSFER DONE"LF);
  1647.         HTT_REQUEST_END
  1648.       } else {
  1649.         printf("Done.\n");
  1650.       }
  1651.     }
  1652.     // ------------------------------------------------------------
  1653.  
  1654.     if (exit_xh ==1) {
  1655.       if (httrack.log) {
  1656.         fprintf(httrack.log,"* * MIRROR ABORTED! * *\nThe current temporary cache is required for any update operation and only contains data downloaded during the present aborted session.\nThe former cache might contain more complete information; if you do not want to lose that information, you have to restore it and delete the current cache.\nThis can easily be done here by erasing the hts-cache/new.* files]\n");
  1657.       }
  1658.     }
  1659.  
  1660. #if HTS_ANALYSTE
  1661.   hts_htmlcheck_uninit();
  1662. #endif
  1663.     
  1664.     if (httrack_logmode==1) {
  1665.       if (httrack.errlog == httrack.log) httrack.errlog=NULL;
  1666.       if (httrack.log) { fclose(httrack.log); httrack.log=NULL; }
  1667.       if (httrack.errlog) { fclose(httrack.errlog); httrack.errlog=NULL; }
  1668.     }  
  1669.     
  1670.     // DΘbuggage des en tΩtes
  1671.     if (_DEBUG_HEAD) {
  1672.       if (ioinfo) {
  1673.         fclose(ioinfo);
  1674.       }
  1675.     }
  1676.     
  1677.     // supprimer lock
  1678.     remove(n_lock);
  1679.   }
  1680.   
  1681.   if (x_argvblk)
  1682.     freet(x_argvblk);
  1683.  
  1684. #if HTS_WIN
  1685. #if HTS_ANALYSTE!=2
  1686. //  WSACleanup();    // ** non en cas de thread tjs prΘsent!..
  1687. #endif
  1688. #endif
  1689. #if HTS_TRACE_MALLOC
  1690.   hts_freeall();
  1691. #endif
  1692.  
  1693.   printf("Thanks for using HTTrack!\n");
  1694.   io_flush;
  1695.   return 0;    // OK
  1696. }
  1697.  
  1698.  
  1699. // main() subroutines
  1700.  
  1701. // vΘrifier chemin path
  1702. void check_path(char* s) {
  1703.   int i;
  1704.   for(i=0;i<(int) strlen(s);i++)    // conversion \ -> /
  1705.     if (s[i]=='\\')
  1706.       s[i]='/';
  1707.   if (s[strlen(s)-1]!='/')    // ajouter slash α la fin
  1708.     strcat(s,"/");
  1709. }
  1710.  
  1711. // dΘtermine si l'argument est une option
  1712. int cmdl_opt(char* s) {
  1713.   if (s[0]=='-') {  // c'est peut Ωtre une option
  1714.     if (strchr(s,'.')!=NULL)
  1715.       return 0;    // sans doute un -www.truc.fr (note: -www n'est pas compris)
  1716.     else if (strchr(s,'/')!=NULL)
  1717.       return 0;    // idem, -*cgi-bin/
  1718.     else if (strchr(s,'*')!=NULL)
  1719.       return 0;    // joker, idem
  1720.     else
  1721.       return 1;
  1722.   } else return 0;
  1723. }
  1724.  
  1725.  
  1726.  
  1727.