| iMatix home page | << | < | > | >> |
SFL Version 2.11 |
#include "sflfile.h" int file_copy ( const char *dest, const char *src, char mode)
Copies a file called src to one called dest. The dest file may not already exist. If mode is 'b', copies a binary file; if mode is 't', copies a text file. This distinction only applies to MS-DOS file systems; on other platforms the two modes are equivalent. Returns 0 if no problems occurred, -1 if an error occurred, 1 if the destination file already exists.
{ FILE *inf, *outf; char *buffer, openmode [3] = "??"; size_t chars_read; /* Amount read from stream */ int feedback = 0; ASSERT (dest); ASSERT (src); if (file exists (dest)) return (1); /* Cancel: dest already exists */ # if (defined (MSDOS_FILESYSTEM)) if (system_devicename (dest) || system_devicename (src)) return (-1); /* Not allowed on device names */ # endif # if (defined (MSDOS_FILESYSTEM)) openmode [1] = mode; # else openmode [1] = 0; # endif openmode [0] = 'r'; if ((inf = fopen (src, openmode)) == NULL) return (-1); /* Input file not found */ if ((buffer = mem_alloc (SHRT_MAX)) == NULL) feedback = -1; /* Insufficient memory for buffer */ else { openmode [0] = 'w'; if ((outf = fopen (dest, openmode)) == NULL) { mem_free (buffer); return (-1); /* Could not create output file */ } while ((chars_read = fread (buffer, 1, SHRT_MAX, inf)) != 0) if (fwrite (buffer, 1, chars_read, outf) != chars_read) { feedback = -1; break; } fclose (outf); mem_free (buffer); } fclose (inf); return (feedback); }
| << | < | > | >> | Copyright © 1996-2000 iMatix Corporation |