-
Notifications
You must be signed in to change notification settings - Fork 0
/
edrename.c
520 lines (476 loc) · 9.98 KB
/
edrename.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
* Copyright (c) 2018 Michał Czarnecki <czarnecky@va.pl>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/uio.h>
#include <regex.h>
#define NO_ARG do { ++*argv; if (!(mid = **argv)) argv++; } while (0)
struct file_name {
struct file_name *next;
short nL;
short rL;
char *n;
char *r;
};
void err(const char*, ...);
int xgetline(int, char*, size_t, char *[2]);
int gather_matching_files(char*, int, char*, struct file_name**);
int gather_fd(int, struct file_name**);
void file_name_free(struct file_name*);
int spawn(char *eargv[]);
char *basename(char*);
void usage(char*);
char *ARG(char***);
char* EARG(char***);
int mvcp(char*, char*, struct file_name*);
int mvcp_output(char*, char*, struct file_name*);
void quot(size_t, char [PATH_MAX]);
void err(const char *fmt, ...)
{
va_list a;
va_start(a, fmt);
vdprintf(2, fmt, a);
va_end(a);
exit(EXIT_FAILURE);
}
#define XGETLINE_GETERRNO(R) (0x0000ffff & R)
/*
* xgetline() returns:
* -2 -> error
* -1 -> no more lines
* 0 <= -> line length (may be 0)
*/
int xgetline(int fd, char *buf, size_t bufs, char *b[2])
{
char *src, *dst;
size_t s;
ssize_t r;
int i, L = 0;
src = b[0];
dst = buf;
i = s = b[1] - b[0];
/* Move next lines to the very begining, having covered old line */
while (i--) {
*dst++ = *src++;
}
/* New values */
b[0] = buf;
b[1] = buf + s;
for (;;) {
/* Find newline */
while (*b[0] != '\n' && *b[0] != '\r' && b[1] - b[0] > 0) {
b[0]++;
}
/* If it was found */
if (b[1] - b[0] > 0 && (*b[0] == '\n' || *b[0] == '\r')) {
L = b[0] - buf;
break;
}
/* If it was NOT found or ran out of buffer data */
else {
r = read(fd, b[1], bufs-(b[1]-buf));
if (r == -1) {
return 0xffff0000 | errno;
}
if (r == 0) {
return -1;
}
b[1] += r;
}
}
/* NULL terminator */
*b[0] = 0;
b[0]++;
if (b[1] - b[0] > 0 && (*b[0] == '\n' || *b[0] == '\r')) {
*b[0] = 0;
b[0]++;
}
return L;
}
int gather_matching_files(char *str, int cflags, char *dir, struct file_name **H)
{
regex_t R;
const size_t nmatch = 1;
regmatch_t pmatch[nmatch];
int e;
char errbuf[1024];
size_t errbufn, L;
struct dirent *ent;
struct file_name *N;
DIR *D;
if ((e = regcomp(&R, str, cflags))) {
errbufn = regerror(e, &R, errbuf, sizeof(errbuf));
fprintf(stderr, "%.*s\n", (int)errbufn, errbuf);
return -1;
}
*H = 0;
D = opendir(dir);
errno = 0;
while ((ent = readdir(D))) {
L = strnlen(ent->d_name, NAME_MAX);
if (
(L == 1 && ent->d_name[0] == '.')
|| (L == 2 && ent->d_name[0] == '.' && ent->d_name[1] == '.')
|| (REG_NOMATCH == regexec(&R, ent->d_name, nmatch, pmatch, 0))
) {
continue;
}
N = malloc(sizeof(struct file_name));
N->next = *H;
N->nL = L;
N->n = malloc(L+1);
memcpy(N->n, ent->d_name, L+1);
N->rL = 0;
N->r = 0;
*H = N;
}
e = errno;
closedir(D);
regfree(&R);
return e;
}
int gather_fd(int fd, struct file_name **H)
{
int L;
char line[PATH_MAX];
char *b[2] = { line, line };
struct file_name *N;
*H = 0;
while (0 <= (L = xgetline(fd, line, sizeof(line), b))) {
if (!L
|| (L == 1 && line[0] == '.')
|| (L == 2 && line[0] == '.' && line[1] == '.')
) {
continue;
}
N = malloc(sizeof(struct file_name));
N->next = *H;
N->nL = L;
N->n = malloc(L+1);
memcpy(N->n, line, L+1);
N->rL = 0;
N->r = 0;
*H = N;
}
return L < -1 ? XGETLINE_GETERRNO(L) : 0;
}
void file_name_free(struct file_name *i)
{
struct file_name *tmp;
while (i) {
tmp = i;
i = i->next;
if (tmp->n) free(tmp->n);
if (tmp->r) free(tmp->r);
free(tmp);
}
}
int spawn(char *eargv[])
{
pid_t p;
int wstatus = 0;
p = fork();
if (p == -1) {
return errno;
}
if (!p) {
execvp(eargv[0], eargv);
exit(EXIT_FAILURE);
}
else {
do {
if (-1 == waitpid(p, &wstatus, 0)) {
return errno;
}
} while (!WIFEXITED(wstatus));
}
return 0;
}
char *basename(char *p)
{
char *P = p;
while (*P) P++;
while (P != p && *P != '/') P--;
if (*P == '/') P++;
return P;
}
void usage(char *argv0)
{
fprintf(stderr, "Usage: %s", basename(argv0));
fprintf(stderr,
" [-chio] [-d DIR] [-eE REGEX]\n"
"Options:\n"
" -c Copy instead of renaming.\n"
" -d DIR When using regex, search in DIR instead.\n"
" -e REGEXP Filter files in the current directory using POSIX regex.\n"
" -E REGEXP Filter files in the current directory using extended regex.\n"
" -h Display this message and exit.\n"
" -i Read file list from stdin.\n"
" -o Just output commands to stdout.\n"
);
}
char *ARG(char ***argv)
{
char *r = 0;
(**argv)++;
if (***argv) { /* -oARG */
r = **argv;
(*argv)++;
}
else { /* -o ARG */
(*argv)++;
if (**argv && ***argv != '-') {
r = **argv;
(*argv)++;
}
}
return r;
}
char* EARG(char ***argv)
{
char *a;
a = ARG(argv);
if (!a) {
err("ERROR: Expected argument.\n");
}
return a;
}
int mvcp(char *cmd, char *dir, struct file_name *fn_list)
{
int e;
char *eargv[8];
struct file_name *i;
unsigned num_proc = 0;
char a[PATH_MAX];
char b[PATH_MAX];
eargv[0] = "/usr/bin/env";
eargv[1] = cmd;
eargv[2] = "-vi";
eargv[3] = "--";
eargv[6] = 0;
for (i = fn_list; i; i = i->next) {
if (i->nL == i->rL && !memcmp(i->n, i->r, i->nL+1)) {
continue;
}
snprintf(a, sizeof(a), "%s/%s", dir, i->n);
snprintf(b, sizeof(b), "%s/%s", dir, i->r);
eargv[4] = a;
eargv[5] = b;
if ((e = spawn(eargv))) {
fprintf(stderr, "error: failed to spawn '%s': %s\n",
eargv[0], strerror(e));
return 1;
}
num_proc++;
}
printf("%d file%s processed\n", num_proc,
num_proc == 1 ? "" : "s");
return 0;
}
int mvcp_output(char *cmd, char *dir, struct file_name *fn_list)
{
struct file_name *i;
char nb[PATH_MAX];
char rb[PATH_MAX];
for (i = fn_list; i; i = i->next) {
if (i->nL == i->rL && !memcmp(i->n, i->r, i->nL+1)) {
continue;
}
snprintf(nb, sizeof(nb), "%s/%s", dir, i->n);
quot(sizeof(nb), nb);
snprintf(rb, sizeof(rb), "%s/%s", dir, i->r);
quot(sizeof(rb), rb);
dprintf(1, "%s -vi -- '%s' '%s'\n", cmd, nb, rb);
}
return 0;
}
void quot(size_t bufs, char *buf)
{
int i, j;
for (i = 0; i < (int)bufs && buf[i]; i++) {
if (buf[i] != '\'') continue;
for (j = bufs; j > i; j--) {
buf[j] = buf[j-1];
}
buf[i] = '\\';
i++;
}
}
/* TODO
* - line endings
* - $EDITOR vs $VISUAL
*/
int main(int argc, char *argv[])
{
char *argv0,
*file_regex = 0,
tmpname[64],
*eargv[8],
*dir = ".";
char buf[PATH_MAX] = { 0 };
char *b[2] = { buf, buf };
struct file_name *fn_list, *i;
int L = 0, ret = 0;
int tmpfd, e, cflags = 0;
struct iovec iov[2] = { { 0, 0 }, { "\n", 1 } };
_Bool mid = 0, from_stdin = 0, just_output = 0;
char mode = 'm';
(void)argc;
argv0 = *argv;
++argv;
while ((mid && **argv) || (*argv && **argv == '-')) {
if (!mid) ++*argv;
mid = 0;
if ((*argv)[0] == '-' && (*argv)[1] == 0) {
argv++;
break;
}
switch (**argv) {
case 'c':
mode = 'c';
NO_ARG;
break;
case 'i':
from_stdin = 1;
NO_ARG;
break;
case 'o':
just_output = 1;
NO_ARG;
break;
case 'd':
dir = EARG(&argv);
break;
case 'e':
file_regex = EARG(&argv);
break;
case 'E':
file_regex = EARG(&argv);
cflags |= REG_EXTENDED;
break;
case 'h':
usage(argv0);
NO_ARG;
return 0;
default:
usage(argv0);
return 1;
}
}
if (!from_stdin && !file_regex) {
usage(argv0);
goto fail_cleanup;
}
e = from_stdin
? gather_fd(0, &fn_list)
: gather_matching_files(file_regex, cflags, dir, &fn_list);
if (e > 0) {
fprintf(stderr, "error: %s\n", strerror(e));
ret = 1;
goto fail_cleanup;
}
if (!fn_list) {
printf("no matching files\n");
return 0;
}
snprintf(tmpname, sizeof(tmpname), "/tmp/edrename.%d", getpid());
tmpfd = creat(tmpname, 0600);
for (i = fn_list; i; i = i->next) {
iov[0].iov_base = i->n;
iov[0].iov_len = i->nL;
writev(tmpfd, iov, 2);
}
close(tmpfd);
eargv[0] = getenv("EDITOR");
if (!eargv[0]) {
eargv[0] = getenv("VISUAL");
}
eargv[1] = tmpname;
eargv[2] = 0;
if ((e = spawn(eargv))) {
fprintf(stderr, "error: failed to spawn '%s': %s\n",
eargv[0], strerror(e));
ret = 1;
goto fail_cleanup;
}
tmpfd = open(tmpname, 0600);
if (tmpfd == -1) {
e = errno;
fprintf(stderr, "error: failed to open '%s': %s\n",
tmpname, strerror(e));
ret = 1;
goto fail_cleanup;
}
i = fn_list;
while (i) {
if (0 < (L = xgetline(tmpfd, buf, sizeof(buf), b))) {
i->rL = L;
i->r = malloc(L+1);
memcpy(i->r, buf, L+1);
}
else if (L == -1) {
fprintf(stderr, "error: missing lines\n");
close(tmpfd);
unlink(tmpname);
ret = 1;
goto fail_cleanup;
}
else {
fprintf(stderr, "error: %s\n",
strerror(XGETLINE_GETERRNO(L)));
close(tmpfd);
unlink(tmpname);
ret = 1;
goto fail_cleanup;
}
i = i->next;
}
close(tmpfd);
unlink(tmpname);
switch (mode) {
case 'm':
if (just_output) {
ret = mvcp_output("mv", dir, fn_list);
}
else {
ret = mvcp("mv", dir, fn_list);
}
break;
case 'c':
if (just_output) {
ret = mvcp_output("cp", dir, fn_list);
}
else {
ret = mvcp("cp", dir, fn_list);
}
break;
}
fail_cleanup:
file_name_free(fn_list);
return ret;
}