home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / fileutil / mmv / mmv.dos < prev    next >
Encoding:
Text File  |  1990-06-14  |  60.4 KB  |  2,904 lines

  1. /*
  2.     mmv 1.01b
  3.     Copyright (c) 1990 Vladimir Lanin.
  4.     This program may be freely used and copied on a non-commercial basis.
  5.     The author assumes no responsibility for any damage or data loss that may
  6.     result from the use of this program.
  7.  
  8.     Author may be reached at:
  9.  
  10.     lanin@csd4.cs.nyu.edu
  11.  
  12.     Vladimir Lanin
  13.     330 Wadsworth Ave, Apt 6F,
  14.     New York, NY 10040
  15.  
  16.     Many thanks to those who have to contributed to the design
  17.     and/or coding of this program:
  18.  
  19.     Tom Albrecht:    initial Sys V adaptation, consultation, and testing
  20.     Carl Mascott:    V7 adaptation
  21.     Mark Lewis:    -n flag idea, consultation.
  22.     Dave Bernhold:    upper/lowercase conversion idea.
  23.     Paul Stodghill:    copy option, argv[0] checking.
  24.     Frank Fiamingo:    consultation and testing.
  25.     Tom Jordahl:    bug reports and testing.
  26.     John Lukas, Hugh Redelmeyer, Barry Nelson, John Sauter,
  27.     Phil Dench, John Nelson:
  28.             bug reports.
  29. */
  30.  
  31. /*
  32.     Define SYSV to compile under System V.
  33.     Define both SYSV and V7 to compile under V7.
  34.     If your System V has a rename() call, define RENAME.
  35.     Otherwise, mmv will only be able to rename directories (via option -r)
  36.     when running as the super-user.
  37.     There is no reason to set the suid bit on mmv if rename() is available.
  38.     It is important that mmv not be run with effective uid set
  39.     to any value other than either the real uid or the super-user.
  40.     Even when running with effective uid set to super-user,
  41.     mmv will only perform actions permitted to the real uid.
  42.  
  43.     Define MSDOS to compile under MS-D*S Turbo C 1.5.
  44.     If you prefer mmv's output to use /'s instead of \'s under MS-D*S,
  45.     define SLASH.
  46.  
  47.     When neither MSDOS nor SYSV are defined, compiles under BSD.
  48.  
  49.     RENAME is automatically defined under MSDOS and BSD.
  50.  
  51.     If you are running a (UN*X) system that provides the
  52.     "struct dirent" readdir() directory reading standard,
  53.     define DIRENT. Otherwise, mmv uses the BSD-like
  54.     "struct direct" readdir().
  55.     If your (UN*X) system has neither of these, get the "dirent"
  56.     by Doug Gwyn, available as gwyn-dir-lib in volume 9
  57.     of the comp.sources.unix archives.
  58. */
  59.  
  60. static char USAGE[] =
  61. #ifdef MSDOS
  62.  
  63. "\nUsage: %s [-m|-x%s|-c|-o|-a|-z] [-h] [-d|-p] [-g|-t] [-v|-n] [from to]\n"
  64. "\nUse =N in the 'to' pattern to get the string matched"
  65. "\nby the N'th 'from' pattern wildcard.\n";
  66.  
  67. #define OTHEROPT (_osmajor < 3 ? "" : "|-r")
  68.  
  69. #else
  70.  
  71. "Usage: \
  72. %s [-m|x|r|c|o|a|l%s] [-h] [-d|p] [-g|t] [-v|n] [from to]\n\
  73. \n\
  74. Use =[l|u]N in the ``to'' pattern to get the [lowercase|uppercase of the]\n\
  75. string matched by the N'th ``from'' pattern wildcard.\n\
  76. \n\
  77. A ``from'' pattern containing wildcards should be quoted when given\n\
  78. on the command line.\n";
  79.  
  80. #ifdef SYSV
  81. #define OTHEROPT ""
  82. #else
  83. #define OTHEROPT "|s"
  84. #endif
  85.  
  86. #endif
  87.  
  88. #include <stdio.h>
  89. #include <ctype.h>
  90.  
  91. #ifdef MSDOS
  92. /* for MS-DOS (under Turbo C 1.5)*/
  93.  
  94. #include <string.h>
  95. #include <stdlib.h>
  96. #include <sys/types.h>
  97. #include <sys/stat.h>
  98. #include <dos.h>
  99. #include <direct.h>
  100. #include <io.h>
  101. #include <fcntl.h>
  102. #include <signal.h>
  103.  
  104. #define ESC '\''
  105. #ifdef SLASH
  106. #define SLASH '/'
  107. #define OTHERSLASH '\\'
  108. #else
  109. #define SLASH '\\'
  110. #define OTHERSLASH '/'
  111. #endif
  112.  
  113. typedef int DIRID;
  114. typedef int DEVID;
  115.  
  116. #define MAXPATH _MAX_PATH
  117. static char TTY[] = "con";
  118.  
  119. #define RENAME
  120.  
  121. #else
  122. /* for various flavors of UN*X */
  123.  
  124. #include <sys/types.h>
  125. #include <sys/stat.h>
  126. #include <sys/file.h>
  127.  
  128. extern char *getenv();
  129. extern long lseek();
  130. extern char *malloc();
  131.  
  132. #ifdef DIRENT
  133. #include <dirent.h>
  134. typedef struct dirent DIRENTRY;
  135. #else
  136. #ifdef SYSV
  137. #include <sys/dir.h>
  138. /* might need to be changed to <dir.h> */
  139. #else
  140. #include <sys/dir.h>
  141. #endif
  142. typedef struct direct DIRENTRY;
  143. #endif
  144.  
  145. #define void char    /* might want to remove this line */
  146.  
  147. #ifndef O_BINARY
  148. #define O_BINARY 0
  149. #endif
  150. #ifndef R_OK
  151. #define R_OK 4
  152. #define W_OK 2
  153. #define X_OK 1
  154. #endif
  155.  
  156. #define ESC '\\'
  157. #define SLASH '/'
  158.  
  159. typedef ino_t DIRID;
  160. typedef dev_t DEVID;
  161.  
  162. #define MAXPATH 1024
  163.  
  164. static char TTY[] = "/dev/tty";
  165.  
  166. #ifdef V7
  167. /* for Version 7 */
  168. #include <errno.h>
  169. extern int errno;
  170. #define strchr index
  171. extern char *strcpy(), *strchr();
  172. #include <signal.h>
  173. #define O_RDONLY 0
  174. #define O_WRONLY 1
  175. #define O_RDWR   2
  176.  
  177. #else
  178. /* for System V and BSD */
  179. #include <string.h>
  180. #include <sys/signal.h>
  181. #include <fcntl.h>
  182. #endif
  183.  
  184. #ifdef SYSV
  185. /* for System V and Version 7*/
  186. struct utimbuf {
  187.     time_t actime;
  188.     time_t modtime;
  189. };
  190. #define utimes(f, t) utime((f), &(t))
  191.  
  192. #else
  193. /* for BSD */
  194. #define RENAME
  195. #include <sys/time.h>
  196.  
  197. #endif
  198. #endif
  199.  
  200. #define mylower(c) (isupper(c) ? (c)-'A'+'a' : (c))
  201. #define myupper(c) (islower(c) ? (c)-'a'+'A' : (c))
  202. #define STRLEN(s) (sizeof(s) - 1)
  203. #define mydup(s) (strcpy((char *)challoc(strlen(s) + 1, 0), (s)))
  204.  
  205.  
  206. #define DFLT 0x001
  207. #define NORMCOPY 0x002
  208. #define OVERWRITE 0x004
  209. #define NORMMOVE 0x008
  210. #define XMOVE 0x010
  211. #define DIRMOVE 0x020
  212. #define NORMAPPEND 0x040
  213. #define ZAPPEND 0x080
  214. #define HARDLINK 0x100
  215. #define SYMLINK 0x200
  216.  
  217. #define COPY (NORMCOPY | OVERWRITE)
  218. #define MOVE (NORMMOVE | XMOVE | DIRMOVE)
  219. #define APPEND (NORMAPPEND | ZAPPEND)
  220. #define LINK (HARDLINK | SYMLINK)
  221.  
  222. static char MOVENAME[] = "mmv";
  223. static char COPYNAME[] = "mcp";
  224. static char APPENDNAME[] = "mad";
  225. static char LINKNAME[] = "mln";
  226.  
  227. #define ASKDEL 0
  228. #define ALLDEL 1
  229. #define NODEL 2
  230.  
  231. #define ASKBAD 0
  232. #define SKIPBAD 1
  233. #define ABORTBAD 2
  234.  
  235. #define STAY 0
  236. #define LOWER 1
  237. #define UPPER 2
  238.  
  239. #define MAXWILD 20
  240. #define MAXPATLEN MAXPATH
  241. #define INITROOM 10
  242. #define CHUNKSIZE 2048
  243. #define BUFSIZE 4096
  244.  
  245. #define FI_STTAKEN 0x01
  246. #define FI_LINKERR 0x02
  247. #define FI_INSTICKY 0x04
  248. #define FI_NODEL 0x08
  249. #define FI_KNOWWRITE 0x010
  250. #define FI_CANWRITE 0x20
  251. #define FI_ISDIR 0x40
  252. #define FI_ISLNK 0x80
  253.  
  254. typedef struct {
  255.     char *fi_name;
  256.     struct rep *fi_rep;
  257. #ifdef MSDOS
  258.     char fi_attrib;
  259. #else
  260.     short fi_mode;
  261.     char fi_stflags;
  262. #endif
  263. } FILEINFO;
  264.  
  265. #define DI_KNOWWRITE 0x01
  266. #define DI_CANWRITE 0x02
  267. #define DI_CLEANED 0x04
  268.  
  269. typedef struct {
  270.     DEVID di_vid;
  271.     DIRID di_did;
  272.     unsigned di_nfils;
  273.     FILEINFO **di_fils;
  274.     char di_flags;
  275. } DIRINFO;
  276.  
  277. #define H_NODIR 1
  278. #define H_NOREADDIR 2
  279.  
  280. typedef struct {
  281.     char *h_name;
  282.     DIRINFO *h_di;
  283.     char h_err;
  284. } HANDLE;
  285.  
  286. #define R_ISX 0x01
  287. #define R_SKIP 0x02
  288. #define R_DELOK 0x04
  289. #define R_ISALIASED 0x08
  290. #define R_ISCYCLE 0x10
  291. #define R_ONEDIRLINK 0x20
  292.  
  293. typedef struct rep {
  294.     HANDLE *r_hfrom;
  295.     FILEINFO *r_ffrom;
  296.     HANDLE *r_hto;
  297.     char *r_nto;            /* non-path part of new name */
  298.     FILEINFO *r_fdel;
  299.     struct rep *r_first;
  300.     struct rep *r_thendo;
  301.     struct rep *r_next;
  302.     char r_flags;
  303. } REP;
  304.  
  305. typedef struct {
  306.     REP *rd_p;
  307.     DIRINFO *rd_dto;
  308.     char *rd_nto;
  309.     unsigned rd_i;
  310. } REPDICT;
  311.  
  312. typedef struct chunk {
  313.     struct chunk *ch_next;
  314.     unsigned ch_len;
  315. } CHUNK;
  316.  
  317. typedef struct {
  318.     CHUNK *sl_first;
  319.     char *sl_unused;
  320.     int sl_len;
  321. } SLICER;
  322.  
  323.  
  324. static void init(/* */);
  325. static void procargs(/* int argc, char **argv,
  326.     char **pfrompat, char **ptopat */);
  327. static void domatch(/* char *cfrom, char *cto */);
  328. static int getpat(/* */);
  329. static int getword(/* char *buf */);
  330. static void matchpat(/*  */);
  331. static int parsepat(/*  */);
  332. static int dostage(/* char *lastend, char *pathend,
  333.     char **start1, int *len1, int stage, int anylev */);
  334. static int trymatch(/* FILEINFO *ffrom, char *pat */);
  335. static int keepmatch(/* FILEINFO *ffrom, char *pathend,
  336.     int *pk, int needslash, int dirs, int fils */);
  337. static int badrep(/* HANDLE *hfrom, FILEINFO *ffrom,
  338.     HANDLE **phto, char **pnto, FILEINFO **pfdel, int *pflags */);
  339. static int checkto(/* HANDLE *hfrom, char *f,
  340.     HANDLE **phto, char **pnto, FILEINFO **pfdel */);
  341. static char *getpath(/* char *tpath */);
  342. static int badname(/* char *s */);
  343. static FILEINFO *fsearch(/* char *s, DIRINFO *d */);
  344. static int ffirst(/* char *s, int n, DIRINFO *d */);
  345. static HANDLE *checkdir(/* char *p, char *pathend, int which */);
  346. static void takedir(/*
  347.     char *p, DIRINFO *di, int sticky
  348. or
  349.     struct ffblk *pff, DIRINFO *di
  350. */);
  351. static int fcmp(/* FILEINFO **pf1, FILEINFO **pf2 */);
  352. static HANDLE *hadd(/* char *n */);
  353. static int hsearch(/* char *n, int which, HANDLE **ph */);
  354. static DIRINFO *dadd(/* DEVID v, DIRID d */);
  355. static DIRINFO *dsearch(/* DEVID v, DIRID d */);
  356. static int match(/* char *pat, char *s, char **start1, int *len1 */);
  357. static void makerep(/*  */);
  358. static void checkcollisions(/*  */);
  359. static int rdcmp(/* REPDICT *rd1, REPDICT *rd2 */);
  360. static void findorder(/*  */);
  361. static void scandeletes(/* int (*pkilldel)(REP *p) */);
  362. static int baddel(/* REP *p */);
  363. static int skipdel(/* REP *p */);
  364. static void nochains(/*  */);
  365. static void printchain(/* REP *p */);
  366. static void goonordie(/*  */);
  367. static void doreps(/*  */);
  368. static long appendalias(/* REP *first, REP *p, int *pprintaliased */);
  369. static int movealias(/* REP *first, REP *p, int *pprintaliased */);
  370. static int snap(/* REP *first, REP *p */);
  371. static void showdone(/* REP *fin */);
  372. static void breakout(/*  */);
  373. static int breakrep(/* */);
  374. static void breakstat(/* */);
  375. static void quit(/*  */);
  376. static int copymove(/* REP *p */);
  377. static int copy(/* FILENFO *f, long len */);
  378. static int myunlink(/* char *n, FILEINFO *f */);
  379. static int getreply(/* char *m, int failact */);
  380. static void *myalloc(/* unsigned k */);
  381. static void *challoc(/* int k, int which */);
  382. static void chgive(/* void *p, unsigned k */);
  383. static int mygetc(/* */);
  384. static char *mygets(/* char *s, int l */);
  385. #ifdef MSDOS
  386. static int leave(/*  */);
  387. static void cleanup(/*  */);
  388. #else
  389. static int getstat(/* char *full, FILEINFO *f */);
  390. static int dwritable(/* HANDLE *h */);
  391. static int fwritable(/* char *hname, FILEINFO *f */);
  392. static void memmove(/* void *to, void *from, int k */);
  393. #endif
  394. #ifndef RENAME
  395. static int rename(/* char *from, char *to */);
  396. #endif
  397.  
  398. static int op, badstyle, delstyle, verbose, noex, matchall;
  399. static int patflags;
  400.  
  401. static unsigned ndirs = 0, dirroom;
  402. static DIRINFO **dirs;
  403. static unsigned nhandles = 0, handleroom;
  404. static HANDLE **handles;
  405. static HANDLE badhandle = {"\200", NULL, 0};
  406. static HANDLE *(lasthandle[2]) = {&badhandle, &badhandle};
  407. static unsigned nreps = 0;
  408. static REP hrep, *lastrep = &hrep;
  409. static CHUNK *freechunks = NULL;
  410. static SLICER slicer[2] = {{NULL, NULL, 0}, {NULL, NULL, 0}};
  411.  
  412. static int badreps = 0, paterr = 0, direrr, failed = 0, gotsig = 0, repbad;
  413. static FILE *outfile = stdout;
  414.  
  415. static char IDF[] = "$$mmvdid.";
  416. static char TEMP[] = "$$mmvtmp.";
  417. static char TOOLONG[] = "(too long)";
  418. static char EMPTY[] = "(empty)";
  419.  
  420. static char SLASHSTR[] = {SLASH, '\0'};
  421.  
  422. static char PATLONG[] = "%.40s... : pattern too long.\n";
  423.  
  424. char from[MAXPATLEN], to[MAXPATLEN];
  425. static int fromlen, tolen;
  426. static char *(stagel[MAXWILD]), *(firstwild[MAXWILD]), *(stager[MAXWILD]);
  427. static int nwilds[MAXWILD];
  428. static int nstages;
  429. char pathbuf[MAXPATH];
  430. char fullrep[MAXPATH + 1];
  431. static char *(start[MAXWILD]);
  432. static int len[MAXWILD];
  433. static char hasdot[MAXWILD];
  434. static REP mistake;
  435. #define MISTAKE (&mistake)
  436.  
  437. #ifdef MSDOS
  438.  
  439. static int olddevflag, curdisk, maxdisk;
  440. static struct {
  441.     char ph_banner[30];
  442.     char ph_name[9];
  443.     int ph_dfltop;
  444.     int ph_safeid;
  445.     int ph_clustoff;
  446.     int ph_driveoff;
  447.     int ph_drivea;
  448. } patch = {"mmv 1.0 patchable flags", "MMV", XMOVE, 1, 0};
  449.  
  450. #define DFLTOP (patch.ph_dfltop)
  451. #define CLUSTNO(pff) (*(int *)(((char *)(pff)) + patch.ph_clustoff))
  452. #define DRIVENO(pff) (*(((char *)(pff)) + patch.ph_driveoff) - patch.ph_drivea)
  453.  
  454.  
  455. #else
  456.  
  457. #define DFLTOP XMOVE
  458.  
  459. static char *home;
  460. static int homelen;
  461. static int uid, euid, oldumask;
  462. static DIRID cwdd = -1;
  463. static DEVID cwdv = -1;
  464.  
  465. #endif
  466.  
  467.  
  468. static char *cmdname;
  469. #define CMDNAME cmdname
  470. static int dostdin = 0;
  471.  
  472.  
  473. int main(argc, argv)
  474.     int argc;
  475.     char *(argv[]);
  476. {
  477.     char *frompat, *topat;
  478.  
  479.     init();
  480.     procargs(argc, argv, &frompat, &topat);
  481.     domatch(frompat, topat);
  482.     if (!(op & APPEND))
  483.         checkcollisions();
  484.     findorder();
  485.     if (op & (COPY | LINK))
  486.         nochains();
  487.     scandeletes(baddel);
  488.     goonordie();
  489.     if (!(op & APPEND) && delstyle == ASKDEL)
  490.         scandeletes(skipdel);
  491.     doreps();
  492.     return(failed ? 2 : nreps == 0 && (paterr || badreps));
  493. }
  494.  
  495.  
  496. static void init()
  497. {
  498. #ifdef MSDOS
  499.         _dos_getdrive(&curdisk);
  500.         _dos_setdrive(curdisk, &maxdisk);
  501.         atexit(cleanup);
  502.         signal(SIGINT, breakout);
  503. #else
  504.     struct stat dstat;
  505.  
  506.     if ((home = getenv("HOME")) == NULL || strcmp(home, SLASHSTR) == 0)
  507.         home = "";
  508.     if (!stat(".", &dstat)) {
  509.         cwdd = dstat.st_ino;
  510.         cwdv = dstat.st_dev;
  511.     }
  512.     oldumask = umask(0);
  513.     euid = geteuid();
  514.     uid = getuid();
  515.     signal(SIGINT, breakout);
  516. #endif
  517.  
  518.     dirroom = handleroom = INITROOM;
  519.     dirs = (DIRINFO **)myalloc(dirroom * sizeof(DIRINFO *));
  520.     handles = (HANDLE **)myalloc(handleroom * sizeof(HANDLE *));
  521.     ndirs = nhandles = 0;
  522. }
  523.  
  524.  
  525. static void usage()
  526. {
  527.         printf(USAGE, CMDNAME, OTHEROPT);
  528.         exit(1);
  529. }
  530.  
  531.  
  532. static void procargs(argc, argv, pfrompat, ptopat)
  533.     int argc;
  534.     char **argv;
  535.     char **pfrompat, **ptopat;
  536. {
  537.         char *p, c;
  538.  
  539.         cmdname = argv[0];
  540.  
  541. #ifdef MSDOS
  542.         if ( (p = strrchr(cmdname, '\\')) != NULL )
  543.           cmdname = p + 1;
  544.         if ( (p = strchr(cmdname, '.')) != NULL )
  545.           *p = 0;
  546.         strlwr(cmdname);
  547. #endif
  548.  
  549.     op = DFLT;
  550.     verbose = noex = matchall = 0;
  551.     delstyle = ASKDEL;
  552.     badstyle = ASKBAD;
  553.     for (argc--, argv++; argc > 0 && **argv == '-'; argc--, argv++)
  554.         for (p = *argv + 1; *p != '\0'; p++) {
  555.             c = mylower(*p);
  556.             if (c == 'v' && !noex)
  557.                 verbose = 1;
  558.             else if (c == 'n' && !verbose)
  559.                 noex = 1;
  560.                         else if (c == 's')
  561.                                 dostdin = 1;
  562.             else if (c == 'h')
  563.                 matchall = 1;
  564.             else if (c == 'd' && delstyle == ASKDEL)
  565.                 delstyle = ALLDEL;
  566.             else if (c == 'p' && delstyle == ASKDEL)
  567.                 delstyle = NODEL;
  568.             else if (c == 'g' && badstyle == ASKBAD)
  569.                 badstyle = SKIPBAD;
  570.             else if (c == 't' && badstyle == ASKBAD)
  571.                 badstyle = ABORTBAD;
  572.             else if (c == 'm' && op == DFLT)
  573.                 op = NORMMOVE;
  574.             else if (c == 'x' && op == DFLT)
  575.                 op = XMOVE;
  576.             else if (c == 'r' && op == DFLT)
  577.                 op = DIRMOVE;
  578.             else if (c == 'c' && op == DFLT)
  579.                 op = NORMCOPY;
  580.             else if (c == 'o' && op == DFLT)
  581.                 op = OVERWRITE;
  582.             else if (c == 'a' && op == DFLT)
  583.                 op = NORMAPPEND;
  584. #ifdef MSDOS
  585.             else if (c == 'z' && op == DFLT)
  586.                 op = ZAPPEND;
  587. #else
  588.             else if (c == 'l' && op == DFLT)
  589.                 op = HARDLINK;
  590. #ifndef SYSV
  591.             else if (c == 's' && op == DFLT)
  592.                 op = SYMLINK;
  593. #endif
  594. #endif
  595.                         else
  596.                                 usage();
  597.         }
  598.  
  599.     if (op == DFLT)
  600.                 if (strcmp(cmdname, MOVENAME) == 0)
  601.             op = XMOVE;
  602.                 else if (strcmp(cmdname, COPYNAME) == 0)
  603.             op = NORMCOPY;
  604.                 else if (strcmp(cmdname, APPENDNAME) == 0)
  605.             op = NORMAPPEND;
  606.                 else if (strcmp(cmdname, LINKNAME) == 0)
  607.             op = HARDLINK;
  608.         else
  609.             op = DFLTOP;
  610.     if (
  611.         op & DIRMOVE &&
  612. #ifdef MSDOS
  613.         _osmajor < 3
  614. #else
  615. #ifndef RENAME
  616.         euid != 0
  617. #else
  618.         0
  619. #endif
  620. #endif
  621.     ) {
  622.         fprintf(stderr,
  623.             "Unable to do directory renames. Option -r refused.\n");
  624.         quit();
  625.     }
  626.  
  627. #ifndef MSDOS
  628.     if (euid != uid && !(op & DIRMOVE)) {
  629.         setuid(uid);
  630.         setgid(getgid());
  631.     }
  632. #endif
  633.  
  634.     if (badstyle != ASKBAD && delstyle == ASKDEL)
  635.         delstyle = NODEL;
  636.  
  637.     if (argc == 0)
  638.         *pfrompat = NULL;
  639.     else if (argc == 2) {
  640.         *pfrompat = *(argv++);
  641.         *ptopat = *(argv++);
  642.     }
  643.         else
  644.                 usage();
  645. }
  646.  
  647.  
  648. static void domatch(cfrom, cto)
  649.     char *cfrom, *cto;
  650. {
  651.         if (cfrom == NULL)
  652.         {
  653.                 if ( dostdin )
  654.                         while (getpat())
  655.                             matchpat();
  656.                 else
  657.                         usage();
  658.         }
  659.     else if ((fromlen = strlen(cfrom)) >= MAXPATLEN) {
  660.         printf(PATLONG, cfrom);
  661.         paterr = 1;
  662.     }
  663.     else if ((tolen = strlen(cto)) >= MAXPATLEN) {
  664.         printf(PATLONG, cto);
  665.         paterr = 1;
  666.     }
  667.     else {
  668.         strcpy(from, cfrom);
  669.         strcpy(to, cto);
  670.         matchpat();
  671.     }
  672. }
  673.  
  674.  
  675. static int getpat()
  676. {
  677.     int c, gotit = 0;
  678.     char extra[MAXPATLEN];
  679.  
  680.     patflags = 0;
  681.     do {
  682.         if ((fromlen = getword(from)) == 0 || fromlen == -1)
  683.             goto nextline;
  684.  
  685.         do {
  686.             if ((tolen = getword(to)) == 0) {
  687.                 printf("%s -> ? : missing replacement pattern.\n", from);
  688.                 goto nextline;
  689.             }
  690.             if (tolen == -1)
  691.                 goto nextline;
  692.         } while (
  693.             tolen == 2 &&
  694.             (to[0] == '-' || to[0] == '=') &&
  695.             (to[1] == '>' || to[1] == '^')
  696.         );
  697.         if (getword(extra) == 0)
  698.             gotit = 1;
  699.         else if (strcmp(extra, "(*)") == 0) {
  700.             patflags |= R_DELOK;
  701.             gotit = (getword(extra) == 0);
  702.         }
  703.  
  704. nextline:
  705.         while ((c = mygetc()) != '\n' && c != EOF)
  706.             ;
  707.         if (c == EOF)
  708.             return(0);
  709.     } while (!gotit);
  710.  
  711.     return(1);
  712. }
  713.  
  714.  
  715. static int getword(buf)
  716.     char *buf;
  717. {
  718.     int c, prevc, n;
  719.     char *p;
  720.  
  721.     p = buf;
  722.     prevc = ' ';
  723.     n = 0;
  724.     while ((c = mygetc()) != EOF && (prevc == ESC || !isspace(c))) {
  725.         if (n == -1)
  726.             continue;
  727.         if (n == MAXPATLEN - 1) {
  728.             *p = '\0';
  729.             printf(PATLONG, buf);
  730.             n = -1;
  731.         }
  732.                 *(p++) = (char) c;
  733.         n++;
  734.         prevc = c;
  735.     }
  736.     *p = '\0';
  737.     while (c != EOF && isspace(c) && c != '\n')
  738.         c = mygetc();
  739.     if (c != EOF)
  740.         ungetc(c, stdin);
  741.     return(n);
  742. }
  743.  
  744.  
  745. static void matchpat()
  746. {
  747.     if (parsepat())
  748.         paterr = 1;
  749.     else if (dostage(from, pathbuf, start, len, 0, 0)) {
  750.         printf("%s -> %s : no match.\n", from, to);
  751.         paterr = 1;
  752.     }
  753. }
  754.  
  755.  
  756. static int parsepat()
  757. {
  758.     char *p, *lastname, c;
  759.     int totwilds, instage, x, havedot;
  760.     static char TRAILESC[] = "%s -> %s : trailing %c is superfluous.\n";
  761.  
  762.     lastname = from;
  763. #ifdef MSDOS
  764.     havedot = 0;
  765.     if (from[0] != '\0' && from[1] == ':')
  766.         lastname += 2;
  767. #else
  768.     if (from[0] == '~' && from[1] == SLASH) {
  769.         if ((homelen = strlen(home)) + fromlen > MAXPATLEN) {
  770.             printf(PATLONG, from);
  771.             return(-1);
  772.         }
  773.         memmove(from + homelen, from + 1, fromlen);
  774.         memmove(from, home, homelen);
  775.         lastname += homelen + 1;
  776.     }
  777. #endif
  778.     totwilds = nstages = instage = 0;
  779.     for (p = lastname; (c = *p) != '\0'; p++)
  780.         switch (c) {
  781. #ifdef MSDOS
  782.         case '.':
  783.             havedot = 1;
  784.             break;
  785.         case OTHERSLASH:
  786.             *p = SLASH;
  787. #endif
  788.          case SLASH:
  789. #ifdef MSDOS
  790.             if (!havedot && lastname != p) {
  791.                 if (fromlen++ == MAXPATLEN) {
  792.                     printf(PATLONG, from);
  793.                     return(-1);
  794.                 }
  795.                 memmove(p + 1, p, strlen(p) + 1);
  796.                 *(p++) = '.';
  797.             }
  798.             else
  799.                 havedot = 0;
  800. #endif
  801.             lastname = p + 1;
  802.             if (instage) {
  803.                 if (firstwild[nstages] == NULL)
  804.                     firstwild[nstages] = p;
  805.                 stager[nstages++] = p;
  806.                 instage = 0;
  807.             }
  808.             break;
  809.         case ';':
  810.             if (lastname != p) {
  811.                 printf("%s -> %s : badly placed ;.\n", from, to);
  812.                 return(-1);
  813.             }
  814.         case '!':
  815.         case '*':
  816.         case '?':
  817.         case '[':
  818. #ifdef MSDOS
  819.                         if ((hasdot[totwilds] = (char) (c == '!')) != 0)
  820.                 havedot = 1;
  821. #endif
  822.             if (totwilds++ == MAXWILD) {
  823.                 printf("%s -> %s : too many wildcards.\n", from, to);
  824.                 return(-1);
  825.             }
  826.             if (instage) {
  827.                 nwilds[nstages]++;
  828.                 if (firstwild[nstages] == NULL)
  829.                     firstwild[nstages] = p;
  830.             }
  831.             else {
  832.                 stagel[nstages] = lastname;
  833.                 firstwild[nstages] = (c == ';' ? NULL : p);
  834.                 nwilds[nstages] = 1;
  835.                 instage = 1;
  836.             }
  837.             if (c != '[')
  838.                 break;
  839.             while ((c = *(++p)) != ']') {
  840.                 switch (c) {
  841.                 case '\0':
  842.                     printf("%s -> %s : missing ].\n", from, to);
  843.                     return(-1);
  844. #ifdef MSDOS
  845.                 case '.':
  846.                 case ':':
  847.                 case OTHERSLASH:
  848. #endif
  849.                 case SLASH:
  850.                     printf("%s -> %s : '%c' can not be part of [].\n",
  851.                         from, to, c);
  852.                     return(-1);
  853.                 case ESC:
  854.                     if ((c = *(++p)) == '\0') {
  855.                         printf(TRAILESC, from, to, ESC);
  856.                         return(-1);
  857.                     }
  858. #ifdef MSDOS
  859.                 default:
  860.                     if (isupper(c))
  861.                         *p = c + ('a' - 'A');
  862. #endif
  863.                 }
  864.             }
  865.             break;
  866.         case ESC:
  867.             if ((c = *(++p)) == '\0') {
  868.                 printf(TRAILESC, from, to, ESC);
  869.                 return(-1);
  870.             }
  871. #ifdef MSDOS
  872.         default:
  873.             if (isupper(c))
  874.                 *p = c + ('a' - 'A');
  875. #endif
  876.         }
  877.  
  878. #ifdef MSDOS
  879.     if (!havedot && lastname != p) {
  880.         if (fromlen++ == MAXPATLEN) {
  881.             printf(PATLONG, from);
  882.             return(-1);
  883.         }
  884.         strcpy(p++, ".");
  885.     }
  886. #endif
  887.  
  888.     if (instage) {
  889.         if (firstwild[nstages] == NULL)
  890.             firstwild[nstages] = p;
  891.         stager[nstages++] = p;
  892.     }
  893.     else {
  894.         stagel[nstages] = lastname;
  895.         nwilds[nstages] = 0;
  896.         firstwild[nstages] = p;
  897.         stager[nstages++] = p;
  898.     }
  899.  
  900.     lastname = to;
  901. #ifdef MSDOS
  902.     havedot = 0;
  903.     if (to[0] != '\0' && to[1] == ':')
  904.         lastname += 2;
  905. #else
  906.     if (to[0] == '~' && to[1] == SLASH) {
  907.         if ((homelen = strlen(home)) + tolen > MAXPATLEN) {
  908.             printf(PATLONG, to);
  909.                 return(-1);
  910.         }
  911.         memmove(to + homelen, to + 1, tolen);
  912.         memmove(to, home, homelen);
  913.         lastname += homelen + 1;
  914.     }
  915. #endif
  916.  
  917.     for (p = lastname; (c = *p) != '\0'; p++)
  918.         switch (c) {
  919. #ifdef MSDOS
  920.         case '.':
  921.             havedot = 1;
  922.             break;
  923.         case OTHERSLASH:
  924.             *p = SLASH;
  925. #endif
  926.         case SLASH:
  927.             if (op & DIRMOVE) {
  928.                 printf("%s -> %s : no path allowed in target under -r.\n",
  929.                     from, to);
  930.                 return(-1);
  931.             }
  932. #ifdef MSDOS
  933.             if (!havedot && lastname != p) {
  934.                 if (tolen++ == MAXPATLEN) {
  935.                     printf(PATLONG, to);
  936.                     return(-1);
  937.                 }
  938.                 memmove(p + 1, p, strlen(p) + 1);
  939.                 *(p++) = '.';
  940.             }
  941.             else
  942.                 havedot = 0;
  943. #endif
  944.             lastname = p + 1;
  945.             break;
  946.         case '=':
  947.             c = *(++p);
  948.             if (c == 'l' || c == 'u') {
  949. #ifdef MSDOS
  950.                 strcpy(p, p + 1);
  951.                 c = *p;
  952. #else
  953.                 c = *(++p);
  954. #endif
  955.             }
  956.             if (!isdigit(c)) {
  957.                 printf("%s -> %s : expected digit (not '%c') after =.\n",
  958.                     from, to, c);
  959.                 return(-1);
  960.             }
  961.             for(x = 0; ;x *= 10) {
  962.                 x += c - '0';
  963.                 c = *(p+1);
  964.                 if (!isdigit(c))
  965.                     break;
  966.                 p++;
  967.             }
  968.             if (x < 1 || x > totwilds) {
  969.                 printf("%s -> %s : wildcard %d does not exist.\n",
  970.                     from, to, x);
  971.                 return(-1);
  972.             }
  973. #ifdef MSDOS
  974.             if (hasdot[x - 1])
  975.                 havedot = 1;
  976. #endif
  977.             break;
  978.         case ESC:
  979.             if ((c = *(++p)) == '\0') {
  980.                 printf(TRAILESC, from, to, ESC);
  981.                 return(-1);
  982.             }
  983.         default:
  984.             if (
  985. #ifdef MSDOS
  986.                 c <= ' ' || c >= 127 ||
  987.                 strchr(":/\\*?[]=+;,\"|<>", c) != NULL
  988. #else
  989.                 c & 0x80
  990. #endif
  991.             ) {
  992.                 printf("%s -> %s : illegal character '%c' (0x%02X).\n",
  993.                     from, to, c, c);
  994.                 return(-1);
  995.             }
  996. #ifdef MSDOS
  997.             if (isupper(c))
  998.                 *p = c + ('a' - 'A');
  999. #endif
  1000.         }
  1001.  
  1002. #ifdef MSDOS
  1003.     if (!havedot && lastname != p) {
  1004.         if (tolen++ == MAXPATLEN) {
  1005.             printf(PATLONG, to);
  1006.             return(-1);
  1007.         }
  1008.         strcpy(p++, ".");
  1009.     }
  1010. #endif
  1011.  
  1012.     return(0);
  1013. }
  1014.  
  1015.  
  1016. static int dostage(lastend, pathend, start1, len1, stage, anylev)
  1017.     char *lastend, *pathend;
  1018.     char **start1;
  1019.     int *len1;
  1020.     int stage;
  1021.     int anylev;
  1022. {
  1023.     DIRINFO *di;
  1024.     HANDLE *h, *hto;
  1025.     int prelen, litlen, nfils, i, k, flags, try;
  1026.     FILEINFO **pf, *fdel;
  1027.     char *nto, *firstesc;
  1028.     REP *p;
  1029.     int wantdirs, ret = 1, laststage = (stage + 1 == nstages);
  1030.  
  1031.     wantdirs = !laststage ||
  1032.         (op & (DIRMOVE | SYMLINK)) ||
  1033.         (nwilds[nstages - 1] == 0);
  1034.  
  1035.     if (!anylev) {
  1036.         prelen = stagel[stage] - lastend;
  1037.         if (pathend - pathbuf + prelen >= MAXPATH) {
  1038.             printf("%s -> %s : search path after %s too long.\n",
  1039.                 from, to, pathbuf);
  1040.             paterr = 1;
  1041.             return(1);
  1042.         }
  1043.         memmove(pathend, lastend, prelen);
  1044.         pathend += prelen;
  1045.         *pathend = '\0';
  1046.         lastend = stagel[stage];
  1047.     }
  1048.  
  1049.     if ((h = checkdir(pathbuf, pathend, 0)) == NULL) {
  1050.         if (stage == 0 || direrr == H_NOREADDIR) {
  1051.             printf("%s -> %s : directory %s does not %s.\n",
  1052.                 from, to, pathbuf, direrr == H_NOREADDIR ?
  1053.                 "allow reads/searches" : "exist");
  1054.             paterr = 1;
  1055.         }
  1056.         return(stage);
  1057.     }
  1058.     di = h->h_di;
  1059.  
  1060.     if (*lastend == ';') {
  1061.         anylev = 1;
  1062.         *start1 = pathend;
  1063.         *len1 = 0;
  1064.         lastend++;
  1065.     }
  1066.  
  1067.     nfils = di->di_nfils;
  1068.  
  1069. #ifndef MSDOS
  1070.     if ((op & MOVE) && !dwritable(h)) {
  1071.         printf("%s -> %s : directory %s does not allow writes.\n",
  1072.             from, to, pathbuf);
  1073.         paterr = 1;
  1074.         goto skiplev;
  1075.     }
  1076. #endif
  1077.  
  1078.     firstesc = strchr(lastend, ESC);
  1079.     if (firstesc == NULL || firstesc > firstwild[stage])
  1080.         firstesc = firstwild[stage];
  1081.     litlen = firstesc - lastend;
  1082.     pf = di->di_fils + (i = ffirst(lastend, litlen, di));
  1083.     if (i < nfils)
  1084.     do {
  1085.         if (
  1086.             (try = trymatch(*pf, lastend)) != 0 &&
  1087.             (
  1088.                 try == 1 ||
  1089.                 match(lastend + litlen, (*pf)->fi_name + litlen,
  1090.                     start1 + anylev, len1 + anylev)
  1091.             ) &&
  1092.             keepmatch(*pf, pathend, &k, 0, wantdirs, laststage)
  1093.         ) {
  1094.             if (!laststage)
  1095.                 ret &= dostage(stager[stage], pathend + k,
  1096.                     start1 + nwilds[stage], len1 + nwilds[stage],
  1097.                     stage + 1, 0);
  1098.             else {
  1099.                 ret = 0;
  1100.                 makerep();
  1101.                 if (badrep(h, *pf, &hto, &nto, &fdel, &flags))
  1102.                     (*pf)->fi_rep = MISTAKE;
  1103.                 else {
  1104.                     (*pf)->fi_rep = p = (REP *)challoc(sizeof(REP), 1);
  1105.                                         p->r_flags = (char) flags | patflags;
  1106.                     p->r_hfrom = h;
  1107.                     p->r_ffrom = *pf;
  1108.                     p->r_hto = hto;
  1109.                     p->r_nto = nto;
  1110.                     p->r_fdel = fdel;
  1111.                     p->r_first = p;
  1112.                     p->r_thendo = NULL;
  1113.                     p->r_next = NULL;
  1114.                     lastrep->r_next = p;
  1115.                     lastrep = p;
  1116.                     nreps++;
  1117.                 }
  1118.             }
  1119.         }
  1120.         i++, pf++;
  1121.     } while (i < nfils && strncmp(lastend, (*pf)->fi_name, litlen) == 0);
  1122.  
  1123. skiplev:
  1124.     if (anylev)
  1125.         for (pf = di->di_fils, i = 0; i < nfils; i++, pf++)
  1126.             if (
  1127.                 *((*pf)->fi_name) != '.' &&
  1128. #ifdef MSDOS
  1129.                                 ((*pf)->fi_attrib & _A_SUBDIR) &&
  1130. #endif
  1131.                 keepmatch(*pf, pathend, &k, 1, 1, 0)
  1132.             ) {
  1133.                 *len1 = pathend - *start1 + k;
  1134.                 ret &= dostage(lastend, pathend + k, start1, len1, stage, 1);
  1135.             }
  1136.  
  1137.     return(ret);
  1138. }
  1139.  
  1140.  
  1141. static int trymatch(ffrom, pat)
  1142.     FILEINFO *ffrom;
  1143.     char *pat;
  1144. {
  1145.     char *p;
  1146.  
  1147.     if (ffrom->fi_rep != NULL)
  1148.         return(0);
  1149.  
  1150.     p = ffrom->fi_name;
  1151.  
  1152. #ifdef MSDOS
  1153.         if (*p == '.' || (!matchall && ffrom->fi_attrib & (_A_HIDDEN | _A_SYSTEM)))
  1154.         return(strcmp(pat, p) == 0);
  1155. #else
  1156.     if (*p == '.')
  1157.         if (p[1] == '\0' || (p[1] == '.' && p[2] == '\0'))
  1158.             return(strcmp(pat, p) == 0);
  1159.         else if (!matchall && *pat != '.')
  1160.             return(0);
  1161. #endif
  1162.     return(-1);
  1163. }
  1164.  
  1165.  
  1166. static int keepmatch(ffrom, pathend, pk, needslash, dirs, fils)
  1167.     FILEINFO *ffrom;
  1168.     char *pathend;
  1169.     int *pk;
  1170.     int needslash;
  1171.     int dirs, fils;
  1172. {
  1173.     *pk = strlen(ffrom->fi_name);
  1174.     if (pathend - pathbuf + *pk + needslash >= MAXPATH) {
  1175.         *pathend = '\0';
  1176.         printf("%s -> %s : search path %s%s too long.\n",
  1177.             from, to, pathbuf, ffrom->fi_name);
  1178.         paterr = 1;
  1179.         return(0);
  1180.     }
  1181.     strcpy(pathend, ffrom->fi_name);
  1182. #ifdef MSDOS
  1183.         if ((ffrom->fi_attrib & _A_SUBDIR) ? !dirs : !fils)
  1184. #else
  1185.     getstat(pathbuf, ffrom);
  1186.     if ((ffrom->fi_stflags & FI_ISDIR) ? !dirs : !fils)
  1187. #endif
  1188.         return(0);
  1189.  
  1190.     if (needslash) {
  1191.         strcpy(pathend + *pk, SLASHSTR);
  1192.         (*pk)++;
  1193.     }
  1194.     return(1);
  1195. }
  1196.  
  1197.  
  1198. static int badrep(hfrom, ffrom, phto, pnto, pfdel, pflags)
  1199.     HANDLE *hfrom;
  1200.     FILEINFO *ffrom;
  1201.     HANDLE **phto;
  1202.     char **pnto;
  1203.     FILEINFO **pfdel;
  1204.     int *pflags;
  1205. {
  1206.     char *f = ffrom->fi_name;
  1207.  
  1208.     *pflags = 0;
  1209.     if (
  1210. #ifdef MSDOS
  1211.                 (ffrom->fi_attrib & _A_SUBDIR) &&
  1212. #else
  1213.         (ffrom->fi_stflags & FI_ISDIR) &&
  1214. #endif
  1215.         !(op & (DIRMOVE | SYMLINK))
  1216.     )
  1217.         printf("%s -> %s : source file is a directory.\n", pathbuf, fullrep);
  1218. #ifndef MSDOS
  1219. #ifndef SYSV
  1220.     else if ((ffrom->fi_stflags & FI_LINKERR) && !(op & (MOVE | SYMLINK)))
  1221.         printf("%s -> %s : source file is a badly aimed symbolic link.\n",
  1222.             pathbuf, fullrep);
  1223.     else if ((ffrom->fi_stflags & FI_NODEL) && (op & MOVE))
  1224.         printf("%s -> %s : no delete permission for source file.\n",
  1225.             pathbuf, fullrep);
  1226. #endif
  1227.     else if ((op & (COPY | APPEND)) && access(pathbuf, R_OK))
  1228.         printf("%s -> %s : no read permission for source file.\n",
  1229.             pathbuf, fullrep);
  1230. #endif
  1231.     else if (
  1232.         *f == '.' &&
  1233.         (f[1] == '\0' || strcmp(f, "..") == 0) &&
  1234.         !(op & SYMLINK)
  1235.     )
  1236.         printf("%s -> %s : . and .. can't be renamed.\n", pathbuf, fullrep);
  1237.     else if (repbad || checkto(hfrom, f, phto, pnto, pfdel) || badname(*pnto))
  1238.         printf("%s -> %s : bad new name.\n", pathbuf, fullrep);
  1239.     else if (*phto == NULL)
  1240.         printf("%s -> %s : %s.\n", pathbuf, fullrep,
  1241. #ifndef MSDOS
  1242.             direrr == H_NOREADDIR ?
  1243.             "no read or search permission for target directory" :
  1244. #endif
  1245.             "target directory does not exist");
  1246. #ifndef MSDOS
  1247.     else if (!dwritable(*phto))
  1248.         printf("%s -> %s : no write permission for target directory.\n",
  1249.             pathbuf, fullrep);
  1250. #endif
  1251.     else if (
  1252.         (*phto)->h_di->di_vid != hfrom->h_di->di_vid &&
  1253.         (*pflags = R_ISX, (op & (NORMMOVE | HARDLINK)))
  1254.     )
  1255.         printf("%s -> %s : cross-device move.\n",
  1256.             pathbuf, fullrep);
  1257. #ifndef MSDOS
  1258.     else if (
  1259.         *pflags && (op & MOVE) &&
  1260.         !(ffrom->fi_stflags & FI_ISLNK) &&
  1261.         access(pathbuf, R_OK)
  1262.     )
  1263.         printf("%s -> %s : no read permission for source file.\n",
  1264.             pathbuf, fullrep);
  1265. #ifndef SYSV
  1266.     else if (
  1267.         (op & SYMLINK) &&
  1268.         !(
  1269.             ((*phto)->h_di->di_vid == cwdv && (*phto)->h_di->di_did == cwdd) ||
  1270.             *(hfrom->h_name) == SLASH ||
  1271.             (*pflags |= R_ONEDIRLINK, hfrom->h_di == (*phto)->h_di)
  1272.         )
  1273.     )
  1274.         printf("%s -> %s : symbolic link would be badly aimed.\n",
  1275.             pathbuf, fullrep);
  1276. #endif
  1277. #endif
  1278.     else
  1279.         return(0);
  1280.     badreps++;
  1281.     return(-1);
  1282. }
  1283.  
  1284.  
  1285. static int checkto(hfrom, f, phto, pnto, pfdel)
  1286.     HANDLE *hfrom;
  1287.     char *f;
  1288.     HANDLE **phto;
  1289.     char **pnto;
  1290.     FILEINFO **pfdel;
  1291. {
  1292.     char tpath[MAXPATH + 1];
  1293.     char *pathend;
  1294.     FILEINFO *fdel;
  1295.     int hlen, tlen;
  1296.  
  1297.     if (op & DIRMOVE) {
  1298.         *phto = hfrom;
  1299.         hlen = strlen(hfrom->h_name);
  1300.         pathend = fullrep + hlen;
  1301.         memmove(pathend, fullrep, strlen(fullrep) + 1);
  1302.         memmove(fullrep, hfrom->h_name, hlen);
  1303.         if ((fdel = *pfdel = fsearch(pathend, hfrom->h_di)) != NULL) {
  1304.             *pnto = fdel->fi_name;
  1305. #ifndef MSDOS
  1306.             getstat(fullrep, fdel);
  1307. #endif
  1308.         }
  1309.         else
  1310.             *pnto = mydup(pathend);
  1311.     }
  1312.     else {
  1313.         pathend = getpath(tpath);
  1314.         hlen = pathend - fullrep;
  1315.         *phto = checkdir(tpath, tpath + hlen, 1);
  1316.         if (
  1317.             *phto != NULL &&
  1318.             *pathend != '\0' &&
  1319.             (fdel = *pfdel = fsearch(pathend, (*phto)->h_di)) != NULL &&
  1320. #ifdef MSDOS
  1321.                         (fdel->fi_attrib & _A_SUBDIR)
  1322. #else
  1323.             (getstat(fullrep, fdel), fdel->fi_stflags & FI_ISDIR)
  1324. #endif
  1325.         ) {
  1326.             tlen = strlen(pathend);
  1327.             strcpy(pathend + tlen, SLASHSTR);
  1328.             tlen++;
  1329.             strcpy(tpath + hlen, pathend);
  1330.             pathend += tlen;
  1331.             hlen += tlen;
  1332.             *phto = checkdir(tpath, tpath + hlen, 1);
  1333.         }
  1334.  
  1335.         if (*pathend == '\0') {
  1336.             *pnto = f;
  1337.             if (pathend - fullrep + strlen(f) >= MAXPATH) {
  1338.                 strcpy(fullrep, TOOLONG);
  1339.                 return(-1);
  1340.             }
  1341.             strcat(pathend, f);
  1342.             if (*phto != NULL) {
  1343.                 fdel = *pfdel = fsearch(f, (*phto)->h_di);
  1344. #ifndef MSDOS
  1345.                 if (fdel != NULL)
  1346.                     getstat(fullrep, fdel);
  1347. #endif
  1348.             }
  1349.         }
  1350.         else if (fdel != NULL)
  1351.             *pnto = fdel->fi_name;
  1352.         else
  1353.             *pnto = mydup(pathend);
  1354.     }
  1355.     return(0);
  1356. }
  1357.  
  1358.  
  1359. static char *getpath(tpath)
  1360.     char *tpath;
  1361. {
  1362.     char *pathstart, *pathend, c;
  1363.  
  1364. #ifdef MSDOS
  1365.     if (*fullrep != '\0' && fullrep[1] == ':')
  1366.         pathstart = fullrep + 2;
  1367.     else
  1368. #endif
  1369.         pathstart = fullrep;
  1370.  
  1371.     pathend = pathstart + strlen(pathstart) - 1;
  1372.     while (pathend >= pathstart && *pathend != SLASH)
  1373.         --pathend;
  1374.     pathend++;
  1375.  
  1376.     c = *pathend;
  1377.     *pathend = '\0';
  1378.     strcpy(tpath, fullrep);
  1379.     *pathend = c;
  1380.     return(pathend);
  1381. }
  1382.  
  1383.  
  1384. static int badname(s)
  1385.     char *s;
  1386. {
  1387.     char *ext;
  1388.  
  1389.     return (
  1390. #ifdef MSDOS
  1391.         *s == ' ' ||
  1392.         *s == '.' ||
  1393.                 (ext = strchr(s, '.')) - s >= _MAX_FNAME ||
  1394.         (*ext == '.' && strchr(ext + 1, '.') != NULL) ||
  1395.                 strlen(ext) >= _MAX_EXT ||
  1396.         strncmp(s, IDF, STRLEN(IDF)) == 0
  1397. #else
  1398.         (*s == '.' && (s[1] == '\0' || strcmp(s, "..") == 0)) ||
  1399.         strlen(s) > MAXNAMLEN
  1400. #endif
  1401.     );
  1402. }
  1403.  
  1404.  
  1405. #ifndef MSDOS
  1406. static int getstat(ffull, f)
  1407.     char *ffull;
  1408.     FILEINFO *f;
  1409. {
  1410.     struct stat fstat;
  1411.     int flags;
  1412.  
  1413.     if ((flags = f->fi_stflags) & FI_STTAKEN)
  1414.         return(flags & FI_LINKERR);
  1415.     flags |= FI_STTAKEN;
  1416. #ifdef SYSV
  1417.     if (stat(ffull, &fstat)) {
  1418.         fprintf(stderr, "Strange, couldn't stat %s.\n", ffull);
  1419.         quit();
  1420.     }
  1421. #else
  1422.     if (lstat(ffull, &fstat)) {
  1423.         fprintf(stderr, "Strange, couldn't lstat %s.\n", ffull);
  1424.         quit();
  1425.     }
  1426.     if ((flags & FI_INSTICKY) && fstat.st_uid != uid && uid != 0)
  1427.         flags |= FI_NODEL;
  1428.     if ((fstat.st_mode & S_IFMT) == S_IFLNK) {
  1429.         flags |= FI_ISLNK;
  1430.         if (stat(ffull, &fstat)) {
  1431.             f->fi_stflags = flags | FI_LINKERR;
  1432.             return(1);
  1433.         }
  1434.     }
  1435. #endif
  1436.     if ((fstat.st_mode & S_IFMT) == S_IFDIR)
  1437.         flags |= FI_ISDIR;
  1438.     f->fi_stflags = flags;
  1439.     f->fi_mode = fstat.st_mode;
  1440.     return(0);
  1441. }
  1442.  
  1443.  
  1444. static int dwritable(h)
  1445.     HANDLE *h;
  1446. {
  1447.     char *p = h->h_name, *myp, *lastslash = NULL, *pathend;
  1448.     char *pw = &(h->h_di->di_flags), r;
  1449.  
  1450.     if (uid == 0)
  1451.         return(1);
  1452.  
  1453.     if (*pw & DI_KNOWWRITE)
  1454.         return(*pw & DI_CANWRITE);
  1455.  
  1456.     pathend = p + strlen(p);
  1457.     if (*p == '\0')
  1458.         myp = ".";
  1459.     else if (pathend == p + 1)
  1460.         myp = SLASHSTR;
  1461.     else {
  1462.         lastslash = pathend - 1;
  1463.         *lastslash = '\0';
  1464.         myp = p;
  1465.     }
  1466.     r = !access(myp, W_OK) ? DI_CANWRITE : 0;
  1467.     *pw |= DI_KNOWWRITE | r;
  1468.  
  1469.     if (lastslash != NULL)
  1470.         *lastslash = SLASH;
  1471.     return(r);
  1472. }
  1473.  
  1474.  
  1475. static int fwritable(hname, f)
  1476.     char *hname;
  1477.     FILEINFO *f;
  1478. {
  1479.     int r;
  1480.  
  1481.     if (f->fi_stflags & FI_KNOWWRITE)
  1482.         return(f->fi_stflags & FI_CANWRITE);
  1483.  
  1484.     strcpy(fullrep, hname);
  1485.     strcat(fullrep, f->fi_name);
  1486.     r = !access(fullrep, W_OK) ? FI_CANWRITE : 0;
  1487.     f->fi_stflags |= FI_KNOWWRITE | r;
  1488.     return(r);
  1489. }
  1490. #endif
  1491.  
  1492.  
  1493. static FILEINFO *fsearch(s, d)
  1494.     char *s;
  1495.     DIRINFO *d;
  1496. {
  1497.     FILEINFO **fils = d->di_fils;
  1498.     int nfils = d->di_nfils;
  1499.     int first, k, last, res;
  1500.  
  1501.     for(first = 0, last = nfils - 1;;) {
  1502.         if (last < first)
  1503.             return(NULL);
  1504.         k = (first + last) >> 1;
  1505.         if ((res = strcmp(s, fils[k]->fi_name)) == 0)
  1506.             return(fils[k]);
  1507.         if (res < 0)
  1508.             last = k - 1;
  1509.         else
  1510.             first = k + 1;
  1511.     }
  1512. }
  1513.  
  1514.  
  1515. static int ffirst(s, n, d)
  1516.     char *s;
  1517.     int n;
  1518.     DIRINFO *d;
  1519. {
  1520.     int first, k, last, res;
  1521.     FILEINFO **fils = d->di_fils;
  1522.     int nfils = d->di_nfils;
  1523.  
  1524.     if (nfils == 0 || n == 0)
  1525.         return(0);
  1526.     first = 0;
  1527.     last = nfils - 1;
  1528.     for(;;) {
  1529.         k = (first + last) >> 1;
  1530.         res = strncmp(s, fils[k]->fi_name, n);
  1531.         if (first == last)
  1532.             return(res == 0 ? k : nfils);
  1533.         else if (res > 0)
  1534.             first = k + 1;
  1535.         else
  1536.             last = k;
  1537.     }
  1538. }
  1539.  
  1540.  
  1541. #ifdef MSDOS
  1542. /* checkdir and takedir for MS-D*S */
  1543.  
  1544. static HANDLE *checkdir(p, pathend, which)
  1545.     char *p, *pathend;
  1546.     int which;
  1547. {
  1548.         struct find_t de;
  1549.     DIRID d;
  1550.     DEVID v;
  1551.     HANDLE *h;
  1552.     char *dirstart = p;
  1553.     int fd;
  1554.     int firstfound;
  1555.     DIRINFO *di;
  1556.  
  1557.     if (hsearch(p, which, &h))
  1558.         if (h->h_di == NULL) {
  1559.             direrr = h->h_err;
  1560.             return(NULL);
  1561.         }
  1562.         else
  1563.             return(h);
  1564.  
  1565.     if (*p == '\0' || p[1] != ':')
  1566.         v = curdisk;
  1567.     else {
  1568.         dirstart += 2;
  1569.         v = mylower(p[0]) - 'a';
  1570.         if (v < 0 || v >= maxdisk)
  1571.             return(NULL);
  1572.     }
  1573.  
  1574.     if (patch.ph_safeid) {
  1575.         strcpy(pathend, IDF);
  1576.         strcpy(pathend + STRLEN(IDF), "*");
  1577.                 if (_dos_findfirst(p, 0, &de)) {
  1578.             if ((d = ndirs) == 1000) {
  1579.                 fprintf(stderr, "Too many different directories.\n");
  1580.                 quit();
  1581.             }
  1582.             sprintf(pathend + STRLEN(IDF), "%03d", d);
  1583.                         if (_dos_creat(p, 0, &fd) != 0) {
  1584.                 direrr = h->h_err = H_NODIR;
  1585.                 return(NULL);
  1586.             }
  1587.                         _dos_close(fd);
  1588.             strcpy(pathend, "*.*");
  1589.                         if (_dos_findfirst(p, _A_SUBDIR | _A_SYSTEM | _A_HIDDEN, &de))
  1590.                 h->h_di = dadd(v, d);
  1591.             else
  1592.                 takedir(&de, h->h_di = dadd(v, d));
  1593.         }
  1594.                 else if ((d = atoi(de.name + STRLEN(IDF))) < ndirs)
  1595.             h->h_di = dirs[d];
  1596.         else {
  1597.                         strcpy(pathend, de.name);
  1598.             fprintf(stderr, "Strange dir-id file encountered: %s.\n", p);
  1599.             quit();
  1600.         }
  1601.         *pathend = '\0';
  1602.     }
  1603.     else {
  1604.         strcpy(pathend, "*.*");
  1605.                 firstfound = !_dos_findfirst(p, _A_SUBDIR | _A_SYSTEM | _A_HIDDEN, &de);
  1606.         *pathend = '\0';
  1607.         if (firstfound) {
  1608.             v = DRIVENO(&de);
  1609.             d = CLUSTNO(&de);
  1610.         }
  1611.         else {
  1612.             strcpy(pathend, "T.D");
  1613.             if (mkdir(p)) {
  1614.                 *pathend = '\0';
  1615.                 direrr = h->h_err = H_NODIR;
  1616.                 return(NULL);
  1617.             }
  1618.             strcpy(pathend, "*.*");
  1619.                         firstfound = !_dos_findfirst(p, _A_SUBDIR | _A_SYSTEM | _A_HIDDEN, &de);
  1620.             *pathend = '\0';
  1621.             v = DRIVENO(&de);
  1622.             d = CLUSTNO(&de);
  1623.             rmdir(p);
  1624.             if (!firstfound || d != 0) {
  1625.                 fprintf(stderr,
  1626.                     "Strange, %s does not seem to be a root dir.\n",
  1627.                     p);
  1628.                 quit();
  1629.             }
  1630.         }
  1631.  
  1632.         if ((di = dsearch(v, d)) == NULL)
  1633.             if (firstfound)
  1634.                 takedir(&de, h->h_di = dadd(v, d));
  1635.             else
  1636.                 h->h_di = dadd(v, d);
  1637.         else
  1638.             h->h_di = di;
  1639.     }
  1640.  
  1641.     return(h);
  1642. }
  1643.  
  1644.  
  1645. static void takedir(pff, di)
  1646.         struct find_t *pff;
  1647.     DIRINFO *di;
  1648. {
  1649.     int cnt, room, namlen, needdot;
  1650.     FILEINFO **fils, *f;
  1651.     char c, *p, *p1;
  1652.  
  1653.     room = INITROOM;
  1654.     di->di_fils = fils = (FILEINFO **)myalloc(room * sizeof(FILEINFO *));
  1655.     cnt = 0;
  1656.     do {
  1657.                 if (strnicmp(pff->name, IDF, STRLEN(IDF)) == 0)
  1658.             continue;
  1659.         if (cnt == room) {
  1660.             room *= 2;
  1661.             fils = (FILEINFO **)myalloc(room * sizeof(FILEINFO *));
  1662.             memcpy(fils, di->di_fils, cnt * sizeof(FILEINFO *));
  1663.             chgive(di->di_fils, cnt * sizeof(FILEINFO *));
  1664.             di->di_fils = fils;
  1665.             fils = di->di_fils + cnt;
  1666.         }
  1667.         needdot = 1;
  1668.                 for (p = pff->name, namlen = 0; (c = *p) != '\0'; p++, namlen++)
  1669.             if (c == '.')
  1670.                 needdot = 0;
  1671.         *fils = f = (FILEINFO *)challoc(sizeof(FILEINFO), 1);
  1672.         f->fi_name = p = (char *)challoc(namlen + needdot + 1, 0);
  1673.                 for (p1 = pff->name; (c = *p1) != '\0'; p1++)
  1674.             *(p++) = mylower(c);
  1675.         if (needdot)
  1676.             *(p++) = '.';
  1677.         *p = '\0';
  1678.                 f->fi_attrib = pff->attrib;
  1679.         f->fi_rep = NULL;
  1680.         cnt++;
  1681.         fils++;
  1682.         } while (_dos_findnext(pff) == 0);
  1683.     qsort(di->di_fils, cnt, sizeof(FILEINFO *), fcmp);
  1684.     di->di_nfils = cnt;
  1685. }
  1686.  
  1687. #else
  1688. /* checkdir, takedir for Un*x */
  1689.  
  1690. static HANDLE *checkdir(p, pathend, which)
  1691.     char *p, *pathend;
  1692.     int which;
  1693. {
  1694.     struct stat dstat;
  1695.     DIRID d;
  1696.     DEVID v;
  1697.     DIRINFO **newdirs, *di;
  1698.     int nfils;
  1699.     FILEINFO **fils;
  1700.     char *myp, *lastslash = NULL;
  1701.     int sticky;
  1702.     HANDLE *h;
  1703.  
  1704.     if (hsearch(p, which, &h))
  1705.         if (h->h_di == NULL) {
  1706.             direrr = h->h_err;
  1707.             return(NULL);
  1708.         }
  1709.         else
  1710.             return(h);
  1711.  
  1712.     if (*p == '\0')
  1713.         myp = ".";
  1714.     else if (pathend == p + 1)
  1715.         myp = SLASHSTR;
  1716.     else {
  1717.         lastslash = pathend - 1;
  1718.         *lastslash = '\0';
  1719.         myp = p;
  1720.     }
  1721.  
  1722.     if (stat(myp, &dstat) || (dstat.st_mode & S_IFMT) != S_IFDIR)
  1723.         direrr = h->h_err = H_NODIR;
  1724.     else if (access(myp, R_OK | X_OK))
  1725.         direrr = h->h_err = H_NOREADDIR;
  1726.     else {
  1727.         direrr = 0;
  1728.         sticky = (dstat.st_mode & S_ISVTX) && uid != 0 && uid != dstat.st_uid ?
  1729.             FI_INSTICKY : 0;
  1730.         v = dstat.st_dev;
  1731.         d = dstat.st_ino;
  1732.  
  1733.         if ((di = dsearch(v, d)) == NULL)
  1734.             takedir(myp, di = dadd(v, d), sticky);
  1735.     }
  1736.  
  1737.     if (lastslash != NULL)
  1738.         *lastslash = SLASH;
  1739.     if (direrr != 0)
  1740.         return(NULL);
  1741.     h->h_di = di;
  1742.     return(h);
  1743. }
  1744.  
  1745.  
  1746. static void takedir(p, di, sticky)
  1747.     char *p;
  1748.     DIRINFO *di;
  1749.     int sticky;
  1750. {
  1751.     int cnt, room;
  1752.     DIRENTRY *dp;
  1753.     FILEINFO *f, **fils;
  1754.     DIR *dirp;
  1755.  
  1756.     if ((dirp = opendir(p)) == NULL) {
  1757.         fprintf(stderr, "Strange, can't scan %s.\n", p);
  1758.         quit();
  1759.     }
  1760.     room = INITROOM;
  1761.     di->di_fils = fils = (FILEINFO **)myalloc(room * sizeof(FILEINFO *));
  1762.     cnt = 0;
  1763.     while ((dp = readdir(dirp)) != NULL) {
  1764.         if (cnt == room) {
  1765.             room *= 2;
  1766.             fils = (FILEINFO **)myalloc(room * sizeof(FILEINFO *));
  1767.             memcpy(fils, di->di_fils, cnt * sizeof(FILEINFO *));
  1768.             chgive(di->di_fils, cnt * sizeof(FILEINFO *));
  1769.             di->di_fils = fils;
  1770.             fils = di->di_fils + cnt;
  1771.         }
  1772.         *fils = f = (FILEINFO *)challoc(sizeof(FILEINFO), 1);
  1773.         f->fi_name = mydup(dp->d_name);
  1774.         f->fi_stflags = sticky;
  1775.         f->fi_rep = NULL;
  1776.         cnt++;
  1777.         fils++;
  1778.     }
  1779.     closedir(dirp);
  1780.     qsort(di->di_fils, cnt, sizeof(FILEINFO *), fcmp);
  1781.     di->di_nfils = cnt;
  1782. }
  1783.  
  1784. /* end of Un*x checkdir, takedir; back to general program */
  1785. #endif
  1786.  
  1787.  
  1788. static int fcmp(pf1, pf2)
  1789.     FILEINFO **pf1, **pf2;
  1790. {
  1791.         return(strcmp((*pf1)->fi_name, (*pf2)->fi_name));
  1792. }
  1793.  
  1794.  
  1795. static HANDLE *hadd(n)
  1796.     char *n;
  1797. {
  1798.     HANDLE **newhandles, *h;
  1799.  
  1800.     if (nhandles == handleroom) {
  1801.         handleroom *= 2;
  1802.         newhandles = (HANDLE **)myalloc(handleroom * sizeof(HANDLE *));
  1803.         memcpy(newhandles, handles, nhandles * sizeof(HANDLE *));
  1804.         chgive(handles, nhandles * sizeof(HANDLE *));
  1805.         handles = newhandles;
  1806.     }
  1807.     handles[nhandles++] = h = (HANDLE *)challoc(sizeof(HANDLE), 1);
  1808.     h->h_name = (char *)challoc(strlen(n) + 1, 0);
  1809.     strcpy(h->h_name, n);
  1810.     h->h_di = NULL;
  1811.     return(h);
  1812. }
  1813.  
  1814.  
  1815. static int hsearch(n, which, pret)
  1816.     char *n;
  1817.     int which;
  1818.     HANDLE **pret;
  1819. {
  1820.     int i;
  1821.     HANDLE **ph;
  1822.  
  1823.     if (strcmp(n, lasthandle[which]->h_name) == 0) {
  1824.         *pret = lasthandle[which];
  1825.         return(1);
  1826.     }
  1827.  
  1828.     for(i = 0, ph = handles; i < nhandles; i++, ph++)
  1829.         if (strcmp(n, (*ph)->h_name) == 0) {
  1830.             lasthandle[which] = *pret = *ph;
  1831.             return(1);
  1832.         }
  1833.  
  1834.     lasthandle[which] = *pret = hadd(n);
  1835.     return(0);
  1836. }
  1837.  
  1838.  
  1839. static DIRINFO *dadd(v, d)
  1840.     DEVID v;
  1841.     DIRID d;
  1842. {
  1843.     DIRINFO *di;
  1844.     DIRINFO **newdirs;
  1845.  
  1846.     if (ndirs == dirroom) {
  1847.         dirroom *= 2;
  1848.         newdirs = (DIRINFO **)myalloc(dirroom * sizeof(DIRINFO *));
  1849.         memcpy(newdirs, dirs, ndirs * sizeof(DIRINFO *));
  1850.         chgive(dirs, ndirs * sizeof(DIRINFO *));
  1851.         dirs = newdirs;
  1852.     }
  1853.     dirs[ndirs++] = di = (DIRINFO *)challoc(sizeof(DIRINFO), 1);
  1854.     di->di_vid = v;
  1855.     di->di_did = d;
  1856.     di->di_nfils = 0;
  1857.     di->di_fils = NULL;
  1858.     di->di_flags = 0;
  1859.     return(di);
  1860. }
  1861.  
  1862.  
  1863. static DIRINFO *dsearch(v, d)
  1864.     DEVID v;
  1865.     DIRID d;
  1866. {
  1867.     int i;
  1868.     DIRINFO *di;
  1869.  
  1870.     for(i = 0, di = *dirs; i < ndirs; i++, di++)
  1871.         if (v == di->di_vid && d == di->di_did)
  1872.             return(di);
  1873.     return(NULL);
  1874. }
  1875.  
  1876.  
  1877. static int match(pat, s, start1, len1)
  1878.     char *pat, *s, **start1;
  1879.     int *len1;
  1880. {
  1881.     char c, *olds;
  1882.  
  1883.     *start1 = 0;
  1884.     for(;;)
  1885.         switch (c = *pat) {
  1886.         case '\0':
  1887.         case SLASH:
  1888.             return(*s == '\0');
  1889. #ifdef MSDOS
  1890.         case '!':
  1891.             *start1 = olds = s;
  1892.             if ((s = strchr(s, '.')) == NULL)
  1893.                 return(0);
  1894.             s++;
  1895.             *len1 = s - olds;
  1896.             if ((c = *(++pat)) == '\0') {
  1897.                 *len1 += strlen(s);
  1898.                 return(1);
  1899.             }
  1900.             for ( ; !match(pat, s, start1 + 1, len1 + 1); (*len1)++, s++)
  1901.                 if (*s == '\0')
  1902.                     return(0);
  1903.             return(1);
  1904. #endif
  1905.         case '*':
  1906.             *start1 = s;
  1907.             if ((c = *(++pat)) == '\0') {
  1908.                 *len1 = strlen(s);
  1909.                 return(1);
  1910.             }
  1911.             else {
  1912.                 for (*len1=0; !match(pat, s, start1+1, len1+1); (*len1)++, s++)
  1913.                     if (
  1914. #ifdef MSDOS
  1915.                         *s == '.' ||
  1916. #endif
  1917.                         *s == '\0'
  1918.                     )
  1919.                         return(0);
  1920.                 return(1);
  1921.             }
  1922.         case '?':
  1923.             if (
  1924. #ifdef MSDOS
  1925.                 *s == '.' ||
  1926. #endif
  1927.                 *s == '\0'
  1928.             )
  1929.                 return(0);
  1930.             *(start1++) = s;
  1931.             *(len1++) = 1;
  1932.             pat++;
  1933.             s++;
  1934.             break;
  1935.         case '[':
  1936.             {
  1937.                 int matched = 0, notin = 0, inrange = 0;
  1938.                 char prevc = '\0';
  1939.  
  1940.                 if ((c = *(++pat)) == '^') {
  1941.                     notin = 1;
  1942.                     c = *(++pat);
  1943.                 }
  1944.                 while (c != ']') {
  1945.                     if (c == '-' && !inrange)
  1946.                         inrange = 1;
  1947.                     else {
  1948.                         if (c == ESC) {
  1949.                             c = *(++pat);
  1950.                         }
  1951.                         if (inrange) {
  1952.                             if (*s >= prevc && *s <= c)
  1953.                                 matched = 1;
  1954.                             inrange = 0;
  1955.                         }
  1956.                         else if (c == *s)
  1957.                             matched = 1;
  1958.                         prevc = c;
  1959.                     }
  1960.                     c = *(++pat);
  1961.                 }
  1962.                 if (inrange && *s >= prevc)
  1963.                     matched = 1;
  1964.                 if (!(matched ^ notin))
  1965.                     return(0);
  1966.                 *(start1++) = s;
  1967.                 *(len1++) = 1;
  1968.                 pat++;
  1969.                 s++;
  1970.             }
  1971.             break;
  1972.         case ESC:
  1973.             c = *(++pat);
  1974.         default:
  1975.             if (c == *s) {
  1976.                  pat++;
  1977.                 s++;
  1978.             }
  1979.             else
  1980.                 return(0);
  1981.         }
  1982. }
  1983.  
  1984.  
  1985. static void makerep()
  1986. {
  1987.     int l, x;
  1988. #ifndef MSDOS
  1989.     int i, cnv;
  1990.     char *q;
  1991. #endif
  1992.     char *p, *pat, c, pc;
  1993.  
  1994.     repbad = 0;
  1995.     p = fullrep;
  1996.     for (pat = to, l = 0; (c = *pat) != '\0'; pat++, l++) {
  1997.         if (c == '=') {
  1998.             c = *(++pat);
  1999. #ifndef MSDOS
  2000.             if (c == 'l') {
  2001.                 cnv = LOWER;
  2002.                 c = *(++pat);
  2003.             }
  2004.             else if (c == 'u') {
  2005.                 cnv = UPPER;
  2006.                 c = *(++pat);
  2007.             }
  2008.             else
  2009.                 cnv = STAY;
  2010. #endif
  2011.             for(x = 0; ;x *= 10) {
  2012.                 x += c - '0';
  2013.                 c = *(pat+1);
  2014.                 if (!isdigit(c))
  2015.                     break;
  2016.                 pat++;
  2017.             }
  2018.             --x;
  2019.             if (l + len[x] >= MAXPATH)
  2020.                 goto toolong;
  2021. #ifdef MSDOS
  2022.             if (
  2023.                 *(start[x]) == '.' &&
  2024.                 (
  2025.                     p == fullrep ||
  2026.                     *(p - 1) == SLASH
  2027.                 )
  2028.             ) {
  2029.                 repbad = 1;
  2030.                 if (l + STRLEN(EMPTY) >= MAXPATH)
  2031.                     goto toolong;
  2032.                 strcpy(p, EMPTY);
  2033.                 p += STRLEN(EMPTY);
  2034.                 l += STRLEN(EMPTY);
  2035.             }
  2036. #else
  2037.             switch (cnv) {
  2038.             case STAY:
  2039. #endif
  2040.                 memmove(p, start[x], len[x]);
  2041.                 p += len[x];
  2042. #ifndef MSDOS
  2043.                 break;
  2044.             case LOWER:
  2045.                 for (i = len[x], q = start[x]; i > 0; i--, p++, q++)
  2046.                     *p = mylower(*q);
  2047.                 break;
  2048.             case UPPER:
  2049.                 for (i = len[x], q = start[x]; i > 0; i--, p++, q++)
  2050.                     *p = myupper(*q);
  2051.             }
  2052. #endif
  2053.         }
  2054.         else {
  2055.             if (c == ESC)
  2056.                 c = *(++pat);
  2057.             if (l == MAXPATH)
  2058.                 goto toolong;
  2059.             if (
  2060.                 (
  2061. #ifdef MSDOS
  2062.                     c == '.' ||
  2063. #endif
  2064.                     c == SLASH
  2065.                 ) &&
  2066.                 (
  2067.                     p == fullrep ? pat != to :
  2068.                     (
  2069.                         (
  2070.                             (pc = *(p - 1)) == SLASH
  2071. #ifdef MSDOS
  2072.                             || pc == ':'
  2073. #endif
  2074.                         ) &&
  2075.                          *(pat - 1) != pc
  2076.                     )
  2077.                 )
  2078.             ) {
  2079.                 repbad = 1;
  2080.                 if (l + STRLEN(EMPTY) >= MAXPATH)
  2081.                     goto toolong;
  2082.                 strcpy(p, EMPTY);
  2083.                 p += STRLEN(EMPTY);
  2084.                 l += STRLEN(EMPTY);
  2085.             }
  2086.             *(p++)= c;
  2087.         }
  2088.     }
  2089.     if (p == fullrep) {
  2090.         strcpy(fullrep, EMPTY);
  2091.         repbad = 1;
  2092.     }
  2093.     *(p++) = '\0';
  2094.     return;
  2095.  
  2096. toolong:
  2097.     repbad = 1;
  2098.     strcpy(fullrep, TOOLONG);
  2099. }
  2100.  
  2101.  
  2102. static void checkcollisions()
  2103. {
  2104.     REPDICT *rd, *prd;
  2105.     REP *p, *q;
  2106.     int i, mult, oldnreps;
  2107.  
  2108.     if (nreps == 0)
  2109.         return;
  2110.     rd = (REPDICT *)myalloc(nreps * sizeof(REPDICT));
  2111.     for (
  2112.         q = &hrep, p = q->r_next, prd = rd, i = 0;
  2113.         p != NULL;
  2114.         q = p, p = p->r_next, prd++, i++
  2115.     ) {
  2116.         prd->rd_p = p;
  2117.         prd->rd_dto = p->r_hto->h_di;
  2118.         prd->rd_nto = p->r_nto;
  2119.         prd->rd_i = i;
  2120.     }
  2121.     qsort(rd, nreps, sizeof(REPDICT), rdcmp);
  2122.     mult = 0;
  2123.     for (i = 0, prd = rd, oldnreps = nreps; i < oldnreps; i++, prd++)
  2124.         if (
  2125.             i < oldnreps - 1 &&
  2126.             prd->rd_dto == (prd + 1)->rd_dto &&
  2127.             strcmp(prd->rd_nto, (prd + 1)->rd_nto) == 0
  2128.         ) {
  2129.             if (!mult)
  2130.                 mult = 1;
  2131.             else
  2132.                 printf(" , ");
  2133.             printf("%s%s", prd->rd_p->r_hfrom->h_name,
  2134.                 prd->rd_p->r_ffrom->fi_name);
  2135.             prd->rd_p->r_flags |= R_SKIP;
  2136.             prd->rd_p->r_ffrom->fi_rep = MISTAKE;
  2137.             nreps--;
  2138.             badreps++;
  2139.         }
  2140.         else if (mult) {
  2141.             prd->rd_p->r_flags |= R_SKIP;
  2142.             prd->rd_p->r_ffrom->fi_rep = MISTAKE;
  2143.             nreps--;
  2144.             badreps++;
  2145.             printf(" , %s%s -> %s%s : collision.\n",
  2146.                 prd->rd_p->r_hfrom->h_name, prd->rd_p->r_ffrom->fi_name,
  2147.                 prd->rd_p->r_hto->h_name, prd->rd_nto);
  2148.             mult = 0;
  2149.         }
  2150.     chgive(rd, oldnreps * sizeof(REPDICT));
  2151. }
  2152.  
  2153.  
  2154. static int rdcmp(rd1, rd2)
  2155.     REPDICT *rd1, *rd2;
  2156. {
  2157.     int ret;
  2158.  
  2159.     if (
  2160.         (ret = rd1->rd_dto - rd2->rd_dto) == 0 &&
  2161.         (ret = strcmp(rd1->rd_nto, rd2->rd_nto)) == 0
  2162.     )
  2163.         ret = rd1->rd_i - rd2->rd_i;
  2164.     return(ret);
  2165. }
  2166.  
  2167.  
  2168. static void findorder()
  2169. {
  2170.     REP *p, *q, *t, *first, *pred;
  2171.     FILEINFO *fi;
  2172.  
  2173.     for (q = &hrep, p = q->r_next; p != NULL; q = p, p = p->r_next)
  2174.         if (p->r_flags & R_SKIP) {
  2175.             q->r_next = p->r_next;
  2176.             p = q;
  2177.         }
  2178.         else if (
  2179.             (fi = p->r_fdel) == NULL ||
  2180.             (pred = fi->fi_rep) == NULL ||
  2181.             pred == MISTAKE
  2182.         )
  2183.             continue;
  2184.         else if ((first = pred->r_first) == p) {
  2185.             p->r_flags |= R_ISCYCLE;
  2186.             pred->r_flags |= R_ISALIASED;
  2187.             if (op & MOVE)
  2188.                 p->r_fdel = NULL;
  2189.         }
  2190.         else {
  2191.             if (op & MOVE)
  2192.                 p->r_fdel = NULL;
  2193.             while (pred->r_thendo != NULL)
  2194.                 pred = pred->r_thendo;
  2195.             pred->r_thendo = p;
  2196.             for (t = p; t != NULL; t = t->r_thendo)
  2197.                 t->r_first = first;
  2198.             q->r_next = p->r_next;
  2199.             p = q;
  2200.         }
  2201. }
  2202.  
  2203.  
  2204. static void nochains()
  2205. {
  2206.     REP *p, *q;
  2207.  
  2208.     for (q = &hrep, p = q->r_next; p != NULL; q = p, p = p->r_next)
  2209.         if (p->r_flags & R_ISCYCLE || p->r_thendo != NULL) {
  2210.             printchain(p);
  2211.             printf("%s%s : no chain copies allowed.\n",
  2212.                 p->r_hto->h_name, p->r_nto);
  2213.             q->r_next = p->r_next;
  2214.             p = q;
  2215.         }
  2216. }
  2217.  
  2218.  
  2219. static void printchain(p)
  2220.     REP *p;
  2221. {
  2222.     if (p->r_thendo != NULL)
  2223.         printchain(p->r_thendo);
  2224.     printf("%s%s -> ", p->r_hfrom->h_name, p->r_ffrom->fi_name);
  2225.     badreps++;
  2226.     nreps--;
  2227.     p->r_ffrom->fi_rep = MISTAKE;
  2228. }
  2229.  
  2230.  
  2231. static void scandeletes(pkilldel)
  2232.     int (*pkilldel)();
  2233. {
  2234.     REP *p, *q, *n;
  2235.  
  2236.     for (q = &hrep, p = q->r_next; p != NULL; q = p, p = p->r_next) {
  2237.         if (p->r_fdel != NULL)
  2238.             while ((*pkilldel)(p)) {
  2239.                 nreps--;
  2240.                 p->r_ffrom->fi_rep = MISTAKE;
  2241.                 if ((n = p->r_thendo) != NULL) {
  2242.                     if (op & MOVE)
  2243.                         n->r_fdel = p->r_ffrom;
  2244.                     n->r_next = p->r_next;
  2245.                     q->r_next = p = n;
  2246.                 }
  2247.                 else {
  2248.                     q->r_next = p->r_next;
  2249.                     p = q;
  2250.                     break;
  2251.                 }
  2252.             }
  2253.     }
  2254. }
  2255.  
  2256.  
  2257. static int baddel(p)
  2258.     REP *p;
  2259. {
  2260.     HANDLE *hfrom = p->r_hfrom, *hto = p->r_hto;
  2261.     FILEINFO *fto = p->r_fdel;
  2262.     char *t = fto->fi_name, *f = p->r_ffrom->fi_name;
  2263.     char *hnf = hfrom->h_name, *hnt = hto->h_name;
  2264.  
  2265.     if (delstyle == NODEL && !(p->r_flags & R_DELOK) && !(op & APPEND))
  2266.         printf("%s%s -> %s%s : old %s%s would have to be %s.\n",
  2267.             hnf, f, hnt, t, hnt, t,
  2268.             (op & OVERWRITE) ? "overwritten" : "deleted");
  2269.     else if (fto->fi_rep == MISTAKE)
  2270.         printf("%s%s -> %s%s : old %s%s was to be done first.\n",
  2271.             hnf, f, hnt, t, hnt, t);
  2272.     else if (
  2273. #ifdef MSDOS
  2274.                 fto->fi_attrib & _A_SUBDIR
  2275. #else
  2276.         fto->fi_stflags & FI_ISDIR
  2277. #endif
  2278.     )
  2279.         printf("%s%s -> %s%s : %s%s%s is a directory.\n",
  2280.             hnf, f, hnt, t, (op & APPEND) ? "" : "old ", hnt, t);
  2281. #ifndef MSDOS
  2282.     else if ((fto->fi_stflags & FI_NODEL) && !(op & (APPEND | OVERWRITE)))
  2283.         printf("%s%s -> %s%s : old %s%s lacks delete permission.\n",
  2284.             hnf, f, hnt, t, hnt, t);
  2285. #endif
  2286.     else if (
  2287.         (op & (APPEND | OVERWRITE)) &&
  2288. #ifdef MSDOS
  2289.                 fto->fi_attrib & _A_RDONLY
  2290. #else
  2291.         !fwritable(hnt, fto)
  2292. #endif
  2293.     ) {
  2294.         printf("%s%s -> %s%s : %s%s %s.\n",
  2295.             hnf, f, hnt, t, hnt, t,
  2296. #ifndef MSDOS
  2297. #ifndef SYSV
  2298.             fto->fi_stflags & FI_LINKERR ?
  2299.             "is a badly aimed symbolic link" :
  2300. #endif
  2301. #endif
  2302.             "lacks write permission");
  2303.     }
  2304.     else
  2305.         return(0);
  2306.     badreps++;
  2307.     return(1);
  2308. }
  2309.  
  2310.  
  2311. static int skipdel(p)
  2312.     REP *p;
  2313. {
  2314.     if (p->r_flags & R_DELOK)
  2315.         return(0);
  2316.     fprintf(stderr, "%s%s -> %s%s : ",
  2317.         p->r_hfrom->h_name, p->r_ffrom->fi_name,
  2318.         p->r_hto->h_name, p->r_nto);
  2319.     if (
  2320. #ifdef MSDOS
  2321.                 p->r_fdel->fi_attrib & _A_RDONLY
  2322. #else
  2323. #ifndef SYSV
  2324.         !(p->r_ffrom->fi_stflags & FI_ISLNK) &&
  2325. #endif
  2326.         !fwritable(p->r_hto->h_name, p->r_fdel)
  2327. #endif
  2328.     )
  2329.         fprintf(stderr, "old %s%s lacks write permission. delete it",
  2330.             p->r_hto->h_name, p->r_nto);
  2331.     else
  2332.         fprintf(stderr, "%s old %s%s",
  2333.             (op & OVERWRITE) ? "overwrite" : "delete",
  2334.             p->r_hto->h_name, p->r_nto);
  2335.     return(!getreply("? ", -1));
  2336. }
  2337.  
  2338.  
  2339. static void goonordie()
  2340. {
  2341.     if ((paterr || badreps) && nreps > 0) {
  2342.         fprintf(stderr, "Not everything specified can be done.");
  2343.         if (badstyle == ABORTBAD) {
  2344.             fprintf(stderr, " Aborting.\n");
  2345.             exit(1);
  2346.         }
  2347.         else if (badstyle == SKIPBAD)
  2348.             fprintf(stderr, " Proceeding with the rest.\n");
  2349.         else if (!getreply(" Proceed with the rest? ", -1))
  2350.             exit(1);
  2351.     }
  2352. }
  2353.  
  2354.  
  2355. static void doreps()
  2356. {
  2357.     char *fstart;
  2358.     int k, printaliased = 0, alias;
  2359.     REP *first, *p;
  2360.     long aliaslen;
  2361.  
  2362.     signal(SIGINT, breakrep);
  2363.  
  2364.     for (first = hrep.r_next, k = 0; first != NULL; first = first->r_next) {
  2365.         for (p = first; p != NULL; p = p->r_thendo, k++) {
  2366.             if (gotsig) {
  2367.                 fflush(stdout);
  2368.                 fprintf(stderr, "User break.\n");
  2369.                 printaliased = snap(first, p);
  2370.                 gotsig = 0;
  2371.             }
  2372.             strcpy(fullrep, p->r_hto->h_name);
  2373.             strcat(fullrep, p->r_nto);
  2374.             if (!noex && (p->r_flags & R_ISCYCLE))
  2375.                 if (op & APPEND)
  2376.                     aliaslen = appendalias(first, p, &printaliased);
  2377.                 else
  2378.                     alias = movealias(first, p, &printaliased);
  2379.             strcpy(pathbuf, p->r_hfrom->h_name);
  2380.             fstart = pathbuf + strlen(pathbuf);
  2381.             if ((p->r_flags & R_ISALIASED) && !(op & APPEND))
  2382.                 sprintf(fstart, "%s%03d", TEMP, alias);
  2383.             else
  2384.                 strcpy(fstart, p->r_ffrom->fi_name);
  2385.             if (!noex) {
  2386.                 if (p->r_fdel != NULL && !(op & (APPEND | OVERWRITE)))
  2387.                     myunlink(fullrep, p->r_fdel);
  2388.                 if (
  2389.                     (op & (COPY | APPEND)) ?
  2390.                         copy(p->r_ffrom,
  2391.                             p->r_flags & R_ISALIASED ? aliaslen : -1L) :
  2392. #ifndef MSDOS
  2393.                     (op & HARDLINK) ?
  2394.                         link(pathbuf, fullrep) :
  2395. #ifndef SYSV
  2396.                     (op & SYMLINK) ?
  2397.                         symlink((p->r_flags & R_ONEDIRLINK) ? fstart : pathbuf,
  2398.                             fullrep) :
  2399. #endif
  2400. #endif
  2401.                     p->r_flags & R_ISX ?
  2402.                         copymove(p) :
  2403.                     /* move */
  2404.                         rename(pathbuf, fullrep)
  2405.                 ) {
  2406.                     fprintf(stderr,
  2407.                         "%s -> %s has failed.\n", pathbuf, fullrep);
  2408.                     printaliased = snap(first, p);
  2409.                 }
  2410.             }
  2411.             if (verbose || noex) {
  2412.                 if (p->r_flags & R_ISALIASED && !printaliased)
  2413.                     strcpy(fstart, p->r_ffrom->fi_name);
  2414.                 fprintf(outfile, "%s %c%c %s%s%s\n",
  2415.                     pathbuf,
  2416.                     p->r_flags & R_ISALIASED ? '=' : '-',
  2417.                     p->r_flags & R_ISCYCLE ? '^' : '>',
  2418.                     fullrep,
  2419.                     (p->r_fdel != NULL && !(op & APPEND)) ? " (*)" : "",
  2420.                     noex ? "" : " : done");
  2421.             }
  2422.         }
  2423.         printaliased = 0;
  2424.     }
  2425.     if (k != nreps)
  2426.         fprintf(stderr, "Strange, did %d reps; %d were expected.\n",
  2427.             k, nreps);
  2428.     if (k == 0)
  2429.         fprintf(stderr, "Nothing done.\n");
  2430. }
  2431.  
  2432.  
  2433. static long appendalias(first, p, pprintaliased)
  2434.     REP *first, *p;
  2435.     int *pprintaliased;
  2436. {
  2437.     long ret;
  2438.  
  2439. #ifdef MSDOS
  2440.     int fd;
  2441.  
  2442.     if ((fd = open(fullrep, O_RDONLY | O_BINARY, 0)) < 0) {
  2443.         fprintf(stderr, "stat on %s has failed.\n", fullrep);
  2444.         *pprintaliased = snap(first, p);
  2445.     }
  2446.     else {
  2447.         ret = filelength(fd);
  2448.         close(fd);
  2449.     }
  2450. #else
  2451.     struct stat fstat;
  2452.  
  2453.     if (stat(fullrep, &fstat)) {
  2454.         fprintf(stderr, "append cycle stat on %s has failed.\n", fullrep);
  2455.         *pprintaliased = snap(first, p);
  2456.     }
  2457.     else
  2458.         ret = fstat.st_size;
  2459. #endif
  2460.  
  2461.     return(ret);
  2462. }
  2463.  
  2464.  
  2465. static int movealias(first, p, pprintaliased)
  2466.     REP *first, *p;
  2467.     int *pprintaliased;
  2468. {
  2469.     char *fstart;
  2470.     int ret;
  2471.  
  2472.     strcpy(pathbuf, p->r_hto->h_name);
  2473.     fstart = pathbuf + strlen(pathbuf);
  2474.     strcpy(fstart, TEMP);
  2475.     for (
  2476.         ret = 0;
  2477.         sprintf(fstart + STRLEN(TEMP), "%03d", ret),
  2478.         fsearch(fstart, p->r_hto->h_di) != NULL;
  2479.         ret++
  2480.     )
  2481.         ;
  2482.     if (rename(fullrep, pathbuf)) {
  2483.         fprintf(stderr,
  2484.             "%s -> %s has failed.\n", fullrep, pathbuf);
  2485.         *pprintaliased = snap(first, p);
  2486.     }
  2487.     return(ret);
  2488. }
  2489.  
  2490.  
  2491. static int snap(first, p)
  2492.     REP *first, *p;
  2493. {
  2494.     char fname[80];
  2495.     int redirected = 0;
  2496.  
  2497.     if (noex)
  2498.         exit(1);
  2499.  
  2500.     failed = 1;
  2501.     signal(SIGINT, breakstat);
  2502.     if (
  2503.         badstyle == ASKBAD &&
  2504.         isatty(fileno(stdout)) &&
  2505.         getreply("Redirect standard output to file? ", 0)
  2506.     ) {
  2507.         redirected = 1;
  2508. #ifndef MSDOS
  2509.         umask(oldumask);
  2510. #endif
  2511.         while (
  2512.             fprintf(stderr, "File name> "),
  2513.             (outfile = fopen(mygets(fname, 80), "w")) == NULL
  2514.         )
  2515.             fprintf(stderr, "Can't open %s.\n", fname);
  2516.     }
  2517.     if (redirected || !verbose)
  2518.         showdone(p);
  2519.     fprintf(outfile, "The following left undone:\n");
  2520.     noex = 1;
  2521.     return(first != p);
  2522. }
  2523.  
  2524.  
  2525. static void showdone(fin)
  2526.     REP *fin;
  2527. {
  2528.     REP *first, *p;
  2529.  
  2530.     for (first = hrep.r_next; ; first = first->r_next)
  2531.         for (p = first; p != NULL; p = p->r_thendo) {
  2532.             if (p == fin)
  2533.                 return;
  2534.             fprintf(outfile, "%s%s %c%c %s%s : done%s\n",
  2535.                 p->r_hfrom->h_name, p->r_ffrom->fi_name,
  2536.                 p->r_flags & R_ISALIASED ? '=' : '-',
  2537.                 p->r_flags & R_ISCYCLE ? '^' : '>',
  2538.                 p->r_hto->h_name, p->r_nto,
  2539.                 (p->r_fdel != NULL && !(op & APPEND)) ? " (*)" : "");
  2540.         }
  2541. }
  2542.  
  2543.  
  2544. static void breakout()
  2545. {
  2546.     fflush(stdout);
  2547.     fprintf(stderr, "Aborting, nothing done.\n");
  2548.     exit(1);
  2549. }
  2550.  
  2551.  
  2552. static int breakrep()
  2553. {
  2554.     gotsig = 1;
  2555.     return(1);
  2556. }
  2557.  
  2558.  
  2559. static void breakstat()
  2560. {
  2561.     exit(1);
  2562. }
  2563.  
  2564.  
  2565. static void quit()
  2566. {
  2567.     fprintf(stderr, "Aborting, nothing done.\n");
  2568.     exit(1);
  2569. }
  2570.  
  2571.  
  2572. static int copymove(p)
  2573.     REP *p;
  2574. {
  2575. #ifndef MSDOS
  2576. #ifndef SYSV
  2577.     {
  2578.         int llen;
  2579.         char linkbuf[MAXPATH];
  2580.  
  2581.         if ((llen = readlink(pathbuf, linkbuf, MAXPATH - 1)) >= 0) {
  2582.             linkbuf[llen] = '\0';
  2583.             return(symlink(linkbuf, fullrep) || myunlink(pathbuf, p->r_ffrom));
  2584.         }
  2585.     }
  2586. #endif
  2587. #endif
  2588.     return(copy(p->r_ffrom, -1L) || myunlink(pathbuf, p->r_ffrom));
  2589. }
  2590.  
  2591.  
  2592.  
  2593. #define IRWMASK (S_IREAD | S_IWRITE)
  2594. #define RWMASK (IRWMASK | (IRWMASK >> 3) | (IRWMASK >> 6))
  2595.  
  2596. static int copy(ff, len)
  2597.     FILEINFO *ff;
  2598.     long len;
  2599. {
  2600.     char buf[BUFSIZE], c;
  2601.     int f, t, k, mode, perm;
  2602. #ifdef MSDOS
  2603.         unsigned time, date;
  2604. #else
  2605. #ifdef SYSV
  2606.     struct utimbuf tim;
  2607. #else
  2608.     struct timeval tim[2];
  2609. #endif
  2610.     struct stat fstat;
  2611. #endif
  2612.  
  2613.     if ((f = open(pathbuf, O_RDONLY | O_BINARY, 0)) < 0)
  2614.         return(-1);
  2615.     perm =
  2616. #ifdef MSDOS
  2617.         IRWMASK        /* will _chmod it later (to get all the attributes) */
  2618. #else
  2619.         (op & (APPEND | OVERWRITE)) ?
  2620.             (~oldumask & RWMASK) | (ff->fi_mode & ~RWMASK) :
  2621.             ff->fi_mode
  2622. #endif
  2623.         ;
  2624.  
  2625. #ifdef V7
  2626.     if (
  2627.         !(op & APPEND) ||
  2628.         (((t = open(fullrep, O_RDWR)) < 0 && errno == ENOENT)
  2629.     )
  2630.         t = creat(fullrep, perm);
  2631. #else
  2632.     mode = O_CREAT | (op & APPEND ? 0 : O_TRUNC) |
  2633. #ifdef MSDOS
  2634.         O_BINARY | (op & ZAPPEND ? O_RDWR : O_WRONLY)
  2635. #else
  2636.         O_WRONLY
  2637. #endif
  2638.         ;
  2639.     t = open(fullrep, mode, perm);
  2640. #endif
  2641.     if (t < 0) {
  2642.         close(f);
  2643.         return(-1);
  2644.     }
  2645.     if (op & APPEND)
  2646.         lseek(t, 0L, 2);
  2647. #ifdef MSDOS
  2648.     if (op & ZAPPEND && filelength(t) != 0) {
  2649.         if (lseek(t, -1L, 1) == -1L || read(t, &c, 1) != 1) {
  2650.             close(f);
  2651.             close(t);
  2652.             return(-1);
  2653.         }
  2654.         if (c == 26)
  2655.             lseek(t, -1L, 1);
  2656.     }
  2657. #endif
  2658.     if ((op & APPEND) && len != -1L) {
  2659.         while (
  2660.             len != 0 &&
  2661.             (k = read(f, buf, len > BUFSIZE ? BUFSIZE : (unsigned)len)) > 0 &&
  2662.             write(t, buf, k) == k
  2663.         )
  2664.             len -= k;
  2665.         if (len == 0)
  2666.             k = 0;
  2667.     }
  2668.     else
  2669.         while ((k = read(f, buf, BUFSIZE)) > 0 && write(t, buf, k) == k)
  2670.             ;
  2671.     if (!(op & (APPEND | OVERWRITE)))
  2672.         if (
  2673. #ifdef MSDOS
  2674.                         _dos_getftime(f, &date, &time) ||
  2675.                         _dos_setftime(t, date, time) ||
  2676.                         _dos_setfileattr(fullrep, ff->fi_attrib)
  2677. #else
  2678.             stat(pathbuf, &fstat) ||
  2679.             (
  2680. #ifdef SYSV
  2681.                 tim.actime = fstat.st_atime,
  2682.                 tim.modtime = fstat.st_mtime,
  2683. #else
  2684.                 tim[0].tv_sec = fstat.st_atime,
  2685.                 tim[1].tv_sec = fstat.st_mtime,
  2686. #endif
  2687.                 utimes(fullrep, tim)
  2688.             )
  2689. #endif
  2690.         )
  2691.             fprintf(stderr, "Strange, couldn't transfer time from %s to %s.\n",
  2692.                 pathbuf, fullrep);
  2693.  
  2694.     close(f);
  2695.     close(t);
  2696.     if (k != 0) {
  2697.         if (!(op & APPEND))
  2698.             unlink(fullrep);
  2699.         return(-1);
  2700.     }
  2701.     return(0);
  2702. }
  2703.  
  2704.  
  2705. #ifndef RENAME
  2706. static int rename(from, to)
  2707.     char *from, *to;
  2708. {
  2709.     if (link(from, to))
  2710.         return(-1);
  2711.     if (unlink(from)) {
  2712.         unlink(to);
  2713.         return(-1);
  2714.     }
  2715.     return(0);
  2716. }
  2717. #endif
  2718.  
  2719.  
  2720. static int myunlink(n, f)
  2721.     char *n;
  2722.     FILEINFO *f;
  2723. {
  2724. #ifdef MSDOS
  2725.     int a;
  2726.  
  2727.         if (((a = f->fi_attrib) & _A_RDONLY) && _dos_setfileattr(n, a & ~_A_RDONLY)) {
  2728.         fprintf(stderr, "Strange, can not _chmod (or unlink) %s.\n", f);
  2729.         return(-1);
  2730.     }
  2731. #endif
  2732.     if (unlink(n)) {
  2733.         fprintf(stderr, "Strange, can not unlink %s.\n", n);
  2734.         return(-1);
  2735.     }
  2736.     return(0);
  2737. }
  2738.  
  2739.  
  2740. static int getreply(m, failact)
  2741.     char *m;
  2742.     int failact;
  2743. {
  2744.     static FILE *tty = NULL;
  2745.     int c, r;
  2746.  
  2747.     fprintf(stderr, m);
  2748.     if (tty == NULL && (tty = fopen(TTY, "r")) == NULL) {
  2749.         fprintf(stderr, "Can not open %s to get reply.\n", TTY);
  2750.         if (failact == -1)
  2751.             quit();
  2752.         else
  2753.             return(failact);
  2754.     }
  2755.     for (;;) {
  2756.         r = fgetc(tty);
  2757.         if (r == EOF) {
  2758.             fprintf(stderr, "Can not get reply.\n");
  2759.             if (failact == -1)
  2760.                 quit();
  2761.             else
  2762.                 return(failact);
  2763.         }
  2764.         if (r != '\n')
  2765.             while ((c = fgetc(tty)) != '\n' && c != EOF)
  2766.                 ;
  2767.         r = mylower(r);
  2768.         if (r == 'y' || r == 'n')
  2769.             return(r == 'y');
  2770.         fprintf(stderr, "Yes or No? ");
  2771.     }
  2772. }
  2773.  
  2774.  
  2775. static void *myalloc(k)
  2776.     unsigned k;
  2777. {
  2778.     void *ret;
  2779.  
  2780.     if (k == 0)
  2781.         return(NULL);
  2782.     if ((ret = (void *)malloc(k)) == NULL) {
  2783.         fprintf(stderr, "Insufficient memory.\n");
  2784.         quit();
  2785.     }
  2786.     return(ret);
  2787. }
  2788.  
  2789.  
  2790. static void *challoc(k, which)
  2791.     int which;
  2792.     int k;
  2793. {
  2794.     void *ret;
  2795.     CHUNK *p, *q;
  2796.     SLICER *sl = &(slicer[which]);
  2797.  
  2798.     if (k > sl->sl_len) {
  2799.         for (
  2800.             q = NULL, p = freechunks;
  2801.             p != NULL && (sl->sl_len = p->ch_len) < k;
  2802.             q = p, p = p->ch_next
  2803.         )
  2804.             ;
  2805.         if (p == NULL) {
  2806.             sl->sl_len = CHUNKSIZE - sizeof(CHUNK *);
  2807.             p = (CHUNK *)myalloc(CHUNKSIZE);
  2808.         }
  2809.         else if (q == NULL)
  2810.             freechunks = p->ch_next;
  2811.         else
  2812.             q->ch_next = p->ch_next;
  2813.         p->ch_next = sl->sl_first;
  2814.         sl->sl_first = p;
  2815.         sl->sl_unused = (char *)&(p->ch_len);
  2816.     }
  2817.     sl->sl_len -= k;
  2818.     ret = (void *)sl->sl_unused;
  2819.     sl->sl_unused += k;
  2820.     return(ret);
  2821. }
  2822.  
  2823.  
  2824. static void chgive(p, k)
  2825.     void *p;
  2826.     unsigned k;
  2827. {
  2828.     ((CHUNK *)p)->ch_len = k - sizeof(CHUNK *);
  2829.     ((CHUNK *)p)->ch_next = freechunks;
  2830.     freechunks = (CHUNK *)p;
  2831. }
  2832.  
  2833.  
  2834. #ifndef MSDOS
  2835. static void memmove(to, from, k)
  2836.     char *to, *from;
  2837.     unsigned k;
  2838. {
  2839.     if (from > to)
  2840.         while (k-- != 0)
  2841.             *(to++) = *(from++);
  2842.     else {
  2843.         from += k;
  2844.         to += k;
  2845.         while (k-- != 0)
  2846.             *(--to) = *(--from);
  2847.     }
  2848. }
  2849. #endif
  2850.  
  2851.  
  2852. static int mygetc()
  2853. {
  2854.     static int lastc = 0;
  2855.  
  2856.     if (lastc == EOF)
  2857.         return(EOF);
  2858.     return(lastc = getchar());
  2859. }
  2860.  
  2861.  
  2862. static char *mygets(s, l)
  2863.     char *s;
  2864.     int l;
  2865. {
  2866.     char *nl;
  2867.  
  2868.     for (;;) {
  2869.         if (fgets(s, l, stdin) == NULL)
  2870.             return(NULL);
  2871.         if ((nl = strchr(s, '\n')) != NULL)
  2872.             break;
  2873.         fprintf(stderr, "Input string too long. Try again> ");
  2874.     }
  2875.     *nl = '\0';
  2876.     return(s);
  2877. }
  2878.  
  2879.  
  2880. #ifdef MSDOS
  2881. static int leave()
  2882. {
  2883.     return(0);
  2884. }
  2885.  
  2886. static void cleanup()
  2887. {
  2888.     int i;
  2889.  
  2890.     if (patch.ph_safeid) {
  2891.         for (i = 0; i < nhandles; i++) {
  2892.             if (!(handles[i]->h_di->di_flags & DI_CLEANED)) {
  2893.                 sprintf(pathbuf, "%s%s%03d",
  2894.                     handles[i]->h_name, IDF, handles[i]->h_di->di_did);
  2895.                 if (unlink(pathbuf))
  2896.                     fprintf(stderr, "Strange, couldn't unlink %s.\n", pathbuf);
  2897.                 handles[i]->h_di->di_flags |= DI_CLEANED;
  2898.             }
  2899.         }
  2900.     }
  2901. }
  2902.  
  2903. #endif
  2904.