home *** CD-ROM | disk | FTP | other *** search
/ BBS in C / CPDCOMM#155.img / BBSCFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-12-24  |  8.6 KB  |  345 lines

  1. /*
  2.     bbscfile.c
  3.  
  4.     Support routines used by BBSc.c to do file i/o for the
  5.     message file.
  6.                 Mike Kelly
  7.  
  8.     06/12/83 v1.0    written
  9.     07/07/83 v1.0    updated
  10. */
  11.  
  12. /* #define DEBUG 1 */
  13.  
  14. #include "bbscdef.h"
  15.  
  16. #define LASTDATE  " 07/07/83 "
  17.  
  18. #define PGMNAME "BBSCFILE "
  19. #define VERSION " 1.0 "
  20.  
  21. hdrwrt()        /* write the header from memory variables */
  22.     {        /* header is a 1 record file */
  23.     int    fd;
  24.     char    buf128[MSGSECT] ;
  25.  
  26.     if ((fd = open(HEADER,WRITE,0666)) < 0)    /* open i/o */
  27.         {
  28.         portsout("Can't open header-file, will create it!") ;
  29.         portsout(CRLF) ;
  30.         if ((fd = creat(HEADER,0666)) < 0)
  31.             {
  32.             portsout("Can't create header-file, aborting!") ;
  33.             portsout(CRLF) ;
  34.             return(ERROR) ;
  35.             }
  36.         }
  37.     itoa(h_next_msg,h_next) ;    /* convert int to char */
  38.     strfill(buf128,26,MSGSECT) ;    /* init buf128 to all hex 1a */
  39.     sprintf(buf128,"%s~%s~",    /* build record */
  40.         h_next_msg,
  41.         h_date) ;
  42.     write(fd,buf128,MSGSECT) ;    /* write it */
  43.     close(fd) ;            /* no need to leave it open */
  44. #ifdef DEBUG
  45.     portsout(CRLF) ;
  46.     portsout("<<< header file written ok >>>") ;
  47.     portsout(CRLF) ;
  48. #endif
  49.     return(OK) ;
  50.     }
  51.  
  52. hdrread()        /* read the header file into memory */
  53.     {
  54.     int    fd,
  55.         cnt;
  56.     char    buf128[MSGSECT];
  57.  
  58.     if ((fd = open(HEADER,READ,0666)) < 0)    /* open input */
  59.         {
  60.         portsout("Can't open header-file, using inital values!") ;
  61.         portsout(CRLF) ;
  62.         h_next = 1 ;
  63.         h_next_msg[0] = '1' ; h_next_msg[1] = 0 ;
  64.         h_date[0] = '0' ; h_date[1] = 0 ;
  65.         hdrwrt() ; return ;
  66.         }
  67.     if((cnt=read(fd,buf128,MSGSECT)) != MSGSECT)
  68.         {
  69.         portsout(CRLF) ;
  70.             portsout("<<< header read error >>>") ;
  71.         portsout(CRLF) ;
  72.         return(ERROR) ;
  73.         }
  74.     cnt = sscanf(buf128,"%[^~]~%[^~]~",
  75.             h_next_msg,
  76.             h_date) ;
  77.     close(fd) ;        /* no need to leave it open */
  78.  
  79. #ifdef DEBUG
  80.     portsout(CRLF) ;
  81.     portsout("<<< header read, next-message='") ;
  82.     portsout(h_next_msg) ;
  83.     portsout("', date='") ;
  84.     portsout(h_date) ;
  85.     portsout("'. >>>") ;
  86.     portsout(CRLF) ;
  87. #endif
  88.  
  89.     if (cnt != 2)
  90.         {
  91. #ifdef DEBUG
  92.     portsout(CRLF) ;
  93.     portsout("<<< Invalid header read! >>>") ;
  94.     portsout(CRLF) ;
  95. #endif
  96.         return(ERROR) ;
  97.         }
  98.     h_next = atoi(h_next_msg) ;
  99.     return(OK) ;
  100.     }
  101.  
  102. msgopen(how)
  103. int    how ;        /* how to open 0=input, 1=output, 2=i/o */
  104.     {
  105.     int    fd ;
  106.  
  107.     if ((fd = open(MESSAGES,how,0666)) < 0)    /* open i/o */
  108.         {
  109.         portsout("can't open message-file, will create it!") ;
  110.         portsout(CRLF) ;
  111.         if ((fd = creat(MESSAGES,0666)) < 0)
  112.             {
  113.             portsout("can't create message-file, aborting!") ;
  114.             portsout(CRLF) ;
  115.             return(ERROR) ;
  116.             }
  117.         }
  118. #ifdef DEBUG
  119.     portsout(CRLF) ;
  120.     portsout("<<< message file opened ok >>>") ;
  121.     portsout(CRLF) ;
  122. #endif
  123.     return(fd) ;
  124.     }
  125.  
  126. msgclose(fd)
  127. int    fd ;
  128.     {
  129. #ifdef DEBUG
  130.     portsout(CRLF) ;
  131.     portsout("<<< closing message file >>>") ;
  132.     portsout(CRLF) ;
  133. #endif
  134.     return(close(fd)) ;
  135.     }
  136.  
  137. msgwrt(fd)        /* write the message file from memory variables */
  138. int    fd;        /* writes a message starting with the h_next msg # */
  139.     {
  140.     int    rc,            /* return code */
  141.         cnt1,
  142.         cnt2,
  143.         len;
  144.     char    bufmsg0[MSG1MAX+1],
  145.         buf128[MSGSECT],
  146.         this1[10],
  147.         next1[10];
  148.  
  149.     rc = cnt1 = len = cnt2 = 0 ;
  150.     itoa(this1,h_next) ;                /* convert int to char */
  151.     rc = seek(fd,h_next - 1,0) ;        /* seek next available sector */
  152.     h_next++ ;
  153.     itoa(next1,h_next) ;
  154.     strfill(buf128,0,MSGSECT) ;        /* init buf128 to all hex 00 */
  155. /*
  156. *            build first piece of msg record
  157. */
  158.     sprintf(buf128,"%s~%s~%s~%s~%s~%s~%s~%s~%s~",    /* build record */
  159.         this1,                    /* this rcd # */
  160.         next1,                    /*  points next rcd # */
  161.         msg_delete,                /* delete byte */
  162.         msg_date,
  163.         msg_time,
  164.         msg_to,
  165.         msg_from,
  166.         msg_pass,
  167.         msg_subject);
  168.     rc = write(fd,buf128,MSGSECT);    /* write the first 128 byte record */
  169.                     /*  for a message record */
  170. /*
  171. *            build the n+1 piece of msg record
  172. */
  173.  
  174.     len = (strlen(msg_text) / MSG1MAX) + 1; /* calc how many more 128 */
  175.                         /*  byte records to write */
  176.     cnt2 = 1 ;                /* init for substr */
  177.     while (len--)
  178.         {
  179.         itoa(this1,h_next);        /* calc/convert record #'s */
  180.         h_next++;
  181.         if (len == 0)
  182.             {
  183.             strcpy(next1,"0");    /* marks last 128 byte piece */
  184.             }            /*  of a msg */
  185.         else
  186.             {
  187.             itoa(next1,h_next);
  188.             }
  189.         strfill(bufmsg0,0,MSG1MAX);
  190.         substr(msg_text,bufmsg0,cnt2,MSG1MAX); /* mv MSG1MAX to buff */
  191.         cnt2 += MSG1MAX;        /* up cnt2 by MSG1MAX */
  192.         strfill(buf128,0,MSGSECT);    /* init buf128 to all hex 00 */
  193.         sprintf(buf128,"%s~%s~%s~%s~",
  194.             this1,            /* this rcd # */
  195.             next1,            /* point to next rcd # */
  196.             msg_delete,        /* delete byte */
  197.             bufmsg0);        /* piece of msg */
  198.         rc = write(fd,buf128,MSGSECT);    /* write n+1 128 byte record */
  199.         }
  200.  
  201.     strfill(buf128,26,MSGSECT);        /* fill with all hex 1a */
  202.     rc = write(fd,buf128,MSGSECT);    /* write all hex 1a 128 byte record */
  203.     return(OK);
  204.     }
  205.  
  206. msgrewrt(fd,r_msg)    /* re-write the message file from memory variables */
  207. int    fd,        /* re-writes only the 1st part of a message */
  208.     r_msg;        /* used to update the delete byte */
  209.     {
  210.     int    rc,            /* return code */
  211.         cnt1,
  212.         file_size;
  213.     char    buf128[MSGSECT],
  214.         this1[10],
  215.         next1[10];
  216.  
  217.     rc = cnt1 = 0;
  218.     if (r_msg > h_next)    /* don't try to seek past end of file */
  219.         {
  220.         return(ERROR);
  221.         }
  222.     if ((rc = seek(fd,r_msg-1,0)) == ERROR)    /* seek to requested sector */
  223.         {
  224.         return(ERROR);
  225.         }
  226.     itoa(this1,r_msg);        /* convert int to char */
  227.     r_msg++;
  228.     itoa(next1,r_msg);
  229.     strfill(buf128,0,MSGSECT);        /* init buf128 to all hex 00 */
  230. /*
  231. *            build first piece of msg record
  232. */
  233.     sprintf(buf128,"%s~%s~%s~%s~%s~%s~%s~%s~%s~",    /* build record */
  234.         this1,                    /* this rcd # */
  235.         next1,                    /* points next rcd # */
  236.         msg_delete,                /* delete byte */
  237.         msg_date,
  238.         msg_time,
  239.         msg_to,
  240.         msg_from,
  241.         msg_pass,
  242.         msg_subject);
  243.     rc = write(fd,buf128,MSGSECT);    /* write the first 128 byte record */
  244.                     /*  for a message record */
  245.     return(OK);
  246.     }
  247.  
  248.  
  249. msgread(fd,msgno)        /* read message number requested */
  250. int    fd,            /* returns ERROR if msg past eof */
  251.     msgno;            /* returns 0 if msg is not 1st piece */
  252.                 /*   of a message */
  253.                 /* returns 0 if msg is deleted */
  254.                 /* returns msg # if successful */
  255.     {
  256.     int    rc,            /* return code */
  257.         cnt1,
  258.         cnt2,
  259.         len,
  260.         next,
  261.         ret_this,
  262.         file_size;
  263.     char    bufmsg0[MSG1MAX+1],
  264.         buf128[MSGSECT+256],
  265.         buftmp[MSGSECT+256],
  266.         this1[10],
  267.         next1[10];
  268.  
  269.     if (msgno > h_next)        /* don't try to seek past end of file */
  270.         {
  271.         return(ERROR);
  272.         }
  273.     if ((rc = seek(fd,msgno-1,0)) == ERROR)
  274.         {
  275.         portsout(CRLF) ;
  276.         portsout("Can't seek on message-file!") ;
  277.         portsout(CRLF) ;
  278.         return(ERROR);        /* when cant find it */
  279.         }
  280.     if (read(fd,buf128,MSGSECT) != MSGSECT) /* read 128 byte sector */
  281.         {
  282.         portsout(CRLF) ;
  283.         portsout("Can't read in message-file!") ;
  284.         portsout(CRLF) ;
  285.         return(ERROR);
  286.         }
  287. /*
  288. *            get first piece of msg record
  289. */
  290. /* do trial read, since if not first record, fields might overflow */
  291. rc = sscanf(buf128,"%[^~]~%[^~]~%[^~]~%[^~]~%[^~]~%[^~]~%[^~]~%[^~]~%[^~]~",
  292.     buftmp,buftmp,buftmp,buftmp,buftmp,buftmp,buftmp,buftmp,buftmp) ;
  293.     if (rc != 9)        /* makes sure we read the 1st piece */
  294.         {        /*  of a message and not in the middle */
  295.         return(0);    /* 0 when is not the msg header */
  296.         }
  297. /* now do the real read since looks like is a good record */
  298. rc = sscanf(buf128,"%[^~]~%[^~]~%[^~]~%[^~]~%[^~]~%[^~]~%[^~]~%[^~]~%[^~]~",
  299.         this1,                /* this rcd # */
  300.         next1,                /*  points to next rcd # */
  301.         msg_delete,            /* delete byte */
  302.         msg_date,
  303.         msg_time,
  304.         msg_to,
  305.         msg_from,
  306.         msg_pass,
  307.         msg_subject);
  308.     if (rc != 9)        /* makes sure we read the 1st piece */
  309.         {        /*  of a message and not in the middle */
  310.         return(0);    /* 0 when is not the msg header */
  311.         }
  312.  
  313.     if (msg_delete[0] == '9')    /* check for deleted messages */
  314.         {            /*  if so, return as if not found */
  315.         return(0);
  316.         }
  317.  
  318.     ret_this = atoi(this1);    /* return this msg no. */
  319.     next = atoi(next1);
  320.     strcpy(msg_no,this1);
  321.     msg_text[0] = '\0';
  322.     while (next)            /* read until no more pieces for */
  323.         {            /*  this message */
  324.         if (read(fd,buf128,MSGSECT) != MSGSECT) /* read next sector */
  325.             {
  326.             portsout(CRLF) ;
  327.             portsout("Can't read in message-file(2)!") ;
  328.             portsout(CRLF) ;
  329.             return(ERROR);
  330.             }
  331.         strfill(bufmsg0,0,MSG1MAX);    /* init bufmsg0 to all hex 00 */
  332.         rc = sscanf(buf128,"%[^~]~%[^~]~%[^~]~%[^~]~",
  333.             this1,            /* this rcd # */
  334.             next1,            /* point to next rcd # */
  335.             msg_delete,        /* delete byte */
  336.             bufmsg0);        /* piece of msg */
  337.         next = atoi(next1);
  338.         strcat(msg_text,bufmsg0);    /* tag piece of msg to */
  339.                         /*  whole msg array */
  340.         }
  341.     return(ret_this);    /* if all ok, return the msg no. found */
  342.     }
  343.  
  344. /*    end of program      */
  345.