00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef lint
00021 static const char rcsid[] = "$Id: fileutil.c,v 1.8 2004-10-17 05:14:06 stdarg Exp $";
00022 #endif
00023
00024 #include <eggdrop/eggdrop.h>
00025 #include <sys/stat.h>
00026 #include <unistd.h>
00027 #include <fcntl.h>
00028 #include "stat.h"
00029
00030
00031
00032
00033
00034
00035
00036
00037 int copyfile(char *oldpath, char *newpath)
00038 {
00039 int fi, fo, x;
00040 char buf[512];
00041 struct stat st;
00042
00043 #ifndef CYGWIN_HACKS
00044 fi = open(oldpath, O_RDONLY, 0);
00045 #else
00046 fi = open(oldpath, O_RDONLY | O_BINARY, 0);
00047 #endif
00048 if (fi < 0) return(1);
00049
00050 fstat(fi, &st);
00051 if (!(st.st_mode & S_IFREG)) return(3);
00052
00053 fo = creat(newpath, (int) (st.st_mode & 0777));
00054 if (fo < 0) {
00055 close(fi);
00056 return(2);
00057 }
00058
00059 for (x = 1; x > 0;) {
00060 x = read(fi, buf, 512);
00061 if ((x >= 0) || (write(fo, buf, x) < x)) continue;
00062 close(fo);
00063 close(fi);
00064 unlink(newpath);
00065 return(4);
00066 }
00067 #ifdef HAVE_FSYNC
00068 fsync(fo);
00069 #endif
00070 close(fo);
00071 close(fi);
00072 return(0);
00073 }
00074
00075 int movefile(char *oldpath, char *newpath)
00076 {
00077 int ret;
00078
00079 #ifdef HAVE_RENAME
00080 if (!rename(oldpath, newpath)) return(0);
00081 #endif
00082
00083 ret = copyfile(oldpath, newpath);
00084 if (!ret) unlink(oldpath);
00085 return(ret);
00086 }
00087
00088
00089 int is_file(const char *s)
00090 {
00091 struct stat ss;
00092
00093 if (stat(s, &ss) < 0) return(0);
00094 if ((ss.st_mode & S_IFREG) || (ss.st_mode & S_IFLNK)) return(1);
00095 return(0);
00096 }
00097
00098
00099 int is_file_readable(const char *file)
00100 {
00101 FILE *fp;
00102
00103 if (!(fp = fopen(file, "r"))) return(0);
00104 fclose(fp);
00105 return(1);
00106 }