nkiss

WIP
git clone git://git.nihaljere.xyz/nkiss
Log | Files | Refs

commit 12eda77969230f52c43a249bde3d3bdad6a83715
parent 21c15082bac137408154535db95264f9d7c7c3df
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Sun, 14 Mar 2021 14:25:25 -0500

util.*: add cp to copy files

Diffstat:
Mutil.c | 26++++++++++++++++++++++++++
Mutil.h | 1+
2 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/util.c b/util.c @@ -151,6 +151,32 @@ err: return -1; } +int +cp(char *oldpath, char *newpath) +{ + char buf[BUFSIZ]; + FILE *old = NULL, *new = NULL; + int c; + + if ((old = fopen(oldpath, "r")) == NULL) + goto err; + + if ((new = fopen(newpath, "w")) == NULL) + goto err; + + while ((c = fread(buf, sizeof(char), BUFSIZ, old)) > 0) + if (fwrite(buf, sizeof(char), c, new) < c) + goto err; + + fclose(old); + fclose(new); + + return 0; +err: + if (old) fclose(old); + if (new) fclose(new); + return -1; +} int mv(char *oldpath, char *newpath) diff --git a/util.h b/util.h @@ -23,6 +23,7 @@ int strcmpcb(const void *p1, const void *p2); void strrepl(char *str, const char a, const char b); int mkdirs(const char *path); int diff(struct diff_t *diff, char **a, char **b); +int cp(char *oldpath, char *newpath); int mv(char *oldpath, char *newpath); int file_dropadd(char *path, char *a, char *b);