home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / vn.jan.88 / part03 / stat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-30  |  4.1 KB  |  186 lines

  1. /*
  2. ** vn news reader.
  3. **
  4. ** stat.c - stat and log file collection
  5. **
  6. ** see copyright disclaimer / history in vn.c source file
  7. */
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #ifdef SYSV
  11. #include <fcntl.h>
  12. #endif
  13. #include <sys/file.h>
  14. #include <sys/stat.h>
  15. #include <pwd.h>
  16. #include "config.h"
  17. #include "node.h"
  18.  
  19. extern NODE *hashfind();
  20. extern char *strtok();
  21. extern int Ncount;
  22. extern NODE **Newsorder;
  23.  
  24. #ifdef VNLOGFILE
  25. static char Start[80];
  26. #endif
  27.  
  28. stat_start()
  29. {
  30. #ifdef VNLOGFILE
  31.     char *ctime();
  32.     long now;
  33.  
  34.     time(&now);
  35.     strcpy(Start,ctime(&now));
  36. #endif
  37. }
  38.  
  39. /*
  40. ** flag = 0, "NO NEWS" type session.
  41. **      = 1, regular session.
  42. **    = -1, aborted session
  43. **
  44. ** CAUTION: routine CALLED from within printex() - do NOT
  45. ** call printex().  Simply do message to stderr on fail.
  46. */
  47. stat_end(flag)
  48. int flag;
  49. {
  50.     NODE *nd;
  51.     char *nl,*index();
  52.     char *how;
  53.     struct passwd *ptr, *getpwuid();
  54.     struct stat buf;
  55.     long now;
  56.     char bufr[80];
  57.     char name[60];
  58.     FILE *fp;
  59.     int fd;
  60.     long chk, rd, pg;
  61.     int i;
  62.  
  63. #ifdef VNLOGFILE
  64.     if (stat(VNLOGFILE,&buf) == 0 && (fp = fopen(VNLOGFILE,"a")) != NULL)
  65.     {
  66.         time(&now);
  67.         strcpy(bufr,ctime(&now));
  68.         if ((nl = index(bufr,'\n')) != NULL)
  69.             *nl = '\0';
  70.         if ((nl = index(Start,'\n')) != NULL)
  71.             *nl = '\0';
  72.         if (flag == 0)
  73.             how = "NO NEWS";
  74.         else
  75.         {
  76.             if (flag > 0)
  77.                 how = "OK";
  78.             else
  79.                 how = "ABORTED";
  80.         }
  81.         ptr = getpwuid (getuid());
  82.         fprintf(fp, "%s\t%s - %s %s\n", ptr->pw_name, Start, bufr, how);
  83.         fclose (fp);
  84.     }
  85. #endif
  86.  
  87. #ifdef VNSTATFILE
  88.     /*
  89.     ** Stat file is done with a fixed record size, and maintaining the
  90.     ** existing record order exactly so that concurrent users will do
  91.     ** the least damage.  If two users actually read & update a single
  92.     ** record simultaneously, we should just lose one user's counts.
  93.     ** Short of implementing a locking scheme, we probably won't do
  94.     ** much better.  Disadvantages are that deleted newsgroups never
  95.     ** get cleaned out, order is set by the first user whose
  96.     ** statistics are collected, it will break if anyone modifies it,
  97.     ** and the file is a bit larger than it needs to be.
  98.     **
  99.     ** record format:
  100.     **
  101.     ** CCCCCC PPPPPP RRRRRR newsgroup name ....  \n
  102.     ** ^      ^      ^      ^                    ^
  103.     ** 0      7      14     21              char 79
  104.     **
  105.     ** CCCCCC - count of sessions searching group
  106.     ** PPPPPP - count of sessions actually finding pages for group
  107.     ** RRRRRR - count of sessions actually accessing articles in group
  108.     */
  109.     if ((fd = open(VNSTATFILE,O_RDWR)) > 0)
  110.     {
  111.         bufr[80] = '\0';
  112.  
  113.         /*
  114.         ** read a record, find the newsgroup, update counts.
  115.         ** If changed, seek back & overwrite.  By using fixed
  116.         ** length records, we should only lose something on
  117.         ** concurrent writes of the same record, and by writing
  118.         ** the ENTIRE record, we keep it consistent
  119.         */
  120.         while ((i = read(fd,bufr,80)) == 80 && bufr[79] == '\n')
  121.         {
  122.             chk = atoi(bufr);
  123.             pg = atoi(bufr+7);
  124.             rd = atoi(bufr+14);
  125.             strcpy(name,bufr+21);
  126.             nl = strtok(name," \n");
  127.             if (nl == NULL || (nd = hashfind(nl)) == NULL)
  128.                 continue;
  129.             nd->flags |= FLG_STAT;
  130.             if ((nd->flags & (FLG_SEARCH|FLG_ACC|FLG_PAGE)) == 0)
  131.                 continue;
  132.             if ((nd->flags & FLG_SEARCH) != 0)
  133.                 ++chk;
  134.             if ((nd->flags & FLG_PAGE) != 0)
  135.                 ++pg;
  136.             if ((nd->flags & FLG_ACC) != 0)
  137.                 ++rd;
  138.             if (chk > 999999L)
  139.                 chk = 999999L;
  140.             if (pg > 999999L)
  141.                 pg = 999999L;
  142.             if (rd > 999999L)
  143.                 rd = 999999L;
  144.             sprintf(bufr,"%6ld",chk);
  145.             bufr[6] = ' ';
  146.             sprintf(bufr+7,"%6ld",pg);
  147.             bufr[13] = ' ';
  148.             sprintf(bufr+14,"%6ld",rd);
  149.             bufr[20] = ' ';
  150.             lseek(fd,-80L,1);
  151.             write(fd,bufr,80);
  152.         }
  153.  
  154.         /* format screwed up ? */
  155.         if (i != 0)
  156.         {
  157.             lseek(fd,(long) -i,1);
  158.             fprintf(stderr,"bad data in %s\n",VNSTATFILE);
  159.         }
  160.  
  161.         /* may have aborted during vns_news() */
  162.         if (Newsorder == NULL)
  163.             Ncount = 0;
  164.  
  165.         /* now append any groups not in file yet */
  166.         for (i = 0; i < Ncount; ++i)
  167.         {
  168.             nd = Newsorder[i];
  169.             if ((nd->flags & FLG_STAT) != 0)
  170.                 continue;
  171.             chk = rd = pg = 0;
  172.             if ((nd->flags & FLG_SEARCH) != 0)
  173.                 chk = 1;
  174.             if ((nd->flags & FLG_PAGE) != 0)
  175.                 pg = 1;
  176.             if ((nd->flags & FLG_ACC) != 0)
  177.                 rd = 1;
  178.             sprintf(bufr,"%6ld %6ld %6ld %-58s\n",
  179.                     chk, pg, rd, nd->nd_name);
  180.             write(fd,bufr,80);
  181.         }
  182.         close(fd);
  183.     }
  184. #endif
  185. }
  186.