home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / jove / part04 / rec.c < prev   
Encoding:
C/C++ Source or Header  |  1987-02-02  |  2.8 KB  |  137 lines

  1. /************************************************************************
  2.  * This program is Copyright (C) 1986 by Jonathan Payne.  JOVE is       *
  3.  * provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is *
  5.  * included in all the files.                                           *
  6.  ************************************************************************/
  7.  
  8. #include "jove.h"
  9. #include "io.h"
  10. #include "rec.h"
  11. #include <sys/file.h>
  12.  
  13. private int    rec_fd = 0;
  14. private char    *recfname;
  15. private File    *rec_out;
  16.  
  17. #ifndef L_SET
  18. #    define L_SET 0
  19. #endif
  20.  
  21. private struct rec_head    Header;
  22.  
  23. recinit()
  24. {
  25.     char    buf[128];
  26.  
  27.     sprintf(buf, "%s/%s", TmpFilePath, p_tempfile);
  28.     recfname = copystr(buf);
  29.     recfname = mktemp(recfname);
  30.     rec_fd = creat(recfname, 0644);
  31.     if (rec_fd == -1) {
  32.         complain("Cannot create \"%s\"; recovery disabled.", recfname);
  33.         return;
  34.     }
  35.     /* Initialize the record IO. */
  36.     rec_out = fd_open(recfname, F_WRITE|F_LOCKED, rec_fd, iobuff, LBSIZE);
  37.  
  38.     /* Initialize the record header. */
  39.     Header.Uid = getuid();
  40.     Header.Pid = getpid();
  41.     Header.UpdTime = 0L;
  42.     Header.Nbuffers = 0;
  43.     (void) write(rec_fd, (char *) &Header, sizeof Header);
  44. }
  45.  
  46. recclose()
  47. {
  48.     if (rec_fd == -1)
  49.         return;
  50.     (void) close(rec_fd);
  51.     (void) unlink(recfname);
  52. }
  53.  
  54. static
  55. putaddr(addr, p)
  56. disk_line    addr;
  57. register File    *p;
  58. {
  59.     register char    *cp = (char *) &addr;
  60.     register int    nchars = sizeof (disk_line);
  61.  
  62.     while (--nchars >= 0)
  63.         putc(*cp++ & 0377, p);
  64. }
  65.  
  66. static
  67. putn(cp, nbytes)
  68. register char    *cp;
  69. register int    nbytes;
  70. {
  71.     while (--nbytes >= 0)
  72.         putc(*cp++ & 0377, rec_out);
  73. }
  74.  
  75. /* Write out the line pointers for buffer B. */
  76.  
  77. static
  78. dmppntrs(b)
  79. register Buffer    *b;
  80. {
  81.     register Line    *lp;
  82.  
  83.     for (lp = b->b_first; lp != 0; lp = lp->l_next)
  84.         putaddr(lp->l_dline, rec_out);
  85. }
  86.  
  87. /* dump the buffer info and then the actual line pointers. */
  88.  
  89. static
  90. dmp_buf(b)
  91. register Buffer    *b;
  92. {
  93.     static struct rec_entry    record;
  94.     register Line    *lp;
  95.     register int    nlines = 0;
  96.  
  97.     for (lp = b->b_first; lp != 0; lp = lp->l_next, nlines++)
  98.         ;
  99.     strcpy(record.r_fname, b->b_fname ? b->b_fname : NullStr);
  100.     strcpy(record.r_bname, b->b_name);
  101.     record.r_nlines = nlines;
  102.     putn((char *) &record, sizeof record);
  103.     dmppntrs(b);
  104. }
  105.  
  106. /* Goes through all the buffers and syncs them to the disk. */
  107.  
  108. int    SyncFreq = 50;
  109.  
  110. SyncRec()
  111. {
  112.     register Buffer    *b;
  113.  
  114.     if (rec_fd == 0)
  115.         recinit();    /* Init recover file. */
  116.     if (rec_fd == -1)
  117.         return;
  118.     lseek(rec_fd, 0L, L_SET);
  119.     (void) time(&Header.UpdTime);
  120.     Header.Nbuffers = 0;
  121.     for (b = world; b != 0; b = b->b_next)
  122.         if (b->b_type == B_SCRATCH || !IsModified(b))
  123.             continue;
  124.         else
  125.             Header.Nbuffers++;
  126.     putn((char *) &Header, sizeof Header);
  127.     if (Header.Nbuffers != 0) {
  128.         SyncTmp();
  129.         for (b = world; b != 0; b = b->b_next)
  130.             if (b->b_type == B_SCRATCH || !IsModified(b))
  131.                 continue;
  132.             else
  133.                 dmp_buf(b);
  134.     }
  135.     flush(rec_out);
  136. }
  137.