-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmv.c
More file actions
68 lines (59 loc) · 1.79 KB
/
mv.c
File metadata and controls
68 lines (59 loc) · 1.79 KB
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
#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char **argv) {
int srccount = argc - 2;
if (srccount < 1) {
fprintf(stderr, "usage: %s <src1> <src2...> <dst>\n", argv[0]);
return EXIT_FAILURE;
}
// check that the destination does not exist or is a directory
char *dst = argv[argc - 1];
size_t dstlen = strlen(dst);
struct stat st;
int stok = stat(dst, &st) == 0;
if (!stok && errno != ENOENT) {
fprintf(stderr, "(%s: stat '%s': %s)", argv[0], dst, strerror(errno));
return EXIT_FAILURE;
}
if (srccount > 1) {
if (!stok) {
fprintf(stderr, "(%s: stat '%s': %s)", argv[0], dst, strerror(ENOENT));
return EXIT_FAILURE;
} else if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "(%s: stat '%s': %s)", argv[0], dst, strerror(ENOTDIR));
return EXIT_FAILURE;
}
}
int failed = 0;
for (int i = 1; i < (argc - 1); i++) {
char *target = dst;
// if dst is a directory, then we should move
// into a file inside that directory
if (stok && S_ISDIR(st.st_mode)) {
char *srcdup = strdup(argv[i]);
char *b = basename(srcdup);
size_t blen = strlen(b);
int needsep = dstlen > 0 && dst[dstlen - 1] != '/';
target = malloc(dstlen + needsep + blen + 1);
memcpy(target, dst, dstlen);
if (needsep)
target[dstlen] = '/';
memcpy(target + dstlen + needsep, b, blen + 1);
free(srcdup);
}
// perform the rename
if (rename(argv[i], target) < 0) {
fprintf(stderr, "(%s: rename '%s' to '%s': %s)\n", argv[0], argv[i],
target, strerror(errno));
failed = 1;
}
if (target != dst)
free(target);
}
return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}