home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / mipsABI / examples / sup / ffilecopy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.7 KB  |  86 lines

  1. /*
  2.  * Copyright (c) 1991 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  12.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  13.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  14.  *
  15.  * Carnegie Mellon requests users of this software to return to
  16.  *
  17.  *  Software Distribution Coordinator   or   Software.Distribution@CS.CMU.EDU
  18.  *  School of Computer Science
  19.  *  Carnegie Mellon University
  20.  *  Pittsburgh PA 15213-3890
  21.  *
  22.  * any improvements or extensions that they make and grant Carnegie the rights
  23.  * to redistribute these changes.
  24.  */
  25. /*  ffilecopy  --  very fast buffered file copy
  26.  *
  27.  *  Usage:  i = ffilecopy (here,there)
  28.  *    int i;
  29.  *    FILE *here, *there;
  30.  *
  31.  *  Ffilecopy is a very fast routine to copy the rest of a buffered
  32.  *  input file to a buffered output file.  Here and there are open
  33.  *  buffers for reading and writing (respectively); ffilecopy
  34.  *  performs a file-copy faster than you should expect to do it
  35.  *  yourself.  Ffilecopy returns 0 if everything was OK; EOF if
  36.  *  there was any error.  Normally, the input file will be left in
  37.  *  EOF state (feof(here) will return TRUE), and the output file will be
  38.  *  flushed (i.e. all data on the file rather in the core buffer).
  39.  *  It is not necessary to flush the output file before ffilecopy.
  40.  *
  41.  *  HISTORY
  42.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  43.  *    Created for VAX.
  44.  *
  45.  */
  46.  
  47. #include <stdio.h>
  48. int filecopy();
  49.  
  50. int ffilecopy (here,there)
  51. FILE *here, *there;
  52. {
  53.     register int i, herefile, therefile;
  54.  
  55.     herefile = fileno(here);
  56.     therefile = fileno(there);
  57.  
  58.     if (fflush (there) == EOF)        /* flush pending output */
  59.         return (EOF);
  60.  
  61. #if    defined(__386BSD__) || defined(__NetBSD__)
  62.     if ((here->_r) > 0) {            /* flush buffered input */
  63.         i = write (therefile, here->_p, here->_r);
  64.         if (i != here->_r)  return (EOF);
  65.         here->_p = here->_bf._base;
  66.         here->_r = 0;
  67.     }
  68. #else
  69.     if ((here->_cnt) > 0) {            /* flush buffered input */
  70.         i = write (therefile, here->_ptr, here->_cnt);
  71.         if (i != here->_cnt)  return (EOF);
  72.         here->_ptr = here->_base;
  73.         here->_cnt = 0;
  74.     }
  75. #endif
  76.     i = filecopy (herefile, therefile);    /* fast file copy */
  77.     if (i < 0)  return (EOF);
  78.  
  79. #if    defined(__386BSD__) || defined(__NetBSD__)
  80.     (here->_flags) |= __SEOF;        /* indicate EOF */
  81. #else
  82.     (here->_flag) |= _IOEOF;        /* indicate EOF */
  83. #endif
  84.     return (0);
  85. }
  86.