diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2021-07-03 16:04:50 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2021-07-03 16:04:50 +0200 |
commit | 8a11a18dc56d1f77661e84fe470e34f9b3bcdb11 (patch) | |
tree | 7bda9d4dd6515e35dc1cc4835cc10ea98d67c0e1 /src | |
parent | 9432b44a2b73acddcb3cc9ac2568a6c1e86204bc (diff) | |
download | weechat-8a11a18dc56d1f77661e84fe470e34f9b3bcdb11.zip |
api: add function file_copy (issue #1667)
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-dir.c | 56 | ||||
-rw-r--r-- | src/core/wee-dir.h | 1 | ||||
-rw-r--r-- | src/plugins/plugin.c | 1 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 5 |
4 files changed, 62 insertions, 1 deletions
diff --git a/src/core/wee-dir.c b/src/core/wee-dir.c index aae69b267..bb5ac16f6 100644 --- a/src/core/wee-dir.c +++ b/src/core/wee-dir.c @@ -556,6 +556,62 @@ error: } /* + * 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. * * If 4 paths are given, they must be separated by colons and given in this diff --git a/src/core/wee-dir.h b/src/core/wee-dir.h index 489ca2a64..9e368c596 100644 --- a/src/core/wee-dir.h +++ b/src/core/wee-dir.h @@ -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 (); diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 6254b613b..7deb32cc6 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -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; diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 9eb42f7e9..b509e952e 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -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) \ |