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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
#!/usr/bin/env slsh
% -*- slang -*-
static variable Confirm_Move = 0;
static define get_yn ()
{
variable args = __pop_args (_NARGS);
() = fprintf (stdout, __push_args (args));
() = fflush (stdout);
variable yn;
if (fgets (&yn, stdin) <= 0)
return -1;
"y" == strlow (strtrim (yn));
}
static define move_file (from, to)
{
if (from == to)
{
() = fprintf (stderr, "%s: Cannot move a file to itself.\n", __argv[0]);
return -1;
}
if (0 == rename (from, to))
return 0;
variable st = stat_file (to);
if (st != NULL)
{
if (1 != get_yn ("%s exists. Overwrite? [y/n]", to))
{
() = fputs ("Not Confirmed\n", stdout);
return -1;
}
() = remove (to);
}
if (0 == rename (from, to))
return 0;
()=fprintf (stderr, "Failed to rename %s to %s: %s\n",
from, to, errno_string (errno));
return -1;
}
define move_files (from_files, to)
{
variable st = stat_file (to);
if (st == NULL)
{
if (length (from_files) != 1)
{
() = fprintf (stderr, "%s must be a directory\n", to);
exit (1);
}
if (-1 == move_file (from_files[0], to))
exit (1);
exit (0);
}
!if (stat_is ("dir", st.st_mode))
{
if (length (from_files) != 1)
{
() = fprintf (stderr, "%s must be a directory\n", to);
exit (1);
}
if (-1 == move_file (from_files[0], to))
exit (1);
exit (0);
}
foreach (from_files)
{
variable old = ();
variable new = path_concat (to, path_basename (old));
if (NULL == stat_file (old))
{
() = fprintf (stderr, "Unable to access %s\n", old);
continue;
}
if (Confirm_Move)
{
if (1 != get_yn ("Move %s to %s/? [y/n]", old, to))
{
() = fputs ("Not Confirmed\n", stdout);
continue;
}
}
() = move_file (old, new);
}
}
static define usage ()
{
() = fprintf (stdout, "Usage: %s [-i] files ... dir\n", __argv[0]);
exit (1);
}
define main (argc, argv)
{
argc--;
argv = argv[[1:]];
while (argc > 1)
{
if (argv[0] == "-i")
{
Confirm_Move = 1;
argc--;
argv = argv[[1:]];
continue;
}
break;
}
if (argc < 2)
usage ();
move_files (argv[[0:argc-2]], argv[argc-1]);
}
define slsh_main ()
{
main (__argc, __argv);
}
|