home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / archivers / arcppc / src / rcs / arcadd.c,v < prev    next >
Text File  |  1998-04-23  |  15KB  |  688 lines

  1. head     1.9;
  2. branch   ;
  3. access   ;
  4. symbols  patch1:1.9;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.9
  10. date     88.07.31.18.45.14;  author hyc;  state Exp;
  11. branches ;
  12. next     1.8;
  13.  
  14. 1.8
  15. date     88.06.13.00.31.15;  author hyc;  state Exp;
  16. branches ;
  17. next     1.7;
  18.  
  19. 1.7
  20. date     88.06.12.18.46.49;  author hyc;  state Exp;
  21. branches ;
  22. next     1.6;
  23.  
  24. 1.6
  25. date     88.06.01.19.26.15;  author hyc;  state Exp;
  26. branches ;
  27. next     1.5;
  28.  
  29. 1.5
  30. date     88.06.01.19.17.24;  author hyc;  state Exp;
  31. branches ;
  32. next     1.4;
  33.  
  34. 1.4
  35. date     88.06.01.15.15.29;  author hyc;  state Exp;
  36. branches ;
  37. next     1.3;
  38.  
  39. 1.3
  40. date     88.06.01.15.11.01;  author hyc;  state Exp;
  41. branches ;
  42. next     1.2;
  43.  
  44. 1.2
  45. date     88.04.11.17.49.38;  author hyc;  state Exp;
  46. branches ;
  47. next     1.1;
  48.  
  49. 1.1
  50. date     88.04.11.17.43.01;  author hyc;  state Exp;
  51. branches ;
  52. next     ;
  53.  
  54.  
  55. desc
  56. @@
  57.  
  58.  
  59. 1.9
  60. log
  61. @fixed arguments to fopen for non-MTS systems
  62. Fixups in input buffer for MTS - use symbolic names for flags
  63. Check for long filenames being added for non-DOS systems
  64. @
  65. text
  66. @/*
  67.  * $Header: arcadd.c,v 1.8 88/06/13 00:31:15 hyc Locked $
  68.  */
  69.  
  70. /*
  71.  * ARC - Archive utility - ARCADD
  72.  * 
  73.  * Version 3.40, created on 06/18/86 at 13:10:18
  74.  * 
  75.  * (C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
  76.  * 
  77.  * By:  Thom Henderson
  78.  * 
  79.  * Description: This file contains the routines used to add files to an archive.
  80.  * 
  81.  * Language: Computer Innovations Optimizing C86
  82.  */
  83. #include <stdio.h>
  84. #include "arc.h"
  85. #if    MTS
  86. #include <mts.h>
  87. #endif
  88.  
  89. static    void    addfile();
  90. char    *strcpy();
  91. int    strcmp(), strlen(), free(), readhdr(), unlink();
  92. #if    UNIX
  93. int    izadir();
  94. #endif
  95. void    writehdr(), filecopy(), getstamp();
  96. void    pack(), closearc(), openarc(), abort();
  97.  
  98. void
  99. addarc(num, arg, move, update, fresh)        /* add files to archive */
  100.     int             num;    /* number of arguments */
  101.     char           *arg[];    /* pointers to arguments */
  102. int             move;        /* true if moving file */
  103. int             update;        /* true if updating */
  104. int             fresh;        /* true if freshening */
  105. {
  106.     char           *d, *dir();    /* directory junk */
  107.     char            buf[STRLEN];    /* pathname buffer */
  108.     char          **path;    /* pointer to pointers to paths */
  109.     char          **name;    /* pointer to pointers to names */
  110.     int             nfiles = 0;    /* number of files in lists */
  111.     int             notemp;    /* true until a template works */
  112.     int             nowork = 1;    /* true until files are added */
  113.     char           *i, *rindex();    /* string indexing junk */
  114.     char           *malloc(), *realloc();    /* memory allocators */
  115.     int             n;    /* index */
  116. #if    MSDOS
  117.     unsigned int    coreleft();    /* remaining memory reporter */
  118. #endif
  119.     int        addbunch();
  120.  
  121.     if (num < 1) {        /* if no files named */
  122.         num = 1;    /* then fake one */
  123. #if    DOS
  124.         arg[0] = "*.*";    /* add everything */
  125. #endif
  126. #if    UNIX
  127.         arg[0] = "*";
  128. #endif
  129. #if    MTS
  130.         arg[0] = "?";
  131. #endif
  132.     }
  133.     path = (char **) malloc(sizeof(char **));
  134.     name = (char **) malloc(sizeof(char **));
  135.  
  136.  
  137.     for (n = 0; n < num; n++) {    /* for each template supplied */
  138.         strcpy(buf, arg[n]);    /* get ready to fix path */
  139. #if    !MTS
  140.         if (!(i = rindex(buf, '\\')))
  141.             if (!(i = rindex(buf, '/')))
  142.                 if (!(i = rindex(buf, ':')))
  143.                     i = buf - 1;
  144. #else
  145.         if (!(i = rindex(buf, sepchr[0])))
  146.             if (buf[0] != tmpchr[0])
  147.                 i = buf - 1;
  148.             else
  149.                 i = buf;
  150. #endif
  151.         i++;        /* pointer to where name goes */
  152.  
  153.         notemp = 1;    /* reset files flag */
  154.         for (d = dir(arg[n]); d; d = dir(NULL)) {
  155.             notemp = 0;    /* template is giving results */
  156.             nfiles++;    /* add each matching file */
  157.             path = (char **) realloc(path, nfiles * sizeof(char **));
  158.             name = (char **) realloc(name, nfiles * sizeof(char **));
  159.             strcpy(i, d);    /* put name in path */
  160.             path[nfiles - 1] = malloc(strlen(buf) + 1);
  161.             strcpy(path[nfiles - 1], buf);
  162.             name[nfiles - 1] = d;    /* save name */
  163. #if    MSDOS
  164.             if (coreleft() < 5120) {
  165.                 nfiles = addbunch(nfiles, path, name, move, update, fresh);
  166.                 nowork = nowork && !nfiles;
  167.                 while (nfiles) {
  168.                     free(path[--nfiles]);
  169.                     free(name[nfiles]);
  170.                 }
  171.                 free(path);
  172.                 free(name);
  173.                 path = name = NULL;
  174.             }
  175. #endif
  176.         }
  177.         if (notemp && warn)
  178.             printf("No files match: %s\n", arg[n]);
  179.     }
  180.  
  181.     if (nfiles) {
  182.         nfiles = addbunch(nfiles, path, name, move, update, fresh);
  183.         nowork = nowork && !nfiles;
  184.         while (nfiles) {
  185.             free(path[--nfiles]);
  186.             free(name[nfiles]);
  187.         }
  188.         free(path);
  189.         free(name);
  190.     }
  191.     if (nowork && warn)
  192.         printf("No files were added.\n");
  193. }
  194.  
  195. int
  196. addbunch(nfiles, path, name, move, update, fresh)    /* add a bunch of files */
  197.     int             nfiles;    /* number of files to add */
  198.     char          **path;    /* pointers to pathnames */
  199.     char          **name;    /* pointers to filenames */
  200.     int             move;    /* true if moving file */
  201.     int             update;    /* true if updating */
  202.     int             fresh;    /* true if freshening */
  203. {
  204.     int             m, n;    /* indices */
  205.     char           *d;    /* swap pointer */
  206.     struct heads    hdr;    /* file header data storage */
  207.  
  208.     for (n = 0; n < nfiles - 1; n++) {    /* sort the list of names */
  209.         for (m = n + 1; m < nfiles; m++) {
  210.             if (strcmp(name[n], name[m]) > 0) {
  211.                 d = path[n];
  212.                 path[n] = path[m];
  213.                 path[m] = d;
  214.                 d = name[n];
  215.                 name[n] = name[m];
  216.                 name[m] = d;
  217.             }
  218.         }
  219.     }
  220.  
  221.     for (n = 0; n < nfiles - 1;) {    /* consolidate the list of names */
  222.         if (!strcmp(path[n], path[n + 1])    /* if duplicate names */
  223.             ||!strcmp(path[n], arcname)    /* or this archive */
  224. #if    UNIX
  225.             ||izadir(path[n])    /* or a directory */
  226. #endif
  227.             ||!strcmp(path[n], newname)    /* or the new version */
  228.             ||!strcmp(path[n], bakname)) {    /* or its backup */
  229.             free(path[n]);    /* then forget the file */
  230.             free(name[n]);
  231.             for (m = n; m < nfiles - 1; m++) {
  232.                 path[m] = path[m + 1];
  233.                 name[m] = name[m + 1];
  234.             }
  235.             nfiles--;
  236.         } else
  237.             n++;    /* else test the next one */
  238.     }
  239.  
  240.     if (!strcmp(path[n], arcname)    /* special check for last file */
  241.         ||!strcmp(path[n], newname)    /* courtesy of Rick Moore */
  242. #if    UNIX
  243.         ||izadir(path[n])
  244. #endif
  245.         || !strcmp(path[n], bakname)) {
  246.         free(path[n]);
  247.         free(name[n]);
  248.         nfiles--;
  249.     }
  250.     if (!nfiles)        /* make sure we got some */
  251.         return 0;
  252.  
  253.     for (n = 0; n < nfiles - 1; n++) {    /* watch out for duplicate
  254.                          * names */
  255.         if (!strcmp(name[n], name[n + 1]))
  256.             abort("Duplicate filenames:\n  %s\n  %s", path[n], path[n + 1]);
  257.     }
  258.     openarc(1);        /* open archive for changes */
  259.  
  260.     for (n = 0; n < nfiles; n++)    /* add each file in the list */
  261.         addfile(path[n], name[n], update, fresh);
  262.  
  263.     /* now we must copy over all files that follow our additions */
  264.  
  265.     while (readhdr(&hdr, arc)) {    /* while more entries to copy */
  266.         writehdr(&hdr, new);
  267.         filecopy(arc, new, hdr.size);
  268.     }
  269.     hdrver = 0;        /* archive EOF type */
  270.     writehdr(&hdr, new);    /* write out our end marker */
  271.     closearc(1);        /* close archive after changes */
  272.  
  273.     if (move) {        /* if this was a move */
  274.         for (n = 0; n < nfiles; n++) {    /* then delete each file
  275.                          * added */
  276.             if (unlink(path[n]) && warn) {
  277.                 printf("Cannot unsave %s\n", path[n]);
  278.                 nerrs++;
  279.             }
  280.         }
  281.     }
  282.     return nfiles;        /* say how many were added */
  283. }
  284.  
  285. static          void
  286. addfile(path, name, update, fresh)    /* add named file to archive */
  287.     char           *path;    /* path name of file to add */
  288.     char           *name;    /* name of file to add */
  289.     int             update;    /* true if updating */
  290.     int             fresh;    /* true if freshening */
  291. {
  292.     struct heads    nhdr;    /* data regarding the new file */
  293.     struct heads    ohdr;    /* data regarding an old file */
  294.     FILE           *f, *fopen();    /* file to add, opener */
  295.     long            starts, ftell();    /* file locations */
  296.     int             upd = 0;/* true if replacing an entry */
  297.  
  298. #if    !MTS
  299.     if (!(f = fopen(path, OPEN_R)))
  300. #else
  301.     if (image)
  302.         f = fopen(path, "rb");
  303.     else
  304.         f = fopen(path, "r");
  305.     if (!f)
  306. #endif
  307.     {
  308.         if (warn) {
  309.             printf("Cannot read file: %s\n", path);
  310.             nerrs++;
  311.         }
  312.         return;
  313.     }
  314. #if    !DOS
  315.     if (strlen(name) >= FNLEN) {
  316.         if (warn) {
  317.             char    buf[STRLEN];
  318.             printf("WARNING: File %s name too long!\n", name);
  319.             name[FNLEN-1]='\0';
  320.             while(1) {
  321.                 printf("  Truncate to %s (y/n)? ", name);
  322.                 fflush(stdout);
  323.                 fgets(buf, STRLEN, stdin);
  324.                 *buf = toupper(*buf);
  325.                 if (*buf == 'Y' || *buf == 'N')
  326.                     break;
  327.             }
  328.             if (*buf == 'N') {
  329.                 printf("Skipping...\n");
  330.                 fclose(f);
  331.                 return;
  332.             }
  333.         }
  334.         else {
  335.             if (note)
  336.                 printf("Skipping file: %s - name too long.\n",
  337.                     name);
  338.             fclose(f);
  339.             return;
  340.         }
  341.     }
  342. #endif
  343.     strcpy(nhdr.name, name);/* save name */
  344.     nhdr.size = 0;        /* clear out size storage */
  345.     nhdr.crc = 0;        /* clear out CRC check storage */
  346. #if    !MTS
  347.     getstamp(f, &nhdr.date, &nhdr.time);
  348. #else
  349.     {
  350.     char *buffer, *malloc();
  351.     int    inlen;
  352.     struct    GDDSECT    *region;
  353.  
  354.     region=gdinfo(f->_fd._fdub);
  355.     inlen=region->GDINLEN;
  356.     buffer=malloc(inlen);    /* maximum line length */
  357.     setbuf(f,buffer);        
  358.     f->_bufsiz=inlen;        
  359.     f->_mods|=_NOIC;    /* Don't do "$continue with" */
  360.     f->_mods&=~_IC;        /* turn it off, if set... */
  361.     }
  362.     getstamp(path, &nhdr.date, &nhdr.time);
  363. #endif
  364.  
  365.     /* position archive to spot for new file */
  366.  
  367.     if (arc) {        /* if adding to existing archive */
  368.         starts = ftell(arc);    /* where are we? */
  369.         while (readhdr(&ohdr, arc)) {    /* while more files to check */
  370.             if (!strcmp(ohdr.name, nhdr.name)) {
  371.                 upd = 1;    /* replace existing entry */
  372.                 if (update || fresh) {    /* if updating or
  373.                              * freshening */
  374.                     if (nhdr.date < ohdr.date
  375.                         || (nhdr.date == ohdr.date && nhdr.time <= ohdr.time)) {
  376.                         fseek(arc, starts, 0);
  377.                         fclose(f);
  378.                         return;    /* skip if not newer */
  379.                     }
  380.                 }
  381.             }
  382.             if (strcmp(ohdr.name, nhdr.name) >= 0)
  383.                 break;    /* found our spot */
  384.  
  385.             writehdr(&ohdr, new);    /* entry preceeds update;
  386.                          * keep it */
  387.             filecopy(arc, new, ohdr.size);
  388.             starts = ftell(arc);    /* now where are we? */
  389.         }
  390.  
  391.         if (upd) {    /* if an update */
  392.             if (note) {
  393.                 printf("Updating file: %-12s  ", name);
  394.                 fflush(stdout);
  395.             }
  396.             fseek(arc, ohdr.size, 1);
  397.         } else if (fresh) {    /* else if freshening */
  398.             fseek(arc, starts, 0);    /* then do not add files */
  399.             fclose(f);
  400.             return;
  401.         } else {    /* else adding a new file */
  402.             if (note) {
  403.                 printf("Adding file:   %-12s  ", name);
  404.                 fflush(stdout);
  405.             }
  406.             fseek(arc, starts, 0);    /* reset for next time */
  407.         }
  408.     } else {        /* no existing archive */
  409.         if (fresh) {    /* cannot freshen nothing */
  410.             fclose(f);
  411.             return;
  412.         } else if (note) {    /* else adding a file */
  413.             printf("Adding file:   %-12s  ", name);
  414.             fflush(stdout);
  415.         }
  416.     }
  417.  
  418.     starts = ftell(new);    /* note where header goes */
  419.     hdrver = ARCVER;        /* anything but end marker */
  420.     writehdr(&nhdr, new);    /* write out header skeleton */
  421.     pack(f, new, &nhdr);    /* pack file into archive */
  422.     fseek(new, starts, 0);    /* move back to header skeleton */
  423.     writehdr(&nhdr, new);    /* write out real header */
  424.     fseek(new, nhdr.size, 1);    /* skip over data to next header */
  425.     fclose(f);        /* all done with the file */
  426. }
  427. @
  428.  
  429.  
  430. 1.8
  431. log
  432. @Add MTS code for determining record size of input file.
  433. @
  434. text
  435. @d2 1
  436. a2 1
  437.  * $Header: arcadd.c,v 1.7 88/06/12 18:46:49 hyc Locked $
  438. d234 1
  439. a234 1
  440.     if (!(f = fopen(path, "rb")))
  441. d249 29
  442. d289 1
  443. a289 1
  444.     region=gdinfo(f->_fd);
  445. d291 1
  446. a291 1
  447.     buffer=malloc(inlen);   /* maximum line length */
  448. d294 2
  449. a295 2
  450.     f->_mods|=0x00040000;   /* Don't do "$continue with" */
  451.     f->_mods&=0xfff7ffff;   /* turn it off, if set... */
  452. a356 1
  453.     strcpy(nhdr.name, name);
  454. @
  455.  
  456.  
  457. 1.7
  458. log
  459. @Removed 'mode' parameter from dir, since it wasn't used anywhere.
  460. @
  461. text
  462. @d2 1
  463. a2 1
  464.  * $Header: arcadd.c,v 1.6 88/06/01 19:26:15 hyc Locked $
  465. d20 3
  466. d255 13
  467. @
  468.  
  469.  
  470. 1.6
  471. log
  472. @Oops. Missed a "BSD" -> "UNIX"
  473. @
  474. text
  475. @d2 1
  476. a2 1
  477.  * $Header: arcadd.c,v 1.5 88/06/01 19:17:24 hyc Locked $
  478. d86 1
  479. a86 1
  480.         for (d = dir(arg[n], 0); d; d = dir(NULL, 0)) {
  481. @
  482.  
  483.  
  484. 1.5
  485. log
  486. @Change compilation conditionals
  487. @
  488. text
  489. @d2 1
  490. a2 1
  491.  * $Header: arcadd.c,v 1.4 88/06/01 15:15:29 hyc Locked $
  492. d24 1
  493. a24 1
  494. #if    BSD
  495. @
  496.  
  497.  
  498. 1.4
  499. log
  500. @Merged Atari ST code
  501. @
  502. text
  503. @d2 1
  504. a2 1
  505.  * $Header: arcadd.c,v 1.3 88/06/01 15:11:01 hyc Locked $
  506. d24 1
  507. a24 1
  508. #ifdef    BSD
  509. d48 1
  510. a48 1
  511. #ifdef MSDOS
  512. d55 1
  513. a55 1
  514. #ifdef DOS
  515. d58 1
  516. a58 1
  517. #ifdef BSD
  518. d61 1
  519. a61 1
  520. #ifdef MTS
  521. d71 1
  522. a71 1
  523. #ifndef MTS
  524. d95 1
  525. a95 1
  526. #ifdef MSDOS
  527. d156 1
  528. a156 1
  529. #ifdef BSD
  530. d174 1
  531. a174 1
  532. #ifdef BSD
  533. d230 1
  534. a230 4
  535. #ifdef BSD
  536.     if (!(f = fopen(path, "r")))
  537. #endif
  538. #ifdef DOS
  539. d232 1
  540. a232 2
  541. #endif
  542. #ifdef MTS
  543. d249 1
  544. a249 1
  545. #ifndef MTS
  546. @
  547.  
  548.  
  549. 1.3
  550. log
  551. @Fixed declarations
  552. @
  553. text
  554. @d2 1
  555. a2 1
  556.  * $Header: arcadd.c,v 1.9 88/04/19 01:39:18 hyc Exp $
  557. d23 4
  558. a26 1
  559. int    strcmp(), strlen(), free(), izadir(), readhdr(), unlink();
  560. d55 1
  561. a55 1
  562. #ifdef MSDOS
  563. d156 1
  564. d158 1
  565. d174 1
  566. d176 1
  567. d230 7
  568. a236 3
  569. #ifndef MTS
  570.     if (!(f = fopen(path, "r"))) {
  571. #else
  572. d241 1
  573. a241 1
  574.     if (!f) {
  575. d243 1
  576. @
  577.  
  578.  
  579. 1.2
  580. log
  581. @formatting changes...
  582. @
  583. text
  584. @d2 1
  585. a2 14
  586.  * $Log:    arcadd.c,v $
  587.  * Revision 1.1  88/04/11  17:43:01  hyc
  588.  * Initial revision
  589.  * 
  590.  * Revision 1.1  87/12/19  04:00:40  hyc
  591.  * Initial revision
  592.  * 
  593.  * Revision 1.4  87/08/13  17:03:01  hyc
  594.  * Run thru the indent program...
  595.  *  Revision 1.3  87/07/21  11:39:49  hyc minor
  596.  * fixups
  597.  * 
  598.  * Revision 1.2  87/07/21  06:50:06  hyc *** empty log message ***
  599.  * 
  600. d21 7
  601. a27 1
  602. INT
  603. d29 1
  604. a29 1
  605.     INT             num;    /* number of arguments */
  606. d31 3
  607. a33 3
  608. INT             move;        /* true if moving file */
  609. INT             update;        /* true if updating */
  610. INT             fresh;        /* true if freshening */
  611. d36 6
  612. a41 6
  613.     char            buf[100];    /* pathname buffer */
  614.     char          **path = NULL;    /* pointer to pointers to paths */
  615.     char          **name = NULL;    /* pointer to pointers to names */
  616.     INT             nfiles = 0;    /* number of files in lists */
  617.     INT             notemp;    /* true until a template works */
  618.     INT             nowork = 1;    /* true until files are added */
  619. d44 5
  620. a48 4
  621.     INT             m, n;    /* indices */
  622.     unsigned INT    coreleft();    /* remaining memory reporter */
  623.     INT             addbunch();
  624.     INT             addfile();
  625. d124 1
  626. a124 1
  627. INT
  628. d126 1
  629. a126 1
  630.     INT             nfiles;    /* number of files to add */
  631. d129 3
  632. a131 3
  633.     INT             move;    /* true if moving file */
  634.     INT             update;    /* true if updating */
  635.     INT             fresh;    /* true if freshening */
  636. d133 1
  637. a133 2
  638.     char            buf[100];    /* pathname buffer */
  639.     INT             m, n;    /* indices */
  640. d210 1
  641. a210 1
  642. static          INT
  643. d214 2
  644. a215 2
  645.     INT             update;    /* true if updating */
  646.     INT             fresh;    /* true if freshening */
  647. d220 2
  648. a221 3
  649.     LONG            starts, ftell();    /* file locations */
  650.     INT             c;    /* one char of file */
  651.     INT             upd = 0;/* true if replacing an entry */
  652. d301 1
  653. a301 1
  654.     hdrver = 8;        /* anything but end marker */
  655. @
  656.  
  657.  
  658. 1.1
  659. log
  660. @Initial revision
  661. @
  662. text
  663. @d3 3
  664. d131 1
  665. a131 2
  666. addbunch(nfiles, path, name, move, update, fresh)
  667.  /* add a bunch of files */
  668. d185 2
  669. a186 2
  670.     for (n = 0; n < nfiles - 1; n++) {
  671.         /* watch out for duplicate * names */
  672. d206 1
  673. a206 1
  674.         for (n = 0; n < nfiles; n++) {    /* then delete each file *
  675. d218 1
  676. a218 2
  677. addfile(path, name, update, fresh)
  678.  /* add named file to archive */
  679. d262 2
  680. a263 2
  681.                 if (update || fresh) {
  682.                     /* if updating or * freshening */
  683. d275 2
  684. a276 2
  685.             writehdr(&ohdr, new);
  686.             /* entry preceeds update; * keep it */
  687. @
  688.