src/misc_file.c File Reference

#include "main.h"
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "stat.h"
Include dependency graph for misc_file.c:

Go to the source code of this file.

Functions

int copyfile (char *oldpath, char *newpath)
int movefile (char *oldpath, char *newpath)
int file_readable (char *file)

Function Documentation

int copyfile ( char *  oldpath,
char *  newpath 
)

Definition at line 40 of file misc_file.c.

References S_IFREG.

00041 {
00042   int fi, fo, x;
00043   char buf[512];
00044   struct stat st;
00045 
00046 #ifndef CYGWIN_HACKS
00047   fi = open(oldpath, O_RDONLY, 0);
00048 #else
00049   fi = open(oldpath, O_RDONLY | O_BINARY, 0);
00050 #endif
00051   if (fi < 0)
00052     return 1;
00053   fstat(fi, &st);
00054   if (!(st.st_mode & S_IFREG))
00055     return 3;
00056   fo = creat(newpath, (int) (st.st_mode & 0777));
00057   if (fo < 0) {
00058     close(fi);
00059     return 2;
00060   }
00061   for (x = 1; x > 0;) {
00062     x = read(fi, buf, 512);
00063     if (x > 0) {
00064       if (write(fo, buf, x) < x) {      /* Couldn't write */
00065         close(fo);
00066         close(fi);
00067         unlink(newpath);
00068         return 4;
00069       }
00070     }
00071   }
00072 #ifdef HAVE_FSYNC
00073   fsync(fo);
00074 #endif /* HAVE_FSYNC */
00075   close(fo);
00076   close(fi);
00077   return 0;
00078 }

int file_readable ( char *  file  ) 

Definition at line 99 of file misc_file.c.

00100 {
00101   FILE *fp;
00102 
00103   if (!(fp = fopen(file, "r")))
00104     return 0;
00105 
00106   fclose(fp);
00107   return 1;
00108 }

int movefile ( char *  oldpath,
char *  newpath 
)

Definition at line 80 of file misc_file.c.

References copyfile.

00081 {
00082   int ret;
00083 
00084 #ifdef HAVE_RENAME
00085   /* Try to use rename first */
00086   if (!rename(oldpath, newpath))
00087     return 0;
00088 #endif /* HAVE_RENAME */
00089 
00090   /* If that fails, fall back to just copying and then
00091    * deleting the file.
00092    */
00093   ret = copyfile(oldpath, newpath);
00094   if (!ret)
00095     unlink(oldpath);
00096   return ret;
00097 }


Generated on 7 Sep 2016 for Eggdrop by  doxygen 1.6.1