home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / XBBS7200.ZIP / XBBS7200.TAR / msgpack / packfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-07  |  9.3 KB  |  368 lines

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