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

  1. #include "packdef.h"
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <signal.h>
  6. #include <fcntl.h>
  7. #include <sgtty.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <pwd.h>
  11.  
  12. static char     linkh[150], linkm[150], linkc[150];
  13. static char     header[150], messages[150], crossref[150];
  14. static char     newheader[150], newmsg[150], newxref[150];
  15. static char     log[150], newlog[150];
  16. static int      table[1001];
  17. static int      goodmsg;
  18. static char     foo[150];
  19.  
  20. main()
  21. {
  22.     struct passwd  *p;
  23.     struct passwd  *getpwnam();
  24.     char           *ptr;
  25.     int             counter, result;
  26.     strcpy(foo, "bbs");
  27.     while (1) {
  28.         setpwent();
  29.         p = getpwnam(foo);
  30.         if (p == NULL) {
  31.             printf("Unable to locate user %s, please enter the proper name: ", foo);
  32.             scanf("%s", foo);
  33.             endpwent();
  34.         } else
  35.             break;
  36.     }
  37.     strcpy(foo, p->pw_shell);
  38.     ptr = strrchr(foo, '/');
  39.     if (ptr == NULL) {
  40.         printf("ERROR in shell location!\n");
  41.         exit(1);
  42.     }
  43.     ptr++;
  44.     *ptr = '\0';
  45.     strcpy(ORGPATH, foo);
  46.     strcpy(MSGS, ORGPATH);
  47.     strcat(MSGS, MSGS1);
  48.  
  49.     for (counter = 1; counter < 100; counter++) {
  50.  
  51.         result = change_msga(counter);
  52.         if (result != 1)
  53.             exit(0);
  54.         goodmsg = 0;
  55.         strcpy(linkh, m_pathname);
  56.         strcat(linkh, LINKH);
  57.         strcpy(log, m_pathname);
  58.         strcat(log, MSGLOG);
  59.         strcpy(newlog, m_pathname);
  60.         strcat(newlog, NEWLOG);
  61.         strcpy(linkm, m_pathname);
  62.         strcat(linkm, LINKM);
  63.         strcpy(linkc, m_pathname);
  64.         strcat(linkc, LINKC);
  65.         strcpy(header, m_pathname);
  66.         strcat(header, HEADER);
  67.         strcpy(messages, m_pathname);
  68.         strcat(messages, MESSAGES);
  69.         strcpy(crossref, m_pathname);
  70.         strcat(crossref, CROSSREF);
  71.         strcpy(newheader, m_pathname);
  72.         strcat(newheader, NEWHEADER);
  73.         strcpy(newmsg, m_pathname);
  74.         strcat(newmsg, NEWMSG);
  75.         strcpy(newxref, m_pathname);
  76.         strcat(newxref, NEWXREF);
  77.         remove();
  78.         hdrreadr();
  79.         hdrreadw();
  80.         hdrwrt();
  81.         cmd_s();
  82.         nlog();
  83.         rename();
  84.     }
  85.  
  86. }
  87.  
  88.  
  89. savemsg()
  90. {                /* save a message */
  91.     int             fd;
  92.  
  93.  
  94.     if ((fd = msgopenw(2)) == ERROR) {    /* open i/o */
  95.         return (ERROR);
  96.     }
  97.     msgwrt(fd);        /* write the message */
  98.     msgclose(fd);
  99.  
  100.     strcpy(h_date, msg_date);
  101.     hdrwrt();        /* update the header file */
  102. }
  103. /* end of function         */
  104.  
  105. cmd_s()
  106. {
  107.     int             q_msg, msgn, fd, msgnmbr, rc;
  108.     msgnmbr = 1;
  109.     for (fd = 0; fd <= 1000; fd++)
  110.         table[fd] = 0;
  111.     if ((fd = msgopenr(0)) == ERROR) {    /* open input */
  112.         return (ERROR);
  113.     }
  114.     q_msg = 1;
  115.     hdrreadr();
  116.     while ((rc = msgread(fd, q_msg++)) != ERROR) {    /* read until eof or
  117.                              * error */
  118.         msgn = q_msg - 1;
  119.         if (rc == 0) {
  120.             goto lpp;    /* not a valid msg */
  121.         }
  122.         reduce();
  123.         hdrreadw();
  124.         goodmsg = 1;
  125.         printf("\n%s%d\n", "Saving new message number ", msgnmbr);
  126.         table[msgn] = msgnmbr;
  127.         msgnmbr++;
  128.         savemsg();
  129. lpp:
  130.         hdrreadr();
  131.     }
  132.     if (!goodmsg) {
  133.         strcpy(msg_delete, "5");
  134.         reduce();
  135.         hdrreadw();
  136.         table[msgn] = msgnmbr;
  137.         savemsg();
  138.     }
  139.     msgclose(fd);
  140. }
  141. reduce()
  142. {
  143.     int             len;
  144.     len = strlen(msg_text);
  145.     len--;
  146.     while (msg_text[len] == 32) {
  147.         msg_text[len] = '\0';
  148.         len--;
  149.     }
  150. }
  151. nlog()
  152. {
  153.     FILE           *input, *output;
  154.     char            firstz[20], lastz[20], msgz[7];
  155.     char            buffer[99], qwerty[10];
  156.     int             i, newi;
  157.     qwerty[0] = '\0';
  158.     if ((input = fopen(log, "r")) == NULL) {
  159.         printf("\nError opening message.log???\n");
  160.         exit(1);
  161.     }
  162.     if ((output = fopen(newlog, "w")) == NULL) {
  163.         printf("\nError opening Omessage.log??\n");
  164.         exit(1);
  165.     }
  166.     while (fscanf(input, "%s%s%s", firstz, lastz, msgz) != EOF) {
  167.         i = atoi(msgz);
  168.         while (1) {
  169.             if (table[i] || !i)
  170.                 break;
  171.             --i;
  172.         }
  173.         newi = table[i];
  174.         itoa(qwerty, newi);
  175.         strcpy(buffer, firstz);
  176.         strcat(buffer, " ");
  177.         strcat(buffer, lastz);
  178.         strcat(buffer, " ");
  179.         strcat(buffer, qwerty);
  180.         i = strlen(qwerty);
  181.         i = 6 - i;
  182.         while (i) {
  183.             strcat(buffer, "_");
  184.             --i;
  185.         }
  186.         fprintf(output, "%s\n", buffer);
  187.     }
  188.     fclose(input);
  189.     fclose(output);
  190. }
  191. rename()
  192. {
  193.     int             result;
  194.     printf("\nRenaming files ...............");
  195.     result = unlink(linkh);
  196.     result = unlink(linkm);
  197.     result = unlink(linkc);
  198.     result = link(log, linkh);
  199.     result = unlink(log);
  200.     result = link(newlog, log);
  201.     result = unlink(newlog);
  202.     result = link(linkh, newlog);
  203.     result = unlink(linkh);
  204.     result = link(header, linkh);
  205.     result = link(messages, linkm);
  206.     result = link(crossref, linkc);
  207.     result = unlink(header);
  208.     result = unlink(messages);
  209.     result = unlink(crossref);
  210.     result = link(newheader, header);
  211.     result = link(newmsg, messages);
  212.     result = link(newxref, crossref);
  213.     result = unlink(newheader);
  214.     result = unlink(newmsg);
  215.     result = unlink(newxref);
  216.     result = link(linkh, newheader);
  217.     result = link(linkm, newmsg);
  218.     result = link(linkc, newxref);
  219.     result = unlink(linkh);
  220.     result = unlink(linkm);
  221.     result = unlink(linkc);
  222.     strcpy(msg_text, CHMOD);
  223.     strcat(msg_text, m_pathname);
  224.     strcat(msg_text, CHMOD1);
  225.     result = system(msg_text);
  226.     printf("DONE\n");
  227. }
  228. remove()
  229. {
  230.     int             result;
  231.     result = unlink(newheader);
  232.     result = unlink(newmsg);
  233.     result = unlink(newxref);
  234. }
  235. /* end of function         */
  236.