home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libcnews / trbatcomm.c < prev    next >
C/C++ Source or Header  |  1993-03-13  |  2KB  |  115 lines

  1. /*
  2.  * transmit batch file management (common support code)
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include "libc.h"
  8. #include "news.h"
  9. #include "msgs.h"
  10. #include "trbatch.h"
  11.  
  12. struct batchfile batchfile[NOPENBFS];    /* try to keep open always */
  13. static struct batchfile fakebatf;    /* for non-cached batch files */
  14. /*
  15.  * More than one pointer in ordtobfs may point at a given batchfile,
  16.  * to permit sharing of open batch files among multiple sys entries.
  17.  * ordtobfs[ordinal # of batch sys entry] -> (usually open) batch file,
  18.  * if the index is in range.
  19.  */
  20. struct batchfile *ordtobfs[NOPENBFS];
  21.  
  22. /*
  23.  * any stream-specific set up (reserved for future use)
  24.  */
  25. bfsetup(bf)
  26. register struct batchfile *bf;
  27. {
  28.     if (!fnlockfile(bf->bf_str))    /* UUNET: lock against exploder */
  29.         warning("can't lock master batch file `%s'", bf->bf_name);
  30.             /* persistent? */
  31. }
  32.  
  33. /* ARGSUSED ord */
  34. statust
  35. bffkclose(ord)            /* close current (ord's) batchfile, if fake */
  36. int ord;
  37. {
  38.     register statust status = ST_OKAY;
  39.  
  40.     if (fakebatf.bf_str != NULL)
  41.         status |= bfclose(&fakebatf);
  42.     return status;
  43. }
  44.  
  45. statust
  46. bfclose(bf)
  47. register struct batchfile *bf;
  48. {
  49.     register statust status = ST_OKAY;
  50.  
  51.     if (nfclose(bf->bf_str) == EOF)
  52.         status = prfulldisk(bf->bf_name);
  53.     bf->bf_str = NULL;    /* prevent accidents; mark as closed */
  54.     return status;
  55. }
  56.  
  57. struct batchfile *
  58. fakebf(stream, name)
  59. FILE *stream;
  60. char *name;
  61. {
  62.     fakebatf.bf_name = name;
  63.     fakebatf.bf_str = stream;
  64.     return &fakebatf;
  65. }
  66.  
  67. /*
  68.  * a performance hack: only fflush bf->bf_str every FLUSHEVERY calls.
  69.  */
  70. int
  71. bfflush(bf)
  72. register struct batchfile *bf;
  73. {
  74.     register int ret = 0;
  75.  
  76.     if (--bf->bf_lines <= 0) {
  77.         bf->bf_lines = FLUSHEVERY;
  78.         ret = fflush(bf->bf_str);
  79.     }
  80.     return ret;
  81. }
  82.  
  83. statust
  84. bfrclose()                /* close an arbitrary batchfile */
  85. {
  86.     register struct batchfile *bf;
  87.     register statust status = ST_OKAY;
  88.  
  89.     for (bf = batchfile; bf <= lastbf; bf++)
  90.         if (bf->bf_str != NULL) {
  91.             status |= bfclose(bf);
  92.             break;
  93.         }
  94.     return status;
  95. }
  96.  
  97. statust
  98. bfrealclose()                /* close all open batch files */
  99. {
  100.     register struct batchfile *bf;
  101.     register statust status = ST_OKAY;
  102.  
  103.     for (bf = batchfile; bf <= lastbf; bf++) {
  104.         if (bf->bf_str != NULL)        /* batch file stream open */
  105.             status |= bfclose(bf);
  106.         nnfree(&bf->bf_name);
  107.         nnfree(&bf->bf_msgid);
  108. #ifdef notdef
  109.         bf->bf_ref = 0;
  110. #endif
  111.         ordtobfs[bf - batchfile] = NULL;    /* unmap batch file */
  112.     }
  113.     return status;
  114. }
  115.