/[cvs]/eggdrop1.9/src/misc_file.c
ViewVC logotype

Annotation of /eggdrop1.9/src/misc_file.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (hide annotations) (download) (as text)
Wed Mar 22 00:42:57 2000 UTC (19 years, 10 months ago) by fabian
Branch: MAIN
CVS Tags: eggdrop10503, eggdrop10403
File MIME type: text/x-chdr
configure_makefile_fix3 patch

1 fabian 1.1 /*
2     * misc.c -- handles:
3     * copyfile() movefile()
4     *
5     * $Id$
6     */
7     /*
8     * Copyright (C) 1997 Robey Pointer
9     * Copyright (C) 1999, 2000 Eggheads
10     *
11     * This program is free software; you can redistribute it and/or
12     * modify it under the terms of the GNU General Public License
13     * as published by the Free Software Foundation; either version 2
14     * of the License, or (at your option) any later version.
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19     * GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU General Public License
22     * along with this program; if not, write to the Free Software
23     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24     */
25    
26     #include "main.h"
27     #include <sys/stat.h>
28     #include <unistd.h>
29     #include <fcntl.h>
30     #include "stat.h"
31    
32    
33     /* Copy a file from one place to another (possibly erasing old copy).
34     *
35     * returns: 0 if OK
36     * 1 if can't open original file
37     * 2 if can't open new file
38     * 3 if original file isn't normal
39     * 4 if ran out of disk space
40     */
41     int copyfile(char *oldpath, char *newpath)
42     {
43     int fi, fo, x;
44     char buf[512];
45     struct stat st;
46    
47     fi = open(oldpath, O_RDONLY, 0);
48     if (fi < 0)
49     return 1;
50     fstat(fi, &st);
51     if (!(st.st_mode & S_IFREG))
52     return 3;
53     fo = creat(newpath, (int) (st.st_mode & 0777));
54     if (fo < 0) {
55     close(fi);
56     return 2;
57     }
58     for (x = 1; x > 0;) {
59     x = read(fi, buf, 512);
60     if (x > 0) {
61     if (write(fo, buf, x) < x) { /* Couldn't write */
62     close(fo);
63     close(fi);
64     unlink(newpath);
65     return 4;
66     }
67     }
68     }
69     close(fo);
70     close(fi);
71     return 0;
72     }
73    
74     int movefile(char *oldpath, char *newpath)
75     {
76     int ret;
77    
78     #ifdef HAVE_RENAME
79     /* Try to use rename first */
80     if (rename(oldpath, newpath) == 0)
81     return 0;
82     #endif /* HAVE_RENAME */
83    
84     /* If that fails, fall back to just copying and then
85     * deleting the file.
86     */
87     ret = copyfile(oldpath, newpath);
88     if (ret == 0)
89     unlink(oldpath);
90     return ret;
91     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23