home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 14 / MA_Cover_14.iso / internet / carchtml / carchtml.c next >
Encoding:
C/C++ Source or Header  |  1999-12-16  |  14.1 KB  |  480 lines

  1. /*------------------------------------------------------------
  2.  CarcHTML v1.0  Mark Weller (c)1999.
  3.  Creates an HTML page for archive directories for downloading.
  4.  You can use this source code - no restrictions.
  5. --------------------------------------------------------------*/
  6.  
  7.  
  8. #include <sys/dir.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #define CAH_PROGRAM             "CarcHTML v1.0 - Mark Weller (c)1999"
  15. #define CAH_MAXPATH         255
  16. #define CAH_MAXFILE         31
  17. #define CAH_MAXSHORT        100
  18. #define CAH_README          ".readme"
  19. #define CAH_HTMLNAME        "index.html"
  20. #define CAH_ON              1
  21. #define CAH_OFF             0
  22.  
  23. #define CAH_EXT             8
  24.  
  25. char *extension_types[] =
  26. {
  27.  "readme","README","htm","HTM","html","HTML","shtml","SHTML",""
  28. };
  29.  
  30. char *defbackcol[] =
  31. {
  32.  "FFFFFF",""
  33. };
  34.  
  35. int IsFileValid(char *);
  36. int IsDir(char *,char *);
  37. char *GetFileSize(char *,char *,char *);
  38. char *GetShort(char *,char *,char *);
  39. void Usage(char *);
  40. char *GetReadme(char *,char *);
  41.  
  42. void main(int argc,char *argv[])
  43. {
  44.  DIR           *dirp;
  45.  struct direct *dp;
  46.  char          *name;
  47.  
  48.  char          fsize[30];
  49.  
  50.  char gdir[CAH_MAXPATH];
  51.  char readme_file[CAH_MAXFILE];
  52.  char short_desc[CAH_MAXSHORT];
  53.  char create_dwnld[CAH_MAXPATH];
  54.  char *tback_col_1;
  55.  char *tback_col_2;
  56.  char *tback_col_3;
  57.  char *pback_col;
  58.  
  59.  int cah_result = 0;
  60.  int temp_cnt   = 0;
  61.  
  62.  FILE *download;
  63.  
  64. /*-command line switches-*/
  65.  int v_switch   = CAH_OFF; /*-verbose            -*/
  66.  int a_switch   = CAH_OFF; /*-aminet short       -*/
  67.  int b_switch   = CAH_OFF; /*-back/table colours -*/
  68.  
  69.  
  70.         gdir[0] ='\0';
  71.  
  72.         /*-set default background colours-*/
  73.  
  74.         tback_col_1 = defbackcol[0];
  75.         tback_col_2 = defbackcol[0];
  76.         tback_col_3 = defbackcol[0];
  77.         pback_col = defbackcol[0];
  78.  
  79.         /*-process command line arguments-*/
  80.  
  81.         if(argc > 7) Usage(NULL);
  82.  
  83.         switch(argc)
  84.               {
  85.                case 1 : Usage(NULL);     break;
  86.                case 2 : name = argv[1];  break;
  87.                case 3 :
  88.                case 7 :   if(argv[1][0] != '-')
  89.                             {
  90.                              Usage(NULL);
  91.                             }
  92.                           else
  93.                             {
  94.                              for(temp_cnt = 0;temp_cnt != strlen(argv[1]);temp_cnt++)
  95.                                 {
  96.                                  switch(argv[1][temp_cnt])
  97.                                        {
  98.                                         case '-' : break;
  99.                                         case 'a' : a_switch = CAH_ON; break;
  100.                                         case 'v' : v_switch = CAH_ON; break;
  101.                                         case 'b' : if(argc == 7)
  102.                                                      {
  103.                                                       b_switch = CAH_ON;
  104.                                                       pback_col = argv[3];
  105.                                                       tback_col_1 = argv[4];
  106.                                                       tback_col_2 = argv[5];
  107.                                                       tback_col_3 = argv[6];
  108.  
  109.                                                      }
  110.                                                     else
  111.                                                      {
  112.                                                       Usage(NULL);
  113.                                                      }
  114.                                                     break;
  115.                                        }
  116.                                 }
  117.                              name = argv[2];
  118.                             }
  119.                           break;
  120.                default :  Usage(NULL); break;
  121.               }
  122.  
  123.  
  124.     /*-open requested directory-*/
  125.  
  126.         if ((dirp = opendir(name)) == NULL) {
  127.                 Usage("Can't open directory ?");
  128.                 }
  129.  
  130.     /*-create index.html -*/
  131.         strcpy(create_dwnld,name);
  132.         AddPart(create_dwnld,CAH_HTMLNAME);
  133.  
  134.         if(!(download = fopen(create_dwnld,"w")))  /*html.file*/
  135.           {
  136.            closedir(dirp);
  137.            Usage("ERR: Can not create HTML file ?");
  138.           }
  139.     /*-create header for index.html-*/
  140.  
  141.         fprintf(download,"<HTML>\n");
  142.         fprintf(download," <HEAD>\n");
  143.         fprintf(download,"  <TITLE>Downloads</TITLE>\n");
  144.         fprintf(download," </HEAD>\n\n");
  145.         fprintf(download," <BODY BGCOLOR=%c%c%s%c>\n",'"','#',pback_col,'"');
  146.         fprintf(download,"  <!-- Created using %s  -->\n",CAH_PROGRAM);
  147.         fprintf(download,"  <!-- ARCHIVE INDEX STARTS HERE -->\n\n");
  148.         fprintf(download,"  <H3><HR ALIGN=LEFT>Downloads</H3>\n");
  149.         fprintf(download,"  <TABLE BORDER=0 WIDTH=100%%>\n");
  150.  
  151.         if(v_switch == CAH_ON)
  152.           {
  153.            printf("------------------------------------------------\n");
  154.            printf("%s\n",CAH_PROGRAM);
  155.            printf("------------------------------------------------\n");
  156.            printf("Archive Directory      = %s\n\n",name);
  157.  
  158.            if(b_switch == CAH_ON)
  159.              {
  160.               printf("Page Background colour = %s USED\n",pback_col);
  161.               printf("FileName column colour = %s USED\n",tback_col_1);
  162.               printf("FileSize column colour = %s USED\n",tback_col_2);
  163.               printf("Readme   column colour = %s USED\n",tback_col_3);
  164.              }
  165.            else
  166.              {
  167.               printf("Default Colours  = USED\n");
  168.              }
  169.  
  170.           }
  171.  
  172.         /*-process archive files in directory-*/
  173.  
  174.         for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  175.              {
  176.               if(IsDir(name,dp->d_name) == 1)  /*-Not Sub Directory-*/
  177.                 {
  178.                  if(IsFileValid(dp->d_name) == 1)
  179.                    {
  180.                     fsize[0] ='\0';
  181.                     GetFileSize(name,dp->d_name,fsize);
  182.                     readme_file[0] = '\0';
  183.                     GetReadme(dp->d_name,readme_file);
  184.  
  185.                     if(a_switch == CAH_ON)
  186.                       {
  187.                        short_desc[0] = '\0';
  188.                        GetShort(name,readme_file,short_desc);
  189.                       }
  190.                     fprintf(download,"   <TR>\n");
  191.                     fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=40%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_1,'"','"',dp->d_name,'"',dp->d_name);
  192.                     fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=10%%><CENTER>%s</TD>\n",'"','#',tback_col_2,'"',fsize);
  193.  
  194.                     if(a_switch == CAH_ON)
  195.                       {
  196.                        if(strlen(short_desc) == 0)  /*- no short found -*/
  197.                          fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=50%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_3,'"','"',readme_file,'"',readme_file);
  198.                       else
  199.                          fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=50%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_3,'"','"',readme_file,'"',short_desc);
  200.                       }
  201.                     else
  202.                       {
  203.                        fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=50%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_3,'"','"',readme_file,'"',readme_file);
  204.                       }
  205.                     fprintf(download,"   </TR>\n");
  206.  
  207.                     /*-process verbose option-*/
  208.                     if(v_switch == CAH_ON)
  209.                       {
  210.                        if(a_switch == CAH_ON)
  211.                          {
  212.                           if(strlen(short_desc) == 0)  /*- no short found -*/
  213.                              printf("%25s - %6s - %s SHORT NOT FOUND\n",dp->d_name,fsize,readme_file);
  214.                           else
  215.                              printf("%25s - %6s - %s SHORT USED\n",dp->d_name,fsize,readme_file);
  216.                          }
  217.                        else
  218.                          {
  219.                           printf("%25s - %6s - %s \n",dp->d_name,fsize,readme_file);
  220.                          }
  221.                       }
  222.                    }
  223.                 }
  224.              }
  225.  
  226.         closedir(dirp);
  227.  
  228.         if(v_switch == CAH_ON)
  229.            printf("------------------------------------------------\n");
  230.  
  231.     /*- Create end for index.html -*/
  232.         fprintf(download,"  </TABLE>\n");
  233.         fprintf(download,"  <!-- END OF INDEX ARCHIVE -->\n");
  234.         fprintf(download,"  <H3><HR ALIGN=LEFT></H3>\n");
  235.         fprintf(download," </BODY>\n");
  236.         fprintf(download,"</HTML>\n");
  237.         fclose(download);
  238.  
  239.         exit(0);
  240. }
  241.  
  242. char *GetShort(char *dir,char *readme,char *s_desc)
  243. {
  244. char temp_path[CAH_MAXPATH];
  245. char temp_sdes[CAH_MAXSHORT];
  246. char temp_sdes2[CAH_MAXSHORT];
  247.  
  248. int des_len = 0;
  249. int flag = 0;
  250. int counter = 5;
  251. FILE *read;
  252.  
  253.     temp_path[0] = '\0';
  254.     temp_sdes[0] = '\0';
  255.     temp_sdes2[0] = '\0';
  256.  
  257.     strcpy(temp_path,dir);
  258.     AddPart(temp_path,readme);
  259.  
  260.     if(!(read = fopen(temp_path,"r")))  /*text file*/
  261.       {
  262.        sprintf(s_desc,"%s",temp_sdes);
  263.       }
  264.     else
  265.       {
  266.        fgets(temp_sdes,CAH_MAXSHORT,read); /*- get first line - should be Short: -*/
  267.        fclose(read);
  268.  
  269.        /*- check for Short: -*/
  270.  
  271.         if(temp_sdes[0] == 'S' || temp_sdes[0] == 's' ) flag += 1;
  272.         if(temp_sdes[1] == 'H' || temp_sdes[1] == 'h' ) flag += 1;
  273.         if(temp_sdes[2] == 'O' || temp_sdes[2] == 'o' ) flag += 1;
  274.         if(temp_sdes[3] == 'R' || temp_sdes[3] == 'r' ) flag += 1;
  275.         if(temp_sdes[4] == 'T' || temp_sdes[4] == 't' ) flag += 1;
  276.         if(temp_sdes[5] == ':') flag += 1;
  277.  
  278.         if(flag == 6)  /*- found Short: -*/
  279.           {
  280.            flag = 0;
  281.            des_len = strlen(temp_sdes); des_len--;
  282.  
  283.            if(temp_sdes[des_len] == '\n') temp_sdes[des_len] = '\0';
  284.  
  285.            des_len = strlen(temp_sdes); des_len--;
  286.  
  287.            for(counter = 6;counter != des_len + 1;counter++)
  288.               {
  289.                temp_sdes2[counter - 6] = temp_sdes[counter];
  290.               }
  291.  
  292.            sprintf(s_desc,"%s",temp_sdes2);
  293.  
  294.           }
  295.         else
  296.           {
  297.            temp_sdes[0] = '\0';
  298.            sprintf(s_desc,"%s",temp_sdes);
  299.           }
  300.       }
  301.  
  302.  
  303. }
  304.  
  305.  
  306. char *GetReadme(char *fname,char *readme)
  307. {
  308. char temp_buff[CAH_MAXFILE];
  309. int name_len = 0;
  310. int flag = 0;
  311.  
  312.     temp_buff[0] = '\0';
  313.     strcpy(temp_buff,fname);
  314.     name_len = (strlen(temp_buff) - 1);
  315.  
  316.     while(flag != 1)
  317.          {
  318.           if(temp_buff[name_len] == '.' || name_len == 0)
  319.             {
  320.              temp_buff[name_len] = '\0';
  321.              flag = 1;
  322.             }
  323.           else
  324.             {
  325.              temp_buff[name_len] = '\0';
  326.             }
  327.           name_len--;
  328.          }
  329.  
  330.      if(name_len == -1)
  331.         sprintf(readme,"%s%s",fname,CAH_README); /*no extension*/
  332.      else
  333.         sprintf(readme,"%s%s",temp_buff,CAH_README);
  334. }
  335.  
  336. void Usage(char *mess)
  337. {
  338.  printf("----------------------------------------\n");
  339.  
  340.  if(mess)
  341.    {
  342.     printf("mess:%s\n",mess);
  343.     printf("----------------------------------------\n");
  344.    }
  345.  
  346.   printf("%s\n",CAH_PROGRAM);
  347.   printf("----------------------------------------\n");
  348.   printf("USAGE:\n");
  349.   printf(" CarcHTML <-vab> [archive directory] [pagecol] [col1] [col2] [col3] \n\n");
  350.   printf("options: -\n");
  351.   printf("           v   = verbose - displays files being indexed\n");
  352.   printf("           a   = aminet  - uses the 'Short:' description\n");
  353.   printf("                 in readme file instead of readme file name\n");
  354.   printf("           b   = page + table background colors\n\n");
  355.   printf("example: CarcHTML -vb Work:myarchive FFFFFF 888888 FFFF00 0000FF\n");
  356.   printf("           -----------*----------\n");
  357.   printf(" This will create a HTML file called 'index.html' inside\n");
  358.   printf("  Work:myarchive, which will display a table with an entry\n");
  359.   printf("  like this:-\n\n");
  360.   printf(" myprogs.lha  1.2MB  myprogs.readme\n\n");
  361.   printf(" The size of the file will be placed in table which is auto\n");
  362.   printf("  calculated.\n");
  363.   printf(" When browsed the page color will be WHITE\n");
  364.   printf("  Column 1 = GREY Column 2 = YELLOW Column 3 = BLUE\n");
  365.   printf("           -----------*----------\n");
  366.   printf("See CarcHTML.readme for more information.\n");
  367.   printf("----------------------------------------\n");
  368.  
  369.   exit(0);
  370. }
  371.  
  372. char *GetFileSize(char *dname,char *fname,char *fsize)
  373. {
  374.  
  375. struct stat load_stat;
  376. char gfile_buff[255];
  377. int size = 0;
  378. int rem  = 0;
  379. int ssize= 0;
  380.  
  381.    gfile_buff[0]='\0';
  382.    strcpy(gfile_buff,dname);
  383.    AddPart(gfile_buff,fname);
  384.  
  385.    stat(gfile_buff,&load_stat);
  386.  
  387.    ssize = (int)load_stat.st_size;
  388.  
  389.    if(ssize < 1024)
  390.      {
  391.       sprintf(fsize,"%db",ssize);             /*bytes*/
  392.      }
  393.    else
  394.      {
  395.       size = ssize / 1024;
  396.       if(size < 1024)
  397.         {
  398.          sprintf(fsize,"%dKb",size);               /*kbytes*/
  399.         }
  400.       else
  401.         {
  402.          rem = size % 1024;
  403.          if(rem < 100) rem = 1; else rem /= 100;
  404.  
  405.          sprintf(fsize,"%d.%dMb",size / 1024,rem); /*Mbytes*/
  406.         }
  407.      }
  408.  
  409. }
  410.  
  411. int IsDir(char *dname,char *fname)
  412. {
  413.  
  414. struct stat load_stat;
  415. char gfile_buff[255];
  416.  
  417.    gfile_buff[0]='\0';
  418.    strcpy(gfile_buff,dname);
  419.    AddPart(gfile_buff,fname);
  420.  
  421.    stat(gfile_buff,&load_stat);
  422.  
  423.    if(S_ISDIR(load_stat.st_mode) != 0)
  424.      return(0);
  425.    else
  426.      return(1);
  427. }
  428.  
  429.  
  430. IsFileValid(char *fname)
  431. {
  432.  char temp_name[31];
  433.  char ext_buff[31];
  434.  int name_length = 0;
  435.  int len_cnt = 0;
  436.  int ext_cnt = 0;
  437.  int check = 0;
  438.  int result = 1;
  439.  
  440.    temp_name[0] = '\0';
  441.    ext_buff[0] = '\0';
  442.  
  443.    strcpy(temp_name,fname);
  444.    name_length = strlen(temp_name);
  445.  
  446.    /*--get file extension--*/
  447.    for(len_cnt = 0;len_cnt != name_length;len_cnt++)
  448.       {
  449.        if(temp_name[len_cnt] == '.') /*-extension start-*/
  450.          {
  451.           if(check == 1)   /*-reset because file must 'my.file.???'-*/
  452.             {
  453.              ext_buff[0] = '\0';
  454.              ext_cnt = 0;
  455.             }
  456.           else
  457.             {
  458.              check = 1;
  459.             }
  460.          }
  461.        else
  462.          {
  463.           if(check == 1) /* copy extension */
  464.             {
  465.              ext_buff[ext_cnt] = temp_name[len_cnt];
  466.              ext_cnt++;
  467.             }
  468.          }
  469.       }
  470.     ext_buff[ext_cnt] = '\0';
  471.  
  472.     ext_cnt = 0;
  473.     for(ext_cnt = 0;ext_cnt != CAH_EXT;ext_cnt++)
  474.        {
  475.         if(strcmp(ext_buff,extension_types[ext_cnt]) == 0)
  476.            result = 0;
  477.        }
  478.  return(result);
  479. }
  480.