copyfile.c

copyfile.c
/* Copy a file                                       W20010401*/
/* This program illustrates basic file operations in Unix.    */
/* Warning: This is a dangerous program!                      */
/*          It is not a replacement for cp!                   */
/* Copyright & Disclaimer: See bottom of document             */
/*                                                            */
/* Usage:                                                     */
/* $ copyfile <source_file> <dest_file>                       */
 
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
 
main(int argc, char *argv[])	/* argc: arg count  */
				/* argv: arg vector */
{
 
char	buffer[512];		/* declare i/o buffer */
int	fd1;			/* file descriptor 1 */
int	fd2;			/* file descriptor 2 */
int	in;			/* nr. of bytes input */
int	out;			/* nr. of bytes output */
 
 
fd1 = open(argv[1], O_RDONLY);
 
   fprintf(stderr, "fd1: %d\n", fd1);  /* print fd1 */
   if (fd1 < 0) exit(2);
 
fd2 = open(argv[2], O_WRONLY|O_CREAT, 0644);
 
   fprintf(stderr, "fd2: %d\n", fd2);  /* print fd2 */
   if (fd2 < 0) exit(3);
 
 
while (1)
{
in = read(fd1, buffer, sizeof(buffer));
   fprintf(stderr, "in : %d\n", in);   /* print in bytes */
   if (in <= 0) break;
 
out = write(fd2, buffer, in);
   fprintf(stderr, "out: %d\n", out);  /* print out bytes */
   if (out < 0) exit (8);
}
 
close(fd1);
close(fd2);
exit (0);
 
} /* end of program */
 
/*
Copyright (C) 2003 Integrated Services; Tux4u.nl
Author: Ing.J.M.Waldorp
        copyfile.c 20030514
 
License: GPL v2
See: http://www.tux4u.nl
 or: http://www.fsf.org
 
Disclaimer
 
This document is provided "as is" in the hope that it will be useful,
but WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
including, but not limited to, the implied warranty of FITNESS FOR A
PARTICULAR PURPOSE. Integrated Services DISAVOWS ANY POTENTIAL
LIABILITY for the contents of this document. The use of the concepts,
examples and/or other content of this document is ENTIRELY AT YOUR
OWN RISK.
 
Use of a term in this document should not be regarded as affecting
the validity of any trade mark or service mark. Naming of particular
products or brands should not be regarded as endorsements.
*/