1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-08 02:33:12 +02:00

api: add function file_copy (issue #1667)

This commit is contained in:
Sébastien Helleu
2021-07-03 16:04:50 +02:00
parent 9432b44a2b
commit 8a11a18dc5
11 changed files with 271 additions and 18 deletions
+56
View File
@@ -555,6 +555,62 @@ error:
return NULL;
}
/*
* Copies a file to another location.
*
* Returns:
* 1: OK
* 0: error
*/
int
dir_file_copy (const char *from, const char *to)
{
FILE *src, *dst;
char *buffer;
int rc;
size_t count;
rc = 0;
buffer = NULL;
src = NULL;
dst = NULL;
if (!from || !from[0] || !to || !to[0])
goto end;
buffer = malloc (65536);
if (!buffer)
goto end;
src = fopen (from, "rb");
if (!src)
goto end;
dst = fopen (to, "wb");
if (!dst)
goto end;
while (!feof (src))
{
count = fread (buffer, 1, 65535, src);
if (count <= 0)
goto end;
if (fwrite (buffer, 1, count, dst) <= 0)
goto end;
}
rc = 1;
end:
if (buffer)
free (buffer);
if (src)
fclose (src);
if (dst)
fclose (dst);
return rc;
}
/*
* Uses one or four different paths for WeeChat home directories.
*
+1
View File
@@ -33,6 +33,7 @@ extern void dir_exec_on_files (const char *directory, int recurse_subdirs,
extern char *dir_search_full_lib_name (const char *filename,
const char *sys_directory);
extern char *dir_file_get_content (const char *filename);
extern int dir_file_copy (const char *from, const char *to);
extern void dir_create_home_dirs ();
extern void dir_remove_home_dirs ();
extern char *dir_get_string_home_dirs ();