home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / unix / volume21 / mmv.pch < prev    next >
Encoding:
Text File  |  1990-06-14  |  11.6 KB  |  516 lines

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