summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htlib/filecopy.cc
blob: 4cd23e85b38a8b20980ca95007e8f5b7378ad66e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// filecopy.c
//
//  Copies files from one file to another.
//  Contains both Unix & Native Win32 Implementations
//
// Part of the ht://Dig package   <http://www.htdig.org/>
// Copyright (c) 2003 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later or later
// <http://www.gnu.org/copyleft/lgpl.html>
// 
// Copyright (c) 2002 RightNow Technologies, Inc.
// Donated to The ht://Dig Group under LGPL License
 
#include <stdio.h>

#ifdef _WIN32

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#else /* UNIX */

#include <stdio.h>
#include <sys/types.h>        
#include <sys/stat.h>        
#include <fcntl.h>                                                                             
#include <unistd.h>
#include <errno.h>

#endif /* _WIN32 | UNIX  */


#include "filecopy.h" 



//----------------------------------------------------------------------
// int file_copy (char * from, char * to, char flags)
//----------------------------------------------------------------------
//
// copy file 'from' -> 'to'
// 
// set flags to FILECOPY_OVERWRITE_ON to overwrite the 'to' file if 
// it exists
//
// set flags to FILECOPY_OVERWRITE_OFF to not overwrite the 'to' file
// if it exists
//
// returns 0/FALSE if unsucessful
// returns 1/TRUE if  sucessful
//
//
#ifdef _WIN32

int file_copy (char * from, char * to, char flags)
{
    if (flags == FILECOPY_OVERWRITE_ON)
    {
        //overwrite
        if (TRUE != (CopyFile(from , to, FALSE)))
            return (FALSE);
    }
    else if (flags == FILECOPY_OVERWRITE_OFF)
    {
         //don't overwrite
        if (TRUE != (CopyFile(from , to, TRUE)))
            return (FALSE);
    }
    else //bad flag
    {
        return (FALSE);
    }
    
    return (TRUE);
}

#else //UNIX
        
int file_copy (char * from, char * to, char flags)
{
    size_t nmemb;
    //int nmemb;
    FILE *ifp, *ofp;
    char buf[BUFSIZ];

    if (flags == FILECOPY_OVERWRITE_OFF) {
        if (access(to, F_OK) == 0) {
            //OUTLOG((FUNC, TRWRN, "file %s already exists\n", to));
            return(FALSE);
        }
        else if (errno != ENOENT) {
            //OUTLOG((FUNC, TRERR, "access(%s, F_OK) failed\n", to));
            return(FALSE);
        }
    }

    if ((ifp=fopen(from, "r")) == NULL) {
        //OUTLOG((FUNC, TRERR, "%s doesn't exist\n", from));
        return(FALSE);
    }
    
    if ((ofp=fopen(to, "w+")) == NULL) {
        //OUTLOG((FUNC, TRERR, "can't create %s\n", to));
        fclose(ifp);
        return(FALSE);
    }

    while ((nmemb=fread(buf, 1, sizeof(buf), ifp)) > 0) {
        if (fwrite(buf, 1, nmemb, ofp) != nmemb) {
            //OUTLOG((FUNC, TRERR, "fwrite failed\n"));
            fclose(ifp);
            fclose(ofp);
            return(FALSE);
        }
    }

    fclose(ifp);
    fclose(ofp);

    return (TRUE);
}

#endif /* _WIN32 | UNIX  */