GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
copy_file.c
Go to the documentation of this file.
1 
2 /****************************************************************************
3  *
4  * MODULE: GRASS GIS library - copy_file.c
5  * AUTHOR(S): Paul Kelly
6  * PURPOSE: Function to copy one file to another.
7  * COPYRIGHT: (C) 2007 by the GRASS Development Team
8  *
9  * This program is free software under the GNU General Public
10  * License (>=v2). Read the file COPYING that comes with GRASS
11  * for details.
12  *
13  *****************************************************************************/
14 
15 #include <stdio.h>
16 #include <errno.h>
17 #include <string.h>
18 
19 #include <grass/gis.h>
20 
21 /**
22  * \brief Copies one file to another
23  *
24  * Creates a copy of a file. The destination file will be overwritten if it
25  * already exists, so the calling module should check this first if it is
26  * important.
27  *
28  * \param infile String containing path to source file
29  * \param outfile String containing path to destination file
30  *
31  * \return 1 on success; 0 if an error occurred (warning will be printed)
32  **/
33 
34 int G_copy_file(const char *infile, const char *outfile)
35 {
36  FILE *infp, *outfp;
37  int inchar, outchar;
38 
39  infp = fopen(infile, "r");
40  if (infp == NULL) {
41  G_warning("Cannot open %s for reading: %s", infile, strerror(errno));
42  return 0;
43  }
44 
45  outfp = fopen(outfile, "w");
46  if (outfp == NULL) {
47  G_warning("Cannot open %s for writing: %s", outfile, strerror(errno));
48  return 0;
49  }
50 
51  while ((inchar = getc(infp)) != EOF) {
52  /* Read a character at a time from infile until EOF
53  * and copy to outfile */
54  outchar = putc(inchar, outfp);
55  if (outchar != inchar) {
56  G_warning("Error writing to %s", outfile);
57  return 0;
58  }
59  }
60  fflush(outfp);
61 
62  fclose(infp);
63  fclose(outfp);
64 
65  return 1;
66 }
#define NULL
Definition: ccmath.h:32
int G_copy_file(const char *infile, const char *outfile)
Copies one file to another.
Definition: copy_file.c:34
void G_warning(const char *,...) __attribute__((format(printf