home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / copy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-20  |  5.0 KB  |  237 lines

  1. /* copy.c
  2.    Copy one file to another for the UUCP package.
  3.  
  4.    Copyright (C) 1991, 1992, 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. #if USE_RCS_ID
  29. const char copy_rcsid[] = "$Id: copy.c,v 1.18 1995/08/02 01:17:31 ian Rel $";
  30. #endif
  31.  
  32. #include "uudefs.h"
  33. #include "system.h"
  34. #include "sysdep.h"
  35.  
  36. #include <stdio.h>
  37. #include <errno.h>
  38.  
  39. /* Copy one file to another.  */
  40.  
  41. #if USE_STDIO
  42.  
  43. boolean
  44. fcopy_file (zfrom, zto, fpublic, fmkdirs, fsignals)
  45.      const char *zfrom;
  46.      const char *zto;
  47.      boolean fpublic;
  48.      boolean fmkdirs;
  49.      boolean fsignals;
  50. {
  51.   FILE *efrom;
  52.   boolean fret;
  53.  
  54.   efrom = fopen (zfrom, BINREAD);
  55.   if (efrom == NULL)
  56.     {
  57.       ulog (LOG_ERROR, "fopen (%s): %s", zfrom, strerror (errno));
  58.       return FALSE;
  59.     }
  60.  
  61.   fret = fcopy_open_file (efrom, zto, fpublic, fmkdirs, fsignals);
  62.   (void) fclose (efrom);
  63.   return fret;
  64. }
  65.  
  66. boolean
  67. fcopy_open_file (efrom, zto, fpublic, fmkdirs, fsignals)
  68.      FILE *efrom;
  69.      const char *zto;
  70.      boolean fpublic;
  71.      boolean fmkdirs;
  72.      boolean fsignals;
  73. {
  74.   FILE *eto;
  75.   char ab[8192];
  76.   int c;
  77.  
  78.   eto = esysdep_fopen (zto, fpublic, FALSE, fmkdirs);
  79.   if (eto == NULL)
  80.     return FALSE;
  81.  
  82.   while ((c = fread (ab, sizeof (char), sizeof ab, efrom)) != 0)
  83.     {
  84.       if (fwrite (ab, sizeof (char), (size_t) c, eto) != c)
  85.     {
  86.       ulog (LOG_ERROR, "fwrite: %s", strerror (errno));
  87.       (void) fclose (eto);
  88.       (void) remove (zto);
  89.       return FALSE;
  90.     }
  91.       if (fsignals && FGOT_SIGNAL ())
  92.     {
  93.       /* Log the signal.  */
  94.       ulog (LOG_ERROR, (const char *) NULL);
  95.       (void) fclose (eto);
  96.       (void) remove (zto);
  97.       return FALSE;
  98.     }
  99.     }
  100.  
  101.   if (! fsysdep_sync (eto, zto))
  102.     {
  103.       (void) fclose (eto);
  104.       (void) remove (zto);
  105.       return FALSE;
  106.     }
  107.  
  108.   if (fclose (eto) != 0)
  109.     {
  110.       ulog (LOG_ERROR, "fclose: %s", strerror (errno));
  111.       (void) remove (zto);
  112.       return FALSE;
  113.     }
  114.  
  115.   return TRUE;
  116. }
  117.  
  118. #else /* ! USE_STDIO */
  119.  
  120. #if HAVE_FCNTL_H
  121. #include <fcntl.h>
  122. #else
  123. #if HAVE_SYS_FILE_H
  124. #include <sys/file.h>
  125. #endif
  126. #endif
  127.  
  128. #ifndef O_RDONLY
  129. #define O_RDONLY 0
  130. #define O_WRONLY 1
  131. #define O_RDWR 2
  132. #endif
  133.  
  134. #ifndef O_NOCTTY
  135. #define O_NOCTTY 0
  136. #endif
  137.  
  138. boolean
  139. fcopy_file (zfrom, zto, fpublic, fmkdirs, fsignals)
  140.      const char *zfrom;
  141.      const char *zto;
  142.      boolean fpublic;
  143.      boolean fmkdirs;
  144.      boolean fsignals;
  145. {
  146.   int ofrom;
  147.   boolean fret;
  148.  
  149.   ofrom = open (zfrom, O_RDONLY | O_NOCTTY, 0);
  150.   if (ofrom < 0)
  151.     {
  152.       ulog (LOG_ERROR, "open (%s): %s", zfrom, strerror (errno));
  153.       return FALSE;
  154.     }
  155.  
  156.   fret = fcopy_open_file (ofrom, zto, fpublic, fmkdirs, fsignals);
  157.   (void) close (ofrom);
  158.   return fret;
  159. }
  160.  
  161. boolean
  162. fcopy_open_file (ofrom, zto, fpublic, fmkdirs, fsignals)
  163.      int ofrom;
  164.      const char *zto;
  165.      boolean fpublic;
  166.      boolean fmkdirs;
  167.      boolean fsignals;
  168. {
  169.   int oto;
  170.   char ab[8192];
  171.   int c;
  172.  
  173.   /* These file mode arguments are from the UNIX version of sysdep.h;
  174.      each system dependent header file will need their own
  175.      definitions.  */
  176.   oto = creat (zto, fpublic ? IPUBLIC_FILE_MODE : IPRIVATE_FILE_MODE);
  177.   if (oto < 0)
  178.     {
  179.       if (errno == ENOENT && fmkdirs)
  180.     {
  181.       if (! fsysdep_make_dirs (zto, fpublic))
  182.         return FALSE;
  183.       oto = creat (zto,
  184.                fpublic ? IPUBLIC_FILE_MODE : IPRIVATE_FILE_MODE);
  185.     }
  186.       if (oto < 0)
  187.     {
  188.       ulog (LOG_ERROR, "open (%s): %s", zto, strerror (errno));
  189.       return FALSE;
  190.     }
  191.     }
  192.  
  193.   while ((c = read (ofrom, ab, sizeof ab)) > 0)
  194.     {
  195.       if (write (oto, ab, (size_t) c) != c)
  196.     {
  197.       ulog (LOG_ERROR, "write: %s", strerror (errno));
  198.       (void) close (oto);
  199.       (void) remove (zto);
  200.       return FALSE;
  201.     }
  202.       if (fsignals && FGOT_SIGNAL ())
  203.     {
  204.       /* Log the signal.  */
  205.       ulog (LOG_ERROR, (const char *) NULL);
  206.       (void) fclose (eto);
  207.       (void) remove (zto);
  208.       return FALSE;
  209.     }
  210.     }
  211.  
  212.   if (! fsysdep_sync (oto, zto))
  213.     {
  214.       (void) close (oto);
  215.       (void) remove (zto);
  216.       return FALSE;
  217.     }
  218.  
  219.   if (close (oto) < 0)
  220.     {
  221.       ulog (LOG_ERROR, "close: %s", strerror (errno));
  222.       (void) remove (zto);
  223.       return FALSE;
  224.     }
  225.  
  226.   if (c < 0)
  227.     {
  228.       ulog (LOG_ERROR, "read: %s", strerror (errno));
  229.       (void) remove (zto);
  230.       return FALSE;
  231.     }
  232.  
  233.   return TRUE;
  234. }
  235.  
  236. #endif /* ! USE_STDIO */
  237.