home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- * *
- * fcopy.c *
- * *
- ******************************************************************************/
-
- /*-------------------------- INITIAL CODING DATE -----------------------------
- Thu Nov 10 10:56:37 EST 1988 by George M. Bogatko
-
- -------------------------------- HEADER FILES -----------------------------*/
- #include <stdio.h>
- #include <fpipe.h>
- #include <fcntl.h>
-
- /*------------------ TYPEDEF'S, DEFINES, STRUCTURE DEF'S ------------------*/
-
- /*---------------- IMPORTED GLOBAL VARIABLE/FUNCTION DEF'S ----------------*/
- extern char *fgets();
-
- /*---------------- EXPORTED GLOBAL VARIABLE/FUNCTION DEF'S ----------------*/
-
- /*---------------- INTERNAL GLOBAL VARIABLE/FUNCTION DEF'S ----------------*/
- #ident "%W% %G% - George M. Bogatko -"
-
- /*-----------------------------------------------------------------------------
-
- SYNOPSIS:
- fcopy(in_fd, out_fname, flag)
-
- DESCRIPTION:
- FCOPY copies lines from an IN_FD (maybe a pipe) to a standard
- file. The arguments are:
-
- in_fd - a 'FILE *' type file descriptor
-
- out_fname - The file you want to write to
-
- flag - Whether your want the READ to be BLOCK or NON_BLOCK
-
- with BLOCK, the fgets will block (in the case of pipes,
- a block occurs if there is nothing in the pipe
- to read.
-
- with NON_BLOCK, the first READ is BLOCKED, (to allow
- pipe synchronization) and subsequent reads are
- NON_BLOCK.
-
- RETURN:
- 0 on success
- -1 on error (use perror())
-
- =============================================================================*/
-
- int fcopy(in_fp, outname, blockflag)
- FILE *in_fp;
- char *outname;
- int blockflag;
- {
- FILE *out_fp;
- char buf[BUFSIZ];
- enum {first, next} time = first;
-
- if( (out_fp = fopen(outname, "w")) == (FILE *)NULL )
- return -1;
-
- while( fgets(buf, BUFSIZ, in_fp) != (char *)NULL )
- {
- if( time == first )
- {
- if( blockflag == NO_BLOCK )
- {
- if(fcntl( fileno(in_fp), F_SETFL, O_NDELAY ) == -1 )
- {
- perror("");
- fclose(out_fp);
- return -1;
- }
- }
- time = next;
- }
- fputs(buf, out_fp);
- }
- fclose(out_fp);
- return 0;
- }
-