#include "uucp.h"
#if USE_RCS_ID
const char picksb_rcsid[] = "$Id: picksb.c,v 1.13 2002/03/05 19:10:42 ian Rel $";
#endif
#include "uudefs.h"
#include "system.h"
#include "sysdep.h"
#include <errno.h>
#include <pwd.h>
#if HAVE_OPENDIR
#if HAVE_DIRENT_H
#include <dirent.h>
#else
#include <sys/dir.h>
#define dirent direct
#endif
#endif
#if GETPWUID_DECLARATION_OK
#ifndef getpwuid
extern struct passwd *getpwuid ();
#endif
#endif
static DIR *qStopdir;
static char *zStopdir;
static DIR *qSsysdir;
static char *zSsysdir;
boolean
fsysdep_uupick_init (zsystem, zpubdir)
const char *zsystem ATTRIBUTE_UNUSED;
const char *zpubdir;
{
const char *zuser;
zuser = zsysdep_login_name ();
zStopdir = (char *) xmalloc (strlen (zpubdir)
+ sizeof "/receive/"
+ strlen (zuser));
sprintf (zStopdir, "%s/receive/%s", zpubdir, zuser);
qStopdir = opendir (zStopdir);
if (qStopdir == NULL && errno != ENOENT)
{
ulog (LOG_ERROR, "opendir (%s): %s", zStopdir,
strerror (errno));
return FALSE;
}
qSsysdir = NULL;
return TRUE;
}
char *
zsysdep_uupick (zsysarg, zpubdir, pzfrom, pzfull)
const char *zsysarg;
const char *zpubdir ATTRIBUTE_UNUSED;
char **pzfrom;
char **pzfull;
{
struct dirent *qentry;
while (TRUE)
{
while (qSsysdir == NULL)
{
const char *zsystem;
char *zdir;
if (qStopdir == NULL)
return NULL;
if (zsysarg != NULL)
{
closedir (qStopdir);
qStopdir = NULL;
zsystem = zsysarg;
}
else
{
do
{
qentry = readdir (qStopdir);
if (qentry == NULL)
{
closedir (qStopdir);
qStopdir = NULL;
return NULL;
}
}
while (strcmp (qentry->d_name, ".") == 0
|| strcmp (qentry->d_name, "..") == 0);
zsystem = qentry->d_name;
}
zdir = zbufalc (strlen (zStopdir) + strlen (zsystem) + sizeof "/");
sprintf (zdir, "%s/%s", zStopdir, zsystem);
qSsysdir = opendir (zdir);
if (qSsysdir == NULL)
{
if (errno != ENOENT && errno != ENOTDIR)
ulog (LOG_ERROR, "opendir (%s): %s", zdir, strerror (errno));
}
else
{
ubuffree (zSsysdir);
zSsysdir = zbufcpy (zsystem);
}
ubuffree (zdir);
}
qentry = readdir (qSsysdir);
if (qentry == NULL)
{
closedir (qSsysdir);
qSsysdir = NULL;
continue;
}
if (strcmp (qentry->d_name, ".") == 0
|| strcmp (qentry->d_name, "..") == 0)
continue;
*pzfrom = zbufcpy (zSsysdir);
*pzfull = zsappend3 (zStopdir, zSsysdir, qentry->d_name);
return zbufcpy (qentry->d_name);
}
}
boolean
fsysdep_uupick_free (zsystem, zpubdir)
const char *zsystem ATTRIBUTE_UNUSED;
const char *zpubdir ATTRIBUTE_UNUSED;
{
xfree ((pointer) zStopdir);
if (qStopdir != NULL)
{
closedir (qStopdir);
qStopdir = NULL;
}
ubuffree (zSsysdir);
zSsysdir = NULL;
if (qSsysdir != NULL)
{
closedir (qSsysdir);
qSsysdir = NULL;
}
return TRUE;
}
char *
zsysdep_uupick_local_file (zfile, pfbadname)
const char *zfile;
boolean *pfbadname;
{
struct passwd *q;
if (pfbadname != NULL)
*pfbadname = FALSE;
if (zfile[0] != '~'
|| (zfile[1] != '/' && zfile[1] != '\0'))
return zsysdep_local_file_cwd (zfile, (const char *) NULL, pfbadname);
q = getpwuid (getuid ());
if (q == NULL)
{
ulog (LOG_ERROR, "Can't get home directory");
return NULL;
}
if (zfile[1] == '\0')
return zbufcpy (q->pw_dir);
return zsysdep_in_dir (q->pw_dir, zfile + 2);
}