-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmmv.c
1793 lines (1618 loc) · 38.7 KB
/
mmv.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
mmv
Copyright (c) 2021-2024 Reuben Thomas.
Copyright (c) 1990 Vladimir Lanin.
This program is distributed under the GNU GPL version 3, or, at your
option, any later version.
Maintainer: Reuben Thomas <rrt@sc3d.org>
The original author of mmv was Vladimir Lanin <lanin@csd2.nyu.edu>.
Many thanks to those who have to contributed to the design
and/or coding of this program:
Tom Albrecht: initial Sys V adaptation, consultation, and testing
Carl Mascott: V7 adaptation
Mark Lewis: -n flag idea, consultation.
Dave Bernhold: upper/lowercase conversion idea.
Paul Stodghill: copy option, argv[0] checking.
Frank Fiamingo: consultation and testing.
Tom Jordahl: bug reports and testing.
John Lukas, Hugh Redelmeyer, Barry Nelson, John Sauter,
Phil Dench, John Nelson:
bug reports.
*/
#include "config.h"
/* NAME_MAX is not guaranteed to exist and its value should really be
* obtained with pathconf(), but that doesn't exist on mingw */
#ifdef _WIN32
#define _POSIX_ /* For NAME_MAX in limits.h on mingw */
#endif
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <utime.h>
#include <dirent.h>
#include <limits.h>
#include "progname.h"
#include "binary-io.h"
#include "dirname.h"
#include "pathmax.h"
#include "xalloc.h"
#include "cmdline.h"
#ifndef PATH_MAX
#define PATH_MAX (pathconf("/", _PC_PATH_MAX))
#endif
#define ESC '\\'
#define SLASH '/'
#define STRLEN(s) (sizeof(s) - 1)
#define NORMCOPY 0x002
#define OVERWRITE 0x004
#define NORMMOVE 0x008
#define XMOVE 0x010
#define APPEND 0x040
#define HARDLINK 0x100
#define SYMLINK 0x200
#define COPY (NORMCOPY | OVERWRITE)
#define MOVE (NORMMOVE | XMOVE)
#define LINK (HARDLINK | SYMLINK)
static char COPYNAME[] = "mcp";
static char APPENDNAME[] = "mad";
static char LINKNAME[] = "mln";
#define ASKDEL 0
#define ALLDEL 1
#define NODEL 2
#define ASKBAD 0
#define SKIPBAD 1
#define ABORTBAD 2
#define STAY 0
#define LOWER 1
#define UPPER 2
#define CAPITALIZE 3
#define MAXWILD 20
#define MAXPATLEN PATH_MAX
#define INITROOM 10
#define FI_STTAKEN 0x01
#define FI_LINKERR 0x02
#define FI_INSTICKY 0x04
#define FI_NODEL 0x08
#define FI_KNOWWRITE 0x010
#define FI_CANWRITE 0x20
#define FI_ISDIR 0x40
#define FI_ISLNK 0x80
typedef struct {
char *fi_name;
struct rep *fi_rep;
mode_t fi_mode;
int fi_stflags;
} FILEINFO;
#define DI_KNOWWRITE 0x01
#define DI_CANWRITE 0x02
#define DI_NONEXISTENT 0x04
typedef struct {
dev_t di_vid;
ino_t di_did;
size_t di_nfils;
FILEINFO **di_fils;
char di_flags;
const char *di_path; /* Only set when DI_NONEXISTENT is set */
} DIRINFO;
#define H_NODIR 1
#define H_NOREADDIR 2
typedef struct {
char *h_name;
DIRINFO *h_di;
char h_err;
} HANDLE;
#define R_SKIP 0x02
#define R_DELOK 0x04
#define R_ISALIASED 0x08
#define R_ISCYCLE 0x10
#define R_ONEDIRLINK 0x20
typedef struct rep {
HANDLE *r_hfrom;
FILEINFO *r_ffrom;
HANDLE *r_hto;
char *r_nto; /* non-path part of new name */
FILEINFO *r_fdel;
struct rep *r_first;
struct rep *r_thendo;
struct rep *r_next;
int r_flags;
} REP;
typedef struct {
REP *rd_p;
DIRINFO *rd_dto;
char *rd_nto;
unsigned rd_i;
} REPDICT;
static int op, badstyle, delstyle, verbose, noex, matchall, mkdirs;
static size_t ndirs = 0, dirroom;
static size_t ndirs_nonexistent = 0, dirroom_nonexistent;
static DIRINFO **dirs, **dirs_nonexistent;
static size_t nhandles = 0, handleroom;
static HANDLE **handles;
static unsigned nreps = 0;
static REP hrep, *lastrep = &hrep;
static int badreps = 0, paterr = 0, direrr, failed = 0, gotsig = 0, repbad;
static char TEMP[] = "$$mmvtmp.";
static char TOOLONG[] = "(too long)";
static char EMPTY[] = "(empty)";
static char SLASHSTR[] = {SLASH, '\0'};
#define PATLONG "%.40s... : pattern too long.\n"
char from[MAXPATLEN], to[MAXPATLEN];
static size_t fromlen, tolen;
static char *(stagel[MAXWILD]), *(firstwild[MAXWILD]), *(stager[MAXWILD]);
static int nwilds[MAXWILD];
static int nstages;
char pathbuf[PATH_MAX];
char fullrep[PATH_MAX + 1];
static char *(start[MAXWILD]);
static size_t length[MAXWILD];
static REP mistake;
#define MISTAKE (&mistake)
static const char *home;
static size_t homelen;
static uid_t uid;
static mode_t oldumask;
static ino_t cwdd = (ino_t)-1L;
static dev_t cwdv = (dev_t)-1L;
static void quit(void)
{
fprintf(stderr, "Aborting, nothing done.\n");
exit(1);
}
static void breakout(int signum _GL_UNUSED)
{
fflush(stdout);
quit();
}
static void breakrep(int signum _GL_UNUSED)
{
gotsig = 1;
}
static void breakstat(int signum _GL_UNUSED)
{
_exit(1);
}
static void printchain(REP *p)
{
if (p->r_thendo != NULL)
printchain(p->r_thendo);
printf("%s%s -> ", p->r_hfrom->h_name, p->r_ffrom->fi_name);
badreps++;
nreps--;
p->r_ffrom->fi_rep = MISTAKE;
}
static void nochains(void)
{
for (REP *q = &hrep, *p = q->r_next; p != NULL; q = p, p = p->r_next)
if (p->r_flags & R_ISCYCLE || p->r_thendo != NULL) {
printchain(p);
printf("%s%s : no chain copies allowed.\n",
p->r_hto->h_name, p->r_nto);
q->r_next = p->r_next;
p = q;
}
}
static bool getreply(void)
{
static FILE *tty = NULL;
if (tty == NULL && (tty = fopen("/dev/tty", "r")) == NULL) {
fprintf(stderr, "Cannot open terminal to get reply.\n");
quit();
}
/* Test against "^[yY]", hardcoded to avoid requiring getline,
regex, and rpmatch. */
int c = getchar();
if (c == EOF) {
fprintf(stderr, "Cannot get reply.\n");
quit();
}
bool yes = (c == 'y' || c == 'Y');
while (c != '\n' && c != EOF)
c = getchar();
return yes;
}
static void goonordie(void)
{
if ((paterr || badreps) && nreps > 0) {
fprintf(stderr, "Not everything specified can be done.");
if (badstyle == ABORTBAD) {
fprintf(stderr, " Aborting.\n");
exit(1);
}
else if (badstyle == SKIPBAD)
fprintf(stderr, " Proceeding with the rest.\n");
else {
fprintf(stderr, " Proceed with the rest? ");
if (!getreply())
exit(1);
}
}
}
static int trymatch(FILEINFO *ffrom, char *pat)
{
char *p;
if (ffrom->fi_rep != NULL)
return(0);
p = ffrom->fi_name;
if (*p == '.') {
if (p[1] == '\0' || (p[1] == '.' && p[2] == '\0'))
return(strcmp(pat, p) == 0);
else if (!matchall && *pat != '.')
return(0);
}
return(-1);
}
static int match(char *pat, char *s, char **start1, size_t *len1)
{
char c;
*start1 = 0;
for(;;)
switch (c = *pat) {
case '\0':
case SLASH:
return(*s == '\0');
case '*':
*start1 = s;
if ((c = *(++pat)) == '\0') {
*len1 = strlen(s);
return(1);
}
else {
for (*len1=0; !match(pat, s, start1+1, len1+1); (*len1)++, s++)
if (
*s == '\0'
)
return(0);
return(1);
}
case '?':
if (
*s == '\0'
)
return(0);
*(start1++) = s;
*(len1++) = 1;
pat++;
s++;
break;
case '[':
{
int matched = 0, notin = 0, inrange = 0;
char prevc = '\0';
if ((c = *(++pat)) == '^') {
notin = 1;
c = *(++pat);
}
while (c != ']') {
if (c == '-' && !inrange)
inrange = 1;
else {
if (c == ESC) {
c = *(++pat);
}
if (inrange) {
if (*s >= prevc && *s <= c)
matched = 1;
inrange = 0;
}
else if (c == *s)
matched = 1;
prevc = c;
}
c = *(++pat);
}
if (inrange && *s >= prevc)
matched = 1;
if (!(matched ^ notin))
return(0);
*(start1++) = s;
*(len1++) = 1;
pat++;
s++;
}
break;
case ESC:
c = *(++pat);
/* FALLTHROUGH */
default:
if (c == *s) {
pat++;
s++;
}
else
return(0);
}
}
static int getstat(char *ffull, FILEINFO *f)
{
struct stat fstat;
int flags;
if ((flags = f->fi_stflags) & FI_STTAKEN)
return(flags & FI_LINKERR);
flags |= FI_STTAKEN;
if (lstat(ffull, &fstat)) {
fprintf(stderr, "Strange, couldn't lstat %s.\n", ffull);
quit();
}
if ((flags & FI_INSTICKY) && fstat.st_uid != uid && uid != 0)
flags |= FI_NODEL;
#ifdef S_IFLNK
if ((fstat.st_mode & S_IFMT) == S_IFLNK) {
flags |= FI_ISLNK;
if (stat(ffull, &fstat)) {
f->fi_stflags = flags | FI_LINKERR;
return(1);
}
}
#endif
if ((fstat.st_mode & S_IFMT) == S_IFDIR)
flags |= FI_ISDIR;
f->fi_stflags = flags;
f->fi_mode = fstat.st_mode;
return(0);
}
static int keepmatch(FILEINFO *ffrom, char *pathend, size_t *pk, int needslash, int fils)
{
*pk = strlen(ffrom->fi_name);
if ((size_t)(pathend - pathbuf) + *pk + (size_t)needslash >= PATH_MAX) {
*pathend = '\0';
printf("%s -> %s : search path %s%s too long.\n",
from, to, pathbuf, ffrom->fi_name);
paterr = 1;
return(0);
}
strcpy(pathend, ffrom->fi_name);
getstat(pathbuf, ffrom);
if (!(ffrom->fi_stflags & FI_ISDIR) && !fils) {
if (verbose)
printf("ignoring file %s\n", ffrom->fi_name);
return(0);
}
if (needslash) {
strcpy(pathend + *pk, SLASHSTR);
(*pk)++;
}
return(1);
}
static char *getpath(char *tpath)
{
char *pathstart, *pathend, c;
pathstart = fullrep;
pathend = pathstart + strlen(pathstart) - 1;
while (pathend >= pathstart && *pathend != SLASH)
--pathend;
pathend++;
c = *pathend;
*pathend = '\0';
strcpy(tpath, fullrep);
*pathend = c;
return(pathend);
}
static int fcmp(const void *pf1, const void *pf2)
{
return(strcmp((*(FILEINFO **)pf1)->fi_name, (*(FILEINFO **)pf2)->fi_name));
}
static FILEINFO *fsearch(char *s, DIRINFO *d)
{
FILEINFO *f = (FILEINFO *)xmalloc(sizeof(FILEINFO));
f->fi_name = s;
FILEINFO **res = bsearch(&f, d->di_fils, d->di_nfils, sizeof(FILEINFO *), fcmp);
return res != NULL ? *res : NULL;
}
static _GL_ATTRIBUTE_PURE size_t ffirst(char *s, size_t n, DIRINFO *d)
{
FILEINFO **fils = d->di_fils;
size_t nfils = d->di_nfils;
if (nfils == 0 || n == 0)
return(0);
size_t first = 0;
size_t last = nfils - 1;
for(;;) {
size_t k = (first + last) / 2;
int res = strncmp(s, fils[k]->fi_name, n);
if (first == last)
return(res == 0 ? k : nfils);
else if (res > 0)
first = k + 1;
else
last = k;
}
}
#define IRWXMASK (S_IRUSR | S_IWUSR | S_IXUSR)
#define RWXMASK (IRWXMASK | (IRWXMASK >> 3) | (IRWXMASK >> 6))
/* This function adapted from bash's mkdir.c
Make all the directories leading up to PATH, then create PATH. Note that
this changes the process's umask; make sure that all paths leading to a
return reset it to OLDUMASK */
static int make_path(char *path)
{
struct stat sb;
char *p, *npath;
int tail;
/* If we don't have to do any work, don't do any work. */
if (stat(path, &sb) == 0) {
if (S_ISDIR(sb.st_mode) == 0) {
if (verbose)
printf("`%s': file exists but is not a directory", path);
return(1);
}
return(0);
}
npath = xstrdup(path); /* So we can write to it. */
/* Check whether or not we need to do anything with intermediate dirs. */
/* Skip leading slashes. */
p = npath;
while (*p == '/')
p++;
tail = 0;
while (tail == 0) {
if (*p == '\0')
tail = 1;
else
p = strchr(p, '/');
if (p)
*p = '\0';
else
tail = 1;
if (mkdir(npath, ~oldumask & RWXMASK) < 0) {
/* "Each dir operand that names an existing directory shall be
ignored without error." */
if (errno == EEXIST || errno == EISDIR) {
int e = errno;
int fail = 0;
if (stat(npath, &sb) != 0) {
fail = 1;
if (verbose)
printf("cannot create directory `%s': %s", npath, strerror(e));
}
else if (e == EEXIST && S_ISDIR(sb.st_mode) == 0) {
fail = 1;
if (verbose)
printf("`%s': file exists but is not a directory", npath);
}
if (fail) {
umask(oldumask);
return(1);
}
}
else {
if (verbose)
printf("cannot create directory `%s': %s", npath, strerror(errno));
umask(oldumask);
return(1);
}
}
if (tail == 0)
*p++ = '/'; /* restore slash */
while (p && *p == '/') /* skip consecutive slashes or trailing slash */
p++;
}
umask(oldumask);
return(0);
}
/* Create a non-existent directory and update its DIRINFO. */
static int make_directory(HANDLE *h) {
int res = make_path(h->h_name);
if (res != 0)
fprintf(stderr, "Strange, couldn't create directory %s.\n", h->h_name);
else {
struct stat dstat;
if (stat(h->h_name, &dstat) || (dstat.st_mode & S_IFMT) != S_IFDIR) {
fprintf(stderr, "Strange, couldn't stat new directory %s.\n", h->h_name);
res = -1;
}
h->h_di->di_vid = dstat.st_dev;
h->h_di->di_did = dstat.st_ino;
}
return res;
}
static HANDLE *hadd(char *n)
{
if (nhandles == handleroom)
handles = (HANDLE **)x2nrealloc(handles, &handleroom, sizeof(HANDLE *));
HANDLE *h = (HANDLE *)xmalloc(sizeof(HANDLE));
h->h_name = xcharalloc(strlen(n) + 1);
strcpy(h->h_name, n);
h->h_di = NULL;
handles[nhandles++] = h;
return(h);
}
static int hsearch(char *n, HANDLE **pret)
{
for (unsigned i = 0; i < nhandles; i++)
if (strcmp(n, handles[i]->h_name) == 0) {
*pret = handles[i];
return(1);
}
*pret = hadd(n);
return(0);
}
static DIRINFO *dadd(dev_t v, ino_t d)
{
if (ndirs == dirroom)
dirs = (DIRINFO **)x2nrealloc(dirs, &dirroom, sizeof(DIRINFO *));
DIRINFO *di = (DIRINFO *)xmalloc(sizeof(DIRINFO));
di->di_vid = v;
di->di_did = d;
di->di_nfils = 0;
di->di_fils = NULL;
di->di_flags = 0;
di->di_path = NULL;
dirs[ndirs++] = di;
return(di);
}
static DIRINFO *dadd_nonexistent(const char *dir)
{
if (ndirs_nonexistent == dirroom_nonexistent)
dirs_nonexistent = (DIRINFO **)x2nrealloc(dirs_nonexistent, &dirroom_nonexistent, sizeof(DIRINFO *));
DIRINFO *di = (DIRINFO *)xmalloc(sizeof(DIRINFO));
di->di_vid = (dev_t)-1;
di->di_did = (ino_t)-1;
di->di_nfils = 0;
di->di_fils = NULL;
di->di_flags = DI_KNOWWRITE | DI_CANWRITE | DI_NONEXISTENT;
di->di_path = xstrdup(dir);
dirs_nonexistent[ndirs_nonexistent++] = di;
return(di);
}
static _GL_ATTRIBUTE_PURE DIRINFO *dsearch(dev_t v, ino_t d)
{
for (unsigned i = 0; i < ndirs; i++)
if (v == dirs[i]->di_vid && d == dirs[i]->di_did)
return(dirs[i]);
return(NULL);
}
static _GL_ATTRIBUTE_PURE DIRINFO *dsearch_nonexistent(const char *dir)
{
for (unsigned i = 0; i < ndirs_nonexistent; i++)
if (strcmp(dirs_nonexistent[i]->di_path, dir) == 0)
return(dirs_nonexistent[i]);
return(NULL);
}
static void takedir(const char *p, DIRINFO *di, int sticky)
{
struct dirent *dp;
FILEINFO *f, **fils;
DIR *dirp;
if ((dirp = opendir(p)) == NULL) {
fprintf(stderr, "Strange, can't scan %s.\n", p);
quit();
}
size_t room = INITROOM;
di->di_fils = fils = (FILEINFO **)xmalloc(room * sizeof(FILEINFO *));
size_t cnt = 0;
while ((dp = readdir(dirp)) != NULL) {
if (cnt == room) {
di->di_fils = (FILEINFO **)x2nrealloc(di->di_fils, &room, sizeof(FILEINFO *));
fils = di->di_fils + cnt;
}
*fils = f = (FILEINFO *)xmalloc(sizeof(FILEINFO));
f->fi_name = xstrdup(dp->d_name);
f->fi_stflags = sticky;
f->fi_rep = NULL;
cnt++;
fils++;
}
closedir(dirp);
qsort(di->di_fils, cnt, sizeof(FILEINFO *), fcmp);
di->di_nfils = cnt;
}
static HANDLE *checkdir(char *p, char *pathend, int makedirs)
{
struct stat dstat;
ino_t d;
dev_t v;
DIRINFO *di = NULL;
const char *myp;
char *lastslash = NULL;
int sticky;
HANDLE *h;
if (hsearch(p, &h)) {
if (h->h_di == NULL) {
direrr = h->h_err;
return(NULL);
}
return(h);
}
if (*p == '\0')
myp = ".";
else if (pathend == p + 1)
myp = SLASHSTR;
else {
lastslash = pathend - 1;
*lastslash = '\0';
myp = p;
}
if (stat(myp, &dstat) || (dstat.st_mode & S_IFMT) != S_IFDIR) {
if (makedirs) {
if ((di = dsearch_nonexistent(myp)) == NULL)
di = dadd_nonexistent(myp);
} else
direrr = h->h_err = H_NODIR;
} else if (access(myp, R_OK | X_OK))
direrr = h->h_err = H_NOREADDIR;
else {
direrr = 0;
sticky = (dstat.st_mode & S_ISVTX) && uid != 0 && uid != dstat.st_uid ?
FI_INSTICKY : 0;
v = dstat.st_dev;
d = dstat.st_ino;
if ((di = dsearch(v, d)) == NULL)
takedir(myp, di = dadd(v, d), sticky);
}
if (lastslash != NULL)
*lastslash = SLASH;
if (direrr != 0)
return(NULL);
h->h_di = di;
return(h);
}
static int checkto(char *f, HANDLE **phto, char **pnto, FILEINFO **pfdel)
{
char tpath[PATH_MAX + 1];
FILEINFO *fdel = NULL;
char *pathend = getpath(tpath);
size_t hlen = (size_t)(pathend - fullrep);
*phto = checkdir(tpath, tpath + hlen, mkdirs);
if (
*phto != NULL &&
*pathend != '\0' &&
(fdel = *pfdel = fsearch(pathend, (*phto)->h_di)) != NULL &&
(getstat(fullrep, fdel), fdel->fi_stflags & FI_ISDIR) &&
(strcmp(pathend, fullrep) != 0)
) {
size_t tlen = strlen(pathend);
strcpy(pathend + tlen, SLASHSTR);
tlen++;
strcpy(tpath + hlen, pathend);
pathend += tlen;
hlen += tlen;
*phto = checkdir(tpath, tpath + hlen, mkdirs);
}
if (*pathend == '\0') {
*pnto = xstrdup(f);
if ((size_t)(pathend - fullrep) + strlen(f) >= PATH_MAX) {
strcpy(fullrep, TOOLONG);
return(-1);
}
strcat(pathend, f);
if (*phto != NULL) {
fdel = *pfdel = fsearch(f, (*phto)->h_di);
if (fdel != NULL)
getstat(fullrep, fdel);
}
}
else if (fdel != NULL)
*pnto = fdel->fi_name;
else
*pnto = xstrdup(pathend);
return(0);
}
static int badname(char *s)
{
return (
(*s == '.' && (s[1] == '\0' || strcmp(s, "..") == 0)) ||
strlen(s) > NAME_MAX
);
}
static int dwritable(HANDLE *h)
{
char *p = h->h_name, *lastslash = NULL, *pathend;
const char *myp;
char *pw = &(h->h_di->di_flags), r;
if (uid == 0)
return(1);
if (*pw & DI_KNOWWRITE)
return(*pw & DI_CANWRITE);
pathend = p + strlen(p);
if (*p == '\0')
myp = ".";
else if (pathend == p + 1)
myp = SLASHSTR;
else {
lastslash = pathend - 1;
*lastslash = '\0';
myp = p;
}
r = !access(myp, W_OK) ? DI_CANWRITE : 0;
*pw |= DI_KNOWWRITE | r;
if (lastslash != NULL)
*lastslash = SLASH;
return(r);
}
static int fwritable(char *hname, FILEINFO *f)
{
int r;
if (f->fi_stflags & FI_KNOWWRITE)
return(f->fi_stflags & FI_CANWRITE);
strcpy(fullrep, hname);
strcat(fullrep, f->fi_name);
r = !access(fullrep, W_OK) ? FI_CANWRITE : 0;
f->fi_stflags |= FI_KNOWWRITE | r;
return(r);
}
static int badrep(HANDLE *hfrom, FILEINFO *ffrom, HANDLE **phto, char **pnto, FILEINFO **pfdel, int *pflags)
{
char *f = ffrom->fi_name;
*pflags = 0;
if ((ffrom->fi_stflags & FI_LINKERR) && !(op & (MOVE | SYMLINK)))
printf("%s -> %s : source file is a badly aimed symbolic link.\n",
pathbuf, fullrep);
else if ((op & (COPY | APPEND)) && access(pathbuf, R_OK))
printf("%s -> %s : no read permission for source file.\n",
pathbuf, fullrep);
else if (
*f == '.' &&
(f[1] == '\0' || strcmp(f, "..") == 0) &&
!(op & SYMLINK)
)
printf("%s -> %s : . and .. can't be renamed.\n", pathbuf, fullrep);
else if (repbad || checkto(f, phto, pnto, pfdel) || badname(*pnto))
printf("%s -> %s : bad new name.\n", pathbuf, fullrep);
else if (*phto == NULL)
printf("%s -> %s : %s.\n", pathbuf, fullrep,
direrr == H_NOREADDIR ?
"no read or search permission for target directory" :
"target directory does not exist (you could use --makedirs)");
else if (!dwritable(*phto))
printf("%s -> %s : no write permission for target directory.\n",
pathbuf, fullrep);
else if (
(*phto)->h_di->di_vid != hfrom->h_di->di_vid &&
(op & (NORMMOVE | HARDLINK))
)
printf("%s -> %s : cross-device move.\n",
pathbuf, fullrep);
else if (
*pflags && (op & MOVE) &&
!(ffrom->fi_stflags & FI_ISLNK) &&
access(pathbuf, R_OK)
)
printf("%s -> %s : no read permission for source file.\n",
pathbuf, fullrep);
else if (
(op & SYMLINK) &&
!(
((*phto)->h_di->di_vid == cwdv && (*phto)->h_di->di_did == cwdd) ||
*(hfrom->h_name) == SLASH ||
(*pflags |= R_ONEDIRLINK, hfrom->h_di == (*phto)->h_di)
)
)
printf("%s -> %s : symbolic link would be badly aimed.\n",
pathbuf, fullrep);
else
return(0);
badreps++;
return(-1);
}
static void makerep(void)
{
size_t l, x, i;
int cnv;
char *q, *p, *pat, c, pc;
repbad = 0;
p = fullrep;
for (pat = to, l = 0; (c = *pat) != '\0'; pat++, l++) {
if (c == '#') {
c = *(++pat);
if (c == 'l') {
cnv = LOWER;
c = *(++pat);
}
else if (c == 'u') {
cnv = UPPER;
c = *(++pat);
}
else if (c == 'c') {
cnv = CAPITALIZE;
c = *(++pat);
}
else
cnv = STAY;
for(x = 0; ;x *= 10) {
x += (size_t)(c - '0');
c = *(pat+1);
if (!isdigit(c))
break;
pat++;
}
--x;
if (l + length[x] >= PATH_MAX)
goto toolong;
switch (cnv) {
case STAY:
memmove(p, start[x], length[x]);
p += length[x];
break;
case LOWER:
for (i = length[x], q = start[x]; i > 0; i--, p++, q++)
*p = (char)tolower(*q);
break;
case UPPER:
for (i = length[x], q = start[x]; i > 0; i--, p++, q++)
*p = (char)toupper(*q);
break;
case CAPITALIZE:
for (i = length[x], q = start[x]; i > 0; i--, p++, q++){
if (i == length[x])
*p = (char)toupper(*q);
else
*p = (char)tolower(*q);
}
break;
}
}
else {
if (c == ESC)
c = *(++pat);
if (l == PATH_MAX)
goto toolong;
if (c == SLASH &&
(
p == fullrep ? pat != to :
(
(
(pc = *(p - 1)) == SLASH
) &&
*(pat - 1) != pc
)
)
) {
repbad = 1;
if (l + STRLEN(EMPTY) >= PATH_MAX)
goto toolong;
strcpy(p, EMPTY);
p += STRLEN(EMPTY);
l += STRLEN(EMPTY);
}
*(p++)= c;
}
}
if (p == fullrep) {
strcpy(fullrep, EMPTY);
repbad = 1;
}
*(p++) = '\0';
return;
toolong:
repbad = 1;