-
Notifications
You must be signed in to change notification settings - Fork 0
/
mv.c
115 lines (97 loc) · 3.09 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
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
// Copyright (C) 2021 Benjamin Stürz
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#define PROG_NAME "mv"
#include <sys/stat.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "errprintf.h"
#include "prompt.h"
static bool is_tty(void) {
return ttyname(STDIN_FILENO) != NULL;
}
#define is_dir(st) (((st).st_mode & S_IFMT) == S_IFDIR)
#define is_writable(path) (access((path), W_OK) == 0)
static char* make_sub_path(const char* dir, const char* ent) {
const size_t len_dir = strlen(dir);
const size_t len_ent = strlen(ent);
const size_t len_buf = len_dir + len_ent + 2;
char* buf = malloc(len_buf);
snprintf(buf, len_buf, "%s/%s", dir, ent);
return buf;
}
int main(int argc, char* argv[]) {
bool prompt = false;
int option;
while ((option = getopt(argc, argv, ":if")) != -1) {
switch (option) {
case 'i':
prompt = true;
break;
case 'f':
prompt = false;
break;
case '?':
printf("mv: unknown option: -%c\n", optopt);
break;
}
}
const int narg = argc - optind;
struct stat st_dest, st_src;
if (narg < 2) {
fputs("Usage: mv [-if] source_file target_file\n", stderr);
fputs(" mv [-if] source_file... target_dir\n", stderr);
return 1;
}
const char* src;
const char* dest = argv[argc - 1];
const int has_dest = stat(dest, &st_dest) == 0;
if (narg == 2 && (!has_dest || !is_dir(st_dest))) {
src = argv[optind];
// if the destination exists and one of the following:
// - `-i` was specified
// - the destination is not writable and stdin is a tty
// then print a prompt
if (has_dest && (prompt || (!is_writable(dest) && is_tty()))) {
if (!do_prompt(true, "overwrite '%s'", dest))
return 0;
}
if (rename(src, dest) != 0) {
errprintf("cannot move '%s' to '%s'", src, dest);
return 1;
}
return 0;
}
if (!has_dest) {
fprintf(stderr, "mv: no such directory '%s'.\n", dest);
return 1;
}
if (!is_dir(st_dest)) {
fprintf(stderr, "mv: '%s' is not a directory.\n", dest);
return 1;
}
for (int i = optind; i < (argc - 1); ++i) {
src = argv[i];
char* dest_path = make_sub_path(dest, src);
if (rename(src, dest_path) != 0) {
errprintf("failed to move '%s' to '%s", src, dest_path);
return 1;
}
free(dest_path);
}
return 0;
}