home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / UTILITY / TOPTEN.ARJ / UPLD / 999 / TOPTEN.C next >
Text File  |  1990-02-05  |  4KB  |  152 lines

  1. /* The following program will make a text file for each of your download
  2. directories in a similar format to the way that WWIV does already, and also
  3. create a text file with Ctrl-C color codes containing the ten files that have
  4. been downloaded the most. It uses your vardec.h header file, so that it should
  5. work even if you have modified and of your structures.
  6.  
  7. [Insert your favorite Standard Mod Disclaimer here]  */
  8.  
  9. /* TOPTEN.C */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <sys\stat.h>
  14. #include <fcntl.h>
  15. #include <string.h>
  16. #include <io.h>
  17. #include "vardec.h"
  18.  
  19. directoryrec directories[64];
  20. configrec syscfg;
  21. int dlf,curdir,numf,num_dirs,i;
  22. usersubrec udir[64];
  23.  
  24. typedef struct {
  25.   char nam[81];
  26.   int  num ;
  27. } toptype;
  28.  
  29. toptype top[9];
  30. char s[81];
  31. FILE *f;
  32.  
  33. #define SETREC(i)  lseek(dlf,((long) (i))*((long)sizeof(uploadsrec)),SEEK_SET);
  34.  
  35. void dliscan1(int dn)
  36. {
  37.   char s[81];
  38.   int i;
  39.   uploadsrec u;
  40.  
  41.   sprintf(s,"%s%s.DIR",syscfg.datadir,directories[dn].filename);
  42.   dlf=open(s,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  43.   i=filelength(dlf)/sizeof(uploadsrec);
  44.   if (i==0) {
  45.     u.numbytes=0;
  46.     SETREC(0);
  47.     write(dlf,(void *)&u,sizeof(uploadsrec));
  48.   } else {
  49.     SETREC(0);
  50.     read(dlf,(void *)&u,sizeof(uploadsrec));
  51.   }
  52.   numf=u.numbytes;
  53.  
  54. }
  55.  
  56. void closedl()
  57. {
  58.   if (dlf>0) {
  59.     close(dlf);
  60.     dlf=-1;
  61.   }
  62. }
  63.  
  64. void sortlist()
  65.  
  66. {
  67.   int i,i1;
  68.   toptype temp;
  69.  
  70.   i=9;
  71.   temp.num=top[9].num;
  72.   strcpy(temp.nam,top[9].nam);
  73.   while ((top[9].num>=top[i].num)&&(i>=0))
  74.   { i--; }
  75.   for (i1=8;i1>i;) {
  76.     top[i1+1].num=top[i1].num;
  77.     strcpy(top[i1+1].nam,top[i1].nam);
  78.     i1--;
  79.   }
  80.   top[i+1].num=temp.num;
  81.   strcpy(top[i+1].nam,temp.nam);
  82. }
  83.  
  84. void searchall()
  85. {
  86.   int i,i1;
  87.   char s[81],s1[81];
  88.   uploadsrec u;
  89.   FILE *fp;
  90.  
  91.   for (i=0; (i<num_dirs); i++) {
  92.     dliscan1(i);
  93.     sprintf(s,"d:\\files\\%s.TXT",directories[i].filename); /* NOTE 1 */
  94.     fp=fopen(s,"wt");
  95.     for (i1=1; (i1<=numf); i1++) {
  96.       SETREC(i1);
  97.       read(dlf,(void *)&u,sizeof(uploadsrec));
  98.       ltoa((((u.numbytes)+1023)/1024),s1,10);
  99.       strcat(s1,"k");
  100.       fprintf(fp,"%s: %4s (%2d) : %.50s\n",u.filename,s1,u.numdloads,
  101.           u.description);
  102.       if (u.numdloads>top[9].num) {
  103.         strcpy(top[9].nam,u.filename);
  104.         top[9].num=u.numdloads;
  105.         sortlist();
  106.       }
  107.     }
  108.     fclose(fp);
  109.     closedl();
  110.   }
  111. }
  112.  
  113. void main()
  114.  
  115. {
  116.   strcpy(s,"CONFIG.DAT");
  117.   i=open(s,O_RDWR | O_BINARY);
  118.   if (i<0) {
  119.     printf("%s NOT FOUND.\n",s);
  120.     exit(1);
  121.   }
  122.   read(i,(void *) (&syscfg), sizeof(configrec));
  123.   close(i);
  124.   strcpy(s,syscfg.datadir);
  125.   strcat(s,"DIRS.DAT");
  126.   i=open(s,O_RDWR | O_BINARY);
  127.   if (i<0) {
  128.     printf("%s NOT FOUND.\n",s);
  129.     exit(1);
  130.   }
  131.   num_dirs=(read(i,(void *) (&directories), (64*sizeof(directoryrec))))/
  132.     sizeof(directoryrec);
  133.   close(i);
  134.   for (i=0;i<10;strcpy(top[i].nam,"None"),top[i++].num=0);
  135.   searchall();
  136.   sprintf(s,"%sxfer.msg",syscfg.gfilesdir);    /*NOTE 2 */
  137.   f=fopen(s,"wt");
  138.   if (!f) exit(1);
  139.   fprintf(f,"6Welcome to the Transfer Section.\n\n");
  140.   fprintf(f,"  4Top Ten Downloaded Files\n\n");
  141.   for (i=0;i<10;fprintf(f," 3%2d. 5%-15s  1%d\n",i+1,top[i].nam,top[i].num),i++);
  142.   fclose(f);
  143.   exit(0);
  144. }
  145.  
  146. /* Note 1: This is where you would place the path that you wish to use for the
  147.         text file listings of your download directories.
  148.  
  149. Note 2: This is where you would place the path and name of where you wish the
  150.         top ten file list to be written. */
  151.  
  152.