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

  1. /* work.c
  2.    Routines to read command files.
  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 work_rcsid[] = "$Id: work.c,v 1.10 1992/11/14 16:14:07 ian Rel $";
  30. #endif
  31.  
  32. #include "uudefs.h"
  33. #include "uuconf.h"
  34. #include "system.h"
  35. #include "sysdep.h"
  36.  
  37. #include <ctype.h>
  38. #include <errno.h>
  39.  
  40. #if HAVE_OPENDIR
  41. #if HAVE_DIRENT_H
  42. #include <dirent.h>
  43. #else /* ! HAVE_DIRENT_H */
  44. #include <sys/dir.h>
  45. #define dirent direct
  46. #endif /* ! HAVE_DIRENT_H */
  47. #endif /* HAVE_OPENDIR */
  48.  
  49. /* Local functions.  */
  50.  
  51. static char *zswork_directory P((const char *zsystem));
  52. static boolean fswork_file P((const char *zsystem, const char *zfile,
  53.                   char *pbgrade));
  54. static int iswork_cmp P((constpointer pkey, constpointer pdatum));
  55.  
  56. /* These functions can support multiple actions going on at once.
  57.    This allows the UUCP package to send and receive multiple files at
  58.    the same time.  This is a very flexible feature, but I'm not sure
  59.    it will actually be used all that much.
  60.  
  61.    The ssfile structure holds a command file name and all the lines
  62.    read in from that command file.  The union within the ssline
  63.    structure initially holds a line from the file and then holds a
  64.    pointer back to the ssfile structure; a pointer to this union is
  65.    used as a sequence pointer.  The ztemp entry of the ssline
  66.    structure holds the name of a temporary file to delete, if any.  */
  67.  
  68. #define CFILELINES (10)
  69.  
  70. struct ssline
  71. {
  72.   char *zline;
  73.   struct ssfile *qfile;
  74.   char *ztemp;
  75. };
  76.  
  77. struct ssfile
  78. {
  79.   char *zfile;
  80.   int clines;
  81.   int cdid;
  82.   struct ssline aslines[CFILELINES];
  83. };
  84.  
  85. /* Static variables for the work scan.  */
  86.  
  87. static char **azSwork_files;
  88. static size_t cSwork_files;
  89. static size_t iSwork_file;
  90. static struct ssfile *qSwork_file;
  91.  
  92. /* Given a system name, return a directory to search for work.  */
  93.  
  94. static char *
  95. zswork_directory (zsystem)
  96.      const char *zsystem;
  97. {
  98. #if SPOOLDIR_V2
  99.   return zbufcpy (".");
  100. #endif /* SPOOLDIR_V2 */
  101. #if SPOOLDIR_BSD42 || SPOOLDIR_BSD43
  102.   return zbufcpy ("C.");
  103. #endif /* SPOOLDIR_BSD42 || SPOOLDIR_BSD43 */
  104. #if SPOOLDIR_HDB || SPOOLDIR_SVR4
  105.   return zbufcpy (zsystem);
  106. #endif /* SPOOLDIR_HDB || SPOOLDIR_SVR4 */
  107. #if SPOOLDIR_ULTRIX
  108.   return zsappend3 ("sys",
  109.             (fsultrix_has_spool (zsystem)
  110.              ? zsystem
  111.              : "DEFAULT"),
  112.             "C.");
  113. #endif /* SPOOLDIR_ULTRIX */
  114. #if SPOOLDIR_TAYLOR
  115.   return zsysdep_in_dir (zsystem, "C.");
  116. #endif /* SPOOLDIR_TAYLOR */
  117. }
  118.  
  119. /* See whether a file name from the directory returned by
  120.    zswork_directory is really a command for a particular system.
  121.    Return the command grade.  */
  122.  
  123. /*ARGSUSED*/
  124. static boolean
  125. fswork_file (zsystem, zfile, pbgrade)
  126.      const char *zsystem;
  127.      const char *zfile;
  128.      char *pbgrade;
  129. {
  130. #if SPOOLDIR_V2 || SPOOLDIR_BSD42 || SPOOLDIR_BSD43 || SPOOLDIR_ULTRIX
  131.   int cfilesys, csys;
  132.  
  133.   /* The file name should be C.ssssssgqqqq, where g is exactly one
  134.      letter and qqqq is exactly four numbers.  The system name may be
  135.      truncated to six or seven characters.  The system name of the
  136.      file must match the system name we're looking for, since there
  137.      could be work files for several systems in one directory.  */
  138.   if (zfile[0] != 'C' || zfile[1] != '.')
  139.     return FALSE;
  140.   csys = strlen (zsystem);
  141.   cfilesys = strlen (zfile) - 7;
  142.   if (csys != cfilesys
  143.       && (csys < 6 || (cfilesys != 6 && cfilesys != 7)))
  144.     return FALSE;
  145.   *pbgrade = zfile[cfilesys + 2];
  146.   return strncmp (zfile + 2, zsystem, cfilesys) == 0;
  147. #endif /* V2 || BSD42 || BSD43 || ULTRIX */
  148. #if SPOOLDIR_HDB || SPOOLDIR_SVR4
  149.   int clen;
  150.  
  151.   /* The HDB file name should be C.ssssssgqqqq where g is exactly one
  152.      letter and qqqq is exactly four numbers or letters.  We don't
  153.      check the system name, because it is guaranteed by the directory
  154.      we are looking in and some versions of uucp set it to the local
  155.      system rather than the remote one.  I'm not sure of the exact
  156.      format of the SVR4 file name, but it does not include the grade
  157.      at all.  */
  158.   if (zfile[0] != 'C' || zfile[1] != '.')
  159.     return FALSE;
  160.   clen = strlen (zfile);
  161.   if (clen < 7)
  162.     return FALSE;
  163. #if ! SPOOLDIR_SVR4
  164.   *pbgrade = zfile[clen - 5];
  165. #endif
  166.   return TRUE;
  167. #endif /* SPOOLDIR_HDB || SPOOLDIR_SVR4 */
  168. #if SPOOLDIR_TAYLOR
  169.   /* We don't keep the system name in the file name, since that
  170.      forces truncation.  Our file names are always C.gqqqq.  */
  171.   *pbgrade = zfile[2];
  172.   return (zfile[0] == 'C'
  173.       && zfile[1] == '.'
  174.       && strlen (zfile) == 7);
  175. #endif /* SPOOLDIR_TAYLOR */
  176. }
  177.  
  178. /* A comparison function to look through the list of file names.  */
  179.  
  180. static int
  181. iswork_cmp (pkey, pdatum)
  182.      constpointer pkey;
  183.      constpointer pdatum;
  184. {
  185.   const char * const *pzkey = (const char * const *) pkey;
  186.   const char * const *pzdatum = (const char * const *) pdatum;
  187.  
  188.   return strcmp (*pzkey, *pzdatum);
  189. }
  190.  
  191. /* See whether there is any work to do for a particular system.  */
  192.  
  193. boolean
  194. fsysdep_has_work (qsys)
  195.      const struct uuconf_system *qsys;
  196. {
  197.   char *zdir;
  198.   DIR *qdir;
  199.   struct dirent *qentry;
  200. #if SPOOLDIR_SVR4
  201.   DIR *qgdir;
  202.   struct dirent *qgentry;
  203. #endif
  204.  
  205.   zdir = zswork_directory (qsys->uuconf_zname);
  206.   if (zdir == NULL)
  207.     return FALSE;
  208.   qdir = opendir ((char *) zdir);
  209.   if (qdir == NULL)
  210.     {
  211.       ubuffree (zdir);
  212.       return FALSE;
  213.     }
  214.  
  215. #if SPOOLDIR_SVR4
  216.   qgdir = qdir;
  217.   while ((qgentry = readdir (qgdir)) != NULL)
  218.     {
  219.       char *zsub;
  220.  
  221.       if (qgentry->d_name[0] == '.'
  222.       || qgentry->d_name[1] != '\0')
  223.     continue;
  224.       zsub = zsysdep_in_dir (zdir, qgentry->d_name);
  225.       qdir = opendir (zsub);
  226.       ubuffree (zsub);
  227.       if (qdir == NULL)
  228.     continue;
  229. #endif
  230.  
  231.       while ((qentry = readdir (qdir)) != NULL)
  232.     {
  233.       char bgrade;
  234.  
  235.       if (fswork_file (qsys->uuconf_zname, qentry->d_name, &bgrade))
  236.         {
  237.           closedir (qdir);
  238. #if SPOOLDIR_SVR4
  239.           closedir (qgdir);
  240. #endif
  241.           ubuffree (zdir);
  242.           return TRUE;
  243.         }
  244.     }
  245.  
  246. #if SPOOLDIR_SVR4
  247.       closedir (qdir);
  248.     }
  249.   qdir = qgdir;
  250. #endif
  251.  
  252.   closedir (qdir);
  253.   ubuffree (zdir);
  254.   return FALSE;
  255. }
  256.  
  257. /* Initialize the work scan.  We have to read all the files in the
  258.    work directory, so that we can sort them by work grade.  The bgrade
  259.    argument is the minimum grade to consider.  We don't want to return
  260.    files that we have already considered; usysdep_get_work_free will
  261.    clear the data out when we are done with the system.  This returns
  262.    FALSE on error.  */
  263.  
  264. #define CWORKFILES (10)
  265.  
  266. boolean
  267. fsysdep_get_work_init (qsys, bgrade)
  268.      const struct uuconf_system *qsys;
  269.      int bgrade;
  270. {
  271.   char *zdir;
  272.   DIR *qdir;
  273.   struct dirent *qentry;
  274.   size_t chad;
  275.   size_t callocated;
  276. #if SPOOLDIR_SVR4
  277.   DIR *qgdir;
  278.   struct dirent *qgentry;
  279. #endif
  280.  
  281.   zdir = zswork_directory (qsys->uuconf_zname);
  282.   if (zdir == NULL)
  283.     return FALSE;
  284.  
  285.   qdir = opendir (zdir);
  286.   if (qdir == NULL)
  287.     {
  288.       boolean fret;
  289.  
  290.       if (errno == ENOENT)
  291.     fret = TRUE;
  292.       else
  293.     {
  294.       ulog (LOG_ERROR, "opendir (%s): %s", zdir, strerror (errno));
  295.       fret = FALSE;
  296.     }
  297.       ubuffree (zdir);
  298.       return fret;
  299.     }
  300.  
  301.   chad = cSwork_files;
  302.   callocated = cSwork_files;
  303.  
  304.   /* Sort the files we already know about so that we can check the new
  305.      ones with bsearch.  It would be faster to use a hash table, and
  306.      the code should be probably be changed.  The sort done at the end
  307.      of this function does not suffice because it only includes the
  308.      files added last time, and does not sort the entire array.  Some
  309.      (bad) qsort implementations are very slow when given a sorted
  310.      array, which causes particularly bad effects here.  */
  311.   if (chad > 0)
  312.     qsort ((pointer) azSwork_files, chad, sizeof (char *), iswork_cmp);
  313.  
  314. #if SPOOLDIR_SVR4
  315.   qgdir = qdir;
  316.   while ((qgentry = readdir (qgdir)) != NULL)
  317.     {
  318.       char *zsub;
  319.  
  320.       if (qgentry->d_name[0] == '.'
  321.       || qgentry->d_name[1] != '\0'
  322.       || UUCONF_GRADE_CMP (bgrade, qgentry->d_name[0]) < 0)
  323.     continue;
  324.       zsub = zsysdep_in_dir (zdir, qgentry->d_name);
  325.       qdir = opendir (zsub);
  326.       if (qdir == NULL)
  327.     {
  328.       if (errno != ENOTDIR && errno != ENOENT)
  329.         {
  330.           ulog (LOG_ERROR, "opendir (%s): %s", zsub,
  331.             strerror (errno));
  332.           ubuffree (zsub);
  333.           return FALSE;
  334.         }
  335.       ubuffree (zsub);
  336.       continue;
  337.     }
  338.       ubuffree (zsub);
  339. #endif
  340.  
  341.       while ((qentry = readdir (qdir)) != NULL)
  342.     {
  343.       char bfilegrade;
  344.       char *zname;
  345.  
  346. #if ! SPOOLDIR_SVR4
  347.       zname = zbufcpy (qentry->d_name);
  348. #else
  349.       zname = zsysdep_in_dir (qgentry->d_name, qentry->d_name);
  350.       bfilegrade = qgentry->d_name[0];
  351. #endif
  352.  
  353.       if (! fswork_file (qsys->uuconf_zname, qentry->d_name,
  354.                  &bfilegrade)
  355.           || UUCONF_GRADE_CMP (bgrade, bfilegrade) < 0
  356.           || (azSwork_files != NULL
  357.           && bsearch ((pointer) &zname,
  358.                   (pointer) azSwork_files,
  359.                   chad, sizeof (char *),
  360.                   iswork_cmp) != NULL))
  361.         ubuffree (zname);
  362.       else
  363.         {
  364.           DEBUG_MESSAGE1 (DEBUG_SPOOLDIR,
  365.                   "fsysdep_get_work_init: Found %s",
  366.                   zname);
  367.  
  368.           if (cSwork_files >= callocated)
  369.         {
  370.           callocated += CWORKFILES;
  371.           azSwork_files =
  372.             (char **) xrealloc ((pointer) azSwork_files,
  373.                     callocated * sizeof (char *));
  374.         }
  375.  
  376.           azSwork_files[cSwork_files] = zname;
  377.           ++cSwork_files;
  378.         }
  379.     }
  380.  
  381. #if SPOOLDIR_SVR4
  382.       closedir (qdir);
  383.     }
  384.   qdir = qgdir;
  385. #endif
  386.  
  387.   closedir (qdir);
  388.   ubuffree (zdir);
  389.  
  390.   /* Sorting the files alphabetically will get the grades in the
  391.      right order, since all the file prefixes are the same.  */
  392.  
  393.   if (cSwork_files > chad)
  394.     qsort ((pointer) (azSwork_files + chad), cSwork_files - chad,
  395.        sizeof (char *), iswork_cmp);
  396.  
  397.   return TRUE;
  398. }
  399.  
  400. /* Get the next work entry for a system.  This must parse the next
  401.    line in the next work file.  The type of command is set into
  402.    qcmd->bcmd; if there are no more commands we call
  403.    fsysdep_get_work_init to rescan, in case any came in since the last
  404.    call.  If there are still no commands, qcmd->bcmd is set to 'H'.
  405.    Each field in the structure is set to point to a spot in an
  406.    malloced string.  The only time we use the grade here is when
  407.    calling fsysdep_get_work_init to rescan.  */
  408.  
  409. boolean
  410. fsysdep_get_work (qsys, bgrade, qcmd)
  411.      const struct uuconf_system *qsys;
  412.      int bgrade;
  413.      struct scmd *qcmd;
  414. {
  415.   char *zdir;
  416.  
  417.   if (qSwork_file != NULL && qSwork_file->cdid >= qSwork_file->clines)
  418.     qSwork_file = NULL;
  419.  
  420.   if (azSwork_files == NULL)
  421.     {
  422.       qcmd->bcmd = 'H';
  423.       return TRUE;
  424.     }
  425.  
  426.   zdir = NULL;
  427.  
  428.   /* This loop continues until a line is returned.  */
  429.   while (TRUE)
  430.     {
  431.       /* This loop continues until a file is opened and read in.  */
  432.       while (qSwork_file == NULL)
  433.     {
  434.       FILE *e;
  435.       struct ssfile *qfile;
  436.       int iline, callocated;
  437.       char *zline;
  438.       size_t cline;
  439.       char *zname;
  440.  
  441.       /* Read all the lines of a command file into memory.  */
  442.       do
  443.         {
  444.           if (iSwork_file >= cSwork_files)
  445.         {
  446.           /* Rescan the work directory.  */
  447.           if (! fsysdep_get_work_init (qsys, bgrade))
  448.             {
  449.               ubuffree (zdir);
  450.               return FALSE;
  451.             }
  452.           if (iSwork_file >= cSwork_files)
  453.             {
  454.               qcmd->bcmd = 'H';
  455.               ubuffree (zdir);
  456.               return TRUE;
  457.             }
  458.         }
  459.  
  460.           if (zdir == NULL)
  461.         {
  462.           zdir = zswork_directory (qsys->uuconf_zname);
  463.           if (zdir == NULL)
  464.             return FALSE;
  465.         }
  466.  
  467.           zname = zsysdep_in_dir (zdir, azSwork_files[iSwork_file]);
  468.  
  469.           ++iSwork_file;
  470.  
  471.           e = fopen (zname, "r");
  472.           if (e == NULL)
  473.         {
  474.           ulog (LOG_ERROR, "fopen (%s): %s", zname,
  475.             strerror (errno));
  476.           ubuffree (zname);
  477.         }
  478.         }
  479.       while (e == NULL);
  480.       
  481.       qfile = (struct ssfile *) xmalloc (sizeof (struct ssfile));
  482.       callocated = CFILELINES;
  483.       iline = 0;
  484.  
  485.       zline = NULL;
  486.       cline = 0;
  487.       while (getline (&zline, &cline, e) > 0)
  488.         {
  489.           if (iline >= callocated)
  490.         {
  491.           /* The sizeof (struct ssfile) includes CFILELINES
  492.              entries already, so using callocated * sizeof
  493.              (struct ssline) will give us callocated *
  494.              CFILELINES entries.  */
  495.           qfile =
  496.             ((struct ssfile *)
  497.              xrealloc ((pointer) qfile,
  498.                    (sizeof (struct ssfile) +
  499.                 (callocated * sizeof (struct ssline)))));
  500.           callocated += CFILELINES;
  501.         }
  502.           qfile->aslines[iline].zline = zbufcpy (zline);
  503.           qfile->aslines[iline].qfile = NULL;
  504.           qfile->aslines[iline].ztemp = NULL;
  505.           iline++;
  506.         }
  507.  
  508.       xfree ((pointer) zline);
  509.  
  510.       if (fclose (e) != 0)
  511.         ulog (LOG_ERROR, "fclose: %s", strerror (errno));
  512.  
  513.       if (iline == 0)
  514.         {
  515.           /* There were no lines in the file; this is a poll file,
  516.          for which we return a 'P' command.  */
  517.           qfile->aslines[0].zline = zbufcpy ("P");
  518.           qfile->aslines[0].qfile = NULL;
  519.           qfile->aslines[0].ztemp = NULL;
  520.           iline = 1;
  521.         }
  522.  
  523.       qfile->zfile = zname;
  524.       qfile->clines = iline;
  525.       qfile->cdid = 0;
  526.       qSwork_file = qfile;
  527.     }
  528.  
  529.       /* This loop continues until all the lines from the current file
  530.      are used up, or a line is returned.  */
  531.       while (TRUE)
  532.     {
  533.       int iline;
  534.       
  535.       if (qSwork_file->cdid >= qSwork_file->clines)
  536.         {
  537.           /* We don't want to free qSwork_file here, since it must
  538.          remain until all the lines have been completed.  It
  539.          is freed in fsysdep_did_work.  */
  540.           qSwork_file = NULL;
  541.           /* Go back to the main loop which finds another file.  */
  542.           break;
  543.         }
  544.  
  545.       iline = qSwork_file->cdid;
  546.       ++qSwork_file->cdid;
  547.  
  548.       /* Now parse the line into a command.  */
  549.       if (! fparse_cmd (qSwork_file->aslines[iline].zline, qcmd))
  550.         {
  551.           ulog (LOG_ERROR, "Bad line in command file %s",
  552.             qSwork_file->zfile);
  553.           ubuffree (qSwork_file->aslines[iline].zline);
  554.           qSwork_file->aslines[iline].zline = NULL;
  555.           continue;
  556.         }
  557.  
  558.       qSwork_file->aslines[iline].qfile = qSwork_file;
  559.       qcmd->pseq = (pointer) (&qSwork_file->aslines[iline]);
  560.  
  561.       if (qcmd->bcmd == 'S' || qcmd->bcmd == 'E')
  562.         {
  563.           char *zreal;
  564.  
  565.           zreal = zsysdep_spool_file_name (qsys, qcmd->ztemp,
  566.                            qcmd->pseq);
  567.           if (zreal == NULL)
  568.         {
  569.           ubuffree (qSwork_file->aslines[iline].zline);
  570.           qSwork_file->aslines[iline].zline = NULL;
  571.           ubuffree (zdir);
  572.           return FALSE;
  573.         }
  574.           qSwork_file->aslines[iline].ztemp = zreal;
  575.         }
  576.  
  577.       ubuffree (zdir);
  578.       return TRUE;
  579.     }
  580.     }
  581. }
  582.  
  583. /* When a command has been complete, fsysdep_did_work is called.  The
  584.    sequence entry was set above to be the address of an aslines
  585.    structure whose pfile entry points to the ssfile corresponding to
  586.    this file.  We can then check whether all the lines have been
  587.    completed (they will have been if the pfile entry is NULL) and
  588.    remove the file if they have been.  This means that we only remove
  589.    a command file if we manage to complete every transfer it specifies
  590.    in a single UUCP session.  I don't know if this is how regular UUCP
  591.    works.  */
  592.  
  593. boolean
  594. fsysdep_did_work (pseq)
  595.      pointer pseq;
  596. {
  597.   struct ssfile *qfile;
  598.   struct ssline *qline;
  599.   int i;
  600.   
  601.   qline = (struct ssline *) pseq;
  602.  
  603.   ubuffree (qline->zline);
  604.   qline->zline = NULL;
  605.  
  606.   qfile = qline->qfile;
  607.   qline->qfile = NULL;
  608.  
  609.   /* Remove the temporary file, if there is one.  It really doesn't
  610.      matter if this fails, and not checking the return value lets us
  611.      attempt to remove D.0 or whatever an unused temporary file is
  612.      called without complaining.  */
  613.   if (qline->ztemp != NULL)
  614.     {
  615.       (void) remove (qline->ztemp);
  616.       ubuffree (qline->ztemp);
  617.       qline->ztemp = NULL;
  618.     }
  619.  
  620.   /* If not all the lines have been returned from fsysdep_get_work,
  621.      we can't remove the file yet.  */
  622.   if (qfile->cdid < qfile->clines)
  623.     return TRUE;
  624.  
  625.   /* See whether all the commands have been completed.  */
  626.   for (i = 0; i < qfile->clines; i++)
  627.     if (qfile->aslines[i].qfile != NULL)
  628.       return TRUE;
  629.  
  630.   /* All commands have finished.  */
  631.   if (remove (qfile->zfile) != 0)
  632.     {
  633.       ulog (LOG_ERROR, "remove (%s): %s", qfile->zfile,
  634.         strerror (errno));
  635.       return FALSE;
  636.     }
  637.  
  638.   ubuffree (qfile->zfile);
  639.   xfree ((pointer) qfile);
  640.  
  641.   if (qfile == qSwork_file)
  642.     qSwork_file = NULL;
  643.  
  644.   return TRUE;
  645. }
  646.  
  647. /* Free up the results of a work scan, when we're done with this
  648.    system.  */
  649.  
  650. /*ARGSUSED*/
  651. void
  652. usysdep_get_work_free (qsys)
  653.      const struct uuconf_system *qsys;
  654. {
  655.   if (azSwork_files != NULL)
  656.     {
  657.       size_t i;
  658.  
  659.       for (i = 0; i < cSwork_files; i++)
  660.     ubuffree ((pointer) azSwork_files[i]);
  661.       xfree ((pointer) azSwork_files);
  662.       azSwork_files = NULL;
  663.       cSwork_files = 0;
  664.       iSwork_file = 0;
  665.     }
  666.   if (qSwork_file != NULL)
  667.     {
  668.       int i;
  669.  
  670.       ubuffree (qSwork_file->zfile);
  671.       for (i = 0; i < qSwork_file->cdid; i++)
  672.     {
  673.       ubuffree (qSwork_file->aslines[i].zline);
  674.       ubuffree (qSwork_file->aslines[i].ztemp);
  675.     }
  676.       for (i = qSwork_file->cdid; i < qSwork_file->clines; i++)
  677.     ubuffree (qSwork_file->aslines[i].zline);
  678.       xfree ((pointer) qSwork_file);
  679.       qSwork_file = NULL;
  680.     }
  681. }
  682.  
  683. /* Save the temporary file used by a send command, and return an
  684.    informative message to mail to the requestor.  This is called when
  685.    a file transfer failed, to make sure that the potentially valuable
  686.    file is not completely lost.  */
  687.  
  688. const char *
  689. zsysdep_save_temp_file (pseq)
  690.      pointer pseq;
  691. {
  692.   struct ssline *qline = (struct ssline *) pseq;
  693.   char *zto, *zslash;
  694.   size_t cwant;
  695.   static char *zbuf;
  696.   static int cbuf;
  697.  
  698.   if (! fsysdep_file_exists (qline->ztemp))
  699.     return NULL;
  700.  
  701.   zslash = strrchr (qline->ztemp, '/');
  702.   if (zslash == NULL)
  703.     zslash = qline->ztemp;
  704.   else
  705.     ++zslash;
  706.  
  707.   zto = zbufalc (sizeof PRESERVEDIR + sizeof "/" + strlen (zslash));
  708.   sprintf (zto, "%s/%s", PRESERVEDIR, zslash);
  709.  
  710.   if (! fsysdep_move_file (qline->ztemp, zto, TRUE, FALSE, FALSE,
  711.                (const char *) NULL))
  712.     {
  713.       ubuffree (zto);
  714.       return "Could not move file to preservation directory";
  715.     }
  716.     
  717.   cwant = sizeof "File saved as\n\t/" + strlen (zSspooldir) + strlen (zto);
  718.   if (cwant > cbuf)
  719.     {
  720.       ubuffree (zbuf);
  721.       zbuf = zbufalc (cwant);
  722.       cbuf = cwant;
  723.     }
  724.  
  725.   sprintf (zbuf, "File saved as\n\t%s/%s", zSspooldir, zto);
  726.   ubuffree (zto);
  727.   return zbuf;
  728. }
  729.  
  730. /* Get the jobid of a work file.  This is needed by uustat.  */
  731.  
  732. char *
  733. zsysdep_jobid (qsys, pseq)
  734.      const struct uuconf_system *qsys;
  735.      pointer pseq;
  736. {
  737.   return zsfile_to_jobid (qsys, ((struct ssline *) pseq)->qfile->zfile,
  738.               bsgrade (pseq));
  739. }
  740.  
  741. /* Get the grade of a work file.  The pseq argument can be NULL when
  742.    this is called from zsysdep_spool_file_name, and simply means that
  743.    this is a remote file; returning -1 will cause zsfind_file to do
  744.    the right thing.  */
  745.  
  746. char
  747. bsgrade (pseq)
  748.      pointer pseq;
  749. {
  750.   const char *zfile;
  751.   char bgrade;
  752.  
  753.   if (pseq == NULL)
  754.     return -1;
  755.  
  756.   zfile = ((struct ssline *) pseq)->qfile->zfile;
  757.  
  758. #if ! SPOOLDIR_SVR4
  759.   bgrade = zfile[strlen (zfile) - CSEQLEN - 1];
  760. #else
  761.   bgrade = *(strchr (zfile, '/') + 1);
  762. #endif
  763.  
  764.   return bgrade;
  765. }
  766.