forked from ghaerr/elks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmv.c
67 lines (54 loc) · 1.13 KB
/
mv.c
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
/*
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* Most simple built-in commands are here.
*/
#include "../sash.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <utime.h>
#include <errno.h>
void
mv_main(argc, argv)
char **argv;
{
int dirflag;
char *srcname;
char *destname;
char *lastarg;
lastarg = argv[argc - 1];
dirflag = isadir(lastarg);
if ((argc > 3) && !dirflag) {
write(STDERR_FILENO, lastarg, strlen(lastarg));
write(STDERR_FILENO, ": not a directory\n", 18);
exit(1);
}
while (argc-- > 2) {
srcname = *(++argv);
if (access(srcname, 0) < 0) {
perror(srcname);
continue;
}
destname = lastarg;
if (dirflag)
destname = buildname(destname, srcname);
if (rename(srcname, destname) >= 0)
continue;
if (errno != EXDEV) {
perror(destname);
continue;
}
if (!copyfile(srcname, destname, TRUE))
continue;
if (unlink(srcname) < 0)
perror(srcname);
}
exit(0);
}