mirror of
https://github.com/weechat/weechat.git
synced 2026-07-07 10:13:12 +02:00
api: add function file_copy (issue #1667)
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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 ();
|
||||
|
||||
@@ -672,6 +672,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv)
|
||||
new_plugin->mkdir_parents = &dir_mkdir_parents;
|
||||
new_plugin->exec_on_files = &dir_exec_on_files;
|
||||
new_plugin->file_get_content = &dir_file_get_content;
|
||||
new_plugin->file_copy = &dir_file_copy;
|
||||
|
||||
new_plugin->util_timeval_cmp = &util_timeval_cmp;
|
||||
new_plugin->util_timeval_diff = &util_timeval_diff;
|
||||
|
||||
@@ -68,7 +68,7 @@ struct timeval;
|
||||
* please change the date with current one; for a second change at same
|
||||
* date, increment the 01, otherwise please keep 01.
|
||||
*/
|
||||
#define WEECHAT_PLUGIN_API_VERSION "20210601-01"
|
||||
#define WEECHAT_PLUGIN_API_VERSION "20210703-01"
|
||||
|
||||
/* macros for defining plugin infos */
|
||||
#define WEECHAT_PLUGIN_NAME(__name) \
|
||||
@@ -395,6 +395,7 @@ struct t_weechat_plugin
|
||||
void (*callback)(void *data, const char *filename),
|
||||
void *callback_data);
|
||||
char *(*file_get_content) (const char *filename);
|
||||
int (*file_copy) (const char *from, const char *to);
|
||||
|
||||
/* util */
|
||||
int (*util_timeval_cmp) (struct timeval *tv1, struct timeval *tv2);
|
||||
@@ -1374,6 +1375,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
|
||||
__callback, __callback_data)
|
||||
#define weechat_file_get_content(__filename) \
|
||||
(weechat_plugin->file_get_content)(__filename)
|
||||
#define weechat_file_copy(__from, __to) \
|
||||
(weechat_plugin->file_copy)(__from, __to)
|
||||
|
||||
/* util */
|
||||
#define weechat_util_timeval_cmp(__time1, __time2) \
|
||||
|
||||
Reference in New Issue
Block a user