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

  1. /* filnam.c
  2.    Get names to use for UUCP 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. #include "uudefs.h"
  29. #include "uuconf.h"
  30. #include "sysdep.h"
  31. #include "system.h"
  32.  
  33. #include <errno.h>
  34.  
  35. #if HAVE_FCNTL_H
  36. #include <fcntl.h>
  37. #else
  38. #if HAVE_SYS_FILE_H
  39. #include <sys/file.h>
  40. #endif
  41. #endif
  42.  
  43. #ifndef O_RDONLY
  44. #define O_RDONLY 0
  45. #define O_WRONLY 1
  46. #define O_RDWR 2
  47. #endif
  48.  
  49. #ifndef O_NOCTTY
  50. #define O_NOCTTY 0
  51. #endif
  52.  
  53. /* We need a definition for SEEK_SET.  */
  54.  
  55. #ifndef SEEK_SET
  56. #define SEEK_SET 0
  57. #endif
  58.  
  59. /* We use POSIX style fcntl locks if they are available, unless
  60.    O_CREAT is not defined.  We could use them in the latter case, but
  61.    the code would have to become more complex to avoid races
  62.    concerning the use of creat.  It is very unlikely that there is any
  63.    system which does have POSIX style locking but does not have
  64.    O_CREAT.  */
  65. #if ! HAVE_BROKEN_SETLKW
  66. #ifdef F_SETLKW
  67. #ifdef O_CREAT
  68. #define USE_POSIX_LOCKS 1
  69. #endif
  70. #endif
  71. #endif
  72. #ifndef USE_POSIX_LOCKS
  73. #define USE_POSIX_LOCKS 0
  74. #endif
  75.  
  76. /* External functions.  */
  77. #ifndef lseek
  78. extern off_t lseek ();
  79. #endif
  80.  
  81. #define ZCHARS \
  82.   "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  83.  
  84. /* Local functions.  */
  85.  
  86. static boolean fscmd_seq P((const char *zsystem, char *zseq));
  87. static char *zsfile_name P((int btype, const char *zsystem,
  88.                 const char *zlocalname, int bgrade,
  89.                 boolean fxqt, char *ztname, char *zdname,
  90.                 char *zxname));
  91.  
  92. /* Get a new command sequence number (this is not a sequence number to
  93.    be used for communicating with another system, but a sequence
  94.    number to be used when generating the name of a command file).
  95.    The sequence number is placed into zseq, which should be five
  96.    characters long.  */
  97.  
  98. static boolean
  99. fscmd_seq (zsystem, zseq)
  100.      const char *zsystem;
  101.      char *zseq;
  102. {
  103.   int cdelay;
  104.   char *zfree;
  105.   const char *zfile;
  106.   int o;
  107.   boolean flockfile;
  108.   int i;
  109.   boolean fret;
  110.  
  111.   cdelay = 5;
  112.  
  113. #if ! USE_POSIX_LOCKS
  114.   {
  115.     boolean ferr;
  116.  
  117.     /* Lock the sequence file.  */
  118.     while (! fsdo_lock ("LCK..SEQ", TRUE, &ferr))
  119.       {
  120.     if (ferr || FGOT_SIGNAL ())
  121.       return FALSE;
  122.     sleep (cdelay);
  123.     if (cdelay < 60)
  124.       ++cdelay;
  125.       }
  126.   }
  127. #endif
  128.  
  129.   zfree = NULL;
  130.  
  131. #if SPOOLDIR_V2 || SPOOLDIR_BSD42 || SPOOLDIR_BSD43
  132.   zfile = "SEQF";
  133. #endif
  134. #if SPOOLDIR_HDB || SPOOLDIR_SVR4
  135.   zfree = zsysdep_in_dir (".Sequence", zsystem);
  136.   zfile = zfree;
  137. #endif
  138. #if SPOOLDIR_ULTRIX
  139.   if (! fsultrix_has_spool (zsystem))
  140.     zfile = "sys/DEFAULT/.SEQF";
  141.   else
  142.     {
  143.       zfree = zsappend3 ("sys", zsystem, ".SEQF");
  144.       zfile = zfree;
  145.     }
  146. #endif /* SPOOLDIR_ULTRIX */
  147. #if SPOOLDIR_TAYLOR
  148.   zfree = zsysdep_in_dir (zsystem, "SEQF");
  149.   zfile = zfree;
  150. #endif /* SPOOLDIR_TAYLOR */
  151.  
  152. #ifdef O_CREAT
  153.   o = open ((char *) zfile, O_RDWR | O_CREAT | O_NOCTTY, IPRIVATE_FILE_MODE);
  154. #else
  155.   o = open ((char *) zfile, O_RDWR | O_NOCTTY);
  156.   if (o < 0 && errno == ENOENT)
  157.     {
  158.       o = creat ((char *) zfile, IPRIVATE_FILE_MODE);
  159.       if (o >= 0)
  160.     {
  161.       (void) close (o);
  162.       o = open ((char *) zfile, O_RDWR | O_NOCTTY);
  163.     }
  164.     }
  165. #endif
  166.  
  167.   if (o < 0)
  168.     {
  169.       if (errno == ENOENT)
  170.     {
  171.       if (! fsysdep_make_dirs (zfile, FALSE))
  172.         {
  173. #if ! USE_POSIX_LOCKS
  174.           (void) fsdo_unlock ("LCK..SEQ", TRUE);
  175. #endif
  176.           return FALSE;
  177.         }
  178. #ifdef O_CREAT
  179.       o = open ((char *) zfile,
  180.             O_RDWR | O_CREAT | O_NOCTTY,
  181.             IPRIVATE_FILE_MODE);
  182. #else
  183.       o = creat ((char *) zfile, IPRIVATE_FILE_MODE);
  184.       if (o >= 0)
  185.         {
  186.           (void) close (o);
  187.           o = open ((char *) zfile, O_RDWR | O_NOCTTY);
  188.         }
  189. #endif
  190.     }
  191.       if (o < 0)
  192.     {
  193.       ulog (LOG_ERROR, "open (%s): %s", zfile, strerror (errno));
  194. #if ! USE_POSIX_LOCKS
  195.       (void) fsdo_unlock ("LCK..SEQ", TRUE);
  196. #endif
  197.       return FALSE;
  198.     }
  199.     }
  200.  
  201. #if ! USE_POSIX_LOCKS
  202.   flockfile = TRUE;
  203. #else
  204.   {
  205.     struct flock slock;
  206.  
  207.     flockfile = FALSE;
  208.  
  209.     slock.l_type = F_WRLCK;
  210.     slock.l_whence = SEEK_SET;
  211.     slock.l_start = 0;
  212.     slock.l_len = 0;
  213.     while (fcntl (o, F_SETLKW, &slock) == -1)
  214.       {
  215.     boolean fagain;
  216.  
  217.     /* Some systems define F_SETLKW, but it does not work.  We try
  218.            to catch those systems at runtime, and revert to using a
  219.            lock file.  */
  220.     if (errno == EINVAL)
  221.       {
  222.         boolean ferr;
  223.  
  224.         /* Lock the sequence file.  */
  225.         while (! fsdo_lock ("LCK..SEQ", TRUE, &ferr))
  226.           {
  227.         if (ferr || FGOT_SIGNAL ())
  228.           {
  229.             (void) close (o);
  230.             return FALSE;
  231.           }
  232.         sleep (cdelay);
  233.         if (cdelay < 60)
  234.           ++cdelay;
  235.           }
  236.  
  237.         flockfile = TRUE;
  238.  
  239.         break;
  240.       }
  241.  
  242.     fagain = FALSE;
  243.     if (errno == ENOMEM)
  244.       fagain = TRUE;
  245. #ifdef ENOLCK
  246.     if (errno == ENOLCK)
  247.       fagain = TRUE;
  248. #endif
  249. #ifdef ENOSPC
  250.     if (errno == ENOSPC)
  251.       fagain = TRUE;
  252. #endif
  253.     if (fagain)
  254.       {
  255.         sleep (cdelay);
  256.         if (cdelay < 60)
  257.           ++cdelay;
  258.         continue;
  259.       }
  260.     ulog (LOG_ERROR, "Locking %s: %s", zfile, strerror (errno));
  261.     (void) close (o);
  262.     return FALSE;
  263.       }
  264.   }
  265. #endif
  266.  
  267.   if (read (o, zseq, CSEQLEN) != CSEQLEN)
  268.     strcpy (zseq, "0000");
  269.   zseq[CSEQLEN] = '\0';
  270.  
  271.   /* We must add one to the sequence number and return the new value.
  272.      On Ultrix, arbitrary characters are allowed in the sequence
  273.      number.  On other systems, the sequence number apparently must be
  274.      in hex.  */
  275. #if SPOOLDIR_V2 || SPOOLDIR_BSD42 || SPOOLDIR_BSD43 || SPOOLDIR_HDB || SPOOLDIR_SVR4
  276.   i = (int) strtol (zseq, (char **) NULL, 16);
  277.   ++i;
  278.   if (i > 0xffff)
  279.     i = 0;
  280.   /* The sprintf argument has CSEQLEN built into it.  */
  281.   sprintf (zseq, "%04x", (unsigned int) i);
  282. #endif
  283. #if SPOOLDIR_ULTRIX || SPOOLDIR_TAYLOR
  284.   for (i = CSEQLEN - 1; i >= 0; i--)
  285.     {
  286.       const char *zdig;
  287.  
  288.       zdig = strchr (ZCHARS, zseq[i]);
  289.       if (zdig == NULL || zdig[0] == '\0' || zdig[1] == '\0')
  290.     zseq[i] = '0';
  291.       else
  292.     {
  293.       zseq[i] = zdig[1];
  294.       break;
  295.     }
  296.     }
  297. #endif /* SPOOLDIR_ULTRIX || SPOOLDIR_TAYLOR */
  298.  
  299.   fret = TRUE;
  300.  
  301.   if (lseek (o, (off_t) 0, SEEK_SET) < 0
  302.       || write (o, zseq, CSEQLEN) != CSEQLEN
  303.       || close (o) < 0)
  304.     {
  305.       ulog (LOG_ERROR, "lseek or write or close %s: %s",
  306.         zfile, strerror (errno));
  307.       (void) close (o);
  308.       fret = FALSE;
  309.     }
  310.  
  311.   if (flockfile)
  312.     (void) fsdo_unlock ("LCK..SEQ", TRUE);
  313.  
  314.   ubuffree (zfree);
  315.  
  316.   return fret;
  317. }
  318.  
  319. /* Get the name of a command or data file for a remote system.  The
  320.    btype argument should be C for a command file or D for a data file.
  321.    If the grade of a data file is X, it is assumed that this is going
  322.    to become an execute file on some other system.  The zsystem
  323.    argument is the system that the file will be transferred to.  The
  324.    ztname argument will be set to a file name that could be passed to
  325.    zsysdep_spool_file_name.  The zdname argument, if not NULL, will be
  326.    set to a data file name appropriate for the remote system.  The
  327.    zxname argument, if not NULL, will be set to the name of an execute
  328.    file on the remote system.  None of the names will be more than 14
  329.    characters long.  */
  330.  
  331. /*ARGSUSED*/
  332. static char *
  333. zsfile_name (btype, zsystem, zlocalname, bgrade, fxqt, ztname, zdname, zxname)
  334.      int btype;
  335.      const char *zsystem;
  336.      const char *zlocalname;
  337.      int bgrade;
  338.      boolean fxqt;
  339.      char *ztname;
  340.      char *zdname;
  341.      char *zxname;
  342. {
  343.   char abseq[CSEQLEN + 1];
  344.   char absimple[11 + CSEQLEN];
  345.   char *zname;
  346.  
  347.   if (zlocalname == NULL)
  348.     zlocalname = zSlocalname;
  349.  
  350.   while (TRUE)
  351.     {
  352.       if (! fscmd_seq (zsystem, abseq))
  353.     return NULL;
  354.  
  355.       if (btype == 'C')
  356.     {
  357. #if ! SPOOLDIR_TAYLOR
  358.       sprintf (absimple, "C.%.7s%c%s", zsystem, bgrade, abseq);
  359. #else
  360.       sprintf (absimple, "C.%c%s", bgrade, abseq);
  361. #endif
  362.     }
  363.       else if (btype == 'D')
  364.     {
  365.       /* This name doesn't really matter that much; it's just the
  366.          name we use on the local system.  The name we use on the
  367.          remote system, which we return in zdname, should contain
  368.          our system name so that remote UUCP's running SPOOLDIR_V2
  369.          and the like can distinguish while files come from which
  370.          systems.  */
  371. #if SPOOLDIR_SVR4
  372.       sprintf (absimple, "D.%.7s%c%s", zsystem, bgrade, abseq);
  373. #else /* ! SPOOLDIR_SVR4 */
  374. #if ! SPOOLDIR_TAYLOR
  375.       sprintf (absimple, "D.%.7s%c%s", zlocalname, bgrade, abseq);
  376. #else /* SPOOLDIR_TAYLOR */
  377.       if (fxqt)
  378.         sprintf (absimple, "D.X%s", abseq);
  379.       else
  380.         sprintf (absimple, "D.%s", abseq);
  381. #endif /* SPOOLDIR_TAYLOR */
  382. #endif /* ! SPOOLDIR_HDB && ! SPOOLDIR_SVR4 */
  383.     }
  384. #if DEBUG > 0
  385.       else
  386.     ulog (LOG_FATAL, "zsfile_name: Can't happen");
  387. #endif
  388.  
  389.       zname = zsfind_file (absimple, zsystem, bgrade);
  390.       if (zname == NULL)
  391.     return NULL;
  392.  
  393.       if (! fsysdep_file_exists (zname))
  394.     break;
  395.  
  396.       ubuffree (zname);
  397.     }
  398.  
  399.   if (ztname != NULL)
  400.     strcpy (ztname, absimple);
  401.  
  402.   if (zdname != NULL)
  403.     sprintf (zdname, "D.%.7s%c%s", zlocalname, bgrade, abseq);
  404.  
  405.   if (zxname != NULL)
  406.     sprintf (zxname, "X.%.7s%c%s", zlocalname, bgrade, abseq);
  407.  
  408.   return zname;
  409. }
  410.  
  411. /* Return a name to use for a data file to be copied to another
  412.    system.  The name returned will be for a real file.  The zlocalname
  413.    argument is the local name as seen by the remote system, the bgrade
  414.    argument is the file grade, and the fxqt argument is TRUE if this
  415.    file will become an execution file.  The ztname argument, if not
  416.    NULL, will be set to a name that could be passed to
  417.    zsysdep_spool_file_name to get back the return value of this
  418.    function.  The zdname argument, if not NULL, will be set to a name
  419.    that the file could be given on another system.  The zxname
  420.    argument, if not NULL, will be set to a name for an execute file on
  421.    another system.  */
  422.  
  423. char *
  424. zsysdep_data_file_name (qsys, zlocalname, bgrade, fxqt, ztname, zdname,
  425.             zxname)
  426.      const struct uuconf_system *qsys;
  427.      const char *zlocalname;
  428.      int bgrade;
  429.      boolean fxqt;
  430.      char *ztname;
  431.      char *zdname;
  432.      char *zxname;
  433. {
  434.   return zsfile_name ('D', qsys->uuconf_zname, zlocalname, bgrade, fxqt, 
  435.               ztname, zdname, zxname);
  436. }
  437.  
  438. #if SPOOLDIR_TAYLOR
  439.  
  440. /* Write out a number in base 62 into a given number of characters,
  441.    right justified with zero fill.  This is used by zscmd_file if
  442.    SPOOLDIR_TAYLOR.  */
  443.  
  444. static void usput62 P((long i, char *, int c));
  445.  
  446. static void
  447. usput62 (i, z, c)
  448.      long i;
  449.      char *z;
  450.      int c;
  451. {
  452.   for (--c; c >= 0; --c)
  453.     {
  454.       int d;
  455.  
  456.       d = i % 62;
  457.       i /= 62;
  458.       if (d < 26)
  459.     z[c] = 'A' + d;
  460.       else if (d < 52)
  461.     z[c] = 'a' + d - 26;
  462.       else
  463.     z[c] = '0' + d - 52;
  464.     }
  465. }
  466.  
  467. #endif /* SPOOLDIR_TAYLOR */
  468.  
  469. /* Get a command file name.  */
  470.  
  471. char *
  472. zscmd_file (qsys, bgrade)
  473.      const struct uuconf_system *qsys;
  474.      int bgrade;
  475. {
  476. #if ! SPOOLDIR_TAYLOR
  477.   return zsfile_name ('C', qsys->uuconf_zname, (const char *) NULL,
  478.               bgrade, FALSE, (char *) NULL, (char *) NULL,
  479.               (char *) NULL);
  480. #else
  481.   char *zname;
  482.   long isecs, imicros;
  483.   pid_t ipid;
  484.  
  485.   /* This file name is never seen by the remote system, so we don't
  486.      actually need to get a sequence number for it.  We just need to
  487.      get a file name which is unique for this system.  We don't try
  488.      this optimization for other spool directory formats, mainly due
  489.      to compatibility concerns.  It would be possible for HDB and SVR4
  490.      spool directory formats.
  491.  
  492.      We get a unique name by combining the process ID and the current
  493.      time.  The file name must start with C.g, where g is the grade.
  494.      Note that although it is likely that this name will be unique, it
  495.      is not guaranteed, so the caller must be careful.  */
  496.  
  497.   isecs = ixsysdep_time (&imicros);
  498.   ipid = getpid ();
  499.  
  500.   /* We are going to represent the file name as a series of numbers in
  501.      base 62 (using the alphanumeric characters).  The maximum file
  502.      name length is 14 characters, so we may use 11.  We use 3 for the
  503.      seconds within the day, 3 for the microseconds, and 5 for the
  504.      process ID.  */
  505.  
  506.   /* Cut the seconds down to a number within a day (maximum value
  507.      86399 < 62 ** 3 == 238328).  */
  508.   isecs %= (long) 24 * (long) 60 * (long) 60;
  509.   /* Divide the microseconds (max 999999) by 5 to make sure they are
  510.      less than 62 ** 3.  */
  511.   imicros %= 1000000;
  512.   imicros /= 5;
  513.  
  514.   while (TRUE)
  515.     {
  516.       char ab[15];
  517.  
  518.       ab[0] = 'C';
  519.       ab[1] = '.';
  520.       ab[2] = bgrade;
  521.       usput62 (isecs, ab + 3, 3);
  522.       usput62 (imicros, ab + 6, 3);
  523.       usput62 ((long) ipid, ab + 9, 5);
  524.       ab[14] = '\0';
  525.  
  526.       zname = zsfind_file (ab, qsys->uuconf_zname, bgrade);
  527.       if (zname == NULL)
  528.     return NULL;
  529.  
  530.       if (! fsysdep_file_exists (zname))
  531.     break;
  532.  
  533.       ubuffree (zname);
  534.  
  535.       /* We hit a duplicate.  Move backward in time until we find an
  536.          available name.  Note that there is still a theoretical race
  537.          condition, since 5 base 62 digits might not be enough for the
  538.          process ID, and some other process might be running these
  539.          checks at the same time as we are.  The caller must deal with
  540.          this.  */
  541.       if (imicros == 0)
  542.     {
  543.       imicros = (long) 62 * (long) 62 * (long) 62;
  544.       if (isecs == 0)
  545.         isecs = (long) 62 * (long) 62 * (long) 62;
  546.       --isecs;
  547.     }
  548.       --imicros;
  549.     }
  550.  
  551.   return zname;
  552.  
  553. #endif
  554. }
  555.  
  556. /* Return a name for an execute file to be created locally.  This is
  557.    used by uux to execute a command locally with remote files.  */
  558.  
  559. char *
  560. zsysdep_xqt_file_name ()
  561. {
  562.   char abseq[CSEQLEN + 1];
  563.   char absx[11 + CSEQLEN];
  564.   char *zname;
  565.  
  566.   while (TRUE)
  567.     {
  568.       if (! fscmd_seq (zSlocalname, abseq))
  569.     return NULL;
  570.  
  571.       sprintf (absx, "X.%.7sX%s", zSlocalname, abseq);
  572.  
  573.       zname = zsfind_file (absx, zSlocalname, -1);
  574.       if (zname == NULL)
  575.     return NULL;
  576.  
  577.       if (! fsysdep_file_exists (zname))
  578.     break;
  579.  
  580.       ubuffree (zname);
  581.     }
  582.  
  583.   return zname;
  584. }
  585.