/*
 * Copyright 1988, 1989, 1990, 1991 Massachusetts Institute of Technology
 */
#include "structs.h"

void read_mount_file()
{
  FILE *fp;

  mtbl.nmounts = 0;
  fp = fopen(MOUNT_FILE,"r");
  if (fp == NULL) return;
  while (fscanf(fp, "%s %s %d\n", mtbl.mount_info[mtbl.nmounts].dev_name,
		mtbl.mount_info[mtbl.nmounts].disc_name,
		&(mtbl.mount_info[mtbl.nmounts].mountable)) == 3) {
    mtbl.mount_info[mtbl.nmounts].realvol = 0;
    mtbl.nmounts++;
  }
  fclose(fp);
}

void write_mount_file ()
{
  int i, fd;
  char buffer[100];
/* This open(2) is written to use HP-UX sync i/o flags if they are
   defined */
  fd = open(MOUNT_FILE, O_CREAT | O_TRUNC | O_WRONLY
#ifdef O_SYNC
| O_SYNC
#endif /* O_SYNC */
#ifdef O_SYNCIO
| O_SYNCIO
#endif /* O_SYNCIO */
, 0666);
if (fd == -1) return;

  for (i = 0; i < mtbl.nmounts; i++)
    if (strlen(mtbl.mount_info[i].disc_name) > 0 && 
	mtbl.mount_info[i].realvol) {
      sprintf(buffer, "%s %s %d\n", mtbl.mount_info[i].dev_name,
	      mtbl.mount_info[i].disc_name,
	      mtbl.mount_info[i].mountable);
      write(fd, buffer, strlen(buffer));
#ifndef SYSV
      fsync(fd);
#endif /* SYSV */
    }
  close(fd);
  chmod(MOUNT_FILE,0666);
}

void compact_mount_table()
{
  int cur_old = 0, cur_new = 0, new_nmounts = 0;

  for ( cur_old = 0; cur_old < mtbl.nmounts; cur_old++) {
    if (mtbl.mount_info[cur_old].realvol) {
      if (cur_new != cur_old)
	mtbl.mount_info[cur_new] = mtbl.mount_info[cur_old];
      cur_new++;
      new_nmounts++;
    }
  }
  mtbl.nmounts = new_nmounts;
}
