home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / id-utils-3.2-src.tgz / tar.out / fsf / id-utils / libidu / idfile.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  4KB  |  138 lines

  1. /* idfile.c -- read & write mkid database file header
  2.    Copyright (C) 1986, 1995, 1996 Free Software Foundation, Inc.
  3.    Written by Greg McGary <gkm@gnu.ai.mit.edu>
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. #include <config.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include "xstdlib.h"
  24. #include "xstring.h"
  25. #include "xsysstat.h"
  26. #include "xnls.h"
  27. #include "xobstack.h"
  28. #include "idfile.h"
  29. #include "error.h"
  30.  
  31. int io_size __P((FILE *, void *, unsigned int size, int));
  32.  
  33.  
  34. /****************************************************************************/
  35.  
  36. /* Discover the name of the ID file.  If ARG is NULL, consult $IDPATH.
  37.    If $IDPATH is undefined, default to "ID".  If the candidate file
  38.    name is relative, search successive ancestor directories until the
  39.    file is found or we reach the root.  If we find it, return the
  40.    relative file name, otherwise return NULL.  */
  41.  
  42. char const *
  43. locate_id_file_name (char const *arg)
  44. {
  45.   static char file_name_buffer[BUFSIZ];
  46.   char *buf = file_name_buffer;
  47.   char *id_path = 0;
  48.   struct stat rootb;
  49.   struct stat statb;
  50.  
  51.   if (arg == 0)
  52.     {
  53.       id_path = getenv ("IDPATH");
  54.       if (id_path)
  55.     {
  56.       id_path = strdup (id_path);
  57.       arg = strtok (id_path, ":");
  58.       /* FIXME: handle multiple ID file names */
  59.     }
  60.     }
  61.   if (arg == 0)
  62.     arg = DEFAULT_ID_FILE_NAME;
  63.  
  64.   /* if we got absolute name, just use it. */
  65.   if (arg[0] == '/')
  66.     return arg;
  67.   /* if the name we were give exists, don't bother searching */
  68.   if (stat (arg, &statb) == 0)
  69.     return arg;
  70.   /* search up the tree until we find a directory where this
  71.      * relative file name is visible.
  72.      * (or we run out of tree to search by hitting root).
  73.      */
  74.  
  75.   if (stat ("/", &rootb) != 0)
  76.     return NULL;
  77.   do
  78.     {
  79.       strcpy (buf, "../");
  80.       buf += 3;
  81.       strcpy (buf, arg);
  82.       if (stat (file_name_buffer, &statb) == 0)
  83.     return file_name_buffer;
  84.       *buf = '\0';
  85.       if (stat (file_name_buffer, &statb) != 0)
  86.     return NULL;
  87.     }
  88.   while (!((statb.st_ino == rootb.st_ino) ||
  89.        (statb.st_dev == rootb.st_dev)));
  90.   return NULL;
  91. }
  92.  
  93.  
  94. /****************************************************************************/
  95.  
  96. int
  97. sizeof_idhead ()
  98. {
  99.   return io_idhead (0, io_size, 0);
  100. }
  101.  
  102. int
  103. io_size (FILE *ignore_FILE, void *ignore_addr, unsigned int size, int io_type)
  104. {
  105.   if (io_type == IO_TYPE_STR)
  106.     error (0, 0, _("can't determine the io_size of a string!"));
  107.   return size;
  108. }
  109.  
  110. /* The sizes of the fields must be hard-coded.  They aren't
  111.    necessarily the sizes of the struct members, because some
  112.    architectures don't have any way to declare 4-byte integers
  113.    (e.g., Cray) */
  114.  
  115. int
  116. io_idhead (FILE *fp, io_func_t iof, struct idhead *idhp)
  117. {
  118.   unsigned int size = 0;
  119.   unsigned char pad = 0;
  120.   if (fp)
  121.     fseek (fp, 0L, 0);
  122.   size += iof (fp, idhp->idh_magic, 2, IO_TYPE_FIX);
  123.   size += iof (fp, &pad, 1, IO_TYPE_FIX);
  124.   size += iof (fp, &idhp->idh_version, 1, IO_TYPE_FIX);
  125.   size += iof (fp, &idhp->idh_flags, 2, IO_TYPE_INT);
  126.   size += iof (fp, &idhp->idh_file_links, 4, IO_TYPE_INT);
  127.   size += iof (fp, &idhp->idh_files, 4, IO_TYPE_INT);
  128.   size += iof (fp, &idhp->idh_tokens, 4, IO_TYPE_INT);
  129.   size += iof (fp, &idhp->idh_buf_size, 4, IO_TYPE_INT);
  130.   size += iof (fp, &idhp->idh_vec_size, 4, IO_TYPE_INT);
  131.   size += iof (fp, &idhp->idh_tokens_offset, 4, IO_TYPE_INT);
  132.   size += iof (fp, &idhp->idh_flinks_offset, 4, IO_TYPE_INT);
  133.   size += iof (fp, &idhp->idh_end_offset, 4, IO_TYPE_INT);
  134.   size += iof (fp, &idhp->idh_max_link, 2, IO_TYPE_INT);
  135.   size += iof (fp, &idhp->idh_max_path, 2, IO_TYPE_INT);
  136.   return size;
  137. }
  138.