home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
drdobbs
/
c_spec
/
sources
/
mv.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-02-20
|
5KB
|
216 lines
#include <stdio.h>
#include <errno.h>
/* MV.C A Unix style rename function. Useage is
*
* mv <file|directory> [filelist] <file|directory>
*
* [filelist] may be present only if the rightmost argument is
* a directory. If the file already exists, it is deleted.
*
* -i Verfify before overwriting an existing file
* -e Do not echo actions to screen as they are done (this will
* make cp behave like the unix version, which is silent).
*
* Exit status: 0 on success, 1 if a file couldn't be moved.
*/
extern char *cpy (char*, char*);
extern void perror (char*);
/*---------------------------------------------------------------------*/
int Verify = 0; /* Verify before overwriting existing file */
int Echo = 1; /* Say what we're doing as we do it */
/*---------------------------------------------------------------------*/
#define E(str) fprintf(stderr, "%s\n", str)
#define exists(file) (!access(file,0))
/*---------------------------------------------------------------------*/
args(argc, argv)
char **argv;
{
/* Process command line arguments. If -i iv found set
* Verify true, if -e is found set Echo false. Return
* 1 if we found an argument, 0 otherwise
*/
register char *p;
if( argv[1][0] == '-' )
{
if( --argc < 3 )
usage("Too few arguments");
for( p = *++argv + 1 ; *p ; p++ )
{
if ( *p == 'i' ) Verify = 1;
else if ( *p == 'e' ) Echo = 0;
else usage("Illegal argument %c", *p );
}
return 1;
}
return 0;
}
/*---------------------------------------------------------------------*/
overwrite(targ)
char *targ;
{
/* Return true if a file exists and we should overwrite
* it. If Verfify is set, ask the user.
*/
if( exists( targ ))
{
/* Target file exists */
if( Verify )
{
fprintf(stderr,"Mv: %s exists, overwrite (y/n)?",
targ);
if( getchar() == 'n' )
return 0;
}
if( unlink(targ) < 0 ) /* Delete the target file */
{
fprintf(stderr,
"File marked read only or is directory\n" );
return 0;
}
}
return 1;
}
/*---------------------------------------------------------------------*/
char *get_fname( src )
register char *src;
{
/* Return a pointer to the filename part of src */
register char *start ;
for(start = src ; *src ; src++ )
if( *src == '/' || *src == '\\' )
start = src + 1;
return( start );
}
/*---------------------------------------------------------------------*/
main(argc, argv)
char **argv;
{
char targ[128];
register char *end = targ;
register int moving_to_dir = 0;
int err ;
int exit_status = 0;
ctlc();
reargv(&argc, &argv);
if( argc < 3 )
usage("Too few arguments");
if( args(argc,argv) ) /* Process command line arguments */
{
argc--;
argv++;
}
argc -= 2; /* Remove the file and target names from argv */
argv++;
/* Copy the target name into targ. If the target is a directory
* add a trailing slash as well. If it's not a directory and
* argc is > 1 then the command line syntax is wrong.
*/
end = cpy( targ, argv[argc] );
if( isdir(targ) )
{
*end++ = '/';
moving_to_dir = 1;
}
else if( argc > 1 )
usage("Can't move multiple files into one file");
for(; --argc >= 0; argv++ )
{
if( moving_to_dir )
{
/* If we're moving to a directory append
* the source name onto the end of the
* target name.
*/
strcpy(end, get_fname(*argv) );
}
/* If we're moving a file to itself, we're already
* done. Similarly if we are told not to overwrite
* an existing file, we're done.
*/
if( !strcmp(*argv, targ) )
continue;
if( !overwrite(targ) )
continue;
if( Echo )
printf("Moving: %20s to %s", *argv, targ );
if( err = rename(targ, *argv) )
{
if( err == EXDEV )
printf(": May not move to another disk\n");
else if( err == ENOENT )
printf(": %s not found\n", *argv );
else
printf("Can't move to another directory or disk\n");
exit_status = 1;
}
else if( Echo )
printf("\n");
}
exit( exit_status );
}
/*---------------------------------------------------------------------*/
usage(str, i)
char *str;
{
E("Mv: Copyright (c) 1986, Allen I. Holub. All rights reserved.\n");
fprintf(stderr, str, i );
E("");
E("Usage: mv [-ie] oldname newname");
E(" mv [-ie] file [... file] directory\n");
E("-i ask before over-writing existing files");
E("-e don't echo names as they're copied\n");
E("Rename a file (the top syntax above) or move a file or group");
E("of files to another directory. A directory may be renamed,");
E("but not moved to another directory. A target directory must");
E("be named (unlike the DOS copy command), . (the current directory)");
E("and .. (the parent directory) alone or in combination");
E("(ie. \"mv file ../..\" or \"mv /dir/file .\") are acceptable.");
exit(1);
}