home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR4 / TAM121.ZIP / UTS.C < prev    next >
C/C++ Source or Header  |  1992-08-10  |  5KB  |  193 lines

  1.  
  2. /***************************************************************************/
  3. /*  This program, UTS (Update Transfer Stats) takes three commandline      */
  4. /* parameters: the user number to update, the number of files to add (or   */
  5. /* delete) from that caller's upload stats, and the number of k to add (or */
  6. /* delete) from that caller's upload stats. This program does nothing to   */
  7. /* the screen but will return an errorlevel of 1 on exit if the update was */
  8. /* unsuccessful, or 0 if it was successful.                                */
  9. /*                                                                         */
  10. /* Jon R. Rickher                                                          */
  11. /* February 3, 1992                                                        */
  12. /* Tolkien 1 @3456                                                         */
  13. /*                                                                         */
  14. /***************************************************************************/
  15.  
  16. #include <sys\stat.h>
  17. #include <stdio.h>
  18. #include <io.h>
  19. #include <fcntl.h>
  20. #include <dos.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <dir.h>
  24. #include "vardec.h"
  25.  
  26.   int configfile;
  27.   int userfile;
  28.   int whichuser;
  29.   int numfiles;
  30.   int numk;
  31.   char s[81];
  32.   userrec u;
  33.   configrec cfg;
  34.  
  35. void fix_user_rec(userrec *u)
  36. {
  37.   u->name[30]=0;
  38.   u->realname[20]=0;
  39.   u->callsign[6]=0;
  40.   u->phone[12]=0;
  41.   u->pw[8]=0;
  42.   u->laston[8]=0;
  43.   u->note[40]=0;
  44.   u->macros[0][80]=0;
  45.   u->macros[1][80]=0;
  46.   u->macros[2][80]=0;
  47. }
  48.  
  49.  
  50. void close_user()
  51. {
  52.   if (userfile!=-1) {
  53.     close(userfile);
  54.     userfile=-1;
  55.   }
  56. }
  57.  
  58. void open_user()
  59. {
  60.   char s[81];
  61.  
  62.   if (userfile==-1) {
  63.     sprintf(s,"%sUSER.LST",cfg.datadir);
  64.     userfile=open(s,O_RDWR | O_BINARY);
  65.     if (userfile<0) {
  66.       userfile=-1;
  67.     }
  68.   }
  69. }
  70.  
  71. int number_userrecs()
  72. {
  73.   open_user();
  74.   return((int) (filelength(userfile)/cfg.userreclen)-1);
  75. }
  76.  
  77. int read_user(unsigned int un, userrec *u)
  78. {
  79.   long pos;
  80.   char s[80];
  81.   int i;
  82.  
  83.   open_user();
  84.   if ((userfile<0) || (un>number_userrecs())) {
  85.     u->inact=inact_deleted;
  86.     fix_user_rec(u);
  87.     return(0);
  88.   }
  89.   pos=((long) cfg.userreclen) * ((long) un);
  90.   lseek(userfile,pos,SEEK_SET);
  91.   i=read(userfile, (void *)u, cfg.userreclen);
  92.   if (i==-1) {
  93.     open_user();
  94.     if ((userfile<0) || (un>number_userrecs())) {
  95.       u->inact=inact_deleted;
  96.       fix_user_rec(u);
  97.       return(0);
  98.     }
  99.     pos=((long) cfg.userreclen) * ((long) un);
  100.     lseek(userfile,pos,SEEK_SET);
  101.     i=read(userfile, (void *)u, cfg.userreclen);
  102.     if (i==-1) {
  103.       close_user();
  104.       return(0);
  105.     }
  106.     close_user();
  107.   }
  108.   fix_user_rec(u);
  109.   return(1);
  110. }
  111.  
  112. int write_user(unsigned int un, userrec *u)
  113. {
  114.   long pos;
  115.   char s[80];
  116.   unsigned char oldsl;
  117.   int i;
  118.  
  119.   if (userfile==-1) {
  120.     sprintf(s,"%sUSER.LST",cfg.datadir);
  121.     userfile=open(s,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  122.   }
  123.   pos=((long) cfg.userreclen) * ((long) un);
  124.   lseek(userfile,pos,SEEK_SET);
  125.   i=write(userfile, (void *)u, cfg.userreclen);
  126.   if (i==-1) {
  127.     sprintf(s,"%sUSER.LST",cfg.datadir);
  128.     userfile=open(s,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  129.     pos=((long) cfg.userreclen) * ((long) un);
  130.     lseek(userfile,pos,SEEK_SET);
  131.     i=write(userfile, (void *)u, cfg.userreclen);
  132.     if (i==-1) {
  133.       close_user();
  134.       return(0);
  135.     }
  136.     close_user();
  137.   }
  138.   return(1);
  139. }
  140.  
  141.  
  142. void main(int argc, char *argv[])
  143. {
  144.   int i;
  145.   int okay;
  146.  
  147.   userfile=-1;
  148.   okay=1;
  149.   if (argc != 4)
  150.     exit(1);
  151.   for (i=1; i<argc; i++) {
  152.     strcpy(s,argv[i]);
  153.     switch(i) {
  154.       case 1: if (atoi(s))
  155.                 whichuser=atoi(s);
  156.               else
  157.                 okay=0;
  158.               break;
  159.       case 2: if (okay)
  160.                 if (atoi(s))
  161.                   numfiles=atoi(s);
  162.                 else
  163.                   okay=0;
  164.               break;
  165.       case 3: if (okay)
  166.                 if (atoi(s))
  167.                   numk=atoi(s);
  168.                 else
  169.                   okay=0;
  170.               break;
  171.     }
  172.   }
  173.   if (!okay)
  174.     exit(1);
  175.    configfile=open("CONFIG.DAT",O_RDWR | O_BINARY);
  176.    if (configfile<0) {
  177.      exit(1);
  178.    }
  179.    read(configfile,(&cfg), sizeof(configrec));
  180.    close(configfile);
  181.  
  182.    if (read_user(whichuser,&u)>0) {
  183.      u.uploaded += numfiles;
  184.      u.uk += numk;
  185.      write_user(whichuser,&u);
  186.      close_user();
  187.      exit(0);
  188.    }
  189.  
  190.    exit(1);
  191. }
  192.  
  193.