From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/mv.c | 136 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/mv.c (limited to 'reference/C/CONTRIB/SNIP/mv.c') diff --git a/reference/C/CONTRIB/SNIP/mv.c b/reference/C/CONTRIB/SNIP/mv.c new file mode 100755 index 0000000..a049d45 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/mv.c @@ -0,0 +1,136 @@ +/* +** mv.c -- move or rename files or directories +** updated for multiple files, 5 jul 92, rlm +** placed in the public domain via C_ECHO by the author, Ray McVay +** +** modified by Bob Stout, 28 Mar 93 +** modified by Bob Stout, 4 Jun 93 +** +** uses file_copy from SNIPPETS file WB_FCOPY.C +*/ + +#include +#include +#include +#include + +/* For portability, make everything look like MSC 6 */ + +#if defined(__TURBOC__) + #include + #define _dos_findfirst(f,a,b) findfirst(f,b,a) + #define find_t ffblk + #define _A_SUBDIR FA_DIREC + #define attrib ff_attrib +#else /* assume MSC/QC */ + #include +#endif + +/* +** Tell 'em they messed up +*/ + +void help(char *s) +{ + puts("usage: mv "); + printf("error: %s\n", s); +} + +/* +** Simple directory test +*/ + +isdir(char *path) +{ + struct find_t f; + + /* "Raw" drive specs are always directories */ + + if (':' == path[1] && '\0' == path[2]) + return 1; + + return (_dos_findfirst(path, _A_SUBDIR, &f) == 0 && + (f.attrib & _A_SUBDIR)); +} + +/* +** Use rename or copy and delete +*/ + +int mv(char *src, char *dest) +{ + int errcount = 0; + char buf[FILENAME_MAX]; + const char *generr = "ERROR: mv - couldn't %s %s %s\n"; + + if (':' == dest[1] && *dest != *getcwd(buf, FILENAME_MAX)) + { + if (file_copy(src, dest)) + { + printf(generr, "move", src, dest); + ++errcount; + } + else if (unlink(src)) + { + printf(generr, "delete", src, ""); + ++errcount; + } + } + else + { + if (rename(src, dest)) + { + printf(generr, "rename", src, dest); + ++errcount; + } + } + return errcount; +} + +/* +** Enter here +*/ + +int main(int argc, char **argv) +{ + int src, errcount = 0; + char target[FILENAME_MAX]; + + puts("mv 1.3 (4 jun 93) - Ray L. McVay/Bob Stout"); + if (argc < 3) + help("Not enough parameters"); + + /* + ** Handle cases where target is a directory + */ + + else if (isdir(argv[argc -1])) + { + for (src = 1; src < argc - 1; src++) + { + char termch; + + strcpy(target, argv[argc - 1]); + termch = target[strlen(target) - 1]; + if ('\\' != termch && ':' != termch) + strcat(target, "\\"); + + if (strrchr(argv[src], '\\')) + strcat(target, strrchr(argv[src], '\\') + 1); + else if (argv[src][1] == ':') + strcat(target, argv[src] + 2); + else strcat(target, argv[src]); + + errcount += mv(argv[src], target); + } + } + + /* + ** Nothing left except 2 explicit file names + */ + + else if (argc == 3) + errcount += mv(argv[1], argv[2]); + + return errcount; +} -- cgit v1.2.3-54-g00ecf