home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / unix.subproj / xqtfil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  7.0 KB  |  299 lines

  1. /* xqtfil.c
  2.    Routines to read execute files.
  3.  
  4.    Copyright (C) 1991, 1992, 1993, 1995 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char xqtfil_rcsid[] = "$Id: xqtfil.c,v 1.10 1995/07/19 04:18:37 ian Rel $";
  30. #endif
  31.  
  32. #include "uudefs.h"
  33. #include "sysdep.h"
  34. #include "system.h"
  35.  
  36. #include <errno.h>
  37.  
  38. #if HAVE_OPENDIR
  39. #if HAVE_DIRENT_H
  40. #include <dirent.h>
  41. #else /* ! HAVE_DIRENT_H */
  42. #include <sys/dir.h>
  43. #define dirent direct
  44. #endif /* ! HAVE_DIRENT_H */
  45. #endif /* HAVE_OPENDIR */
  46.  
  47. /* Under the V2 or BSD42 spool directory scheme, all execute files are
  48.    in the main spool directory.  Under the BSD43 scheme, they are all
  49.    in the directory X..  Under the HDB or SVR4 scheme, they are in
  50.    directories named after systems.  Under the ULTRIX scheme, they are
  51.    in X.  subdirectories of subdirectories of sys.  Under the TAYLOR
  52.    scheme, they are all in the subdirectory X. of a directory named
  53.    after the system.
  54.  
  55.    This means that for HDB, ULTRIX, SVR4 or TAYLOR, we have to search
  56.    directories of directories.  */
  57.  
  58. #if SPOOLDIR_V2 || SPOOLDIR_BSD42
  59. #define ZDIR "."
  60. #define SUBDIRS 0
  61. #endif
  62. #if SPOOLDIR_HDB || SPOOLDIR_SVR4 || SPOOLDIR_TAYLOR
  63. #define ZDIR "."
  64. #define SUBDIRS 1
  65. #endif
  66. #if SPOOLDIR_ULTRIX
  67. #define ZDIR "sys"
  68. #define SUBDIRS 1
  69. #endif
  70. #if SPOOLDIR_BSD43
  71. #define ZDIR "X."
  72. #define SUBDIRS 0
  73. #endif
  74.  
  75. /* Static variables for the execute file scan.  */
  76.  
  77. static DIR *qSxqt_topdir;
  78. #if ! SUBDIRS
  79. static const char *zSdir;
  80. #else /* SUBDIRS */
  81. static boolean fSone_dir;
  82. static char *zSdir;
  83. static DIR *qSxqt_dir;
  84. static char *zSsystem;
  85. #endif /* SUBDIRS */
  86.  
  87. /* Initialize the scan for execute files.  The function
  88.    usysdep_get_xqt_free will clear the data out when we are done with
  89.    the system.  This returns FALSE on error.  */
  90.  
  91. /*ARGSUSED*/
  92. boolean
  93. fsysdep_get_xqt_init (zsystem)
  94.      const char *zsystem;
  95. {
  96.   usysdep_get_xqt_free ((const char *) NULL);
  97.  
  98. #if SUBDIRS
  99.   if (zsystem != NULL)
  100.     {
  101. #if SPOOLDIR_HDB || SPOOLDIR_SVR4
  102.       zSdir = zbufcpy (zsystem);
  103. #endif
  104. #if SPOOLDIR_ULTRIX
  105.       zSdir = zsappend3 ("sys", zsystem, "X.");
  106. #endif
  107. #if SPOOLDIR_TAYLOR
  108.       zSdir = zsysdep_in_dir (zsystem, "X.");
  109. #endif
  110.  
  111.       qSxqt_dir = opendir ((char *) zSdir);
  112.       if (qSxqt_dir != NULL)
  113.     {
  114.       qSxqt_topdir = qSxqt_dir;
  115.       fSone_dir = TRUE;
  116.       zSsystem = zbufcpy (zsystem);
  117.       return TRUE;
  118.     }
  119.     }
  120.  
  121.   fSone_dir = FALSE;
  122. #endif
  123.  
  124.   qSxqt_topdir = opendir ((char *) ZDIR);
  125.   if (qSxqt_topdir == NULL)
  126.     {
  127.       if (errno == ENOENT)
  128.     return TRUE;
  129.       ulog (LOG_ERROR, "opendir (%s): %s", ZDIR, strerror (errno));
  130.       return FALSE;
  131.     }
  132.  
  133.   return TRUE;
  134. }
  135.  
  136. /* Return the name of the next execute file to read and process.  If
  137.    this returns NULL, *pferr must be checked.  If will be TRUE on
  138.    error, FALSE if there are no more files.  On a successful return
  139.    *pzsystem will be set to the system for which the execute file was
  140.    created.  */
  141.  
  142. /*ARGSUSED*/
  143. char *
  144. zsysdep_get_xqt (zsystem, pzsystem, pferr)
  145.      const char *zsystem;
  146.      char **pzsystem;
  147.      boolean *pferr;
  148. {
  149.   *pferr = FALSE;
  150.  
  151.   if (qSxqt_topdir == NULL)
  152.     return NULL;
  153.  
  154.   /* This loop continues until we find a file.  */
  155.   while (TRUE)
  156.     {
  157.       DIR *qdir;
  158.       struct dirent *q;
  159.  
  160. #if ! SUBDIRS
  161.       zSdir = ZDIR;
  162.       qdir = qSxqt_topdir;
  163. #else /* SUBDIRS */
  164.       /* This loop continues until we find a subdirectory to read.  */
  165.       while (qSxqt_dir == NULL)
  166.     {
  167.       struct dirent *qtop;
  168.  
  169.       qtop = readdir (qSxqt_topdir);
  170.       if (qtop == NULL)
  171.         {
  172.           (void) closedir (qSxqt_topdir);
  173.           qSxqt_topdir = NULL;
  174.           return NULL;
  175.         }
  176.  
  177.       /* No system name may start with a dot This allows us to
  178.          quickly skip impossible directories.  */
  179.       if (qtop->d_name[0] == '.')
  180.         continue;
  181.  
  182.       DEBUG_MESSAGE1 (DEBUG_SPOOLDIR,
  183.               "zsysdep_get_xqt: Found %s in top directory",
  184.               qtop->d_name);
  185.  
  186.       ubuffree (zSdir);
  187.  
  188. #if SPOOLDIR_HDB || SPOOLDIR_SVR4
  189.       zSdir = zbufcpy (qtop->d_name);
  190. #endif
  191. #if SPOOLDIR_ULTRIX
  192.       zSdir = zsappend3 ("sys", qtop->d_name, "X.");
  193. #endif
  194. #if SPOOLDIR_TAYLOR
  195.       zSdir = zsysdep_in_dir (qtop->d_name, "X.");
  196. #endif
  197.  
  198.       ubuffree (zSsystem);
  199.       zSsystem = zbufcpy (qtop->d_name);
  200.  
  201.       qSxqt_dir = opendir (zSdir);
  202.  
  203.       if (qSxqt_dir == NULL
  204.           && errno != ENOTDIR
  205.           && errno != ENOENT)
  206.         ulog (LOG_ERROR, "opendir (%s): %s", zSdir, strerror (errno));
  207.     }
  208.  
  209.       qdir = qSxqt_dir;
  210. #endif /* SUBDIRS */
  211.  
  212.       q = readdir (qdir);
  213.  
  214. #if DEBUG > 1
  215.       if (q != NULL)
  216.     DEBUG_MESSAGE2 (DEBUG_SPOOLDIR,
  217.             "zsysdep_get_xqt: Found %s in subdirectory %s",
  218.             q->d_name, zSdir);
  219. #endif
  220.  
  221.       /* If we've found an execute file, return it.  We have to get
  222.      the system name, which is easy for HDB or TAYLOR.  For other
  223.      spool directory schemes, we have to pull it out of the X.
  224.      file name; this would be insecure, except that zsfind_file
  225.      clobbers the file name to include the real system name.  */
  226.       if (q != NULL
  227.       && q->d_name[0] == 'X'
  228.       && q->d_name[1] == '.')
  229.     {
  230.       char *zret;
  231.  
  232. #if SPOOLDIR_HDB || SPOOLDIR_SVR4 || SPOOLDIR_TAYLOR
  233.       *pzsystem = zbufcpy (zSsystem);
  234. #else
  235.       {
  236.         size_t clen;
  237.  
  238.         clen = strlen (q->d_name) - 7;
  239.         *pzsystem = zbufalc (clen + 1);
  240.         memcpy (*pzsystem, q->d_name + 2, clen);
  241.         (*pzsystem)[clen] = '\0';
  242.       }
  243. #endif
  244.  
  245.       zret = zsysdep_in_dir (zSdir, q->d_name);
  246. #if DEBUG > 1
  247.       DEBUG_MESSAGE2 (DEBUG_SPOOLDIR,
  248.               "zsysdep_get_xqt: Returning %s (system %s)",
  249.               zret, *pzsystem);
  250. #endif
  251.       return zret;
  252.     }
  253.         
  254.       /* If we've reached the end of the directory, then if we are
  255.      using subdirectories loop around to read the next one,
  256.      otherwise we are finished.  */
  257.       if (q == NULL)
  258.     {
  259.       (void) closedir (qdir);
  260.  
  261. #if SUBDIRS
  262.       qSxqt_dir = NULL;
  263.       if (! fSone_dir)
  264.         continue;
  265. #endif
  266.  
  267.       qSxqt_topdir = NULL;
  268.       return NULL;
  269.     }
  270.     }
  271. }
  272.  
  273. /* Free up the results of an execute file scan, when we're done with
  274.    this system.  */
  275.  
  276. /*ARGSUSED*/
  277. void
  278. usysdep_get_xqt_free (zsystem)
  279.      const char *zsystem;
  280. {
  281.   if (qSxqt_topdir != NULL)
  282.     {
  283.       (void) closedir (qSxqt_topdir);
  284.       qSxqt_topdir = NULL;
  285.     }
  286. #if SUBDIRS
  287.   if (qSxqt_dir != NULL)
  288.     {
  289.       (void) closedir (qSxqt_dir);
  290.       qSxqt_dir = NULL;
  291.     }
  292.   ubuffree (zSdir);
  293.   zSdir = NULL;
  294.   ubuffree (zSsystem);
  295.   zSsystem = NULL;
  296.   fSone_dir = FALSE;
  297. #endif
  298. }
  299.