home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 April / enter-2004-04.iso / files / httrack-3.30.exe / {app} / src / htstools.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-10-11  |  20.2 KB  |  802 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. #include "htstools.h"
  39.  
  40. /* specific definitions */
  41. #include "htsbase.h"
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <ctype.h>
  46. /* END specific definitions */
  47.  
  48.  
  49. // forme α partir d'un lien et du contexte (origin_fil et origin_adr d'o∙ il est tirΘ) adr et fil
  50. // [adr et fil sont des buffers de 1ko]
  51. // 0 : ok
  52. // -1 : erreur
  53. // -2 : protocole non supportΘ (ftp)
  54. int ident_url_relatif(char *lien,char* origin_adr,char* origin_fil,char* adr,char* fil) {
  55.   int ok=0;
  56.   int scheme=0;
  57.  
  58.   adr[0]='\0'; fil[0]='\0';    //effacer buffers
  59.  
  60.   // lien non vide!
  61.   if (strnotempty(lien)==0) return -1;    // erreur!
  62.  
  63.   // Scheme?
  64.   {
  65.     char* a=lien;
  66.     while (isalpha((unsigned char)*a))
  67.       a++;
  68.     if (*a == ':')
  69.       scheme=1;
  70.   }
  71.  
  72.   // filtrer les parazites (mailto & cie)
  73.   // scheme+authority (//)
  74.   if (
  75.                (strfield(lien,"http://"))        // scheme+//
  76.             || (strfield(lien,"file://"))   // scheme+//
  77.             || (strncmp(lien,"//",2)==0)    // // sans scheme (-> default)
  78.        ) {
  79.     if (ident_url_absolute(lien,adr,fil)==-1) {        
  80.       ok=-1;    // erreur URL
  81.     }
  82.   }
  83.   else if (strfield(lien,"ftp://")) {
  84.     // Note: ftp:foobar.gif is not valid
  85.     if (ftp_available()) {     // ftp supportΘ
  86.       if (ident_url_absolute(lien,adr,fil)==-1) {        
  87.         ok=-1;    // erreur URL
  88.       }
  89.     } else {
  90.       ok=-2;  // non supportΘ
  91.     }
  92. #if HTS_USEOPENSSL
  93.   } else if (SSL_is_available && strfield(lien,"https://")) {
  94.     // Note: ftp:foobar.gif is not valid
  95.     if (ident_url_absolute(lien,adr,fil)==-1) {        
  96.       ok=-1;    // erreur URL
  97.     }
  98. #endif
  99.   } else if ((scheme) && (
  100.     (!strfield(lien,"http:"))
  101.     && (!strfield(lien,"https:"))
  102.     && (!strfield(lien,"ftp:"))
  103.     )) {
  104.     ok=-1;      // unknown scheme
  105.   } else {    // c'est un lien relatif
  106.     char* a;
  107.     
  108.     // On forme l'URL complΦte α partie de l'url actuelle
  109.     // et du chemin actuel si besoin est.
  110.     
  111.     // copier adresse
  112.     if (((int) strlen(origin_adr)<HTS_URLMAXSIZE) && ((int) strlen(origin_fil)<HTS_URLMAXSIZE) && ((int) strlen(lien)<HTS_URLMAXSIZE)) {
  113.  
  114.       /* patch scheme if necessary */
  115.       if (strfield(lien,"http:")) {
  116.         lien+=5;
  117.         strcpybuff(adr, jump_protocol(origin_adr));    // mΩme adresse ; protocole vide (http)
  118.       } else if (strfield(lien,"https:")) {
  119.         lien+=6;
  120.         strcpybuff(adr, "https://");   // mΩme adresse forcΘe en https
  121.         strcatbuff(adr, jump_protocol(origin_adr));
  122.       } else if (strfield(lien,"ftp:")) {
  123.         lien+=4;
  124.         strcpybuff(adr, "ftp://");   // mΩme adresse forcΘe en ftp
  125.         strcatbuff(adr, jump_protocol(origin_adr));
  126.       } else {
  127.         strcpybuff(adr,origin_adr);    // mΩme adresse ; et mΩme Θventuel protocole
  128.       }
  129.       
  130.       if (*lien!='/') {  // sinon c'est un lien absolu
  131.         if (*lien == '\0') {
  132.           strcpybuff(fil,origin_fil);
  133.         } else if (*lien == '?') {     // example: a href="?page=2"
  134.           char* a;
  135.           strcpybuff(fil,origin_fil);
  136.           a=strchr(fil,'?');
  137.           if (a) *a='\0';
  138.           strcatbuff(fil,lien);
  139.         } else {
  140.           a=strchr(origin_fil,'?');
  141.           if (a == NULL) a=origin_fil+strlen(origin_fil);
  142.           while((*a!='/') && ( a > origin_fil) ) a--;
  143.           if (*a=='/') {    // ok on a un '/'
  144.             if ( (((int) (a - origin_fil))+1+strlen(lien)) < HTS_URLMAXSIZE) {
  145.               // copier chemin
  146.               strncpy(fil,origin_fil,((int) (a - origin_fil))+1);
  147.               *(fil + ((int) (a - origin_fil))+1)='\0';
  148.               
  149.               // copier chemin relatif
  150.               if (((int) strlen(fil)+(int) strlen(lien)) < HTS_URLMAXSIZE) {
  151.                 strcatbuff(fil,lien + ((*lien=='/')?1:0) );      
  152.                 // simplifier url pour les ../
  153.                 fil_simplifie(fil);
  154.               } else
  155.                 ok=-1;    // erreur
  156.             } else {    // erreur
  157.               ok=-1;    // erreur URL
  158.             }
  159.           } else {    // erreur
  160.             ok=-1;    // erreur URL
  161.           }
  162.         }
  163.       } else { // chemin absolu
  164.         // copier chemin directement
  165.         strcatbuff(fil,lien);      
  166.         fil_simplifie(fil);
  167.       }  // *lien!='/'
  168.     } else
  169.       ok=-1;
  170.     
  171.   }  // test news: etc.
  172.  
  173.   // case insensitive pour adresse
  174.   {
  175.     char *a=jump_identification(adr);
  176.     while(*a) {
  177.       if ((*a>='A') && (*a<='Z'))
  178.         *a+='a'-'A';       
  179.       a++;
  180.     }
  181.   }
  182.   
  183.   return ok;
  184. }
  185.  
  186.  
  187.  
  188.  
  189.  
  190. // crΘer dans s, α partir du chemin courant curr_fil, le lien vers link (absolu)
  191. // un ident_url_relatif a dΘja ΘtΘ fait avant, pour que link ne soit pas un chemin relatif
  192. int lienrelatif(char* s,char* link,char* curr_fil) {
  193.   char _curr[HTS_URLMAXSIZE*2];
  194.   char newcurr_fil[HTS_URLMAXSIZE*2],newlink[HTS_URLMAXSIZE*2];
  195.   char* curr;
  196.   //int n=0;
  197.   char* a;
  198.   int slash=0;
  199.   //
  200.   newcurr_fil[0]='\0'; newlink[0]='\0';
  201.   //
  202.  
  203.   // patch: Θliminer les ? (paramΦtres) sinon bug
  204.   if ( (a=strchr(curr_fil,'?')) ) {
  205.     strncatbuff(newcurr_fil,curr_fil,(int) (a - curr_fil));
  206.     curr_fil = newcurr_fil;
  207.   }
  208.   if ( (a=strchr(link,'?')) ) {
  209.     strncatbuff(newlink,link,(int) (a - link));
  210.     link = newlink;
  211.   }
  212.  
  213.   // recopier uniquement le chemin courant
  214.   curr=_curr;
  215.   strcpybuff(curr,curr_fil);
  216.   if ((a=strchr(curr,'?'))==NULL)  // couper au ? (params)
  217.     a=curr+strlen(curr)-1;         // pas de params: aller α la fin
  218.   while((*a!='/') && ( a> curr)) a--;       // chercher dernier / du chemin courant
  219.   if (*a=='/') *(a+1)='\0';                           // couper dernier /
  220.   
  221.   // "effacer" s
  222.   s[0]='\0';
  223.   
  224.   // sauter ce qui est commun aux 2 chemins
  225.   {
  226.     char *l,*c;
  227.     if (*link=='/') link++;  // sauter slash
  228.     if (*curr=='/') curr++;
  229.     l=link;
  230.     c=curr;
  231.     // couper ce qui est commun
  232. #if HTS_CASSE
  233.     while ((*link==*curr) && (*link!=0)) {link++; curr++; }
  234. #else
  235.     while ((streql(*link,*curr)) && (*link!=0)) {link++; curr++; }
  236. #endif
  237.     // mais on veut un rΘpertoirer entier!
  238.     // si on a /toto/.. et /toto2/.. on ne veut pas sauter /toto !
  239.     while(((*link!='/') || (*curr!='/')) && ( link > l)) { link--; curr--; }
  240.     //if (*link=='/') link++;
  241.     //if (*curr=='/') curr++;
  242.   }
  243.   
  244.   // calculer la profondeur du rΘpertoire courant et remonter
  245.   // LES ../ ONT ETE SIMPLIFIES
  246.   a=curr;
  247.   if (*a=='/') a++;
  248.   while(*a) if (*(a++)=='/') strcatbuff(s,"../");
  249.   //if (strlen(s)==0) strcatbuff(s,"/");
  250.  
  251.   if (slash) strcatbuff(s,"/");    // garder absolu!!
  252.   
  253.   // on est dans le rΘpertoire de dΘpart, copier
  254.   strcatbuff(s,link + ((*link=='/')?1:0) );
  255.  
  256.   /* Security check */
  257.   if (strlen(s) >= HTS_URLMAXSIZE)
  258.     return -1;
  259.  
  260.   // on a maintenant une chaine de la forme ../../test/truc.html  
  261.   return 0;
  262. }
  263.  
  264. /* Is the link absolute (http://www..) or relative (/bar/foo.html) ? */
  265. int link_has_authority(char* lien) {
  266.   char* a=lien;
  267.   if (isalpha((unsigned char)*a)) {
  268.     // Skip scheme?
  269.     while (isalpha((unsigned char)*a))
  270.       a++;
  271.     if (*a == ':')
  272.       a++;
  273.     else
  274.       return 0;
  275.   }
  276.   if (strncmp(a,"//",2) == 0)
  277.     return 1;
  278.   return 0;
  279. }
  280.  
  281. int link_has_authorization(char* lien) {
  282.   char* adr = jump_protocol(lien);
  283.   char* firstslash = strchr(adr, '/');
  284.   char* detect = strchr(adr, '@');
  285.   if (firstslash) {
  286.     if (detect) {
  287.       return (detect < firstslash);
  288.     }
  289.   } else {
  290.     return (detect != NULL);
  291.   }
  292.   return 0;
  293. }
  294.  
  295.  
  296. // conversion chemin de fichier/dossier vers 8-3 ou ISO9660
  297. void long_to_83(int mode,char* n83,char* save) {
  298.   n83[0]='\0';
  299.  
  300.   while(*save) {
  301.     char fn83[256],fnl[256];
  302.     int i=0;
  303.     fn83[0]=fnl[0]='\0';
  304.     while((save[i]) && (save[i]!='/')) { fnl[i]=save[i]; i++; }
  305.     fnl[i]='\0';
  306.     // conversion
  307.     longfile_to_83(mode,fn83,fnl);
  308.     strcatbuff(n83,fn83);
  309.  
  310.     save+=i;
  311.     if (*save=='/') { strcatbuff(n83,"/"); save++; }
  312.   }
  313. }
  314.  
  315.  
  316. // conversion nom de fichier/dossier isolΘ vers 8-3 ou ISO9660
  317. void longfile_to_83(int mode,char* n83,char* save) {
  318.   int i=0,j=0,max=0;
  319.   char nom[256];
  320.   char ext[256];
  321.   nom[0]=ext[0]='\0';
  322.   
  323.   switch(mode) {
  324.   case 1:
  325.     max=8;
  326.     break;
  327.   case 2:
  328.     max=30;
  329.     break;
  330.   default:
  331.     max=8;
  332.     break;
  333.   }
  334.  
  335.   /* No starting . */
  336.   if (save[0] == '.') {
  337.     save[0]='_';
  338.   }
  339.   /* No multiple dots */
  340.   {
  341.     char* last_dot=strrchr(save, '.');
  342.     char* dot;
  343.     while((dot=strchr(save, '.'))) {
  344.       *dot = '_';
  345.     }
  346.     if (last_dot) {
  347.       *last_dot='.';
  348.     }
  349.   }
  350.   /* 
  351.     Avoid: (ISO9660, but also suitable for 8-3)
  352.     (Thanks to jonat@cellcast.com for te hint)
  353.     /:;?\#*~
  354.     0x00-0x1f and 0x80-0xff
  355.   */
  356.   for(i=0 ; i < (int) strlen(save) ; i++) {
  357.     if (
  358.       (strchr("/:;?\\#*~", save[i]))
  359.       ||
  360.       (save[i] < 32)
  361.       ||
  362.       (save[i] >= 127)
  363.       ) {
  364.       save[i]='_';
  365.     }
  366.   }
  367.  
  368.   i=j=0;
  369.   while((i<max) && (save[j]) && (save[j]!='.')) {
  370.     if (save[j]!=' ') {
  371.       nom[i]=save[j]; 
  372.       i++; 
  373.     } 
  374.     j++; 
  375.   }  // recopier nom
  376.   nom[i]='\0';
  377.   if (save[j]) {  // il reste au moins un point
  378.     i=strlen(save)-1;
  379.     while((i>0) && (save[i]!='.') && (save[i]!='/')) i--;    // rechercher dernier .
  380.     if (save[i]=='.') {  // point!
  381.       int j=0;
  382.       i++;
  383.       while((j<3) && (save[i]) ) { if (save[i]!=' ') { ext[j]=save[i]; j++; } i++; }
  384.       ext[j]='\0';
  385.     }
  386.   }
  387.   // corriger vers 8-3
  388.   n83[0]='\0';
  389.   strncatbuff(n83,nom,8);
  390.   if (strnotempty(ext)) {
  391.     strcatbuff(n83,".");
  392.     strncatbuff(n83,ext,3);    
  393.   }
  394. }
  395.  
  396. // Θcrire backblue.gif
  397. int verif_backblue(httrackp* opt,char* base) {
  398.   int* done;
  399.   int ret=0;
  400.   NOSTATIC_RESERVE(done, int, 1);
  401.   //
  402.   if (!base) {   // init
  403.     *done=0;
  404.     return 0;
  405.   }
  406.   if ( (!*done)
  407.     || (fsize(fconcat(base,"backblue.gif")) != HTS_DATA_BACK_GIF_LEN)) {
  408.     FILE* fp = filecreate(fconcat(base,"backblue.gif"));
  409.     *done=1;
  410.     if (fp) {
  411.       if (fwrite(HTS_DATA_BACK_GIF,HTS_DATA_BACK_GIF_LEN,1,fp) != HTS_DATA_BACK_GIF_LEN)
  412.         ret=1;
  413.       fclose(fp);
  414.       usercommand(opt,0,NULL,fconcat(base,"backblue.gif"),"","");
  415.     } else
  416.       ret=1;
  417.     //
  418.     fp = filecreate(fconcat(base,"fade.gif"));
  419.     if (fp) {
  420.       if (fwrite(HTS_DATA_FADE_GIF,HTS_DATA_FADE_GIF_LEN,1,fp) != HTS_DATA_FADE_GIF_LEN)
  421.         ret=1;
  422.       fclose(fp);
  423.       usercommand(opt,0,NULL,fconcat(base,"fade.gif"),"","");
  424.     } else
  425.       ret=1;
  426.   } 
  427.   return ret;
  428. }
  429.  
  430. // flag
  431. int verif_external(int nb,int test) {
  432.   int* status;
  433.   NOSTATIC_RESERVE(status, int, 2);
  434.   if (!test)
  435.     status[nb]=0;   // reset
  436.   else if (!status[nb]) {
  437.     status[nb]=1;
  438.     return 1;
  439.   }
  440.   return 0;
  441. }
  442.  
  443.  
  444. // recherche chaεne de type truc<espaces>=
  445. // renvoi dΘcalage α effectuer ou 0 si non trouvΘ
  446. /* SECTION OPTIMISEE:
  447. #define rech_tageq(adr,s) ( \
  448.   ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) ? \
  449.     ( (streql(*adr,*s)) ? \
  450.       (__rech_tageq(adr,s)) \
  451.       : 0 \
  452.     ) \
  453.     : 0\
  454.   )
  455. */
  456. /*
  457. HTS_INLINE int rech_tageq(const char* adr,const char* s) { 
  458.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  459.     if (streql(*adr,*s)) {                           // tester premier octet (optimisation)
  460.       return __rech_tageq(adr,s);
  461.     }
  462.   }
  463.   return 0;
  464. }
  465. */
  466. // DeuxiΦme partie
  467. HTS_INLINE int __rech_tageq(const char* adr,const char* s) { 
  468.   int p;
  469.   p=strfield(adr,s);
  470.   if (p) {
  471.     while(is_space(adr[p])) p++;
  472.     if (adr[p]=='=') {
  473.       return p+1;
  474.     }
  475.   }
  476.   return 0;
  477. }
  478. // same, but check begining of adr wirh s (for <object src="bar.mov" .. hotspot123="foo.html">)
  479. HTS_INLINE int __rech_tageqbegdigits(const char* adr,const char* s) { 
  480.   int p;
  481.   p=strfield(adr,s);
  482.   if (p) {
  483.     while(isdigit((unsigned char)adr[p]))  p++;      // jump digits
  484.     while(is_space(adr[p])) p++;
  485.     if (adr[p]=='=') {
  486.       return p+1;
  487.     }
  488.   }
  489.   return 0;
  490. }
  491.  
  492. // tag sans =
  493. HTS_INLINE int rech_sampletag(const char* adr,const char* s) { 
  494.   int p;
  495.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  496.     p=strfield(adr,s);
  497.     if (p) {
  498.       if (!isalnum((unsigned char)adr[p])) {  // <srcbis n'est pas <src
  499.         return 1;
  500.       }
  501.       return 0;
  502.     }
  503.   }
  504.   return 0;
  505. }
  506.  
  507. // teste si le tag contenu dans from est Θgal α "tag"
  508. HTS_INLINE int check_tag(char* from,const char* tag) {
  509.   char* a=from+1;
  510.   int i=0;
  511.   char s[256];
  512.   while(is_space(*a)) a++;
  513.   while((isalnum((unsigned char)*a) || (*a=='/')) && (i<250)) { s[i++]=*a; a++; }
  514.   s[i++]='\0';
  515.   return (strfield2(s,tag));  // comparer
  516. }
  517.  
  518. // teste si un fichier dΘpasse le quota
  519. int istoobig(LLint size,LLint maxhtml,LLint maxnhtml,char* type) {
  520.   int ok=1;
  521.   if (size>0) {
  522.     if (is_hypertext_mime(type)) {
  523.       if (maxhtml>0) {
  524.         if (size>maxhtml)
  525.           ok=0;
  526.       }
  527.     } else {
  528.       if (maxnhtml>0) {
  529.         if (size>maxnhtml)
  530.           ok=0;
  531.       }
  532.     }
  533.   }
  534.   return (!ok);
  535. }
  536.  
  537.  
  538. HTSEXT_API int hts_buildtopindex(httrackp* opt,char* path,char* binpath) {
  539.   FILE* fpo;
  540.   int retval=0;
  541.   char rpath[1024*2];
  542.   char *toptemplate_header=NULL,*toptemplate_body=NULL,*toptemplate_footer=NULL;
  543.   
  544.   // et templates html
  545.   toptemplate_header=readfile_or(fconcat(binpath,"templates/topindex-header.html"),HTS_INDEX_HEADER);
  546.   toptemplate_body=readfile_or(fconcat(binpath,"templates/topindex-body.html"),HTS_INDEX_BODY);
  547.   toptemplate_footer=readfile_or(fconcat(binpath,"templates/topindex-footer.html"),HTS_INDEX_FOOTER);
  548.   
  549.   if (toptemplate_header && toptemplate_body && toptemplate_footer) {
  550.     
  551.     strcpybuff(rpath,path);
  552.     if (rpath[0]) {
  553.       if (rpath[strlen(rpath)-1]=='/')
  554.         rpath[strlen(rpath)-1]='\0';
  555.     }
  556.     
  557.     fpo=fopen(fconcat(rpath,"/index.html"),"wb");
  558.     if (fpo) {
  559.       find_handle h;
  560.       verif_backblue(opt,concat(rpath,"/"));    // gΘnΘrer gif
  561.       // Header
  562.       fprintf(fpo,toptemplate_header,
  563.         "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"
  564.         );
  565.       
  566.       /* Find valid project names */
  567.       h = hts_findfirst(rpath);
  568.       if (h) {
  569.         struct topindex_chain * chain=NULL;
  570.         struct topindex_chain * startchain=NULL;
  571.         do {
  572.           if (hts_findisdir(h)) {
  573.             char iname[HTS_URLMAXSIZE*2];
  574.             strcpybuff(iname,rpath);
  575.             strcatbuff(iname,"/");
  576.             strcatbuff(iname,hts_findgetname(h));
  577.             strcatbuff(iname,"/index.html");
  578.             if (fexist(iname)) {
  579.               struct topindex_chain * oldchain=chain;
  580.               chain=calloc(sizeof(struct topindex_chain), 1);
  581.               if (!startchain) {
  582.                 startchain=chain;
  583.               }
  584.               if (chain) {
  585.                 if (oldchain) {
  586.                   oldchain->next=chain;
  587.                 }
  588.                 chain->next=NULL;
  589.                 strcpybuff(chain->name, hts_findgetname(h));
  590.               }
  591.             }
  592.             
  593.           }
  594.         } while(hts_findnext(h));
  595.         hts_findclose(h);
  596.  
  597.         /* Build sorted index */
  598.         chain=startchain;
  599.         while(chain) {
  600.           char hname[HTS_URLMAXSIZE*2];
  601.           strcpybuff(hname,chain->name);
  602.           escape_check_url(hname);
  603.           fprintf(fpo,toptemplate_body,
  604.             hname,
  605.             chain->name
  606.             );
  607.  
  608.           chain=chain->next;
  609.         }
  610.  
  611.  
  612.         retval=1;
  613.       }
  614.       
  615.       // Footer
  616.       fprintf(fpo,toptemplate_footer,
  617.         "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"
  618.         );
  619.       
  620.       fclose(fpo);
  621.       
  622.     }
  623.     
  624.   }
  625.  
  626.   if (toptemplate_header)
  627.     freet(toptemplate_header);
  628.   if (toptemplate_body)
  629.     freet(toptemplate_body);
  630.   if (toptemplate_footer)
  631.     freet(toptemplate_footer);
  632.   
  633.   return retval;
  634. }
  635.  
  636.  
  637.  
  638.  
  639. // Portable directory find functions
  640. /*
  641. // Example:
  642. find_handle h = hts_findfirst("/tmp");
  643. if (h) {
  644.   do {
  645.     if (hts_findisfile(h))
  646.       printf("File: %s (%d octets)\n",hts_findgetname(h),hts_findgetsize(h));
  647.     else if (hts_findisdir(h))
  648.       printf("Dir: %s\n",hts_findgetname(h));
  649.   } while(hts_findnext(h));
  650.   hts_findclose(h);
  651. }
  652. */
  653. HTSEXT_API find_handle hts_findfirst(char* path) {
  654.   if (path) {
  655.     if (strnotempty(path)) {
  656.       find_handle_struct* find = (find_handle_struct*) calloc(1,sizeof(find_handle_struct));
  657.       if (find) {
  658.         memset(find, 0, sizeof(find_handle_struct));
  659. #if HTS_WIN
  660.         {
  661.           char rpath[1024*2];
  662.           strcpybuff(rpath,path);
  663.           if (rpath[0]) {
  664.             if (rpath[strlen(rpath)-1]!='\\')
  665.               strcatbuff(rpath,"\\");
  666.           }
  667.           strcatbuff(rpath,"*.*");
  668.           find->handle = FindFirstFile(rpath,&find->hdata);
  669.           if (find->handle != INVALID_HANDLE_VALUE)
  670.             return find;
  671.         }
  672. #else
  673.         strcpybuff(find->path,path);
  674.         {
  675.           if (find->path[0]) {
  676.             if (find->path[strlen(find->path)-1]!='/')
  677.               strcatbuff(find->path,"/");
  678.           }
  679.         }
  680.         find->hdir=opendir(path);
  681.         if (find->hdir != NULL) {
  682.           if (hts_findnext(find) == 1)
  683.             return find;
  684.         }
  685. #endif
  686.         free((void*)find);
  687.       }
  688.     }
  689.   }
  690.   return NULL;   
  691. }
  692.  
  693. HTSEXT_API int hts_findnext(find_handle find) {
  694.   if (find) {
  695. #if HTS_WIN
  696.     if ( (FindNextFile(find->handle,&find->hdata)))
  697.       return 1;
  698. #else
  699.     memset(&(find->filestat), 0, sizeof(find->filestat));
  700.     if ((find->dirp=readdir(find->hdir)))
  701.       if (find->dirp->d_name)
  702.         if (!stat(concat(find->path,find->dirp->d_name),&find->filestat))
  703.           return 1;
  704. #endif
  705.   }
  706.   return 0;
  707. }
  708.  
  709. HTSEXT_API int hts_findclose(find_handle find) {
  710.   if (find) {
  711. #if HTS_WIN
  712.     if (find->handle) {
  713.       FindClose(find->handle);
  714.       find->handle=NULL;
  715.     }
  716. #else
  717.     if (find->hdir) {
  718.       closedir (find->hdir);
  719.       find->hdir=NULL;
  720.     }
  721. #endif
  722.     free((void*)find);
  723.   }
  724.   return 0;
  725. }
  726.  
  727. HTSEXT_API char* hts_findgetname(find_handle find) {
  728.   if (find) {
  729. #if HTS_WIN
  730.     return find->hdata.cFileName;
  731. #else
  732.     if (find->dirp)
  733.       return find->dirp->d_name;
  734. #endif
  735.   }
  736.   return NULL;
  737. }
  738.  
  739. HTSEXT_API int hts_findgetsize(find_handle find) {
  740.   if (find) {
  741. #if HTS_WIN
  742.     return find->hdata.nFileSizeLow;
  743. #else
  744.     return find->filestat.st_size;
  745. #endif
  746.   }
  747.   return -1;
  748. }
  749.  
  750. HTSEXT_API int hts_findisdir(find_handle find) {
  751.   if (find) {
  752.     if (!hts_findissystem(find)) {
  753. #if HTS_WIN
  754.       if (find->hdata.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY)
  755.         return 1;
  756. #else
  757.       if (S_ISDIR(find->filestat.st_mode))
  758.         return 1;
  759. #endif
  760.     }
  761.   }
  762.   return 0;
  763. }
  764. HTSEXT_API int hts_findisfile(find_handle find) {
  765.   if (find) {
  766.     if (!hts_findissystem(find)) {
  767. #if HTS_WIN
  768.       if (!(find->hdata.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY))
  769.         return 1;
  770. #else
  771.       if (S_ISREG(find->filestat.st_mode))
  772.         return 1;
  773. #endif
  774.     }
  775.   }
  776.   return 0;
  777. }
  778. HTSEXT_API int hts_findissystem(find_handle find) {
  779.   if (find) {
  780. #if HTS_WIN
  781.     if (find->hdata.dwFileAttributes  & (FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_TEMPORARY))
  782.       return 1;
  783.     else if ( (!strcmp(find->hdata.cFileName,"..")) || (!strcmp(find->hdata.cFileName,".")) )
  784.       return 1;
  785. #else
  786.     if (
  787.       (S_ISCHR(find->filestat.st_mode))
  788.       || 
  789.       (S_ISBLK(find->filestat.st_mode))
  790.       || 
  791.       (S_ISFIFO(find->filestat.st_mode))
  792.       || 
  793.       (S_ISSOCK(find->filestat.st_mode))
  794.       )
  795.       return 1;
  796.     else if ( (!strcmp(find->dirp->d_name,"..")) || (!strcmp(find->dirp->d_name,".")) )
  797.       return 1;
  798. #endif
  799.   }
  800.   return 0;
  801. }
  802.