home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Comunicatii / htttrack / httrack-3.32-2.exe / {app} / src / htstools.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-25  |  26.2 KB  |  993 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche 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. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       various tools (filename analyzing ..)                  */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htstools.h"
  42.  
  43. /* specific definitions */
  44. #include "htsbase.h"
  45. #include <ctype.h>
  46. /* String */
  47. #include "htsstrings.h"
  48. /* END specific definitions */
  49.  
  50.  
  51. // forme α partir d'un lien et du contexte (origin_fil et origin_adr d'o∙ il est tirΘ) adr et fil
  52. // [adr et fil sont des buffers de 1ko]
  53. // 0 : ok
  54. // -1 : erreur
  55. // -2 : protocole non supportΘ (ftp)
  56. int ident_url_relatif(char *lien,char* origin_adr,char* origin_fil,char* adr,char* fil) {
  57.   int ok=0;
  58.   int scheme=0;
  59.  
  60.   adr[0]='\0'; fil[0]='\0';    //effacer buffers
  61.  
  62.   // lien non vide!
  63.   if (strnotempty(lien)==0) return -1;    // erreur!
  64.  
  65.   // Scheme?
  66.   {
  67.     char* a=lien;
  68.     while (isalpha((unsigned char)*a))
  69.       a++;
  70.     if (*a == ':')
  71.       scheme=1;
  72.   }
  73.  
  74.   // filtrer les parazites (mailto & cie)
  75.   // scheme+authority (//)
  76.   if (
  77.                (strfield(lien,"http://"))        // scheme+//
  78.             || (strfield(lien,"file://"))   // scheme+//
  79.             || (strncmp(lien,"//",2)==0)    // // sans scheme (-> default)
  80.        ) {
  81.     if (ident_url_absolute(lien,adr,fil)==-1) {        
  82.       ok=-1;    // erreur URL
  83.     }
  84.   }
  85.   else if (strfield(lien,"ftp://")) {
  86.     // Note: ftp:foobar.gif is not valid
  87.     if (ftp_available()) {     // ftp supportΘ
  88.       if (ident_url_absolute(lien,adr,fil)==-1) {        
  89.         ok=-1;    // erreur URL
  90.       }
  91.     } else {
  92.       ok=-2;  // non supportΘ
  93.     }
  94. #if HTS_USEOPENSSL
  95.   } else if (SSL_is_available && strfield(lien,"https://")) {
  96.     // Note: ftp:foobar.gif is not valid
  97.     if (ident_url_absolute(lien,adr,fil)==-1) {        
  98.       ok=-1;    // erreur URL
  99.     }
  100. #endif
  101.   } else if ((scheme) && (
  102.     (!strfield(lien,"http:"))
  103.     && (!strfield(lien,"https:"))
  104.     && (!strfield(lien,"ftp:"))
  105.     )) {
  106.     ok=-1;      // unknown scheme
  107.   } else {    // c'est un lien relatif
  108.     char* a;
  109.     
  110.     // On forme l'URL complΦte α partie de l'url actuelle
  111.     // et du chemin actuel si besoin est.
  112.     
  113.     // copier adresse
  114.     if (((int) strlen(origin_adr)<HTS_URLMAXSIZE) && ((int) strlen(origin_fil)<HTS_URLMAXSIZE) && ((int) strlen(lien)<HTS_URLMAXSIZE)) {
  115.  
  116.       /* patch scheme if necessary */
  117.       if (strfield(lien,"http:")) {
  118.         lien+=5;
  119.         strcpybuff(adr, jump_protocol(origin_adr));    // mΩme adresse ; protocole vide (http)
  120.       } else if (strfield(lien,"https:")) {
  121.         lien+=6;
  122.         strcpybuff(adr, "https://");   // mΩme adresse forcΘe en https
  123.         strcatbuff(adr, jump_protocol(origin_adr));
  124.       } else if (strfield(lien,"ftp:")) {
  125.         lien+=4;
  126.         strcpybuff(adr, "ftp://");   // mΩme adresse forcΘe en ftp
  127.         strcatbuff(adr, jump_protocol(origin_adr));
  128.       } else {
  129.         strcpybuff(adr,origin_adr);    // mΩme adresse ; et mΩme Θventuel protocole
  130.       }
  131.       
  132.       if (*lien!='/') {  // sinon c'est un lien absolu
  133.         if (*lien == '\0') {
  134.           strcpybuff(fil,origin_fil);
  135.         } else if (*lien == '?') {     // example: a href="?page=2"
  136.           char* a;
  137.           strcpybuff(fil,origin_fil);
  138.           a=strchr(fil,'?');
  139.           if (a) *a='\0';
  140.           strcatbuff(fil,lien);
  141.         } else {
  142.           a=strchr(origin_fil,'?');
  143.           if (a == NULL) a=origin_fil+strlen(origin_fil);
  144.           while((*a!='/') && ( a > origin_fil) ) a--;
  145.           if (*a=='/') {    // ok on a un '/'
  146.             if ( (((int) (a - origin_fil))+1+strlen(lien)) < HTS_URLMAXSIZE) {
  147.               // copier chemin
  148.               strncpy(fil,origin_fil,((int) (a - origin_fil))+1);
  149.               *(fil + ((int) (a - origin_fil))+1)='\0';
  150.               
  151.               // copier chemin relatif
  152.               if (((int) strlen(fil)+(int) strlen(lien)) < HTS_URLMAXSIZE) {
  153.                 strcatbuff(fil,lien + ((*lien=='/')?1:0) );      
  154.                 // simplifier url pour les ../
  155.                 fil_simplifie(fil);
  156.               } else
  157.                 ok=-1;    // erreur
  158.             } else {    // erreur
  159.               ok=-1;    // erreur URL
  160.             }
  161.           } else {    // erreur
  162.             ok=-1;    // erreur URL
  163.           }
  164.         }
  165.       } else { // chemin absolu
  166.         // copier chemin directement
  167.         strcatbuff(fil,lien);      
  168.         fil_simplifie(fil);
  169.       }  // *lien!='/'
  170.     } else
  171.       ok=-1;
  172.     
  173.   }  // test news: etc.
  174.  
  175.   // case insensitive pour adresse
  176.   {
  177.     char *a=jump_identification(adr);
  178.     while(*a) {
  179.       if ((*a>='A') && (*a<='Z'))
  180.         *a+='a'-'A';       
  181.       a++;
  182.     }
  183.   }
  184.   
  185.   return ok;
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192. // crΘer dans s, α partir du chemin courant curr_fil, le lien vers link (absolu)
  193. // un ident_url_relatif a dΘja ΘtΘ fait avant, pour que link ne soit pas un chemin relatif
  194. int lienrelatif(char* s,char* link,char* curr_fil) {
  195.   char _curr[HTS_URLMAXSIZE*2];
  196.   char newcurr_fil[HTS_URLMAXSIZE*2],newlink[HTS_URLMAXSIZE*2];
  197.   char* curr;
  198.   //int n=0;
  199.   char* a;
  200.   int slash=0;
  201.   //
  202.   newcurr_fil[0]='\0'; newlink[0]='\0';
  203.   //
  204.  
  205.   // patch: Θliminer les ? (paramΦtres) sinon bug
  206.   if ( (a=strchr(curr_fil,'?')) ) {
  207.     strncatbuff(newcurr_fil,curr_fil,(int) (a - curr_fil));
  208.     curr_fil = newcurr_fil;
  209.   }
  210.   if ( (a=strchr(link,'?')) ) {
  211.     strncatbuff(newlink,link,(int) (a - link));
  212.     link = newlink;
  213.   }
  214.  
  215.   // recopier uniquement le chemin courant
  216.   curr=_curr;
  217.   strcpybuff(curr,curr_fil);
  218.   if ((a=strchr(curr,'?'))==NULL)  // couper au ? (params)
  219.     a=curr+strlen(curr)-1;         // pas de params: aller α la fin
  220.   while((*a!='/') && ( a> curr)) a--;       // chercher dernier / du chemin courant
  221.   if (*a=='/') *(a+1)='\0';                           // couper dernier /
  222.   
  223.   // "effacer" s
  224.   s[0]='\0';
  225.   
  226.   // sauter ce qui est commun aux 2 chemins
  227.   {
  228.     char *l,*c;
  229.     if (*link=='/') link++;  // sauter slash
  230.     if (*curr=='/') curr++;
  231.     l=link;
  232.     c=curr;
  233.     // couper ce qui est commun
  234. #if HTS_CASSE
  235.     while ((*link==*curr) && (*link!=0)) {link++; curr++; }
  236. #else
  237.     while ((streql(*link,*curr)) && (*link!=0)) {link++; curr++; }
  238. #endif
  239.     // mais on veut un rΘpertoirer entier!
  240.     // si on a /toto/.. et /toto2/.. on ne veut pas sauter /toto !
  241.     while(((*link!='/') || (*curr!='/')) && ( link > l)) { link--; curr--; }
  242.     //if (*link=='/') link++;
  243.     //if (*curr=='/') curr++;
  244.   }
  245.   
  246.   // calculer la profondeur du rΘpertoire courant et remonter
  247.   // LES ../ ONT ETE SIMPLIFIES
  248.   a=curr;
  249.   if (*a=='/') a++;
  250.   while(*a) if (*(a++)=='/') strcatbuff(s,"../");
  251.   //if (strlen(s)==0) strcatbuff(s,"/");
  252.  
  253.   if (slash) strcatbuff(s,"/");    // garder absolu!!
  254.   
  255.   // on est dans le rΘpertoire de dΘpart, copier
  256.   strcatbuff(s,link + ((*link=='/')?1:0) );
  257.  
  258.   /* Security check */
  259.   if (strlen(s) >= HTS_URLMAXSIZE)
  260.     return -1;
  261.  
  262.   // on a maintenant une chaine de la forme ../../test/truc.html  
  263.   return 0;
  264. }
  265.  
  266. /* Is the link absolute (http://www..) or relative (/bar/foo.html) ? */
  267. int link_has_authority(char* lien) {
  268.   char* a=lien;
  269.   if (isalpha((unsigned char)*a)) {
  270.     // Skip scheme?
  271.     while (isalpha((unsigned char)*a))
  272.       a++;
  273.     if (*a == ':')
  274.       a++;
  275.     else
  276.       return 0;
  277.   }
  278.   if (strncmp(a,"//",2) == 0)
  279.     return 1;
  280.   return 0;
  281. }
  282.  
  283. int link_has_authorization(char* lien) {
  284.   char* adr = jump_protocol(lien);
  285.   char* firstslash = strchr(adr, '/');
  286.   char* detect = strchr(adr, '@');
  287.   if (firstslash) {
  288.     if (detect) {
  289.       return (detect < firstslash);
  290.     }
  291.   } else {
  292.     return (detect != NULL);
  293.   }
  294.   return 0;
  295. }
  296.  
  297.  
  298. // conversion chemin de fichier/dossier vers 8-3 ou ISO9660
  299. void long_to_83(int mode,char* n83,char* save) {
  300.   n83[0]='\0';
  301.  
  302.   while(*save) {
  303.     char fn83[256],fnl[256];
  304.     int i=0;
  305.     fn83[0]=fnl[0]='\0';
  306.     while((save[i]) && (save[i]!='/')) { fnl[i]=save[i]; i++; }
  307.     fnl[i]='\0';
  308.     // conversion
  309.     longfile_to_83(mode,fn83,fnl);
  310.     strcatbuff(n83,fn83);
  311.  
  312.     save+=i;
  313.     if (*save=='/') { strcatbuff(n83,"/"); save++; }
  314.   }
  315. }
  316.  
  317.  
  318. // conversion nom de fichier/dossier isolΘ vers 8-3 ou ISO9660
  319. void longfile_to_83(int mode,char* n83,char* save) {
  320.   int i=0,j=0,max=0;
  321.   char nom[256];
  322.   char ext[256];
  323.   nom[0]=ext[0]='\0';
  324.   
  325.   switch(mode) {
  326.   case 1:
  327.     max=8;
  328.     break;
  329.   case 2:
  330.     max=30;
  331.     break;
  332.   default:
  333.     max=8;
  334.     break;
  335.   }
  336.  
  337.   /* No starting . */
  338.   if (save[0] == '.') {
  339.     save[0]='_';
  340.   }
  341.   /* No multiple dots */
  342.   {
  343.     char* last_dot=strrchr(save, '.');
  344.     char* dot;
  345.     while((dot=strchr(save, '.'))) {
  346.       *dot = '_';
  347.     }
  348.     if (last_dot) {
  349.       *last_dot='.';
  350.     }
  351.   }
  352.   /* 
  353.     Avoid: (ISO9660, but also suitable for 8-3)
  354.     (Thanks to jonat@cellcast.com for te hint)
  355.     /:;?\#*~
  356.     0x00-0x1f and 0x80-0xff
  357.   */
  358.   for(i=0 ; i < (int) strlen(save) ; i++) {
  359.     if (
  360.       (strchr("/:;?\\#*~", save[i]))
  361.       ||
  362.       (save[i] < 32)
  363.       ||
  364.       (save[i] >= 127)
  365.       ) {
  366.       save[i]='_';
  367.     }
  368.   }
  369.  
  370.   i=j=0;
  371.   while((i<max) && (save[j]) && (save[j]!='.')) {
  372.     if (save[j]!=' ') {
  373.       nom[i]=save[j]; 
  374.       i++; 
  375.     } 
  376.     j++; 
  377.   }  // recopier nom
  378.   nom[i]='\0';
  379.   if (save[j]) {  // il reste au moins un point
  380.     i=strlen(save)-1;
  381.     while((i>0) && (save[i]!='.') && (save[i]!='/')) i--;    // rechercher dernier .
  382.     if (save[i]=='.') {  // point!
  383.       int j=0;
  384.       i++;
  385.       while((j<3) && (save[i]) ) { if (save[i]!=' ') { ext[j]=save[i]; j++; } i++; }
  386.       ext[j]='\0';
  387.     }
  388.   }
  389.   // corriger vers 8-3
  390.   n83[0]='\0';
  391.   strncatbuff(n83,nom,8);
  392.   if (strnotempty(ext)) {
  393.     strcatbuff(n83,".");
  394.     strncatbuff(n83,ext,3);    
  395.   }
  396. }
  397.  
  398. // Θcrire backblue.gif
  399. int verif_backblue(httrackp* opt,char* base) {
  400.   int* done;
  401.   int ret=0;
  402.   NOSTATIC_RESERVE(done, int, 1);
  403.   //
  404.   if (!base) {   // init
  405.     *done=0;
  406.     return 0;
  407.   }
  408.   if ( (!*done)
  409.     || (fsize(fconcat(base,"backblue.gif")) != HTS_DATA_BACK_GIF_LEN)) {
  410.     FILE* fp = filecreate(fconcat(base,"backblue.gif"));
  411.     *done=1;
  412.     if (fp) {
  413.       if (fwrite(HTS_DATA_BACK_GIF,HTS_DATA_BACK_GIF_LEN,1,fp) != HTS_DATA_BACK_GIF_LEN)
  414.         ret=1;
  415.       fclose(fp);
  416.       usercommand(opt,0,NULL,fconcat(base,"backblue.gif"),"","");
  417.     } else
  418.       ret=1;
  419.     //
  420.     fp = filecreate(fconcat(base,"fade.gif"));
  421.     if (fp) {
  422.       if (fwrite(HTS_DATA_FADE_GIF,HTS_DATA_FADE_GIF_LEN,1,fp) != HTS_DATA_FADE_GIF_LEN)
  423.         ret=1;
  424.       fclose(fp);
  425.       usercommand(opt,0,NULL,fconcat(base,"fade.gif"),"","");
  426.     } else
  427.       ret=1;
  428.   } 
  429.   return ret;
  430. }
  431.  
  432. // flag
  433. int verif_external(int nb,int test) {
  434.   int* status;
  435.   NOSTATIC_RESERVE(status, int, 2);
  436.   if (!test)
  437.     status[nb]=0;   // reset
  438.   else if (!status[nb]) {
  439.     status[nb]=1;
  440.     return 1;
  441.   }
  442.   return 0;
  443. }
  444.  
  445.  
  446. // recherche chaεne de type truc<espaces>=
  447. // renvoi dΘcalage α effectuer ou 0 si non trouvΘ
  448. /* SECTION OPTIMISEE:
  449. #define rech_tageq(adr,s) ( \
  450.   ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) ? \
  451.     ( (streql(*adr,*s)) ? \
  452.       (__rech_tageq(adr,s)) \
  453.       : 0 \
  454.     ) \
  455.     : 0\
  456.   )
  457. */
  458. /*
  459. HTS_INLINE int rech_tageq(const char* adr,const char* s) { 
  460.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  461.     if (streql(*adr,*s)) {                           // tester premier octet (optimisation)
  462.       return __rech_tageq(adr,s);
  463.     }
  464.   }
  465.   return 0;
  466. }
  467. */
  468. // DeuxiΦme partie
  469. HTS_INLINE int __rech_tageq(const char* adr,const char* s) { 
  470.   int p;
  471.   p=strfield(adr,s);
  472.   if (p) {
  473.     while(is_space(adr[p])) p++;
  474.     if (adr[p]=='=') {
  475.       return p+1;
  476.     }
  477.   }
  478.   return 0;
  479. }
  480.  
  481. HTS_INLINE int rech_endtoken(const char* adr, const char** start) {
  482.   char quote = '\0';
  483.   int length = 0;
  484.   while(is_space(*adr)) adr++;
  485.   if (*adr == '"' || *adr == '\'') 
  486.     quote = *adr++;
  487.   *start = adr;
  488.   while(*adr != 0 && *adr != quote && (quote != '\0' || !is_space(*adr)) ) {
  489.     length++;
  490.     adr++;
  491.   }
  492.   return length;
  493. }
  494. // same, but check begining of adr wirh s (for <object src="bar.mov" .. hotspot123="foo.html">)
  495. HTS_INLINE int __rech_tageqbegdigits(const char* adr,const char* s) { 
  496.   int p;
  497.   p=strfield(adr,s);
  498.   if (p) {
  499.     while(isdigit((unsigned char)adr[p]))  p++;      // jump digits
  500.     while(is_space(adr[p])) p++;
  501.     if (adr[p]=='=') {
  502.       return p+1;
  503.     }
  504.   }
  505.   return 0;
  506. }
  507.  
  508. // tag sans =
  509. HTS_INLINE int rech_sampletag(const char* adr,const char* s) { 
  510.   int p;
  511.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  512.     p=strfield(adr,s);
  513.     if (p) {
  514.       if (!isalnum((unsigned char)adr[p])) {  // <srcbis n'est pas <src
  515.         return 1;
  516.       }
  517.       return 0;
  518.     }
  519.   }
  520.   return 0;
  521. }
  522.  
  523. // teste si le tag contenu dans from est Θgal α "tag"
  524. HTS_INLINE int check_tag(char* from,const char* tag) {
  525.   char* a=from+1;
  526.   int i=0;
  527.   char s[256];
  528.   while(is_space(*a)) a++;
  529.   while((isalnum((unsigned char)*a) || (*a=='/')) && (i<250)) { s[i++]=*a; a++; }
  530.   s[i++]='\0';
  531.   return (strfield2(s,tag));  // comparer
  532. }
  533.  
  534. // teste si un fichier dΘpasse le quota
  535. int istoobig(LLint size,LLint maxhtml,LLint maxnhtml,char* type) {
  536.   int ok=1;
  537.   if (size>0) {
  538.     if (is_hypertext_mime(type, "")) {
  539.       if (maxhtml>0) {
  540.         if (size>maxhtml)
  541.           ok=0;
  542.       }
  543.     } else {
  544.       if (maxnhtml>0) {
  545.         if (size>maxnhtml)
  546.           ok=0;
  547.       }
  548.     }
  549.   }
  550.   return (!ok);
  551. }
  552.  
  553.  
  554. static int sortTopIndexFnc(const void * a_, const void * b_) {
  555.   int cmp;
  556.   topindex_chain** a = (topindex_chain**) a_;
  557.   topindex_chain** b = (topindex_chain**) b_;
  558.   /* Category first, then name */
  559.   if ((cmp = (*a)->level - (*b)->level) == 0) {
  560.     if ((cmp = strcmpnocase((*a)->category, (*b)->category)) == 0) {
  561.       cmp = strcmpnocase((*a)->name, (*b)->name);
  562.     }
  563.   }
  564.   return cmp;
  565. }
  566.  
  567. HTSEXT_API char* hts_getcategory(char* filename);
  568.  
  569. HTSEXT_API int hts_buildtopindex(httrackp* opt,char* path,char* binpath) {
  570.   FILE* fpo;
  571.   int retval=0;
  572.   char rpath[1024*2];
  573.   char *toptemplate_header=NULL,*toptemplate_body=NULL,*toptemplate_footer=NULL,*toptemplate_bodycat=NULL;
  574.   
  575.   // et templates html
  576.   toptemplate_header=readfile_or(fconcat(binpath,"templates/topindex-header.html"),HTS_INDEX_HEADER);
  577.   toptemplate_body=readfile_or(fconcat(binpath,"templates/topindex-body.html"),HTS_INDEX_BODY);
  578.   toptemplate_bodycat=readfile_or(fconcat(binpath,"templates/topindex-bodycat.html"),HTS_INDEX_BODYCAT);
  579.   toptemplate_footer=readfile_or(fconcat(binpath,"templates/topindex-footer.html"),HTS_INDEX_FOOTER);
  580.   
  581.   if (toptemplate_header && toptemplate_body && toptemplate_footer && toptemplate_bodycat) {
  582.     
  583.     strcpybuff(rpath,path);
  584.     if (rpath[0]) {
  585.       if (rpath[strlen(rpath)-1]=='/')
  586.         rpath[strlen(rpath)-1]='\0';
  587.     }
  588.     
  589.     fpo=fopen(fconcat(rpath,"/index.html"),"wb");
  590.     if (fpo) {
  591.       String iname = STRING_EMPTY;
  592.       find_handle h;
  593.       verif_backblue(opt,concat(rpath,"/"));    // gΘnΘrer gif
  594.       // Header
  595.       fprintf(fpo,toptemplate_header,
  596.         "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"
  597.         );
  598.       
  599.       /* Find valid project names */
  600.       h = hts_findfirst(rpath);
  601.       if (h) {
  602.         struct topindex_chain * chain=NULL;
  603.         struct topindex_chain * startchain=NULL;
  604.         String iname = STRING_EMPTY;
  605.         int chainSize = 0;
  606.         do {
  607.           if (hts_findisdir(h)) {
  608.             StringStrcpy(iname,rpath);
  609.             StringStrcat(iname,"/");
  610.             StringStrcat(iname,hts_findgetname(h));
  611.             StringStrcat(iname,"/index.html");
  612.             if (fexist(StringBuff(iname))) {
  613.               int level = 0;
  614.               char* category = NULL;
  615.               struct topindex_chain * oldchain=chain;
  616.               
  617.               /* Check for an existing category */
  618.               StringStrcpy(iname,rpath);
  619.               StringStrcat(iname,"/");
  620.               StringStrcat(iname,hts_findgetname(h));
  621.               StringStrcat(iname,"/hts-cache/winprofile.ini");
  622.               if (fexist(StringBuff(iname))) {
  623.                 category = hts_getcategory(StringBuff(iname));
  624.                 if (category != NULL) {
  625.                   if (*category == '\0') {
  626.                     freet(category);
  627.                     category = NULL;
  628.                   }
  629.                 }
  630.               }
  631.               if (category == NULL) {
  632.                 category = strdupt("No categories");
  633.                 level = 1;
  634.               }
  635.  
  636.               chain=calloc(sizeof(struct topindex_chain), 1);
  637.               chainSize++;
  638.               if (!startchain) {
  639.                 startchain=chain;
  640.               }
  641.               if (chain) {
  642.                 if (oldchain) {
  643.                   oldchain->next=chain;
  644.                 }
  645.                 chain->next=NULL;
  646.                 strcpybuff(chain->name, hts_findgetname(h));
  647.                 chain->category = category;
  648.                 chain->level = level;
  649.               }
  650.             }
  651.             
  652.           }
  653.         } while(hts_findnext(h));
  654.         hts_findclose(h);
  655.         StringFree(iname);
  656.         
  657.         /* Sort chain */
  658.         {
  659.           struct topindex_chain** sortedElts = (struct topindex_chain**) calloct(sizeof(topindex_chain*), chainSize);
  660.           assertf(sortedElts != NULL);
  661.           if (sortedElts != NULL) {
  662.             int i;
  663.             char* category = "";
  664.             
  665.             /* Build array */
  666.             struct topindex_chain * chain = startchain;
  667.             for(i = 0 ; i < chainSize ; i++) {
  668.               assertf(chain != NULL);
  669.               sortedElts[i] = chain;
  670.               chain = chain->next;
  671.             }
  672.             qsort(sortedElts, chainSize, sizeof(topindex_chain*), sortTopIndexFnc);
  673.             
  674.             /* Build sorted index */
  675.             for(i = 0 ; i < chainSize ; i++) {
  676.               char hname[HTS_URLMAXSIZE*2];
  677.               strcpybuff(hname,sortedElts[i]->name);
  678.               escape_check_url(hname);
  679.  
  680.               /* Changed category */
  681.               if (strcmp(category, sortedElts[i]->category) != 0) {
  682.                 category = sortedElts[i]->category;
  683.                 fprintf(fpo,toptemplate_bodycat, category);
  684.               }
  685.               fprintf(fpo,toptemplate_body,
  686.                 hname,
  687.                 sortedElts[i]->name
  688.                 );
  689.             }
  690.             
  691.             /* Wipe elements */
  692.             for(i = 0 ; i < chainSize ; i++) {
  693.               freet(sortedElts[i]->category);
  694.               freet(sortedElts[i]);
  695.               sortedElts[i] = NULL;
  696.             }
  697.             freet(sortedElts);
  698.             
  699.             /* Return value */
  700.             retval=1;
  701.           }
  702.         }
  703.         
  704.       }
  705.       
  706.       // Footer
  707.       fprintf(fpo,toptemplate_footer,
  708.         "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"
  709.         );
  710.       
  711.       fclose(fpo);
  712.       
  713.     }
  714.     
  715.   }
  716.  
  717.   if (toptemplate_header)
  718.     freet(toptemplate_header);
  719.   if (toptemplate_body)
  720.     freet(toptemplate_body);
  721.   if (toptemplate_footer)
  722.     freet(toptemplate_footer);
  723.   if (toptemplate_body)
  724.     freet(toptemplate_body);
  725.   
  726.   return retval;
  727. }
  728.  
  729. HTSEXT_API char* hts_getcategory(char* filename) {
  730.   String categ = STRING_EMPTY;
  731.   if (fexist(filename)) {
  732.     FILE* fp = fopen(filename, "rb");
  733.     if (fp != NULL) {
  734.       int done=0;
  735.       while(!feof(fp) && !done) {
  736.         char line[1024];
  737.         int n = linput(fp, line, sizeof(line) - 2);
  738.         if (n > 0) {
  739.           if (strfield(line, "category=")) {
  740.             unescapehttp(line+9, &categ);
  741.             done=1;
  742.           }
  743.         }
  744.       }
  745.       fclose(fp);
  746.     }
  747.   }
  748.   return StringBuff(categ);
  749. }
  750.  
  751. HTSEXT_API char* hts_getcategories(char* path, int type) {
  752.   String categ = STRING_EMPTY;
  753.   String profiles = STRING_EMPTY;
  754.   char* rpath = path;
  755.   find_handle h;
  756.   inthash hashCateg = NULL;
  757.   if (rpath[0]) {
  758.     if (rpath[strlen(rpath)-1]=='/') {
  759.       rpath[strlen(rpath)-1]='\0';      /* note: patching stored (inhash) value */
  760.     }
  761.   }
  762.   h = hts_findfirst(rpath);
  763.   if (h) {
  764.     struct topindex_chain * chain=NULL;
  765.     struct topindex_chain * startchain=NULL;
  766.     String iname = STRING_EMPTY;
  767.     if (type == 1) {
  768.       hashCateg = inthash_new(127);
  769.       StringStrcat(categ, "Test category 1");
  770.       StringStrcat(categ, "\r\nTest category 2");
  771.     }
  772.     do {
  773.       if (hts_findisdir(h)) {
  774.         char line2[1024];
  775.         StringStrcpy(iname,rpath);
  776.         StringStrcat(iname,"/");
  777.         StringStrcat(iname,hts_findgetname(h));
  778.         StringStrcat(iname,"/hts-cache/winprofile.ini");
  779.         if (fexist(StringBuff(iname))) {
  780.           if (type == 1) {
  781.             FILE* fp = fopen(StringBuff(iname), "rb");
  782.             if (fp != NULL) {
  783.               int done=0;
  784.               while(!feof(fp) && !done) {
  785.                 int n = linput(fp, line2, sizeof(line2) - 2);
  786.                 if (n > 0) {
  787.                   if (strfield(line2, "category=")) {
  788.                     if (*(line2+9)) {
  789.                       if (!inthash_read(hashCateg, line2+9, NULL)) {
  790.                         inthash_write(hashCateg, line2+9, 0);
  791.                         if (StringLength(categ) > 0) {
  792.                           StringStrcat(categ, "\r\n");
  793.                         }
  794.                         unescapehttp(line2+9, &categ);
  795.                       }
  796.                     }
  797.                     done=1;
  798.                   }
  799.                 }
  800.               }
  801.               line2[0] = '\0';
  802.               fclose(fp);
  803.             }
  804.           } else {
  805.             if (StringLength(profiles) > 0) {
  806.               StringStrcat(profiles, "\r\n");
  807.             }
  808.             StringStrcat(profiles, hts_findgetname(h));
  809.           }
  810.         }
  811.         
  812.       }
  813.     } while(hts_findnext(h));
  814.     hts_findclose(h);
  815.     StringFree(iname);
  816.   }
  817.   if (hashCateg) {
  818.     inthash_delete(&hashCateg);
  819.     hashCateg = NULL;
  820.   }
  821.   if (type == 1)
  822.     return StringBuff(categ);
  823.   else
  824.     return StringBuff(profiles);
  825. }
  826.  
  827.  
  828.  
  829.  
  830. // Portable directory find functions
  831. /*
  832. // Example:
  833. find_handle h = hts_findfirst("/tmp");
  834. if (h) {
  835.   do {
  836.     if (hts_findisfile(h))
  837.       printf("File: %s (%d octets)\n",hts_findgetname(h),hts_findgetsize(h));
  838.     else if (hts_findisdir(h))
  839.       printf("Dir: %s\n",hts_findgetname(h));
  840.   } while(hts_findnext(h));
  841.   hts_findclose(h);
  842. }
  843. */
  844. HTSEXT_API find_handle hts_findfirst(char* path) {
  845.   if (path) {
  846.     if (strnotempty(path)) {
  847.       find_handle_struct* find = (find_handle_struct*) calloc(1,sizeof(find_handle_struct));
  848.       if (find) {
  849.         memset(find, 0, sizeof(find_handle_struct));
  850. #if HTS_WIN
  851.         {
  852.           char rpath[1024*2];
  853.           strcpybuff(rpath,path);
  854.           if (rpath[0]) {
  855.             if (rpath[strlen(rpath)-1]!='\\')
  856.               strcatbuff(rpath,"\\");
  857.           }
  858.           strcatbuff(rpath,"*.*");
  859.           find->handle = FindFirstFileA(rpath,&find->hdata);
  860.           if (find->handle != INVALID_HANDLE_VALUE)
  861.             return find;
  862.         }
  863. #else
  864.         strcpybuff(find->path,path);
  865.         {
  866.           if (find->path[0]) {
  867.             if (find->path[strlen(find->path)-1]!='/')
  868.               strcatbuff(find->path,"/");
  869.           }
  870.         }
  871.         find->hdir=opendir(path);
  872.         if (find->hdir != NULL) {
  873.           if (hts_findnext(find) == 1)
  874.             return find;
  875.         }
  876. #endif
  877.         free((void*)find);
  878.       }
  879.     }
  880.   }
  881.   return NULL;   
  882. }
  883.  
  884. HTSEXT_API int hts_findnext(find_handle find) {
  885.   if (find) {
  886. #if HTS_WIN
  887.     if ( (FindNextFileA(find->handle,&find->hdata)))
  888.       return 1;
  889. #else
  890.     memset(&(find->filestat), 0, sizeof(find->filestat));
  891.     if ((find->dirp=readdir(find->hdir)))
  892.       if (find->dirp->d_name)
  893.         if (!stat(concat(find->path,find->dirp->d_name),&find->filestat))
  894.           return 1;
  895. #endif
  896.   }
  897.   return 0;
  898. }
  899.  
  900. HTSEXT_API int hts_findclose(find_handle find) {
  901.   if (find) {
  902. #if HTS_WIN
  903.     if (find->handle) {
  904.       FindClose(find->handle);
  905.       find->handle=NULL;
  906.     }
  907. #else
  908.     if (find->hdir) {
  909.       closedir (find->hdir);
  910.       find->hdir=NULL;
  911.     }
  912. #endif
  913.     free((void*)find);
  914.   }
  915.   return 0;
  916. }
  917.  
  918. HTSEXT_API char* hts_findgetname(find_handle find) {
  919.   if (find) {
  920. #if HTS_WIN
  921.     return find->hdata.cFileName;
  922. #else
  923.     if (find->dirp)
  924.       return find->dirp->d_name;
  925. #endif
  926.   }
  927.   return NULL;
  928. }
  929.  
  930. HTSEXT_API int hts_findgetsize(find_handle find) {
  931.   if (find) {
  932. #if HTS_WIN
  933.     return find->hdata.nFileSizeLow;
  934. #else
  935.     return find->filestat.st_size;
  936. #endif
  937.   }
  938.   return -1;
  939. }
  940.  
  941. HTSEXT_API int hts_findisdir(find_handle find) {
  942.   if (find) {
  943.     if (!hts_findissystem(find)) {
  944. #if HTS_WIN
  945.       if (find->hdata.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY)
  946.         return 1;
  947. #else
  948.       if (S_ISDIR(find->filestat.st_mode))
  949.         return 1;
  950. #endif
  951.     }
  952.   }
  953.   return 0;
  954. }
  955. HTSEXT_API int hts_findisfile(find_handle find) {
  956.   if (find) {
  957.     if (!hts_findissystem(find)) {
  958. #if HTS_WIN
  959.       if (!(find->hdata.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY))
  960.         return 1;
  961. #else
  962.       if (S_ISREG(find->filestat.st_mode))
  963.         return 1;
  964. #endif
  965.     }
  966.   }
  967.   return 0;
  968. }
  969. HTSEXT_API int hts_findissystem(find_handle find) {
  970.   if (find) {
  971. #if HTS_WIN
  972.     if (find->hdata.dwFileAttributes  & (FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_TEMPORARY))
  973.       return 1;
  974.     else if ( (!strcmp(find->hdata.cFileName,"..")) || (!strcmp(find->hdata.cFileName,".")) )
  975.       return 1;
  976. #else
  977.     if (
  978.       (S_ISCHR(find->filestat.st_mode))
  979.       || 
  980.       (S_ISBLK(find->filestat.st_mode))
  981.       || 
  982.       (S_ISFIFO(find->filestat.st_mode))
  983.       || 
  984.       (S_ISSOCK(find->filestat.st_mode))
  985.       )
  986.       return 1;
  987.     else if ( (!strcmp(find->dirp->d_name,"..")) || (!strcmp(find->dirp->d_name,".")) )
  988.       return 1;
  989. #endif
  990.   }
  991.   return 0;
  992. }
  993.