#include "uucp.h"
#if USE_RCS_ID
const char xqtfil_rcsid[] = "$Id: xqtfil.c,v 1.13 2002/03/05 19:10:42 ian Rel $";
#endif
#include "uudefs.h"
#include "sysdep.h"
#include "system.h"
#include <errno.h>
#if HAVE_OPENDIR
#if HAVE_DIRENT_H
#include <dirent.h>
#else
#include <sys/dir.h>
#define dirent direct
#endif
#endif
#if SPOOLDIR_V2 || SPOOLDIR_BSD42
#define ZDIR "."
#define SUBDIRS 0
#endif
#if SPOOLDIR_HDB || SPOOLDIR_SVR4 || SPOOLDIR_TAYLOR
#define ZDIR "."
#define SUBDIRS 1
#endif
#if SPOOLDIR_ULTRIX
#define ZDIR "sys"
#define SUBDIRS 1
#endif
#if SPOOLDIR_BSD43
#define ZDIR "X."
#define SUBDIRS 0
#endif
static DIR *qSxqt_topdir;
#if ! SUBDIRS
static const char *zSdir;
#else
static boolean fSone_dir;
static char *zSdir;
static DIR *qSxqt_dir;
static char *zSsystem;
#endif
boolean
fsysdep_get_xqt_init (zsystem)
const char *zsystem;
{
usysdep_get_xqt_free ((const char *) NULL);
#if SUBDIRS
if (zsystem != NULL)
{
#if SPOOLDIR_HDB || SPOOLDIR_SVR4
zSdir = zbufcpy (zsystem);
#endif
#if SPOOLDIR_ULTRIX
zSdir = zsappend3 ("sys", zsystem, "X.");
#endif
#if SPOOLDIR_TAYLOR
zSdir = zsysdep_in_dir (zsystem, "X.");
#endif
qSxqt_dir = opendir ((char *) zSdir);
if (qSxqt_dir != NULL)
{
qSxqt_topdir = qSxqt_dir;
fSone_dir = TRUE;
zSsystem = zbufcpy (zsystem);
return TRUE;
}
}
fSone_dir = FALSE;
#endif
qSxqt_topdir = opendir ((char *) ZDIR);
if (qSxqt_topdir == NULL)
{
if (errno == ENOENT)
return TRUE;
ulog (LOG_ERROR, "opendir (%s): %s", ZDIR, strerror (errno));
return FALSE;
}
return TRUE;
}
char *
zsysdep_get_xqt (zsystem, pzsystem, pferr)
const char *zsystem ATTRIBUTE_UNUSED;
char **pzsystem;
boolean *pferr;
{
*pferr = FALSE;
if (qSxqt_topdir == NULL)
return NULL;
while (TRUE)
{
DIR *qdir;
struct dirent *q;
#if ! SUBDIRS
zSdir = ZDIR;
qdir = qSxqt_topdir;
#else
while (qSxqt_dir == NULL)
{
struct dirent *qtop;
qtop = readdir (qSxqt_topdir);
if (qtop == NULL)
{
(void) closedir (qSxqt_topdir);
qSxqt_topdir = NULL;
return NULL;
}
if (qtop->d_name[0] == '.')
continue;
DEBUG_MESSAGE1 (DEBUG_SPOOLDIR,
"zsysdep_get_xqt: Found %s in top directory",
qtop->d_name);
ubuffree (zSdir);
#if SPOOLDIR_HDB || SPOOLDIR_SVR4
zSdir = zbufcpy (qtop->d_name);
#endif
#if SPOOLDIR_ULTRIX
zSdir = zsappend3 ("sys", qtop->d_name, "X.");
#endif
#if SPOOLDIR_TAYLOR
zSdir = zsysdep_in_dir (qtop->d_name, "X.");
#endif
ubuffree (zSsystem);
zSsystem = zbufcpy (qtop->d_name);
qSxqt_dir = opendir (zSdir);
if (qSxqt_dir == NULL
&& errno != ENOTDIR
&& errno != ENOENT)
ulog (LOG_ERROR, "opendir (%s): %s", zSdir, strerror (errno));
}
qdir = qSxqt_dir;
#endif
q = readdir (qdir);
#if DEBUG > 1
if (q != NULL)
DEBUG_MESSAGE2 (DEBUG_SPOOLDIR,
"zsysdep_get_xqt: Found %s in subdirectory %s",
q->d_name, zSdir);
#endif
if (q != NULL
&& q->d_name[0] == 'X'
&& q->d_name[1] == '.')
{
char *zret;
#if SPOOLDIR_HDB || SPOOLDIR_SVR4 || SPOOLDIR_TAYLOR
*pzsystem = zbufcpy (zSsystem);
#else
{
size_t clen;
clen = strlen (q->d_name) - 7;
*pzsystem = zbufalc (clen + 1);
memcpy (*pzsystem, q->d_name + 2, clen);
(*pzsystem)[clen] = '\0';
}
#endif
zret = zsysdep_in_dir (zSdir, q->d_name);
#if DEBUG > 1
DEBUG_MESSAGE2 (DEBUG_SPOOLDIR,
"zsysdep_get_xqt: Returning %s (system %s)",
zret, *pzsystem);
#endif
return zret;
}
if (q == NULL)
{
(void) closedir (qdir);
#if SUBDIRS
qSxqt_dir = NULL;
if (! fSone_dir)
continue;
#endif
qSxqt_topdir = NULL;
return NULL;
}
}
}
void
usysdep_get_xqt_free (zsystem)
const char *zsystem ATTRIBUTE_UNUSED;
{
if (qSxqt_topdir != NULL)
{
(void) closedir (qSxqt_topdir);
qSxqt_topdir = NULL;
}
#if SUBDIRS
if (qSxqt_dir != NULL)
{
(void) closedir (qSxqt_dir);
qSxqt_dir = NULL;
}
ubuffree (zSdir);
zSdir = NULL;
ubuffree (zSsystem);
zSsystem = NULL;
fSone_dir = FALSE;
#endif
}