home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume16 / conf2 / part03 / confrw.c < prev   
Encoding:
C/C++ Source or Header  |  1988-09-14  |  6.7 KB  |  296 lines

  1. #include "conf.h"
  2.  
  3. read_log()
  4. {
  5.     char *ptr, *p;
  6.     char rduser[MAXNAMELEN], rdtty[MAXTTYLEN];
  7.     int tous, toline;
  8.  
  9.     while (read(log_rfd, (char *)&tlog, (unsigned)sizeof(struct clogfil)) ==
  10.     sizeof(struct clogfil))
  11.     {
  12.     switch(tlog.type)
  13.     {
  14.         case NORMAL:
  15.         if (tlog.f_line == cuser.cu_line)
  16.         {
  17.             if (tlog.messlen > wdlen)
  18.             wrdata = myrealloc(wrdata, wdlen = tlog.messlen);
  19.  
  20.             read(log_rfd, rduser, tlog.f_usrlen);
  21.             read(log_rfd, rdtty, tlog.f_ttylen);
  22.             read(log_rfd, wrdata, tlog.messlen);
  23.  
  24.             if (!seeme && !strcmp(rdtty, cuser.cu_tty))
  25.             break;
  26.  
  27.             if (beep)
  28.             (void)putchar(BELL);
  29.  
  30.             printmess(stdout, normform, rduser, rdtty,
  31.                 wrdata, tlog.messlen);
  32.  
  33.             if (cuser.cu_flags&USER_RECORD)
  34.             printmess(rec_fp, normform, rduser, rdtty,
  35.                 wrdata, tlog.messlen);
  36.         }
  37.         else
  38.             (void)lseek(log_rfd, (long)(tlog.f_usrlen + tlog.f_ttylen +
  39.             tlog.messlen), 1);
  40.         break;
  41.  
  42.         case SEND:
  43.         read(log_rfd, rduser, tlog.f_usrlen);
  44.         read(log_rfd, rdtty, tlog.f_ttylen);
  45.  
  46.         ptr = mymalloc(tlog.t_utlen);
  47.         read(log_rfd, ptr, tlog.t_utlen);
  48.         read(log_rfd, wrdata, tlog.messlen);
  49.  
  50.         p = ptr;
  51.         toline = tous = FALSE;
  52.         do
  53.         {
  54.             switch(*p)
  55.             {
  56.             case '/':
  57.                 if (atoi(p+1) == cuser.cu_line)
  58.                 toline = TRUE;
  59.                 break;
  60.  
  61.             case ':':
  62.                 if (!strcmp(p+1, cuser.cu_tty))
  63.                 tous = TRUE;
  64.                 break;
  65.  
  66.             default:
  67.                 if (!strcmp(p, cuser.cu_cname))
  68.                 tous = TRUE;
  69.                 break;
  70.             }
  71.             
  72.             while (*p++) ;  /* to next name */
  73.         } while (*p);       /* while another name exists */
  74.  
  75.         free(ptr);
  76.  
  77.         if (tous)
  78.         {
  79.             if (beep)
  80.             (void)putchar(BELL);
  81.  
  82.             printmess(stdout, sendform, rduser, rdtty,
  83.             wrdata, tlog.messlen);
  84.  
  85.             if (cuser.cu_flags&USER_RECORD)
  86.             printmess(rec_fp, sendform, rduser, rdtty,
  87.                 wrdata, tlog.messlen);
  88.  
  89.             (void)strcpy(replytty, rdtty);
  90.             replytty[strlen(replytty)] = '\0'; /* extra nul 4 send*/
  91.             (void)strcpy(replyname, rduser);
  92.         }
  93.  
  94.         if (toline)
  95.         {
  96.             if (beep)
  97.             (void)putchar(BELL);
  98.  
  99.             printmess(stdout, lineform, rduser, rdtty,
  100.                 wrdata, tlog.messlen);
  101.  
  102.             if (cuser.cu_flags&USER_RECORD)
  103.             printmess(rec_fp, lineform, rduser, rdtty,
  104.                 wrdata, tlog.messlen);
  105.         }
  106.         break;
  107.  
  108.         case SHOUT:
  109.         read(log_rfd, rduser, tlog.f_usrlen);
  110.         read(log_rfd, rdtty, tlog.f_ttylen);
  111.         read(log_rfd, wrdata, tlog.messlen);
  112.  
  113.         if (beep)
  114.             (void)putchar(BELL);
  115.  
  116.         printmess(stdout, shoutform, rduser, rdtty, wrdata,
  117.             tlog.messlen);
  118.  
  119.         if (cuser.cu_flags&USER_RECORD)
  120.             printmess(rec_fp, shoutform, rduser, rdtty,
  121.                 wrdata, tlog.messlen);
  122.         break;
  123.  
  124.         case INFORM:
  125.         if (tlog.f_line == cuser.cu_line)
  126.         {
  127.             if (tlog.messlen > wdlen)
  128.             wrdata = myrealloc(wrdata, wdlen = tlog.messlen);
  129.  
  130.             read(log_rfd, rduser, tlog.f_usrlen);
  131.             read(log_rfd, rdtty, tlog.f_ttylen);
  132.             read(log_rfd, wrdata, tlog.messlen);
  133.  
  134.             if (!informe && !strcmp(rdtty, cuser.cu_tty))
  135.             break;
  136.  
  137.             if (beep)
  138.             (void)putchar(BELL);
  139.  
  140.             printmess(stdout, informform, rduser, rdtty,
  141.                 wrdata, tlog.messlen);
  142.  
  143.             if (cuser.cu_flags&USER_RECORD)
  144.             printmess(rec_fp, informform, rduser,
  145.                 rdtty,wrdata, tlog.messlen);
  146.         }
  147.         else
  148.         {
  149.             (void)lseek(log_rfd, (long)(tlog.f_usrlen + tlog.f_ttylen +
  150.             tlog.messlen), 1);
  151.  
  152. #ifdef DEBUG1
  153.             (void)puts("flushin' this inform...");
  154.             (void)printf("f_line = %d, cu_line = %d.\n", 
  155.             tlog.f_line, cuser.cu_line);
  156. #endif DEBUG1
  157.         }
  158.         break;
  159.  
  160.         default:
  161.         /* ignore invalid type lseek to end */
  162. #ifdef    DEBUG0
  163.         (void)printf("invalid type (0x%x)\n",tlog.type);
  164. #endif    DEBUG0
  165.         (void)lseek(log_rfd, 0L, 2);
  166.         break;
  167.     }
  168.     }
  169. }
  170.  
  171. write_log(type, message, user, userlen, siz)
  172. int type;
  173. unsigned int userlen, siz;
  174. char *message, *user;
  175. {
  176.     static char *wrbuf;
  177.     static unsigned wblen=0;
  178.     unsigned wrlen;
  179.  
  180.     clog.type = type;
  181.     clog.messlen = siz;
  182.     clog.t_utlen = userlen;
  183.  
  184.     switch(type)
  185.     {
  186.     case NORMAL:
  187.         wrlen = sizeof(struct clogfil) + clog.f_usrlen + clog.f_ttylen +
  188.             clog.messlen;
  189.  
  190.         if (wrlen > wblen)
  191.         wrbuf = myrealloc(wrbuf, wblen = wrlen);
  192.  
  193.         /* move data into wrbuf */
  194.         cpystr(wrbuf, (char *)&clog, (unsigned)sizeof(struct clogfil));
  195.  
  196.         strcpy(wrbuf+sizeof(struct clogfil), cuser.cu_cname);
  197.         strcpy(wrbuf+sizeof(struct clogfil)+clog.f_usrlen, cuser.cu_tty);
  198.         cpystr(wrbuf+sizeof(struct clogfil)+clog.f_usrlen+
  199.            clog.f_ttylen, message, clog.messlen);
  200.  
  201.         write(log_wfd, wrbuf, wrlen);
  202.         break;
  203.  
  204.     case SEND:
  205.         wrlen = sizeof(struct clogfil) + clog.f_usrlen + clog.f_ttylen +
  206.         clog.t_utlen + clog.messlen;
  207.  
  208.         if (wrlen > wblen)
  209.         wrbuf = myrealloc(wrbuf, wblen = wrlen);
  210.  
  211.         /* move data into wrbuf */
  212.  
  213.         cpystr(wrbuf, (char *)&clog, sizeof(struct clogfil));
  214.         (void)strcpy(wrbuf+sizeof(struct clogfil), cuser.cu_cname);
  215.         (void)strcpy(wrbuf+sizeof(struct clogfil)+clog.f_usrlen,
  216.              cuser.cu_tty);
  217.         cpystr(wrbuf+sizeof(struct clogfil)+clog.f_usrlen+clog.f_ttylen,
  218.            user, clog.t_utlen);
  219.  
  220.         cpystr(wrbuf+sizeof(struct clogfil)+clog.f_usrlen+
  221.            clog.f_ttylen+clog.t_utlen, message, clog.messlen);
  222.  
  223.         write(log_wfd, wrbuf, wrlen);
  224.         break;
  225.  
  226.     case SHOUT:
  227.         wrlen = sizeof(struct clogfil) + clog.f_usrlen + clog.f_ttylen +
  228.             clog.messlen;
  229.  
  230.         if (wrlen > wblen)
  231.         wrbuf = myrealloc(wrbuf, wblen = wrlen);
  232.  
  233.         /* move data into wrbuf */
  234.  
  235.  
  236.         cpystr(wrbuf, (char *)&clog, sizeof(struct clogfil));
  237.         (void)strcpy(wrbuf+sizeof(struct clogfil), cuser.cu_cname);
  238.         (void)strcpy(wrbuf+sizeof(struct clogfil)+clog.f_usrlen,
  239.              cuser.cu_tty);
  240.         cpystr(wrbuf+sizeof(struct clogfil)+clog.f_usrlen+clog.f_ttylen,
  241.            message, clog.messlen);
  242.  
  243.         write(log_wfd, wrbuf, wrlen);
  244.         break;
  245.  
  246.     case INFORM:
  247.         wrlen = sizeof(struct clogfil) + clog.f_usrlen + clog.f_ttylen +
  248.             clog.messlen;
  249.  
  250.         if (wrlen > wblen)
  251.         wrbuf = myrealloc(wrbuf, wblen = wrlen);
  252.  
  253.         /* move data into wrbuf */
  254.  
  255.         cpystr(wrbuf, (char *)&clog, (unsigned)sizeof(struct clogfil));
  256.         (void)strcpy(wrbuf+sizeof(struct clogfil), cuser.cu_cname);
  257.         (void)strcpy(wrbuf+sizeof(struct clogfil)+clog.f_usrlen,
  258.              cuser.cu_tty);
  259.         cpystr(wrbuf+sizeof(struct clogfil)+clog.f_usrlen+clog.f_ttylen,
  260.            message, clog.messlen);
  261.  
  262.         write(log_wfd, wrbuf, wrlen);
  263.         break;
  264.  
  265. #ifdef    DEBUG0
  266.     default:
  267.         (void)printf("warning .. Bogus type, no write (type = %d)\n", type);
  268.         break;
  269. #endif    DEBUG0
  270.     }
  271. }
  272.  
  273. write_usr()
  274. {
  275.     (void)lseek(usr_fd, ourplace, 0);
  276.  
  277. #ifdef    SYSV
  278.     lockf(usr_fd, F_LOCK, (long)sizeof(struct cusrfil));
  279. #endif    SYSV
  280.  
  281. #ifdef    BSD
  282.     flock(usr_fd, LOCK_EX);
  283. #endif    BSD
  284.  
  285.     write(usr_fd, (char *)&cuser, sizeof(struct cusrfil));
  286.  
  287. #ifdef    SYSV
  288.     (void)lseek(usr_fd, ourplace, 0);
  289.     lockf(usr_fd, F_ULOCK, (long)sizeof(struct cusrfil));
  290. #endif    SYSV
  291.  
  292. #ifdef    BSD
  293.     flock(usr_fd, LOCK_UN);
  294. #endif    BSD
  295. }
  296.