home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 71 / CDROM71.ISO / internet / navoff / data1.cab / Sources / src / htsmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-01  |  64.0 KB  |  1,761 lines

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