home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 14 / MA_Cover_14.iso / internet / ldirhtml / ldirhtml.c next >
Encoding:
C/C++ Source or Header  |  2000-02-03  |  9.4 KB  |  319 lines

  1. ;/* LdirHTML v1.0  Mark Weller (c)2000
  2. dcc -o LdirHTML Ldirhtml.c  date.o
  3. quit
  4. */
  5.  
  6. #include <exec/types.h>
  7. #include <dos/dos.h>
  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 LAH_PROGRAM        "LdirHTML v1.0 - Mark Weller (c)2000"
  15. #define LAH_MAXPATH         255
  16. #define LAH_MAXFILE         31
  17. #define LAH_DIRHTML        "dir.html"
  18. #define LAH_SUBHTML        "sub.html"
  19. #define LAH_ON              1
  20. #define LAH_OFF             0
  21.  
  22. int IsFileValid(char *);
  23. int IsDir(char *,char *);
  24. char *GetFileSize(char *,char *,char *);
  25. void Usage(char *);
  26.  
  27.  
  28. main(int argc,char *argv[])
  29. {
  30.  struct DateStamp Date;            /*-(amiga)store system datestamp-*/
  31.  UBYTE todaydate[82];              /*-(amiga)store system time     -*/
  32.  
  33.  DIR           *dirp;
  34.  struct direct *dp;
  35.  char          *name;
  36.  
  37.  char          fsize[30];
  38.  
  39.  char gdir[LAH_MAXPATH];
  40.  char dirhtml_file[LAH_MAXPATH];
  41.  char subhtml_file[LAH_MAXPATH];
  42.  char sub_path[LAH_MAXPATH];
  43.  
  44.  int temp_cnt   = 0;
  45.  int t_border   = 0;               /*-html table border size-*/
  46.  
  47.  FILE *dirfile;
  48.  FILE *subfile;
  49.  
  50. /*-command line switches-*/
  51.  int v_switch   = LAH_OFF; /*-verbose-*/
  52.  
  53.         gdir[0] ='\0';
  54.         dirhtml_file[0] = '\0';
  55.         subhtml_file[0] = '\0';
  56.         sub_path[0]     = '\0';
  57.  
  58.  
  59.         switch(argc)
  60.               {
  61.                case 4 :
  62.                case 3 : if(argv[1][0] == '-')
  63.                           {
  64.                              for(temp_cnt = 0;temp_cnt != strlen(argv[1]);temp_cnt++)
  65.                                 {
  66.                                  switch(argv[1][temp_cnt])
  67.                                        {
  68.                                         case '-' : break;
  69.                                         case 'v' : v_switch = LAH_ON; break;
  70.                                         case 'b' : if(argc == 4)
  71.                                                      {
  72.                                                       t_border = atoi(argv[3]);
  73.                                                      }
  74.                                                    else { Usage(NULL); } break;
  75.                                        }
  76.                                 }
  77.  
  78.                            name = argv[2];
  79.                           }
  80.                         else
  81.                           {
  82.                            Usage(NULL);
  83.                           }
  84.                         break;
  85.                case 2 : name = argv[1];  break;
  86.                case 1 : Usage(NULL);     break;
  87.               }
  88.  
  89.  
  90.         if ((dirp = opendir(name)) == NULL) {
  91.                 Usage("Can't open directory ?");
  92.                 }
  93.  
  94.     /*-(amiga)get todays date-*/
  95.         DateStamp(&Date);               /*-get system date/time  -*/
  96.         DateString(todaydate,&Date,5);  /*- 5 = dd/mm/yyy -*/
  97.  
  98.  
  99.     /*-create dir.html-*/
  100.         strcpy(dirhtml_file,name);
  101.         AddPart(dirhtml_file,LAH_DIRHTML); /*-(amiga) join path + file-*/
  102.  
  103.  
  104.         if(!(dirfile = fopen(dirhtml_file,"w")))
  105.           {
  106.            closedir(dirp);
  107.            Usage("Can't create dir.html file ?");
  108.           }
  109.      /*-create header for dir.html-*/
  110.  
  111.         fprintf(dirfile,"<HTML>\n");
  112.         fprintf(dirfile," <HEAD>\n");
  113.         fprintf(dirfile,"  <TITLE>Directory File Listing</TITLE>\n");
  114.         fprintf(dirfile," </HEAD>\n\n");
  115.         fprintf(dirfile," <BODY>\n");
  116.         fprintf(dirfile,"  <!-- Created on %s using %s  -->\n",todaydate,LAH_PROGRAM);
  117.         fprintf(dirfile,"  <!-- DIRECTORY INDEX STARTS HERE -->\n\n");
  118.         fprintf(dirfile,"  <H3><HR ALIGN=LEFT>%s Files</H3>Created on %s <BR>\n",name,todaydate);
  119.         fprintf(dirfile,"  <TABLE BORDER=%d WIDTH=100%%>\n",t_border);
  120.  
  121.     /*-create sub.html-*/
  122.         strcpy(subhtml_file,name);
  123.         AddPart(subhtml_file,LAH_SUBHTML);
  124.  
  125.         if(!(subfile = fopen(subhtml_file,"w")))
  126.           {
  127.            closedir(dirp);
  128.            fclose(dirfile);
  129.            Usage("Can't create sub.html file ?");
  130.           }
  131.      /*-create header for sub.html-*/
  132.  
  133.         fprintf(subfile,"<HTML>\n");
  134.         fprintf(subfile," <HEAD>\n");
  135.         fprintf(subfile,"  <TITLE>Sub Directory Listing</TITLE>\n");
  136.         fprintf(subfile," </HEAD>\n\n");
  137.         fprintf(subfile," <BODY>\n");
  138.         fprintf(subfile,"  <!-- Created on %s using %s  -->\n",todaydate,LAH_PROGRAM);
  139.         fprintf(subfile,"  <!-- SUB DIRECTORY INDEX STARTS HERE -->\n\n");
  140.         fprintf(subfile,"  <H3><HR ALIGN=LEFT>%s Sub Directories</H3>Created on %s <BR> \n",name,todaydate);
  141.         fprintf(subfile,"  <TABLE BORDER=%d WIDTH=100%%>\n",t_border);
  142.  
  143.  
  144.         if(v_switch == LAH_ON)
  145.           {
  146.            printf("------------------------------------------------\n");
  147.            printf("%s\n",LAH_PROGRAM);
  148.            printf("------------------------------------------------\n");
  149.            printf("Directory        :  %s\n",name);
  150.            printf("TableBorderSize  :  %d\n\n",t_border);
  151.           }
  152.  
  153.  
  154.         for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  155.              {
  156.               if(IsDir(name,dp->d_name) == 1)  /*-Not Sub Directory-*/
  157.                 {
  158.                  if(IsFileValid(dp->d_name) == 1)
  159.                    {
  160.                     fsize[0] ='\0';
  161.                     GetFileSize(name,dp->d_name,fsize);
  162.                     fprintf(dirfile,"   <TR>\n");
  163.                     fprintf(dirfile,"    <TD WIDTH=10%%><LEFT>%s</TD>\n",fsize);
  164.                     fprintf(dirfile,"    <TD WIDTH=90%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"',dp->d_name,'"',dp->d_name);
  165.                     fprintf(dirfile,"   </TR>\n");
  166.  
  167.                     if(v_switch == LAH_ON)
  168.                       {
  169.                        printf("%6s - %50s -> dir.html\n",fsize,dp->d_name);
  170.                       }
  171.                    }
  172.                 }
  173.               else
  174.                 {
  175.                 /*-sub directory-*/
  176.                  strcpy(sub_path,name);
  177.                  AddPart(sub_path,dp->d_name);
  178.  
  179.                  fprintf(subfile,"   <TR>\n");
  180.                  fprintf(subfile,"    <TD WIDTH=100%%><LEFT><A HREF=%c%s/dir.html%c>%s</A></TD>\n",'"',dp->d_name,'"',sub_path);
  181.                  fprintf(subfile,"   </TR>\n");
  182.  
  183.                  if(v_switch == LAH_ON)
  184.                    {
  185.                     printf("%59s -> sub.html\n",sub_path);
  186.                    }
  187.  
  188.                  sub_path[0] = '\0';
  189.                 }
  190.              }
  191.  
  192.         closedir(dirp);
  193.  
  194.         if(v_switch == LAH_ON)
  195.            printf("------------------------------------------------\n");
  196.  
  197.     /*- Create end for dir.html -*/
  198.         fprintf(dirfile,"  </TABLE> <BR>\n");
  199.         fprintf(dirfile,"  <TABLE BORDER=0 WIDTH=100%%>\n");
  200.         fprintf(dirfile,"   <TR>\n");
  201.         fprintf(dirfile,"    <TD WIDTH=100%%><LEFT><A HREF=%csub.html%c>Sub Directories</A></TD>\n",'"','"');
  202.         fprintf(dirfile,"   </TR>\n");
  203.         fprintf(dirfile,"  </TABLE>\n");
  204.         fprintf(dirfile,"  <!-- END OF DIRECTORY INDEX ARCHIVE -->\n");
  205.         fprintf(dirfile,"  <H3><HR ALIGN=LEFT></H3>\n");
  206.         fprintf(dirfile," </BODY>\n");
  207.         fprintf(dirfile,"</HTML>\n");
  208.         fclose(dirfile);
  209.  
  210.     /*- Create end for dir.html -*/
  211.         fprintf(subfile,"  </TABLE>\n");
  212.         fprintf(subfile,"  <!-- END OF SUB DIRECTORY INDEX -->\n");
  213.         fprintf(subfile,"  <H3><HR ALIGN=LEFT></H3>\n");
  214.         fprintf(subfile," </BODY>\n");
  215.         fprintf(subfile,"</HTML>\n");
  216.         fclose(subfile);
  217.  
  218.         exit(0);
  219. }
  220.  
  221.  
  222.  
  223. void Usage(char *mess)
  224. {
  225.  printf("----------------------------------------\n");
  226.  
  227.  if(mess)
  228.    {
  229.     printf("mess:%s\n",mess);
  230.     printf("----------------------------------------\n");
  231.    }
  232.  
  233.   printf("%s\n",LAH_PROGRAM);
  234.   printf("----------------------------------------\n");
  235.   printf("USAGE: LdirHTML <-vb> [directory] <table border size>\n\n");
  236.   printf("Chosen options: -\n");
  237.   printf("           v   = verbose - displays output\n");
  238.   printf("           b   = set table border size (default = 0)\n");
  239.   printf("example: LdirHTML -vb Work:  2\n");
  240.   printf("  this will create dir.html of work: file contents & sub.html\n");
  241.   printf("  of work: sub directorys & the tables border size of 2 \n");
  242.   printf("  and display the output.\n");
  243.   printf("----------------------------------------\n");
  244.   printf("See LdirHTML.readme for more information.\n");
  245.   printf("----------------------------------------\n");
  246.  
  247.   exit(0);
  248. }
  249.  
  250. char *GetFileSize(char *dname,char *fname,char *fsize)
  251. {
  252.  
  253. struct stat load_stat;
  254. char gfile_buff[255];
  255. int size = 0;
  256. int rem  = 0;
  257. int ssize= 0;
  258.  
  259.    gfile_buff[0]='\0';
  260.    strcpy(gfile_buff,dname);
  261.    AddPart(gfile_buff,fname);
  262.  
  263.    stat(gfile_buff,&load_stat);
  264.  
  265.    ssize = (int)load_stat.st_size;
  266.  
  267.    if(ssize < 1024)
  268.      {
  269.       sprintf(fsize,"%db",ssize);             /*bytes*/
  270.      }
  271.    else
  272.      {
  273.       size = ssize / 1024;
  274.       if(size < 1024)
  275.         {
  276.          sprintf(fsize,"%dKb",size);               /*kbytes*/
  277.         }
  278.       else
  279.         {
  280.          rem = size % 1024;
  281.          if(rem < 100) rem = 1; else rem /= 100;
  282.  
  283.          sprintf(fsize,"%d.%dMb",size / 1024,rem); /*Mbytes*/
  284.         }
  285.      }
  286.  
  287. }
  288.  
  289. int IsDir(char *dname,char *fname)
  290. {
  291.  
  292. struct stat load_stat;
  293. char gfile_buff[255];
  294.  
  295.    gfile_buff[0]='\0';
  296.    strcpy(gfile_buff,dname);
  297.    AddPart(gfile_buff,fname);
  298.  
  299.    stat(gfile_buff,&load_stat);
  300.  
  301.    if(S_ISDIR(load_stat.st_mode) != 0)
  302.      return(0);
  303.    else
  304.      return(1);
  305. }
  306.  
  307.  
  308. IsFileValid(char *fname)
  309. {
  310.  char temp_name[LAH_MAXFILE];
  311.  int result = 1;
  312.  
  313.    strcpy(temp_name,fname);
  314.  
  315.    if(strcmp(temp_name,LAH_DIRHTML) == 0) { result = 0; }
  316.    else { if(strcmp(temp_name,LAH_SUBHTML) == 0) { result = 0; } }
  317.  return(result);
  318. }
  319.