From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/OR_PRACTICAL_C/13_2.c | 70 +++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 reference/C/CONTRIB/OR_PRACTICAL_C/13_2.c (limited to 'reference/C/CONTRIB/OR_PRACTICAL_C/13_2.c') diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/13_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/13_2.c new file mode 100644 index 0000000..a1dd50c --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/13_2.c @@ -0,0 +1,70 @@ +/**************************************** + * copy -- copy one file to another. * + * * + * Usage * + * copy * + * * + * -- the file to copy from * + * -- the file to copy into * + ****************************************/ +#include +#ifndef __MSDOS__ /* if we are not MS-DOS */ +#define __UNIX__ /* then we are UNIX */ +#endif __MSDOS__ + +#include /* ANSI Standard C file */ + +#ifdef __UNIX__ +#include /* file defines for UNIX filesystem */ +#include +#include +#endif __UNIX__ + +#ifdef __MSDOS__ +#include /* file defines for DOS filesystem */ +#include +#include +#endif __MSDOS__ + +void exit(); /* lib routine */ + +#define BUFFER_SIZE (16 * 1024) /* use 16K buffers */ + +main(int argc, char *argv[]) +{ + char buffer[BUFFER_SIZE]; /* buffer for data */ + int in_file; /* input file descriptor */ + int out_file; /* output file descriptor */ + int read_size; /* number of bytes on last read */ + + if (argc != 3) { + (void) fprintf(stderr, "Error:Wrong number of arguments\n"); + (void) fprintf(stderr, "Usage is: copy \n"); + exit(8); + } + in_file = open(argv[1], O_RDONLY); + if (in_file < 0) { + (void) fprintf("Error:Unable to open %s\n", argv[1]); + exit(8); + } + out_file = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0666); + if (out_file < 0) { + (void) fprintf("Error:Unable to open %s\n", argv[2]); + exit(8); + } + while (1) { + read_size = read(in_file, buffer, sizeof(buffer)); + + if (read_size == 0) + break; /* end of file */ + + if (read_size < 0) { + (void) fprintf(stderr, "Error:Read error\n"); + exit(8); + } + (void) write(out_file, buffer, (unsigned int) read_size); + } + (void) close(in_file); + (void) close(out_file); + return (0); +} -- cgit v1.2.3-54-g00ecf