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

  1. /* picksb.c
  2.    System dependent routines for uupick.
  3.  
  4.    Copyright (C) 1992, 1993 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 picksb_rcsid[] = "$Id: picksb.c,v 1.10 1995/06/21 19:19:54 ian Rel $";
  30. #endif
  31.  
  32. #include "uudefs.h"
  33. #include "system.h"
  34. #include "sysdep.h"
  35.  
  36. #include <errno.h>
  37. #include <pwd.h>
  38.  
  39. #if HAVE_OPENDIR
  40. #if HAVE_DIRENT_H
  41. #include <dirent.h>
  42. #else /* ! HAVE_DIRENT_H */
  43. #include <sys/dir.h>
  44. #define dirent direct
  45. #endif /* ! HAVE_DIRENT_H */
  46. #endif /* HAVE_OPENDIR */
  47.  
  48. #if GETPWUID_DECLARATION_OK
  49. #ifndef getpwuid
  50. extern struct passwd *getpwuid ();
  51. #endif
  52. #endif
  53.  
  54. /* Local variables.  */
  55.  
  56. /* Directory of ~/receive/USER.  */
  57. static DIR *qStopdir;
  58.  
  59. /* Name of ~/receive/USER.  */
  60. static char *zStopdir;
  61.  
  62. /* Directory of ~/receive/USER/SYSTEM.  */
  63. static DIR *qSsysdir;
  64.  
  65. /* Name of system.  */
  66. static char *zSsysdir;
  67.  
  68. /* Prepare to get a list of all the file to uupick for this user.  */
  69.  
  70. /*ARGSUSED*/
  71. boolean
  72. fsysdep_uupick_init (zsystem, zpubdir)
  73.      const char *zsystem;
  74.      const char *zpubdir;
  75. {
  76.   const char *zuser;
  77.  
  78.   zuser = zsysdep_login_name ();
  79.  
  80.   zStopdir = (char *) xmalloc (strlen (zpubdir)
  81.                    + sizeof "/receive/"
  82.                    + strlen (zuser));
  83.   sprintf (zStopdir, "%s/receive/%s", zpubdir, zuser);
  84.  
  85.   qStopdir = opendir (zStopdir);
  86.   if (qStopdir == NULL && errno != ENOENT)
  87.     {
  88.       ulog (LOG_ERROR, "opendir (%s): %s", zStopdir,
  89.         strerror (errno));
  90.       return FALSE;
  91.     }
  92.  
  93.   qSsysdir = NULL;
  94.  
  95.   return TRUE;
  96. }
  97.  
  98. /* Return the next file from the uupick directories.  */
  99.  
  100. /*ARGSUSED*/
  101. char *
  102. zsysdep_uupick (zsysarg, zpubdir, pzfrom, pzfull)
  103.      const char *zsysarg;
  104.      const char *zpubdir;
  105.      char **pzfrom;
  106.      char **pzfull;
  107. {
  108.   struct dirent *qentry;
  109.  
  110.   while (TRUE)
  111.     {
  112.       while (qSsysdir == NULL)
  113.     {
  114.       const char *zsystem;
  115.       char *zdir;
  116.  
  117.       if (qStopdir == NULL)
  118.         return NULL;
  119.  
  120.       if (zsysarg != NULL)
  121.         {
  122.           closedir (qStopdir);
  123.           qStopdir = NULL;
  124.           zsystem = zsysarg;
  125.         }
  126.       else
  127.         {
  128.           do
  129.         {
  130.           qentry = readdir (qStopdir);
  131.           if (qentry == NULL)
  132.             {
  133.               closedir (qStopdir);
  134.               qStopdir = NULL;
  135.               return NULL;
  136.             }
  137.         }
  138.           while (strcmp (qentry->d_name, ".") == 0
  139.              || strcmp (qentry->d_name, "..") == 0);
  140.  
  141.           zsystem = qentry->d_name;
  142.         }
  143.  
  144.       zdir = zbufalc (strlen (zStopdir) + strlen (zsystem) + sizeof "/");
  145.       sprintf (zdir, "%s/%s", zStopdir, zsystem);
  146.  
  147.       qSsysdir = opendir (zdir);
  148.       if (qSsysdir == NULL)
  149.         {
  150.           if (errno != ENOENT && errno != ENOTDIR)
  151.         ulog (LOG_ERROR, "opendir (%s): %s", zdir, strerror (errno));
  152.         }
  153.       else
  154.         {
  155.           ubuffree (zSsysdir);
  156.           zSsysdir = zbufcpy (zsystem);
  157.         }
  158.  
  159.       ubuffree (zdir);
  160.     }
  161.  
  162.       qentry = readdir (qSsysdir);
  163.       if (qentry == NULL)
  164.     {
  165.       closedir (qSsysdir);
  166.       qSsysdir = NULL;
  167.       continue;
  168.     }
  169.  
  170.       if (strcmp (qentry->d_name, ".") == 0
  171.       || strcmp (qentry->d_name, "..") == 0)
  172.     continue;
  173.  
  174.       *pzfrom = zbufcpy (zSsysdir);
  175.       *pzfull = zsappend3 (zStopdir, zSsysdir, qentry->d_name);
  176.       return zbufcpy (qentry->d_name);
  177.     }
  178. }
  179.  
  180. /*ARGSUSED*/
  181. boolean
  182. fsysdep_uupick_free (zsystem, zpubdir)
  183.      const char *zsystem;
  184.      const char *zpubdir;
  185. {
  186.   xfree ((pointer) zStopdir);
  187.   if (qStopdir != NULL)
  188.     {
  189.       closedir (qStopdir);
  190.       qStopdir = NULL;
  191.     }
  192.   ubuffree (zSsysdir);
  193.   zSsysdir = NULL;
  194.   if (qSsysdir != NULL)
  195.     {
  196.       closedir (qSsysdir);
  197.       qSsysdir = NULL;
  198.     }
  199.  
  200.   return TRUE;
  201. }
  202.  
  203. /* Expand a local file name for uupick.  */
  204.  
  205. char *
  206. zsysdep_uupick_local_file (zfile, pfbadname)
  207.      const char *zfile;
  208.      boolean *pfbadname;
  209. {
  210.   struct passwd *q;
  211.  
  212.   if (pfbadname != NULL)
  213.     *pfbadname = FALSE;
  214.  
  215.   /* If this does not start with a simple ~, pass it to
  216.      zsysdep_local_file_cwd; as it happens, zsysdep_local_file_cwd
  217.      only uses the zpubdir argument if the file starts with a simple
  218.      ~, so it doesn't really matter what we pass for zpubdir.  */
  219.   if (zfile[0] != '~'
  220.       || (zfile[1] != '/' && zfile[1] != '\0'))
  221.     return zsysdep_local_file_cwd (zfile, (const char *) NULL, pfbadname);
  222.   
  223.   q = getpwuid (getuid ());
  224.   if (q == NULL)
  225.     {
  226.       ulog (LOG_ERROR, "Can't get home directory");
  227.       return NULL;
  228.     }
  229.  
  230.   if (zfile[1] == '\0')
  231.     return zbufcpy (q->pw_dir);
  232.  
  233.   return zsysdep_in_dir (q->pw_dir, zfile + 2);
  234. }
  235.