home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1694 / fcopy.c next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.3 KB  |  86 lines

  1. /******************************************************************************
  2. *                                                                             *
  3. *                                  fcopy.c                                    *
  4. *                                                                             *
  5. ******************************************************************************/
  6.  
  7. /*--------------------------  INITIAL CODING DATE -----------------------------
  8. Thu Nov 10 10:56:37 EST 1988 by George M. Bogatko
  9.  
  10. --------------------------------  HEADER FILES  -----------------------------*/
  11. #include <stdio.h>
  12. #include <fpipe.h>
  13. #include <fcntl.h>
  14.  
  15. /*------------------  TYPEDEF'S, DEFINES, STRUCTURE DEF'S  ------------------*/
  16.  
  17. /*----------------  IMPORTED GLOBAL VARIABLE/FUNCTION DEF'S  ----------------*/
  18. extern char *fgets();
  19.  
  20. /*----------------  EXPORTED GLOBAL VARIABLE/FUNCTION DEF'S  ----------------*/
  21.  
  22. /*----------------  INTERNAL GLOBAL VARIABLE/FUNCTION DEF'S  ----------------*/
  23. #ident "%W% %G% - George M. Bogatko -"
  24.  
  25. /*-----------------------------------------------------------------------------
  26.  
  27. SYNOPSIS:
  28.     fcopy(in_fd, out_fname, flag)
  29.  
  30. DESCRIPTION:
  31.     FCOPY copies lines from an IN_FD (maybe a pipe) to a standard
  32.     file.  The arguments are:
  33.  
  34.     in_fd - a 'FILE *' type file descriptor
  35.  
  36.     out_fname - The file you want to write to
  37.  
  38.     flag - Whether your want the READ to be BLOCK or NON_BLOCK
  39.  
  40.         with BLOCK, the fgets will block (in the case of pipes,
  41.             a block occurs if there is nothing in the pipe
  42.             to read.
  43.  
  44.         with NON_BLOCK, the first READ is BLOCKED, (to allow
  45.             pipe synchronization) and subsequent reads are
  46.             NON_BLOCK.
  47.  
  48. RETURN:
  49.     0 on success
  50.     -1 on error  (use perror())
  51.  
  52. =============================================================================*/
  53.  
  54. int fcopy(in_fp, outname, blockflag)
  55. FILE *in_fp;
  56. char *outname;
  57. int blockflag;
  58. {
  59. FILE *out_fp;
  60. char buf[BUFSIZ];
  61. enum {first, next} time = first;
  62.  
  63.     if( (out_fp = fopen(outname, "w")) == (FILE *)NULL )
  64.         return -1;
  65.  
  66.     while( fgets(buf, BUFSIZ, in_fp) != (char *)NULL )
  67.     {
  68.         if( time == first )
  69.         {
  70.             if( blockflag == NO_BLOCK )
  71.             {
  72.                 if(fcntl( fileno(in_fp), F_SETFL, O_NDELAY ) == -1 )
  73.                 {
  74.                     perror("");
  75.                     fclose(out_fp);
  76.                     return -1;
  77.                 }
  78.             }
  79.             time = next;
  80.         }
  81.         fputs(buf, out_fp);
  82.     }
  83.     fclose(out_fp);
  84.     return 0;
  85. }
  86.