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

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

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


Revision 1.1.1.1 - (hide annotations) (download) (as text) (vendor branch)
Mon Jul 26 21:11:06 2010 UTC (9 years, 3 months ago) by simple
Branch: eggheads, MAIN
CVS Tags: v1, HEAD
Branch point for: gettext
Changes since 1.1: +0 -0 lines
File MIME type: text/x-chdr
Imported Eggdrop 1.6.20

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23