home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / mmv.pch < prev    next >
Text File  |  1990-06-07  |  12KB  |  508 lines

  1. Subject:  v22i103:  CRITICAL patch to MMV
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: 30ca48d9 a276f24e 5f353026 1fcfba52
  5.  
  6. Submitted-by: Vladimir Lanin <lanin@csd4.cs.nyu.edu>
  7. Posting-number: Volume 22, Issue 103
  8. Archive-name: mmv.pch
  9. Patch-To: volume21/mmv
  10.  
  11. The following patch fixes a number of bugs in mmv 1.0 (as distributed on
  12. comp.sources.unix) and provides V7 compatibility.  The new version is
  13. known as mmv 1.01b.
  14.  
  15. Since one of the bugs can result in the unintended DELETION of the source
  16. file(s), it is imperative that the old version be replaced with the new
  17. one.
  18.  
  19. The two major bugs fixed are:
  20. 1) in its BSD reincarnation, whenever a file had to be moved between devices
  21. (by copying, then deleting), mmv 1.0 made a symbolic link to "/" instead
  22. of copying the file. It still deleted the original, though. Oops!
  23. 2) the uppercase/lowercase conversion feature did not work.
  24.  
  25. Vladimir Lanin
  26.  
  27. ---cut here---
  28. ***************
  29. *** 1,5 ****
  30.   /*
  31. !     mmv 1.0
  32.       Copyright (c) 1990 Vladimir Lanin.
  33.       This program may be freely used and copied on a non-commercial basis.
  34.       The author assumes no responsibility for any damage or data loss that may
  35. --- 1,5 ----
  36.   /*
  37. !     mmv 1.01b
  38.       Copyright (c) 1990 Vladimir Lanin.
  39.       This program may be freely used and copied on a non-commercial basis.
  40.       The author assumes no responsibility for any damage or data loss that may
  41. ***************
  42. *** 7,21 ****
  43.   
  44.       Author may be reached at:
  45.   
  46. !     lanin@csd4.nyu.edu
  47.   
  48.       Vladimir Lanin
  49.       330 Wadsworth Ave, Apt 6F,
  50.       New York, NY 10040
  51.   */
  52.   
  53.   /*
  54.       Define SYSV to compile under System V.
  55.       If your System V has a rename() call, define RENAME.
  56.       Otherwise, mmv will only be able to rename directories (via option -r)
  57.       when running as the super-user.
  58. --- 7,36 ----
  59.   
  60.       Author may be reached at:
  61.   
  62. !     lanin@csd4.cs.nyu.edu
  63.   
  64.       Vladimir Lanin
  65.       330 Wadsworth Ave, Apt 6F,
  66.       New York, NY 10040
  67. +     Many thanks to those who have to contributed to the design
  68. +     and/or coding of this program:
  69. +     Tom Albrecht:    initial Sys V adaptation, consultation, and testing
  70. +     Carl Mascott:    V7 adaptation
  71. +     Mark Lewis:    -n flag idea, consultation.
  72. +     Dave Bernhold:    upper/lowercase conversion idea.
  73. +     Paul Stodghill:    copy option, argv[0] checking.
  74. +     Frank Fiamingo:    consultation and testing.
  75. +     Tom Jordahl:    bug reports and testing.
  76. +     John Lukas, Hugh Redelmeyer, Barry Nelson, John Sauter,
  77. +     Phil Dench, John Nelson:
  78. +             bug reports.
  79.   */
  80.   
  81.   /*
  82.       Define SYSV to compile under System V.
  83. +     Define both SYSV and V7 to compile under V7.
  84.       If your System V has a rename() call, define RENAME.
  85.       Otherwise, mmv will only be able to rename directories (via option -r)
  86.       when running as the super-user.
  87. ***************
  88. *** 74,84 ****
  89.   
  90.   #include <stdio.h>
  91.   #include <ctype.h>
  92. - #include <string.h>
  93.   
  94.   #ifdef MSDOS
  95.   /* for MS-DOS (under Turbo C 1.5)*/
  96.   
  97.   #include <stdlib.h>
  98.   #include <sys/stat.h>
  99.   #include <dos.h>
  100. --- 89,99 ----
  101.   
  102.   #include <stdio.h>
  103.   #include <ctype.h>
  104.   
  105.   #ifdef MSDOS
  106.   /* for MS-DOS (under Turbo C 1.5)*/
  107.   
  108. + #include <string.h>
  109.   #include <stdlib.h>
  110.   #include <sys/stat.h>
  111.   #include <dos.h>
  112. ***************
  113. *** 109,116 ****
  114.   #include <sys/types.h>
  115.   #include <sys/stat.h>
  116.   #include <sys/file.h>
  117. ! #include <sys/signal.h>
  118. ! #include <fcntl.h>
  119.   extern char *getenv();
  120.   extern long lseek();
  121.   extern char *malloc();
  122. --- 124,130 ----
  123.   #include <sys/types.h>
  124.   #include <sys/stat.h>
  125.   #include <sys/file.h>
  126.   extern char *getenv();
  127.   extern long lseek();
  128.   extern char *malloc();
  129. ***************
  130. *** 149,176 ****
  131.   
  132.   static char TTY[] = "/dev/tty";
  133.   
  134. ! #ifdef SYSV
  135. ! /* for System V */
  136.   
  137.   struct utimbuf {
  138.       time_t actime;
  139.       time_t modtime;
  140.   };
  141. ! #define utimes(f, t) utime((f), (t))
  142.   
  143.   #else
  144.   /* for BSD */
  145.   #define RENAME
  146.   #include <sys/time.h>
  147.   
  148.   #endif
  149.   #endif
  150.   
  151.   #define mylower(c) (isupper(c) ? (c)-'A'+'a' : (c))
  152.   #define myupper(c) (islower(c) ? (c)-'a'+'A' : (c))
  153.   #define STRLEN(s) (sizeof(s) - 1)
  154. --- 163,202 ----
  155.   
  156.   static char TTY[] = "/dev/tty";
  157.   
  158. ! #ifdef V7
  159. ! /* for Version 7 */
  160. ! #include <errno.h>
  161. ! extern int errno;
  162. ! #define strchr index
  163. ! extern char *strcpy(), *strchr();
  164. ! #include <signal.h>
  165. ! #define O_RDONLY 0
  166. ! #define O_WRONLY 1
  167. ! #define O_RDWR   2
  168.   
  169. + #else
  170. + /* for System V and BSD */
  171. + #include <string.h>
  172. + #include <sys/signal.h>
  173. + #include <fcntl.h>
  174. + #endif
  175. + #ifdef SYSV
  176. + /* for System V and Version 7*/
  177.   struct utimbuf {
  178.       time_t actime;
  179.       time_t modtime;
  180.   };
  181. ! #define utimes(f, t) utime((f), &(t))
  182.   
  183.   #else
  184.   /* for BSD */
  185.   #define RENAME
  186.   #include <sys/time.h>
  187.   
  188.   #endif
  189.   #endif
  190.   
  191.   #define mylower(c) (isupper(c) ? (c)-'A'+'a' : (c))
  192.   #define myupper(c) (islower(c) ? (c)-'a'+'A' : (c))
  193.   #define STRLEN(s) (sizeof(s) - 1)
  194. ***************
  195. *** 298,304 ****
  196.   static void init(/* */);
  197.   static void procargs(/* int argc, char **argv,
  198.       char **pfrompat, char **ptopat */);
  199. ! static void matchpats(/* char *cfrom, char *cto */);
  200.   static int getpat(/* */);
  201.   static int getword(/* char *buf */);
  202.   static void matchpat(/*  */);
  203. --- 324,330 ----
  204.   static void init(/* */);
  205.   static void procargs(/* int argc, char **argv,
  206.       char **pfrompat, char **ptopat */);
  207. ! static void domatch(/* char *cfrom, char *cto */);
  208.   static int getpat(/* */);
  209.   static int getword(/* char *buf */);
  210.   static void matchpat(/*  */);
  211. ***************
  212. *** 355,360 ****
  213. --- 381,387 ----
  214.   static void *challoc(/* int k, int which */);
  215.   static void chgive(/* void *p, unsigned k */);
  216.   static int mygetc(/* */);
  217. + static char *mygets(/* char *s, int l */);
  218.   #ifdef MSDOS
  219.   static int leave(/*  */);
  220.   static void cleanup(/*  */);
  221. ***************
  222. *** 394,406 ****
  223.   
  224.   static char PATLONG[] = "%.40s... : pattern too long.\n";
  225.   
  226. ! static char from[MAXPATLEN], to[MAXPATLEN];
  227.   static int fromlen, tolen;
  228.   static char *(stagel[MAXWILD]), *(firstwild[MAXWILD]), *(stager[MAXWILD]);
  229.   static int nwilds[MAXWILD];
  230.   static int nstages;
  231. ! static char pathbuf[MAXPATH];
  232. ! static char fullrep[MAXPATH + 1];
  233.   static char *(start[MAXWILD]);
  234.   static int len[MAXWILD];
  235.   static char hasdot[MAXWILD];
  236. --- 421,433 ----
  237.   
  238.   static char PATLONG[] = "%.40s... : pattern too long.\n";
  239.   
  240. ! char from[MAXPATLEN], to[MAXPATLEN];
  241.   static int fromlen, tolen;
  242.   static char *(stagel[MAXWILD]), *(firstwild[MAXWILD]), *(stager[MAXWILD]);
  243.   static int nwilds[MAXWILD];
  244.   static int nstages;
  245. ! char pathbuf[MAXPATH];
  246. ! char fullrep[MAXPATH + 1];
  247.   static char *(start[MAXWILD]);
  248.   static int len[MAXWILD];
  249.   static char hasdot[MAXWILD];
  250. ***************
  251. *** 446,452 ****
  252.   
  253.       init();
  254.       procargs(argc, argv, &frompat, &topat);
  255. !     matchpats(frompat, topat);
  256.       if (!(op & APPEND))
  257.           checkcollisions();
  258.       findorder();
  259. --- 473,479 ----
  260.   
  261.       init();
  262.       procargs(argc, argv, &frompat, &topat);
  263. !     domatch(frompat, topat);
  264.       if (!(op & APPEND))
  265.           checkcollisions();
  266.       findorder();
  267. ***************
  268. *** 616,622 ****
  269.   }
  270.   
  271.   
  272. ! static void matchpats(cfrom, cto)
  273.       char *cfrom, *cto;
  274.   {
  275.       if (cfrom == NULL)
  276. --- 643,649 ----
  277.   }
  278.   
  279.   
  280. ! static void domatch(cfrom, cto)
  281.       char *cfrom, *cto;
  282.   {
  283.       if (cfrom == NULL)
  284. ***************
  285. *** 1381,1392 ****
  286.       flags |= FI_STTAKEN;
  287.   #ifdef SYSV
  288.       if (stat(ffull, &fstat)) {
  289. !         fprintf("Strange, couldn't stat %s.\n", ffull);
  290.           quit();
  291.       }
  292.   #else
  293.       if (lstat(ffull, &fstat)) {
  294. !         fprintf("Strange, couldn't lstat %s.\n", ffull);
  295.           quit();
  296.       }
  297.       if ((flags & FI_INSTICKY) && fstat.st_uid != uid && uid != 0)
  298. --- 1408,1419 ----
  299.       flags |= FI_STTAKEN;
  300.   #ifdef SYSV
  301.       if (stat(ffull, &fstat)) {
  302. !         fprintf(stderr, "Strange, couldn't stat %s.\n", ffull);
  303.           quit();
  304.       }
  305.   #else
  306.       if (lstat(ffull, &fstat)) {
  307. !         fprintf(stderr, "Strange, couldn't lstat %s.\n", ffull);
  308.           quit();
  309.       }
  310.       if ((flags & FI_INSTICKY) && fstat.st_uid != uid && uid != 0)
  311. ***************
  312. *** 1967,1973 ****
  313.                   cnv = LOWER;
  314.                   c = *(++pat);
  315.               }
  316. !             if (c == 'u') {
  317.                   cnv = UPPER;
  318.                   c = *(++pat);
  319.               }
  320. --- 1994,2000 ----
  321.                   cnv = LOWER;
  322.                   c = *(++pat);
  323.               }
  324. !             else if (c == 'u') {
  325.                   cnv = UPPER;
  326.                   c = *(++pat);
  327.               }
  328. ***************
  329. *** 2358,2364 ****
  330.                   if (
  331.                       (op & (COPY | APPEND)) ?
  332.                           copy(p->r_ffrom,
  333. !                             p->r_flags & R_ISALIASED ? aliaslen : -1) :
  334.   #ifndef MSDOS
  335.                       (op & HARDLINK) ?
  336.                           link(pathbuf, fullrep) :
  337. --- 2385,2391 ----
  338.                   if (
  339.                       (op & (COPY | APPEND)) ?
  340.                           copy(p->r_ffrom,
  341. !                             p->r_flags & R_ISALIASED ? aliaslen : -1L) :
  342.   #ifndef MSDOS
  343.                       (op & HARDLINK) ?
  344.                           link(pathbuf, fullrep) :
  345. ***************
  346. *** 2484,2490 ****
  347.   #endif
  348.           while (
  349.               fprintf(stderr, "File name> "),
  350. !             (outfile = fopen(gets(fname), "w")) == NULL
  351.           )
  352.               fprintf(stderr, "Can't open %s.\n", fname);
  353.       }
  354. --- 2511,2517 ----
  355.   #endif
  356.           while (
  357.               fprintf(stderr, "File name> "),
  358. !             (outfile = fopen(mygets(fname, 80), "w")) == NULL
  359.           )
  360.               fprintf(stderr, "Can't open %s.\n", fname);
  361.       }
  362. ***************
  363. *** 2552,2558 ****
  364.           int llen;
  365.           char linkbuf[MAXPATH];
  366.   
  367. !         if ((llen = readlink(pathbuf, linkbuf, MAXPATH - 1)) != 1) {
  368.               linkbuf[llen] = '\0';
  369.               return(symlink(linkbuf, fullrep) || myunlink(pathbuf, p->r_ffrom));
  370.           }
  371. --- 2579,2585 ----
  372.           int llen;
  373.           char linkbuf[MAXPATH];
  374.   
  375. !         if ((llen = readlink(pathbuf, linkbuf, MAXPATH - 1)) >= 0) {
  376.               linkbuf[llen] = '\0';
  377.               return(symlink(linkbuf, fullrep) || myunlink(pathbuf, p->r_ffrom));
  378.           }
  379. ***************
  380. *** 2559,2565 ****
  381.       }
  382.   #endif
  383.   #endif
  384. !     return(copy(p->r_ffrom, -1) || myunlink(pathbuf, p->r_ffrom));
  385.   }
  386.   
  387.   
  388. --- 2586,2592 ----
  389.       }
  390.   #endif
  391.   #endif
  392. !     return(copy(p->r_ffrom, -1L) || myunlink(pathbuf, p->r_ffrom));
  393.   }
  394.   
  395.   
  396. ***************
  397. *** 2595,2601 ****
  398.               ff->fi_mode
  399.   #endif
  400.           ;
  401. !     mode = O_CREAT |
  402.   #ifdef MSDOS
  403.           O_BINARY | (op & ZAPPEND ? O_RDWR : O_WRONLY)
  404.   #else
  405. --- 2622,2636 ----
  406.               ff->fi_mode
  407.   #endif
  408.           ;
  409. ! #ifdef V7
  410. !     if (
  411. !         !(op & APPEND) ||
  412. !         (((t = open(fullrep, O_RDWR)) < 0 && errno == ENOENT)
  413. !     )
  414. !         t = creat(fullrep, perm);
  415. ! #else
  416. !     mode = O_CREAT | (op & APPEND ? 0 : O_TRUNC) |
  417.   #ifdef MSDOS
  418.           O_BINARY | (op & ZAPPEND ? O_RDWR : O_WRONLY)
  419.   #else
  420. ***************
  421. *** 2602,2624 ****
  422.           O_WRONLY
  423.   #endif
  424.           ;
  425. !     if (!(op & APPEND))
  426. !         mode |= O_TRUNC;
  427. !     if ((t = open(fullrep, mode, perm)) < 0) {
  428.           close(f);
  429.           return(-1);
  430.       }
  431.       if (op & APPEND)
  432. !         lseek(t, 0, 2);
  433.   #ifdef MSDOS
  434.       if (op & ZAPPEND && filelength(t) != 0) {
  435. !         if (lseek(t, -1, 1) == -1L || read(t, &c, 1) != 1) {
  436.               close(f);
  437.               close(t);
  438.               return(-1);
  439.           }
  440.           if (c == 26)
  441. !             lseek(t, -1, 1);
  442.       }
  443.   #endif
  444.       if ((op & APPEND) && len != -1L) {
  445. --- 2637,2659 ----
  446.           O_WRONLY
  447.   #endif
  448.           ;
  449. !     t = open(fullrep, mode, perm);
  450. ! #endif
  451. !     if (t < 0) {
  452.           close(f);
  453.           return(-1);
  454.       }
  455.       if (op & APPEND)
  456. !         lseek(t, 0L, 2);
  457.   #ifdef MSDOS
  458.       if (op & ZAPPEND && filelength(t) != 0) {
  459. !         if (lseek(t, -1L, 1) == -1L || read(t, &c, 1) != 1) {
  460.               close(f);
  461.               close(t);
  462.               return(-1);
  463.           }
  464.           if (c == 26)
  465. !             lseek(t, -1L, 1);
  466.       }
  467.   #endif
  468.       if ((op & APPEND) && len != -1L) {
  469. ***************
  470. *** 2822,2827 ****
  471. --- 2857,2880 ----
  472.       if (lastc == EOF)
  473.           return(EOF);
  474.       return(lastc = getchar());
  475. + }
  476. + static char *mygets(s, l)
  477. +     char *s;
  478. +     int l;
  479. + {
  480. +     char *nl;
  481. +     for (;;) {
  482. +         if (fgets(s, l, stdin) == NULL)
  483. +             return(NULL);
  484. +         if ((nl = strchr(s, '\n')) != NULL)
  485. +             break;
  486. +         fprintf(stderr, "Input string too long. Try again> ");
  487. +     }
  488. +     *nl = '\0';
  489. +     return(s);
  490.   }
  491.   
  492.   
  493.  
  494. exit 0 # Just in case...
  495.