diff options
author | Waldemar Brodkorb <wbx@uclibc-ng.org> | 2018-02-25 13:53:05 +0000 |
---|---|---|
committer | Waldemar Brodkorb <wbx@uclibc-ng.org> | 2018-02-25 13:53:11 +0000 |
commit | 9d7518c2cb43f1fb1eb54495899945523fd5dc99 (patch) | |
tree | 57fcfe56162934f5f78b3b22920623546c8d126c /package/toolbox/src/src/mv.c | |
parent | 30f5a076475d08daa882fa2e636a19200877aa33 (diff) |
toolbox: add OpenADK toolbox for very small systems, thx Thorsten Glaser
Diffstat (limited to 'package/toolbox/src/src/mv.c')
-rw-r--r-- | package/toolbox/src/src/mv.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/package/toolbox/src/src/mv.c b/package/toolbox/src/src/mv.c new file mode 100644 index 000000000..d5406b8ac --- /dev/null +++ b/package/toolbox/src/src/mv.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <limits.h> +#include <sys/stat.h> +#include <sys/types.h> + + +int main(int argc, char *argv[]) +{ + const char* dest; + struct stat st; + int i; + + if (argc < 3) { + fprintf(stderr,"USAGE: %s <source...> <destination>\n", argv[0]); + return -1; + } + + /* check if destination exists */ + dest = argv[argc - 1]; + if (stat(dest, &st)) { + /* an error, unless the destination was missing */ + if (errno != ENOENT) { + fprintf(stderr, "failed on %s - %s\n", dest, strerror(errno)); + return -1; + } + st.st_mode = 0; + } + + for (i = 1; i < argc - 1; i++) { + const char *source = argv[i]; + char fullDest[PATH_MAX + 1 + PATH_MAX + 1]; + /* assume we build "dest/source", and let rename() fail on pathsize */ + if (strlen(dest) + 1 + strlen(source) + 1 > sizeof(fullDest)) { + fprintf(stderr, "path too long\n"); + return -1; + } + strcpy(fullDest, dest); + + /* if destination is a directory, concat the source file name */ + if (S_ISDIR(st.st_mode)) { + const char *fileName = strrchr(source, '/'); + if (fullDest[strlen(fullDest)-1] != '/') { + strcat(fullDest, "/"); + } + strcat(fullDest, fileName ? fileName + 1 : source); + } + + /* attempt to move it */ + if (rename(source, fullDest)) { + fprintf(stderr, "failed on '%s' - %s\n", source, strerror(errno)); + return -1; + } + } + + return 0; +} + |