home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / hint / part01 / hinttab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-24  |  2.0 KB  |  97 lines

  1. /* hinttab.c -- functions for dealing with hinttab
  2.    Copyright (C) 1989 David MacKenzie
  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 1, 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; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  17.  
  18. #include "hint.h"
  19.  
  20. static int fd = -1;
  21. static off_t offset;
  22.  
  23. /* Open (rewind if already open) the hinttab for reading and writing.  */
  24.  
  25. void
  26. sethtent ()
  27. {
  28.   if (fd == -1)
  29.     {
  30.       fd = open (HINTTAB, O_RDWR);
  31.       if (fd == -1)
  32.     pfatal (HINTTAB);
  33.     }
  34.   else
  35.     {
  36.       offset = 0;
  37.       if (lseek (fd, offset, 0) == -1)
  38.     pfatal (HINTTAB);
  39.     }
  40. }
  41.  
  42. /* Open the hinttab for appending.  */
  43.  
  44. void
  45. sethtapp ()
  46. {
  47.   if (fd != -1)
  48.     close (fd);
  49.   fd = open (HINTTAB, O_WRONLY | O_APPEND);
  50.   if (fd == -1)
  51.     pfatal (HINTTAB);
  52.   offset = 0;
  53. }
  54.  
  55. /* Return a pointer to the hinttab entry matching `tty', or
  56.    NULL if none do.
  57.    The offset into hinttab will be left pointing to the start of the
  58.    matching entry. */
  59.  
  60. struct hinttab *
  61. gethttty (tty)
  62.      char *tty;
  63. {
  64.   static struct hinttab h;
  65.  
  66.   while (offset = lseek (fd, (off_t) 0, 1),
  67.      read (fd, &h, sizeof (h)) == sizeof (h))
  68.     {
  69.       if (!strcmp (tty, h.h_tty))
  70.     {
  71.       /* Seek back to the start of the record. */
  72.       lseek (fd, offset, 0);
  73.       return &h;
  74.     }
  75.     }
  76.   return NULL;
  77. }
  78.  
  79. /* Update the current entry (the last one matched by gethttty).  */
  80.  
  81. void
  82. writehtent (h)
  83.      struct hinttab *h;
  84. {
  85.   if (write (fd, h, sizeof (*h)) != sizeof (*h))
  86.     pfatal (HINTTAB);
  87. }
  88.  
  89. /* Close the hinttab.  */
  90.  
  91. void
  92. endhtent ()
  93. {
  94.   close (fd);
  95.   fd = -1;
  96. }
  97.