summaryrefslogtreecommitdiff
path: root/mv.c
blob: 22c4a12077c0222276969ea67d7599e05262c6c8 (plain)
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
#include "internal.h"
#include <stdio.h>
#include <errno.h>

const char	mv_usage[] = "mv source-file destination-file\n"
"\t\tmv source-file [source-file ...] destination-directory\n"
"\n"
"\tMove the source files to the destination.\n"
"\n";

extern int
mv_fn(const struct FileInfo * i)
{
	struct stat		destination_stat;
	char			d[1024];
	struct FileInfo	n;

	if ( stat(i->destination, &destination_stat) == 0 ) {
		if ( i->stat.st_ino == destination_stat.st_ino
		 &&  i->stat.st_dev == destination_stat.st_dev )
			return 0;	/* Move file to itself. */
	}
	if ( (destination_stat.st_mode & S_IFMT) == S_IFDIR ) {
		n = *i;
		n.destination = join_paths(d, i->destination, basename(i->source));
		i = &n;
	}
	if ( rename(i->source, i->destination) == 0 )
		return 0;
	else if ( errno == EXDEV && is_a_directory(i->source) ) {
		fprintf(stderr
		,"%s: Can't move directory across filesystems.\n"
		,i->source);
		return 1;
	}
	else
		return cp_fn(i);
}