home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / uucp-1.04 / unix / spool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-13  |  13.7 KB  |  421 lines

  1. /* spool.c
  2.    Find a file in the spool directory.
  3.  
  4.    Copyright (C) 1991, 1992 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Infinity Development Systems, P.O. Box 520, Waltham, MA 02254.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char spool_rcsid[] = "$Id: spool.c,v 1.5 1992/10/08 04:21:04 ian Rel $";
  30. #endif
  31.  
  32. #include "uudefs.h"
  33. #include "sysdep.h"
  34. #include "system.h"
  35.  
  36. /* There are several types of files that go in the spool directory,
  37.    and they go into various different subdirectories.  Whenever the
  38.    system name LOCAL appears below, it means whatever the local system
  39.    name is.
  40.  
  41.    Command files
  42.    These contain instructions for uucico indicating what files to transfer
  43.    to and from what systems.  Each line of a work file is a command
  44.    beginning with S, R or X.
  45.    #if ! SPOOLDIR_TAYLOR
  46.    They are named C.ssssssgqqqq, where ssssss is the system name to
  47.    transfer to or from, g is the grade and qqqq is the sequence number.
  48.    #if SPOOLDIR_V2
  49.    They are put in the spool directory.
  50.    #elif SPOOLDIR_BSD42 || SPOOLDIR_BSD43
  51.    They are put in the directory "C.".
  52.    #elif SPOOLDIR_HDB
  53.    They are put in a directory named for the system for which they were
  54.    created.
  55.    #elif SPOOLDIR_ULTRIX
  56.    If the directory sys/ssssss exists, they are put in the directory
  57.    sys/ssssss/C; otherwise, they are put in the directory sys/DEFAULT/C.
  58.    #endif
  59.    #elif SPOOLDIR_SVR4
  60.    They are put in the directory sys/g, where sys is the system name
  61.    and g is the grade.
  62.    #endif
  63.    #else SPOOLDIR_TAYLOR
  64.    They are named C.gqqqq, where g is the grade and qqqq is the sequence
  65.    number, and are placed in the directory ssssss/C. where ssssss is
  66.    the system name to transfer to or from.
  67.    #endif
  68.  
  69.    Data files
  70.    There are files to be transferred to other systems.  Some files to
  71.    be transferred may not be in the spool directory, depending on how
  72.    uucp was invoked.  Data files are named in work files, so it is
  73.    never necessary to look at them directly (except to remove old ones);
  74.    it is only necessary to create them.  These means that the many
  75.    variations in naming are inconsequential.
  76.    #if ! SPOOLDIR_TAYLOR
  77.    They are named D.ssssssgqqqq where ssssss is a system name (which
  78.    may be LOCAL for locally initiated transfers or a remote system for
  79.    remotely initiated transfers, except that HDB appears to use the
  80.    system the file is being transferred to), g is the grade and qqqq
  81.    is the sequence number.  Some systems use a trailing subjob ID
  82.    number, but we currently do not.  The grade is not important, and
  83.    some systems do not use it.  If the data file is to become an
  84.    execution file on another system the grade (if present) will be
  85.    'X'.  Otherwise Ultrix appears to use 'b'; the uux included with
  86.    gnuucp 1.0 appears to use 'S'; SCO does not appear to use a grade,
  87.    although it does use a subjob ID number.
  88.    #if SPOOLDIR_V2
  89.    They are put in the spool directory.
  90.    #elif SPOOLDIR_BSD42
  91.    If the name begins with D.LOCAL, the file is put in the directory
  92.    D.LOCAL.  Otherwise the file is put in the directory D..
  93.    #elif SPOOLDIR_BSD43
  94.    If the name begins with D.LOCALX, the file is put in the directory
  95.    D.LOCALX.  Otherwise if the name begins with D.LOCAL, the file is
  96.    put in the directory D.LOCAL Otherwise the file is put in the
  97.    directory "D.".
  98.    #elif SPOOLDIR_HDB
  99.    They are put in a directory named for the system for which they
  100.    were created.
  101.    #elif SPOOLDIR_ULTRIX
  102.    Say the file is being transferred to system REMOTE.  If the
  103.    directory sys/REMOTE exists, then if the file begins with D.LOCALX
  104.    it is put in sys/REMOTE/D.LOCALX, if the file begins with D.LOCAL
  105.    it is put in sys/REMOTE/D.LOCAL, and otherwise it is put in
  106.    "sys/REMOTE/D.".  If the directory sys/REMOTE does not exist, the
  107.    same applies except that DEFAULT is used instead of REMOTE.
  108.    #elif SPOOLDIR_SVR4
  109.    They are put in the directory sys/g, where sys is the system name
  110.    and g is the grade.
  111.    #endif
  112.    #else SPOOLDIR_TAYLOR
  113.    If the file is to become an executable file on another system it is
  114.    named D.Xqqqq, otherwise it is named D.qqqq where in both cases
  115.    qqqq is a sequence number.  If the corresponding C. file is in
  116.    directory ssssss/C., a D.X file is placed in ssssss/D.X and a D.
  117.    file is placed in "ssssss/D.".
  118.    #endif
  119.  
  120.    Execute files
  121.    These are files that specify programs to be executed.  They are
  122.    created by uux, perhaps as run on another system.  These names are
  123.    important, because a file transfer done to an execute file name
  124.    causes an execution to occur.  The name is X.ssssssgqqqq, where
  125.    ssssss is the requesting system, g is the grade, and qqqq is a
  126.    sequence number.
  127.    #if SPOOLDIR_V2 || SPOOLDIR_BSD42
  128.    These files are placed in the spool directory.
  129.    #elif SPOOLDIR_BSD43
  130.    These files are placed in the directory X..
  131.    #elif SPOOLDIR_HDB || SPOOLDIR_SVR4
  132.    These files are put in a directory named for the system for which
  133.    the files were created.
  134.    #elif SPOOLDIR_ULTRIX
  135.    If there is a spool directory (sys/ssssss) for the requesting
  136.    system, the files are placed in sys/ssssss/X.; otherwise, the files
  137.    are placed in "sys/DEFAULT/X.".
  138.    #elif SPOOLDIR_TAYLOR
  139.    The system name is automatically truncated to seven characters when
  140.    a file is created.  The files are placed in the subdirectory X. of
  141.    a directory named for the system for which the files were created.
  142.    #endif
  143.  
  144.    Temporary receive files
  145.    These are used when receiving files from another system.  They are
  146.    later renamed to the final name.  The actual name is unimportant,
  147.    although it generally begins with TM..
  148.    #if SPOOLDIR_V2 || SPOOLDIR_BSD42
  149.    These files are placed in the spool directory.
  150.    #elif SPOOLDIR_BSD43 || SPOOLDIR_ULTRIX || SPOOLDIR_TAYLOR
  151.    These files are placed in the directory .Temp.
  152.    #elif SPOOLDIR_HDB || SPOOLDIR_SVR4
  153.    These files are placed in a directory named for the system for
  154.    which they were created.
  155.    #endif
  156.  
  157.    System status files
  158.    These are used to record when the last call was made to the system
  159.    and what the status is.  They are used to prevent frequent recalls
  160.    to a system which is not responding.  I will not attempt to
  161.    recreate the format of these exactly, since they are not all that
  162.    important.  They will be put in the directory .Status, as in HDB,
  163.    and they use the system name as the name of the file.
  164.  
  165.    Sequence file
  166.    This is used to generate a unique sequence number.  It contains an
  167.    ASCII number.
  168.    #if SPOOLDIR_V2 || SPOOLDIR_BSD42 || SPOOLDIR_BSD43
  169.    The file is named SEQF and is kept in the spool directory.
  170.    #elif SPOOLDIR_HDB || SPOOLDIR_SVR4
  171.    A separate sequence file is kept for each system in the directory
  172.    .Sequence with the name of the system.
  173.    #elif SPOOLDIR_ULTRIX
  174.    Each system with a file sys/ssssss has a sequence file in
  175.    sys/ssssss/.SEQF.  Other systems use sys/DEFAULT/.SEQF.
  176.    #else SPOOLDIR_TAYLOR
  177.    A sequence file named SEQF is kept in the directory ssssss for each
  178.    system.
  179.    #endif
  180.    */
  181.  
  182. /* Given the name of a file as specified in a UUCP command, and the
  183.    system for which this file has been created, return where to find
  184.    it in the spool directory.  The file will begin with C. (a command
  185.    file), D. (a data file) or X. (an execution file).  Under
  186.    SPOOLDIR_SVR4 we need to know the grade of the file created by the
  187.    local system; this is the bgrade argument, which is -1 for a file
  188.    from a remote system.  */
  189.  
  190. /*ARGSUSED*/
  191. char *
  192. zsfind_file (zsimple, zsystem, bgrade)
  193.      const char *zsimple;
  194.      const char *zsystem;
  195.      int bgrade;
  196. {
  197.   if (! fspool_file (zsimple))
  198.     {
  199.       ulog (LOG_ERROR, "Unrecognized file name %s", zsimple);
  200.       return NULL;
  201.     }
  202.  
  203. #if ! SPOOLDIR_HDB && ! SPOOLDIR_SVR4 && ! SPOOLDIR_TAYLOR
  204.   if (*zsimple == 'X')
  205.     {
  206.       size_t clen;
  207.  
  208.       /* Files beginning with X. are execute files.  It is important
  209.      for security reasons that we know the system which created
  210.      the X. file.  This is easy under SPOOLDIR_HDB or
  211.      SPOOLDIR_SVR4 SPOOLDIR_TAYLOR, because the file will be in a
  212.      directory named for the system.  Under other schemes, we must
  213.      get the system name from the X. file name.  To prevent
  214.      security violations, we set the system name directly here;
  215.      this will cause problems if the maximum file name length is
  216.      too short, but hopefully no problem will occur since any
  217.      System V systems will be using HDB or SVR4 or TAYLOR.  */
  218.       clen = strlen (zsimple);
  219.       if (clen <= 7 || strncmp (zsimple + 2, zsystem, clen - 7) != 0)
  220.     {
  221.       static char *zbuf;
  222.       static size_t cbuf;
  223.       size_t cwant;
  224.  
  225.       cwant = strlen (zsystem) + 8;
  226.       if (cwant > cbuf)
  227.         {
  228.           zbuf = (char *) xrealloc ((pointer) zbuf, cwant);
  229.           cbuf = cwant;
  230.         }
  231.       sprintf (zbuf, "X.%s%s", zsystem,
  232.            clen < 5 ? zsimple : zsimple + clen - 5);
  233.       zsimple = zbuf;
  234.     }
  235.     }
  236. #endif /* ! SPOOLDIR_HDB && ! SPOOLDIR_SVR4 && ! SPOOLDIR_TAYLOR */
  237.  
  238. #if SPOOLDIR_V2
  239.   /* V2 never uses subdirectories.  */
  240.   return zbufcpy (zsimple);
  241. #endif /* SPOOLDIR_V2 */
  242.  
  243. #if SPOOLDIR_HDB
  244.   /* HDB always uses the system name as a directory.  */
  245.   return zsysdep_in_dir (zsystem, zsimple);
  246. #endif /* SPOOLDIR_HDB */
  247.  
  248. #if SPOOLDIR_SVR4
  249.   /* SVR4 uses grade directories within the system directory for local
  250.      command and data files.  */
  251.   if (bgrade < 0 || *zsimple == 'X')
  252.     return zsysdep_in_dir (zsystem, zsimple);
  253.   else
  254.     {
  255.       char abgrade[2];
  256.  
  257.       abgrade[0] = bgrade;
  258.       abgrade[1] = '\0';
  259.       return zsappend3 (zsystem, abgrade, zsimple);
  260.     }
  261. #endif /* SPOOLDIR_SVR4 */
  262.  
  263. #if ! SPOOLDIR_V2 && ! SPOOLDIR_HDB && ! SPOOLDIR_SVR4
  264.   switch (*zsimple)
  265.     {
  266.     case 'C':
  267. #if SPOOLDIR_BSD42 || SPOOLDIR_BSD43
  268.       return zsysdep_in_dir ("C.", zsimple);
  269. #endif /* SPOOLDIR_BSD42 || SPOOLDIR_BSD43 */
  270. #if SPOOLDIR_ULTRIX
  271.       if (fsultrix_has_spool (zsystem))
  272.     return zsappend4 ("sys", zsystem, "C.", zsimple);
  273.       else
  274.     return zsappend4 ("sys", "DEFAULT", "C.", zsimple);
  275. #endif /* SPOOLDIR_ULTRIX */
  276. #if SPOOLDIR_TAYLOR
  277.       return zsappend3 (zsystem, "C.", zsimple);
  278. #endif /* SPOOLDIR_TAYLOR */
  279.  
  280.     case 'D':
  281. #if SPOOLDIR_BSD42 || SPOOLDIR_BSD43
  282.       {
  283.     size_t c;
  284.     boolean ftruncated;
  285.       
  286.     /* D.LOCAL in D.LOCAL/, others in D./.  If BSD43, D.LOCALX in
  287.        D.LOCALX/.  */
  288.     ftruncated = TRUE;
  289.     if (strncmp (zsimple + 2, zSlocalname, strlen (zSlocalname)) == 0)
  290.       {
  291.         c = strlen (zSlocalname);
  292.         ftruncated = FALSE;
  293.       }
  294.     else if (strncmp (zsimple + 2, zSlocalname, 7) == 0)
  295.       c = 7;
  296.     else if (strncmp (zsimple + 2, zSlocalname, 6) == 0)
  297.       c = 6;
  298.     else
  299.       c = 0;
  300. #if SPOOLDIR_BSD43
  301.     if (c > 0 && zsimple[c + 2] == 'X')
  302.       c++;
  303. #endif /* SPOOLDIR_BSD43 */
  304.     if (c > 0)
  305.       {
  306.         char *zalloc;
  307.  
  308.         zalloc = zbufalc (c + 3);
  309.         memcpy (zalloc, zsimple, c + 2);
  310.         zalloc[c + 2] = '\0';
  311.  
  312.         /* If we truncated the system name, and there is no existing
  313.            directory with the truncated name, then just use D..  */
  314.         if (! ftruncated || fsysdep_directory (zalloc))
  315.           {
  316.         char *zret;
  317.  
  318.         zret = zsysdep_in_dir (zalloc, zsimple);
  319.         ubuffree (zalloc);
  320.         return zret;
  321.           }
  322.         ubuffree (zalloc);
  323.       }
  324.     return zsysdep_in_dir ("D.", zsimple);
  325.       }
  326. #endif /* SPOOLDIR_BSD42 || SPOOLDIR_BSD43 */
  327. #if SPOOLDIR_ULTRIX
  328.       {
  329.     size_t c;
  330.     boolean ftruncated;
  331.     char *zfree;
  332.     const char *zdir;
  333.     char *zret;
  334.       
  335.     /* D.LOCALX in D.LOCALX/, D.LOCAL in D.LOCAL/, others in D./.  */
  336.     ftruncated = TRUE;
  337.     if (strncmp (zsimple + 2, zSlocalname, strlen (zSlocalname)) == 0)
  338.       {
  339.         c = strlen (zSlocalname);
  340.         ftruncated = FALSE;
  341.       }
  342.     else if (strncmp (zsimple + 2, zSlocalname, 7) == 0)
  343.       c = 7;
  344.     else if (strncmp (zsimple + 2, zSlocalname, 6) == 0)
  345.       c = 6;
  346.     else
  347.       c = 0;
  348.     if (c > 0 && zsimple[c + 2] == 'X')
  349.       ++c;
  350.     if (c > 0)
  351.       {
  352.         zfree = zbufalc (c + 3);
  353.         memcpy (zfree, zsimple, c + 2);
  354.         zfree[c + 2] = '\0';
  355.         zdir = zfree;
  356.  
  357.         /* If we truncated the name, and there is no directory for
  358.            the truncated name, then don't use it.  */
  359.         if (ftruncated)
  360.           {
  361.         char *zlook;
  362.  
  363.         zlook = zsappend3 ("sys",
  364.                    (fsultrix_has_spool (zsystem)
  365.                     ? zsystem
  366.                     : "DEFAULT"),
  367.                    zdir);
  368.         if (! fsysdep_directory (zlook))
  369.           zdir = "D.";
  370.         ubuffree (zlook);
  371.           }
  372.       }
  373.     else
  374.       {
  375.         zfree = NULL;
  376.         zdir = "D.";
  377.       }
  378.       
  379.     zret = zsappend4 ("sys",
  380.               (fsultrix_has_spool (zsystem)
  381.                ? zsystem
  382.                : "DEFAULT"),
  383.               zdir,
  384.               zsimple);
  385.     ubuffree (zfree);
  386.     return zret;
  387.       }
  388. #endif /* SPOOLDIR_ULTRIX */
  389. #if SPOOLDIR_TAYLOR
  390.       if (zsimple[2] == 'X')
  391.     return zsappend3 (zsystem, "D.X", zsimple);
  392.       else
  393.     return zsappend3 (zsystem, "D.", zsimple);
  394. #endif /* SPOOLDIR_TAYLOR */
  395.  
  396.  
  397.     case 'X':
  398. #if SPOOLDIR_BSD42
  399.       return zbufcpy (zsimple);
  400. #endif
  401. #if SPOOLDIR_BSD43
  402.       return zsysdep_in_dir ("X.", zsimple);
  403. #endif
  404. #if SPOOLDIR_ULTRIX
  405.       return zsappend4 ("sys",
  406.             (fsultrix_has_spool (zsystem)
  407.              ? zsystem
  408.              : "DEFAULT"),
  409.             "X.",
  410.             zsimple);
  411. #endif
  412. #if SPOOLDIR_TAYLOR
  413.       return zsappend3 (zsystem, "X.", zsimple);
  414. #endif
  415.     }
  416.  
  417.   /* This is just to avoid warnings; it will never be executed.  */
  418.   return NULL;
  419. #endif /* ! SPOOLDIR_V2 && ! SPOOLDIR_HDB && ! SPOOLDIR_SVR4 */
  420. }
  421.