1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
/* For portability, make everything look like MSC 6 */
#if defined(__TURBOC__)
#include <dir.h>
#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 <direct.h>
#endif
/*
** Tell 'em they messed up
*/
void help(char *s)
{
puts("usage: mv <oldname [...]> <newname|newdir>");
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;
}
|