home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / ftp-102.zip / ftape-1.02 / qic / volume.c < prev    next >
C/C++ Source or Header  |  1992-10-12  |  5KB  |  190 lines

  1. /* Manipulate the volume header.
  2.    Copyright (C) 1992 David L. Brown, Jr.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; see the file COPYING.  If not, write to
  16.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <alloca.h>
  20. #include "qic.h"
  21. #include "header.h"
  22.  
  23. /* The volume table. */
  24.  
  25. struct vt_entry *vt;
  26.  
  27. /* Table entries. */
  28.  
  29. int vt_size;
  30.  
  31. /* Table size in bytes. */
  32.  
  33. int vt_byte_size;
  34.  
  35. /* From date.c */
  36. extern long current_qic_time (void);
  37.  
  38. void
  39. create_vt (void)
  40. {
  41.   int size;
  42.  
  43.   size = count_space (current_header.data_first);
  44.   vt_size = size * BLOCK_SIZE / sizeof (struct vt_entry);
  45.   vt_byte_size = (vt_size
  46.                 * sizeof (struct vt_entry));
  47.  
  48.   vt = ((struct vt_entry *)
  49.           xmalloc (vt_byte_size));
  50.  
  51.   bzero (vt, vt_byte_size);
  52. }
  53.  
  54. void
  55. read_vt (void)
  56. {
  57.   struct memory_segment data;
  58.   data.data = alloca (BLOCK_SIZE * BLOCKS_PER_SEGMENT);
  59.   data.blocks = BLOCKS_PER_SEGMENT;
  60.   data.marked_bad = current_header.bad_sectors[current_header.data_first];
  61.  
  62.   if (!read_good_segment (&data, current_header.data_first))
  63.     error (1, 0, "Cannot read volume table");
  64.  
  65.   vt_size = (count_space (current_header.data_first)
  66.          * BLOCK_SIZE / sizeof (struct vt_entry));
  67.   vt_byte_size = (vt_size * sizeof (struct vt_entry));
  68.   vt = (struct vt_entry *) xmalloc (vt_byte_size);
  69.   bcopy (data.data, vt, vt_byte_size);
  70. }
  71.  
  72. void
  73. write_vt (void)
  74. {
  75.   struct memory_segment data;
  76.   data.data = alloca (BLOCK_SIZE * BLOCKS_PER_SEGMENT);
  77.   /* Not quite right. */
  78.   data.blocks = count_space (current_header.data_first) + 3;
  79.   data.marked_bad = current_header.bad_sectors[current_header.data_first];
  80.  
  81.   bcopy (vt, data.data, vt_byte_size);
  82.  
  83.   write_good_segment (&data, current_header.data_first);
  84. }
  85.  
  86. int
  87. valid_vt (struct vt_entry *v)
  88. {
  89.   return (v->signature[0] == 'V'
  90.       && v->signature[1] == 'T'
  91.       && v->signature[2] == 'B'
  92.       && v->signature[3] == 'L');
  93. }
  94.  
  95. /* Append an entry to the vt. */
  96.  
  97. void
  98. append_vt (char *name, int start, int last, int bytes,
  99.        int continued, int sequence)
  100. {
  101.   int i;
  102.   int first = current_header.data_first + 1;
  103.  
  104.   for (i = 0; i < vt_size && valid_vt (&vt[i]); i++)
  105.     {
  106.       first = vt[i].last_segment + 1;
  107.     }
  108.  
  109.   /* !!!!! This should be checked before the data is written. */
  110.  
  111.   if (i >= vt_size)
  112.     error (1, 0, "Volume table is full.");
  113.  
  114.   vt[i].signature[0] = 'V';
  115.   vt[i].signature[1] = 'T';
  116.   vt[i].signature[2] = 'B';
  117.   vt[i].signature[3] = 'L';
  118.   vt[i].start_segment = start;
  119.   vt[i].last_segment = last;
  120.   strncpy (vt[i].description, name, 44);
  121.   LLONG_SET (vt[i].time, current_qic_time ());
  122.   vt[i].flags = (continued ? 2 : 0);
  123.   vt[i].sequence = sequence;
  124.   vt[i].password[0] = 0;
  125.   LLONG_SET (vt[i].directory_size, 0);
  126.   LLONG_SET (vt[i].data_size, bytes);
  127.   vt[i].os_version = 0x0403;
  128.   vt[i].volume_label[0] = 0;
  129.   vt[i].logical_origin = ' ';
  130.   vt[i].physical_origin = 0;
  131.   vt[i].compression_type = 0;
  132. }
  133.  
  134. /* Delete entries past a numbered entry. */
  135.  
  136. void
  137. delete_vt (int from)
  138. {
  139.   int i;
  140.   for (i = from; i < vt_size; i++)
  141.     {
  142.       bzero (&vt[i], sizeof (struct vt_entry));
  143.     }
  144. }
  145.  
  146. /* Produce a listing of the vt. */
  147.  
  148. void
  149. list_vt ()
  150. {
  151.   int i;
  152.   char name[45];
  153.   for (i = 0; i < vt_size && valid_vt (&vt[i]); i++)
  154.     {
  155.       name[44] = 0;
  156.       strncpy (name, vt[i].description, 44);
  157.  
  158. #if 0
  159.       fprintf (stderr, "%d, %d\n", vt[i].start_segment, vt[i].last_segment);
  160. #endif
  161.       fprintf (stdout, "%3d %c%8d (%5.fk) %s %s\n",
  162.            i,
  163.            (vt[i].flags & 2) ? '*' : ' ',
  164.            LLONG (vt[i].data_size),
  165.            ((BLOCK_SIZE
  166.          * count_spaces (vt[i].start_segment, vt[i].last_segment))
  167.         / 1024.0),
  168.            qic_to_text_time (LLONG(vt[i].time)),
  169.            name);
  170.       if (vt[i].sequence > 1)
  171.     fprintf (stdout, "        Volume %d\n", vt[i].sequence);
  172.     }
  173. }
  174.  
  175. /* Find first free segment on tape. */
  176.  
  177. int
  178. first_free (void)
  179. {
  180.   int i;
  181.   int first = current_header.data_first + 1;
  182.  
  183.   for (i = 0; i < vt_size && valid_vt (&vt[i]); i++)
  184.     {
  185.       first = vt[i].last_segment + 1;
  186.     }
  187.  
  188.   return (first);
  189. }
  190.