home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 September / PCO_0998.ISO / filesbbs / dos / sbbs_src.exe / SBBS / USERREC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-13  |  5.8 KB  |  195 lines

  1. #line 1 "USERREC.C"
  2.  
  3. /* Developed 1990-1997 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885 */
  4.  
  5. #include "sbbs.h"
  6.  
  7. /****************************************************************************/
  8. /* Fills 'str' with record for usernumber starting at start for length bytes*/
  9. /* Called from function ???                                                    */
  10. /****************************************************************************/
  11. void getuserrec(int usernumber,int start, char length, char *str)
  12. {
  13.     char c,path[256];
  14.     int i,file;
  15.  
  16. if(!usernumber) {
  17.     errormsg(WHERE,ERR_CHK,"user number",0);
  18.     return; }
  19. sprintf(path,"%sUSER\\USER.DAT",data_dir);
  20. if((file=nopen(path,O_RDONLY|O_DENYNONE))==-1) {
  21.     errormsg(WHERE,ERR_OPEN,path,O_RDONLY);
  22.     return; }
  23. if(usernumber<1
  24.     || filelength(file)<(long)((long)(usernumber-1L)*U_LEN)+(long)start) {
  25.     close(file);
  26.     errormsg(WHERE,ERR_CHK,"user number",usernumber);
  27.     return; }
  28. lseek(file,(long)((long)(usernumber-1)*U_LEN)+start,SEEK_SET);
  29.  
  30. i=0;
  31. while(i<LOOP_NODEDAB
  32.     && lock(file,(long)((long)(usernumber-1)*U_LEN)+start,length)==-1) {
  33.     if(i>10)
  34.         mswait(55);
  35.     i++; }
  36.  
  37. if(i>=LOOP_NODEDAB) {
  38.     close(file);
  39.     errormsg(WHERE,ERR_LOCK,"USER.DAT",usernumber);
  40.     return; }
  41.  
  42. if(read(file,str,length)!=length) {
  43.     unlock(file,(long)((long)(usernumber-1)*U_LEN)+start,length);
  44.     close(file);
  45.     errormsg(WHERE,ERR_READ,"USER.DAT",length);
  46.     return; }
  47.  
  48. unlock(file,(long)((long)(usernumber-1)*U_LEN)+start,length);
  49. close(file);
  50. for(c=0;c<length;c++)
  51.     if(str[c]==ETX || str[c]==CR) break;
  52. str[c]=0;
  53. }
  54.  
  55. /****************************************************************************/
  56. /* Places into USER.DAT at the offset for usernumber+start for length bytes */
  57. /* Called from various locations                                            */
  58. /****************************************************************************/
  59. void putuserrec(int usernumber,int start, char length, char *str)
  60. {
  61.     char c,str2[256];
  62.     int file,i;
  63.     node_t node;
  64.  
  65. if(usernumber<1) {
  66.     errormsg(WHERE,ERR_CHK,"user number",usernumber);
  67.     return; }
  68. sprintf(str2,"%sUSER\\USER.DAT",data_dir);
  69. if((file=nopen(str2,O_WRONLY|O_DENYNONE))==-1) {
  70.     errormsg(WHERE,ERR_OPEN,str2,O_WRONLY);
  71.     return; }
  72. strcpy(str2,str);
  73. if(strlen(str2)<length) {
  74.     for(c=strlen(str2);c<length;c++)
  75.         str2[c]=ETX;
  76.     str2[c]=0; }
  77. lseek(file,(long)((long)((long)((long)usernumber-1)*U_LEN)+start),SEEK_SET);
  78.  
  79. i=0;
  80. while(i<LOOP_NODEDAB
  81.     && lock(file,(long)((long)(usernumber-1)*U_LEN)+start,length)==-1) {
  82.     if(i>10)
  83.         mswait(55);
  84.     i++; }
  85.  
  86. if(i>=LOOP_NODEDAB) {
  87.     close(file);
  88.     errormsg(WHERE,ERR_LOCK,"USER.DAT",usernumber);
  89.     return; }
  90.  
  91. write(file,str2,length);
  92. unlock(file,(long)((long)(usernumber-1)*U_LEN)+start,length);
  93. close(file);
  94. for(i=1;i<=sys_nodes;i++) {    /* instant user data update */
  95.     if(i==node_num)
  96.         continue;
  97.     getnodedat(i,&node,0);
  98.     if(node.useron==usernumber && (node.status==NODE_INUSE
  99.         || node.status==NODE_QUIET)) {
  100.         getnodedat(i,&node,1);
  101.         node.misc|=NODE_UDAT;
  102.         putnodedat(i,node);
  103.         break; } }
  104. }
  105.  
  106. /****************************************************************************/
  107. /* Updates user 'usernumber's record (numeric string) by adding 'adj' to it */
  108. /* returns the new value.                                                    */
  109. /****************************************************************************/
  110. ulong adjustuserrec(int usernumber,int start, char length, long adj)
  111. {
  112.     char str[256],c,path[256];
  113.     int i,file;
  114.     ulong val;
  115.     node_t node;
  116.  
  117. if(usernumber<1) {
  118.     errormsg(WHERE,ERR_CHK,"user number",usernumber);
  119.     return(0UL); }
  120. sprintf(path,"%sUSER\\USER.DAT",data_dir);
  121. if((file=nopen(path,O_RDWR|O_DENYNONE))==-1) {
  122.     errormsg(WHERE,ERR_OPEN,path,O_RDWR);
  123.     return(0UL); }
  124. lseek(file,(long)((long)(usernumber-1)*U_LEN)+start,SEEK_SET);
  125.  
  126. i=0;
  127. while(i<LOOP_NODEDAB
  128.     && lock(file,(long)((long)(usernumber-1)*U_LEN)+start,length)==-1) {
  129.     if(i>10)
  130.         mswait(55);
  131.     i++; }
  132.  
  133. if(i>=LOOP_NODEDAB) {
  134.     close(file);
  135.     errormsg(WHERE,ERR_LOCK,"USER.DAT",usernumber);
  136.     return(0); }
  137.  
  138. if(read(file,str,length)!=length) {
  139.     unlock(file,(long)((long)(usernumber-1)*U_LEN)+start,length);
  140.     close(file);
  141.     errormsg(WHERE,ERR_READ,path,length);
  142.     return(0UL); }
  143. for(c=0;c<length;c++)
  144.     if(str[c]==ETX || str[c]==CR) break;
  145. str[c]=0;
  146. val=atol(str);
  147. if(adj<0L && val<-adj)        /* don't go negative */
  148.     val=0UL;
  149. else val+=adj;
  150. lseek(file,(long)((long)(usernumber-1)*U_LEN)+start,SEEK_SET);
  151. putrec(str,0,length,ultoa(val,tmp,10));
  152. if(write(file,str,length)!=length) {
  153.     unlock(file,(long)((long)(usernumber-1)*U_LEN)+start,length);
  154.     close(file);
  155.     errormsg(WHERE,ERR_WRITE,path,length);
  156.     return(val); }
  157. unlock(file,(long)((long)(usernumber-1)*U_LEN)+start,length);
  158. close(file);
  159. for(i=1;i<=sys_nodes;i++) { /* instant user data update */
  160.     if(i==node_num)
  161.         continue;
  162.     getnodedat(i,&node,0);
  163.     if(node.useron==usernumber && (node.status==NODE_INUSE
  164.         || node.status==NODE_QUIET)) {
  165.         getnodedat(i,&node,1);
  166.         node.misc|=NODE_UDAT;
  167.         putnodedat(i,node);
  168.         break; } }
  169. return(val);
  170. }
  171.  
  172. /****************************************************************************/
  173. /* Subtract credits from the current user online, accounting for the new    */
  174. /* "free credits" field.                                                    */
  175. /****************************************************************************/
  176. void subtract_cdt(long amt)
  177. {
  178.     long mod;
  179.  
  180. if(!amt)
  181.     return;
  182. if(useron.freecdt) {
  183.     if(amt>useron.freecdt) {      /* subtract both credits and */
  184.         mod=amt-useron.freecdt;   /* free credits */
  185.         putuserrec(useron.number,U_FREECDT,10,"0");
  186.         useron.freecdt=0;
  187.         useron.cdt=adjustuserrec(useron.number,U_CDT,10,-mod); }
  188.     else {                          /* subtract just free credits */
  189.         useron.freecdt-=amt;
  190.         putuserrec(useron.number,U_FREECDT,10
  191.             ,ultoa(useron.freecdt,tmp,10)); } }
  192. else    /* no free credits */
  193.     useron.cdt=adjustuserrec(useron.number,U_CDT,10,-amt);
  194. }
  195.