home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume21 / mmv / part01 / mmv.c.2 < prev    next >
Text File  |  1990-04-08  |  7KB  |  423 lines

  1. static int movealias(first, p, pprintaliased)
  2.     REP *first, *p;
  3.     int *pprintaliased;
  4. {
  5.     char *fstart;
  6.     int ret;
  7.  
  8.     strcpy(pathbuf, p->r_hto->h_name);
  9.     fstart = pathbuf + strlen(pathbuf);
  10.     strcpy(fstart, TEMP);
  11.     for (
  12.         ret = 0;
  13.         sprintf(fstart + STRLEN(TEMP), "%03d", ret),
  14.         fsearch(fstart, p->r_hto->h_di) != NULL;
  15.         ret++
  16.     )
  17.         ;
  18.     if (rename(fullrep, pathbuf)) {
  19.         fprintf(stderr,
  20.             "%s -> %s has failed.\n", fullrep, pathbuf);
  21.         *pprintaliased = snap(first, p);
  22.     }
  23.     return(ret);
  24. }
  25.  
  26.  
  27. static int snap(first, p)
  28.     REP *first, *p;
  29. {
  30.     char fname[80];
  31.     int redirected = 0;
  32.  
  33.     if (noex)
  34.         exit(1);
  35.  
  36.     failed = 1;
  37. #ifdef MSDOS
  38.     ctrlbrk((int (*)())breakstat);
  39. #else
  40.     signal(SIGINT, breakstat);
  41. #endif
  42.     if (
  43.         badstyle == ASKBAD &&
  44.         isatty(fileno(stdout)) &&
  45.         getreply("Redirect standard output to file? ", 0)
  46.     ) {
  47.         redirected = 1;
  48. #ifndef MSDOS
  49.         umask(oldumask);
  50. #endif
  51.         while (
  52.             fprintf(stderr, "File name> "),
  53.             (outfile = fopen(gets(fname), "w")) == NULL
  54.         )
  55.             fprintf(stderr, "Can't open %s.\n", fname);
  56.     }
  57.     if (redirected || !verbose)
  58.         showdone(p);
  59.     fprintf(outfile, "The following left undone:\n");
  60.     noex = 1;
  61.     return(first != p);
  62. }
  63.  
  64.  
  65. static void showdone(fin)
  66.     REP *fin;
  67. {
  68.     REP *first, *p;
  69.  
  70.     for (first = hrep.r_next; ; first = first->r_next)
  71.         for (p = first; p != NULL; p = p->r_thendo) {
  72.             if (p == fin)
  73.                 return;
  74.             fprintf(outfile, "%s%s %c%c %s%s : done%s\n",
  75.                 p->r_hfrom->h_name, p->r_ffrom->fi_name,
  76.                 p->r_flags & R_ISALIASED ? '=' : '-',
  77.                 p->r_flags & R_ISCYCLE ? '^' : '>',
  78.                 p->r_hto->h_name, p->r_nto,
  79.                 (p->r_fdel != NULL && !(op & APPEND)) ? " (*)" : "");
  80.         }
  81. }
  82.  
  83.  
  84. static void breakout()
  85. {
  86.     fflush(stdout);
  87.     fprintf(stderr, "Aborting, nothing done.\n");
  88.     exit(1);
  89. }
  90.  
  91.  
  92. static int breakrep()
  93. {
  94.     gotsig = 1;
  95.     return(1);
  96. }
  97.  
  98.  
  99. static void breakstat()
  100. {
  101.     exit(1);
  102. }
  103.  
  104.  
  105. static void quit()
  106. {
  107.     fprintf(stderr, "Aborting, nothing done.\n");
  108.     exit(1);
  109. }
  110.  
  111.  
  112. static int copymove(p)
  113.     REP *p;
  114. {
  115. #ifndef MSDOS
  116. #ifndef SYSV
  117.     {
  118.         int llen;
  119.         char linkbuf[MAXPATH];
  120.  
  121.         if ((llen = readlink(pathbuf, linkbuf, MAXPATH - 1)) != 1) {
  122.             linkbuf[llen] = '\0';
  123.             return(symlink(linkbuf, fullrep) || myunlink(pathbuf, p->r_ffrom));
  124.         }
  125.     }
  126. #endif
  127. #endif
  128.     return(copy(p->r_ffrom, -1) || myunlink(pathbuf, p->r_ffrom));
  129. }
  130.  
  131.  
  132.  
  133. #define IRWMASK (S_IREAD | S_IWRITE)
  134. #define RWMASK (IRWMASK | (IRWMASK >> 3) | (IRWMASK >> 6))
  135.  
  136. static int copy(ff, len)
  137.     FILEINFO *ff;
  138.     long len;
  139. {
  140.     char buf[BUFSIZE], c;
  141.     int f, t, k, mode, perm;
  142. #ifdef MSDOS
  143.     struct ftime tim;
  144. #else
  145. #ifdef SYSV
  146.     struct utimbuf tim;
  147. #else
  148.     struct timeval tim[2];
  149. #endif
  150.     struct stat fstat;
  151. #endif
  152.  
  153.     if ((f = open(pathbuf, O_RDONLY | O_BINARY, 0)) < 0)
  154.         return(-1);
  155.     perm =
  156. #ifdef MSDOS
  157.         IRWMASK        /* will _chmod it later (to get all the attributes) */
  158. #else
  159.         (op & (APPEND | OVERWRITE)) ?
  160.             (~oldumask & RWMASK) | (ff->fi_mode & ~RWMASK) :
  161.             ff->fi_mode
  162. #endif
  163.         ;
  164.     mode = O_CREAT |
  165. #ifdef MSDOS
  166.         O_BINARY | (op & ZAPPEND ? O_RDWR : O_WRONLY)
  167. #else
  168.         O_WRONLY
  169. #endif
  170.         ;
  171.     if (!(op & APPEND))
  172.         mode |= O_TRUNC;
  173.     if ((t = open(fullrep, mode, perm)) < 0) {
  174.         close(f);
  175.         return(-1);
  176.     }
  177.     if (op & APPEND)
  178.         lseek(t, 0, 2);
  179. #ifdef MSDOS
  180.     if (op & ZAPPEND && filelength(t) != 0) {
  181.         if (lseek(t, -1, 1) == -1L || read(t, &c, 1) != 1) {
  182.             close(f);
  183.             close(t);
  184.             return(-1);
  185.         }
  186.         if (c == 26)
  187.             lseek(t, -1, 1);
  188.     }
  189. #endif
  190.     if ((op & APPEND) && len != -1L) {
  191.         while (
  192.             len != 0 &&
  193.             (k = read(f, buf, len > BUFSIZE ? BUFSIZE : (unsigned)len)) > 0 &&
  194.             write(t, buf, k) == k
  195.         )
  196.             len -= k;
  197.         if (len == 0)
  198.             k = 0;
  199.     }
  200.     else 
  201.         while ((k = read(f, buf, BUFSIZE)) > 0 && write(t, buf, k) == k)
  202.             ;
  203.     if (!(op & (APPEND | OVERWRITE)))
  204.         if (
  205. #ifdef MSDOS
  206.             getftime(f, &tim) ||
  207.             setftime(t, &tim) ||
  208.             _chmod(fullrep, 1, ff->fi_attrib) == -1
  209. #else
  210.             stat(pathbuf, &fstat) ||
  211.             (
  212. #ifdef SYSV
  213.                 tim.actime = fstat.st_atime,
  214.                 tim.modtime = fstat.st_mtime,
  215. #else
  216.                 tim[0].tv_sec = fstat.st_atime,
  217.                 tim[1].tv_sec = fstat.st_mtime,
  218. #endif
  219.                 utimes(fullrep, tim)
  220.             )
  221. #endif
  222.         )
  223.             fprintf(stderr, "Strange, couldn't transfer time from %s to %s.\n",
  224.                 pathbuf, fullrep);
  225.  
  226.     close(f);
  227.     close(t);
  228.     if (k != 0) {
  229.         if (!(op & APPEND))
  230.             unlink(fullrep);
  231.         return(-1);
  232.     }
  233.     return(0);
  234. }
  235.  
  236.  
  237. #ifndef RENAME
  238. static int rename(from, to)
  239.     char *from, *to;
  240. {
  241.     if (link(from, to))
  242.         return(-1);
  243.     if (unlink(from)) {
  244.         unlink(to);
  245.         return(-1);
  246.     }
  247.     return(0);
  248. }
  249. #endif
  250.  
  251.  
  252. static int myunlink(n, f)
  253.     char *n;
  254.     FILEINFO *f;
  255. {
  256. #ifdef MSDOS
  257.     int a;
  258.  
  259.     if (((a = f->fi_attrib) & FA_RDONLY) && _chmod(n, 1, a & ~FA_RDONLY) < 0) {
  260.         fprintf(stderr, "Strange, can not _chmod (or unlink) %s.\n", f);
  261.         return(-1);
  262.     }
  263. #endif
  264.     if (unlink(n)) {
  265.         fprintf(stderr, "Strange, can not unlink %s.\n", n);
  266.         return(-1);
  267.     }
  268.     return(0);
  269. }
  270.  
  271.  
  272. static int getreply(m, failact)
  273.     char *m;
  274.     int failact;
  275. {
  276.     static FILE *tty = NULL;
  277.     int c, r;
  278.  
  279.     fprintf(stderr, m);
  280.     if (tty == NULL && (tty = fopen(TTY, "r")) == NULL) {
  281.         fprintf(stderr, "Can not open %s to get reply.\n", TTY);
  282.         if (failact == -1)
  283.             quit();
  284.         else
  285.             return(failact);
  286.     }
  287.     for (;;) {
  288.         r = fgetc(tty);
  289.         if (r == EOF) {
  290.             fprintf(stderr, "Can not get reply.\n");
  291.             if (failact == -1)
  292.                 quit();
  293.             else
  294.                 return(failact);
  295.         }
  296.         if (r != '\n')
  297.             while ((c = fgetc(tty)) != '\n' && c != EOF)
  298.                 ;
  299.         r = mylower(r);
  300.         if (r == 'y' || r == 'n')
  301.             return(r == 'y');
  302.         fprintf(stderr, "Yes or No? ");
  303.     }
  304. }
  305.  
  306.  
  307. static void *myalloc(k)
  308.     unsigned k;
  309. {
  310.     void *ret;
  311.  
  312.     if (k == 0)
  313.         return(NULL);
  314.     if ((ret = (void *)malloc(k)) == NULL) {
  315.         fprintf(stderr, "Insufficient memory.\n");
  316.         quit();
  317.     }
  318.     return(ret);
  319. }
  320.  
  321.  
  322. static void *challoc(k, which)
  323.     int which;
  324.     int k;
  325. {
  326.     void *ret;
  327.     CHUNK *p, *q;
  328.     SLICER *sl = &(slicer[which]);
  329.  
  330.     if (k > sl->sl_len) {
  331.         for (
  332.             q = NULL, p = freechunks;
  333.             p != NULL && (sl->sl_len = p->ch_len) < k;
  334.             q = p, p = p->ch_next
  335.         )
  336.             ;
  337.         if (p == NULL) {
  338.             sl->sl_len = CHUNKSIZE - sizeof(CHUNK *);
  339.             p = (CHUNK *)myalloc(CHUNKSIZE);
  340.         }
  341.         else if (q == NULL)
  342.             freechunks = p->ch_next;
  343.         else
  344.             q->ch_next = p->ch_next;
  345.         p->ch_next = sl->sl_first;
  346.         sl->sl_first = p;
  347.         sl->sl_unused = (char *)&(p->ch_len);
  348.     }
  349.     sl->sl_len -= k;
  350.     ret = (void *)sl->sl_unused;
  351.     sl->sl_unused += k;
  352.     return(ret);
  353. }
  354.  
  355.  
  356. static void chgive(p, k)
  357.     void *p;
  358.     unsigned k;
  359. {
  360.     ((CHUNK *)p)->ch_len = k - sizeof(CHUNK *);
  361.     ((CHUNK *)p)->ch_next = freechunks;
  362.     freechunks = (CHUNK *)p;
  363. }
  364.  
  365.  
  366. #ifndef MSDOS
  367. static void memmove(to, from, k)
  368.     char *to, *from;
  369.     unsigned k;
  370. {
  371.     if (from > to)
  372.         while (k-- != 0)
  373.             *(to++) = *(from++);
  374.     else {
  375.         from += k;
  376.         to += k;
  377.         while (k-- != 0)
  378.             *(--to) = *(--from);
  379.     }
  380. }
  381. #endif
  382.  
  383.  
  384. static int mygetc()
  385. {
  386.     static int lastc = 0;
  387.  
  388.     if (lastc == EOF)
  389.         return(EOF);
  390.     return(lastc = getchar());
  391. }
  392.  
  393.  
  394. #ifdef MSDOS
  395. static int leave()
  396. {
  397.     return(0);
  398. }
  399.  
  400. static void cleanup()
  401. {
  402.     int i;
  403.  
  404.     if (patch.ph_safeid) {
  405.         for (i = 0; i < nhandles; i++) {
  406.             if (!(handles[i]->h_di->di_flags & DI_CLEANED)) {
  407.                 sprintf(pathbuf, "%s%s%03d",
  408.                     handles[i]->h_name, IDF, handles[i]->h_di->di_did);
  409.                 if (unlink(pathbuf))
  410.                     fprintf(stderr, "Strange, couldn't unlink %s.\n", pathbuf);
  411.                 handles[i]->h_di->di_flags |= DI_CLEANED;
  412.             }
  413.         }
  414.     }
  415. /*
  416.     Write device availability: undocumented internal MS-D*S function.
  417.     Restore previous value.
  418. */
  419.     bdos(0x37, olddevflag, 3);
  420. }
  421.  
  422. #endif
  423.