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

  1. head     1.9;
  2. branch   ;
  3. access   ;
  4. symbols  patch1:1.9 arc521:1.5;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.9
  10. date     88.07.31.18.49.19;  author hyc;  state Exp;
  11. branches ;
  12. next     1.8;
  13.  
  14. 1.8
  15. date     88.07.19.16.00.12;  author hyc;  state Exp;
  16. branches ;
  17. next     1.7;
  18.  
  19. 1.7
  20. date     88.06.02.16.27.32;  author hyc;  state Exp;
  21. branches ;
  22. next     1.6;
  23.  
  24. 1.6
  25. date     88.06.01.19.26.50;  author hyc;  state Exp;
  26. branches ;
  27. next     1.5;
  28.  
  29. 1.5
  30. date     88.06.01.17.58.29;  author hyc;  state Exp;
  31. branches ;
  32. next     1.4;
  33.  
  34. 1.4
  35. date     88.06.01.15.40.40;  author hyc;  state Exp;
  36. branches ;
  37. next     1.3;
  38.  
  39. 1.3
  40. date     88.06.01.14.46.33;  author hyc;  state Exp;
  41. branches ;
  42. next     1.2;
  43.  
  44. 1.2
  45. date     88.04.11.18.01.16;  author hyc;  state Exp;
  46. branches ;
  47. next     1.1;
  48.  
  49. 1.1
  50. date     88.04.11.17.59.43;  author hyc;  state Exp;
  51. branches ;
  52. next     ;
  53.  
  54.  
  55. desc
  56. @@
  57.  
  58.  
  59. 1.9
  60. log
  61. @Initialize hdr size & length to zero before using 'em.
  62. @
  63. text
  64. @/*
  65.  * $Header: arcio.c,v 1.8 88/07/19 16:00:12 hyc Locked $
  66.  */
  67.  
  68. /*  ARC - Archive utility - ARCIO
  69.  
  70.     Version 2.50, created on 04/22/87 at 13:25:20
  71.  
  72. (C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
  73.  
  74.     By:     Thom Henderson
  75.  
  76.     Description:
  77.      This file contains the file I/O routines used to manipulate
  78.      an archive.
  79.  
  80.     Language:
  81.      Computer Innovations Optimizing C86
  82. */
  83. #include <stdio.h>
  84. #include "arc.h"
  85. #if    MTS
  86. #include <ctype.h>
  87. #endif
  88.  
  89. void    abort();
  90. int    strlen(), free();
  91.  
  92. int
  93. readhdr(hdr, f)            /* read a header from an archive */
  94.     struct heads   *hdr;    /* storage for header */
  95.     FILE           *f;    /* archive to read header from */
  96. {
  97. #if    !MSDOS
  98.     unsigned char   dummy[28];
  99.     int             i;
  100. #endif
  101.     char            name[FNLEN];    /* filename buffer */
  102.     int             try = 0;/* retry counter */
  103.     static int      first = 1;    /* true only on first read */
  104.  
  105.     if (!f)            /* if archive didn't open */
  106.         return 0;    /* then pretend it's the end */
  107.     if (feof(f))        /* if no more data */
  108.         return 0;    /* then signal end of archive */
  109.  
  110.     if (fgetc(f) != ARCMARK) {    /* check archive validity */
  111.         if (warn) {
  112.             printf("An entry in %s has a bad header.", arcname);
  113.             nerrs++;
  114.         }
  115.         while (!feof(f)) {
  116.             try++;
  117.             if (fgetc(f) == ARCMARK) {
  118.                 ungetc(hdrver = fgetc(f), f);
  119.                 if (!(hdrver & 0x80) && hdrver <= ARCVER)
  120.                     break;
  121.             }
  122.         }
  123.  
  124.         if (feof(f) && first)
  125.             abort("%s is not an archive", arcname);
  126.  
  127.         if (changing && warn)
  128.             abort("%s is corrupted -- changes disallowed", arcname);
  129.  
  130.         if (warn)
  131.             printf("  %d bytes skipped.\n", try);
  132.  
  133.         if (feof(f))
  134.             return 0;
  135.     }
  136.     hdrver = fgetc(f);    /* get header version */
  137.     if (hdrver & 0x80)    /* sign bit? negative? */
  138.         abort("Invalid header in archive %s", arcname);
  139.     if (hdrver == 0)
  140.         return 0;    /* note our end of archive marker */
  141.     if (hdrver > ARCVER) {
  142.         fread(name, sizeof(char), FNLEN, f);
  143. #if    MTS
  144.         atoe(name, strlen(name));
  145. #endif
  146.         printf("I don't know how to handle file %s in archive %s\n",
  147.                name, arcname);
  148.         printf("I think you need a newer version of ARC.\n");
  149.         exit(1);
  150.     }
  151.     /* amount to read depends on header type */
  152.  
  153.     if (hdrver == 1) {    /* old style is shorter */
  154.         fread(hdr, sizeof(struct heads) - sizeof(long int), 1, f);
  155.         hdrver = 2;    /* convert header to new format */
  156.         hdr->length = hdr->size;    /* size is same when not
  157.                          * packed */
  158.     } else
  159. #if    MSDOS
  160.         fread(hdr, sizeof(struct heads), 1, f);
  161. #else
  162.         fread(dummy, 27, 1, f);
  163.  
  164.     for (i = 0; i < FNLEN; hdr->name[i] = dummy[i], i++);
  165. #if    MTS
  166.     (void) atoe(hdr->name, strlen(hdr->name));
  167. #endif
  168.     for (i = 0, hdr->size=0; i<4; hdr->size<<=8, hdr->size += dummy[16-i], i++);
  169.     hdr->date = (short) ((dummy[18] << 8) + dummy[17]);
  170.     hdr->time = (short) ((dummy[20] << 8) + dummy[19]);
  171.     hdr->crc = (short) ((dummy[22] << 8) + dummy[21]);
  172.     for (i = 0, hdr->length=0; i<4; hdr->length<<=8, hdr->length += dummy[26-i], i++);
  173. #endif
  174.  
  175.     if (hdr->date > olddate
  176.         || (hdr->date == olddate && hdr->time > oldtime)) {
  177.         olddate = hdr->date;
  178.         oldtime = hdr->time;
  179.     }
  180.     first = 0;
  181.     return 1;        /* we read something */
  182. }
  183.  
  184. void
  185. put_int(number, f)        /* write a 2 byte integer */
  186.     short           number;
  187.     FILE           *f;
  188. {
  189.     fputc((char) (number & 255), f);
  190.     fputc((char) (number >> 8), f);
  191. }
  192.  
  193. void
  194. put_long(number, f)        /* write a 4 byte integer */
  195.     long            number;
  196.     FILE           *f;
  197. {
  198.     put_int((short) (number & 0xFFFF), f);
  199.     put_int((short) (number >> 16), f);
  200. }
  201.  
  202. void
  203. writehdr(hdr, f)        /* write a header to an archive */
  204.     struct heads   *hdr;    /* header to write */
  205.     FILE           *f;    /* archive to write to */
  206. {
  207.     fputc(ARCMARK, f);        /* write out the mark of ARC */
  208.     fputc(hdrver, f);    /* write out the header version */
  209.     if (!hdrver)        /* if that's the end */
  210.         return;        /* then write no more */
  211. #if    MSDOS
  212.     fwrite(hdr, sizeof(struct heads), 1, f);
  213. #else
  214.     /* byte/word ordering hassles... */
  215. #if    MTS
  216.     etoa(hdr->name, strlen(hdr->name));
  217. #endif
  218.     fwrite(hdr->name, 1, FNLEN, f);
  219.     put_long(hdr->size, f);
  220.     put_int(hdr->date, f);
  221.     put_int(hdr->time, f);
  222.     put_int(hdr->crc, f);
  223.     put_long(hdr->length, f);
  224. #endif
  225.  
  226.     /* note the newest file for updating the archive timestamp */
  227.  
  228.     if (hdr->date > arcdate
  229.         || (hdr->date == arcdate && hdr->time > arctime)) {
  230.         arcdate = hdr->date;
  231.         arctime = hdr->time;
  232.     }
  233. }
  234.  
  235. void
  236. putc_tst(c, t)            /* put a character, with tests */
  237.     char            c;    /* character to output */
  238.     FILE           *t;    /* file to write to */
  239. {
  240.     c &= 0xff;
  241.     if (t) {
  242. #if    UNIX
  243.         fputc(c, t);
  244.         if (ferror(t))
  245.             abort("Write failed");
  246. #else
  247.         if (fputc(c, t) == EOF)
  248.             abort("Write fail (disk full?)");
  249. #endif
  250.     }
  251. }
  252.  
  253. /*
  254.  * NOTE:  The filecopy() function is used to move large numbers of bytes from
  255.  * one file to another.  This particular version has been modified to improve
  256.  * performance in Computer Innovations C86 version 2.3 in the small memory
  257.  * model.  It may not work as expected with other compilers or libraries, or
  258.  * indeed with different versions of the CI-C86 compiler and library, or with
  259.  * the same version in a different memory model.
  260.  * 
  261.  * The following is a functional equivalent to the filecopy() routine that
  262.  * should work properly on any system using any compiler, albeit at the cost
  263.  * of reduced performance:
  264.  * 
  265.  * filecopy(f,t,size) 
  266.  *      FILE *f, *t; long size; 
  267.  * { 
  268.  *      while(size--)
  269.  *              putc_tst(fgetc(f),t); 
  270.  * }
  271.  */
  272. #if    MSDOS
  273. #include <fileio2.h>
  274.  
  275. filecopy(f, t, size)        /* bulk file copier */
  276.     FILE           *f, *t;    /* files from and to */
  277.     long            size;    /* bytes to copy */
  278. {
  279.     char           *buf;    /* buffer pointer */
  280.     char           *alloc();/* buffer allocator */
  281.     unsigned int    bufl;    /* buffer length */
  282.     unsigned int    coreleft();    /* space available reporter */
  283.     unsigned int    cpy;    /* bytes being copied */
  284.     long            floc, tloc, fseek();    /* file pointers, setter */
  285.     struct regval   reg;    /* registers for DOS calls */
  286.  
  287.     if ((bufl = coreleft()) < 1000)    /* see how much space we have */
  288.         abort("Out of memory");
  289.     bufl -= 1000;        /* fudge factor for overhead */
  290.     if (bufl > 60000)
  291.         bufl = 60000;    /* avoid choking alloc() */
  292.     if (bufl > size)
  293.         bufl = size;    /* avoid wasting space */
  294.     buf = alloc(bufl);    /* allocate our buffer */
  295.  
  296.     floc = fseek(f, 0L, 1);    /* reset I/O system */
  297.     tloc = fseek(t, 0L, 1);
  298.  
  299.     segread(®.si);    /* set segment registers for DOS */
  300.  
  301.     while (size > 0) {    /* while more to copy */
  302.         reg.ax = 0x3F00;/* read from handle */
  303.         reg.bx = filehand(f);
  304.         reg.cx = bufl < size ? bufl : size;    /* amount to read */
  305.         reg.dx = buf;
  306.         if (sysint21(®, ®) & 1)
  307.             abort("Read fail");
  308.  
  309.         cpy = reg.ax;    /* amount actually read */
  310.         reg.ax = 0x4000;/* write to handle */
  311.         reg.bx = filehand(t);
  312.         reg.cx = cpy;
  313.         reg.dx = buf;
  314.         sysint21(®, ®);
  315.  
  316.         if (reg.ax != cpy)
  317.             abort("Write fail (disk full?)");
  318.  
  319.         size -= (long) cpy;
  320.     }
  321.  
  322.     free(buf);        /* all done with buffer */
  323. }
  324. #else
  325.  
  326. void
  327. filecopy(f, t, size)        /* bulk file copier */
  328.     FILE           *f, *t;    /* files from and to */
  329.     long            size;    /* bytes to copy */
  330. {
  331.     char           *buf;    /* buffer pointer */
  332.     char           *malloc();    /* buffer allocator */
  333.     unsigned int    bufl;    /* buffer length */
  334.     unsigned int    cpy;    /* bytes being copied */
  335.  
  336.     bufl = 32760;
  337.     if (bufl > size)
  338.         bufl = size;    /* don't waste space */
  339.  
  340.     buf = malloc(bufl);
  341.  
  342.     while (size > 0) {
  343.         cpy = fread(buf, sizeof(char),
  344.             bufl < size ? bufl : (unsigned short) size, f);
  345.         if (fwrite(buf, sizeof(char), cpy, t) != cpy)
  346.             abort("Write fail (no space?)");
  347.         size -= cpy;
  348.     }
  349.  
  350.     free(buf);
  351. }
  352. #endif
  353. @
  354.  
  355.  
  356. 1.8
  357. log
  358. @Fixes from John Gilmore, message date Mon, 4 Jul 88 03:02:43 PDT
  359. @
  360. text
  361. @d2 1
  362. a2 1
  363.  * $Header: arcio.c,v 1.7 88/06/02 16:27:32 hyc Locked $
  364. d105 1
  365. a105 1
  366.     for (i = 0; i<4; hdr->size<<=8, hdr->size += dummy[16-i], i++);
  367. d109 1
  368. a109 1
  369.     for (i = 0; i<4; hdr->length<<=8, hdr->length += dummy[26-i], i++);
  370. @
  371.  
  372.  
  373. 1.7
  374. log
  375. @Minor fixes & speedups
  376. @
  377. text
  378. @d2 1
  379. a2 1
  380.  * $Header: arcio.c,v 1.6 88/06/01 19:26:50 hyc Locked $
  381. d56 1
  382. a56 1
  383.                 if (hdrver >= 0 && hdrver <= ARCVER)
  384. d74 1
  385. a74 1
  386.     if (hdrver < 0)
  387. d178 1
  388. a178 1
  389.     if (t)
  390. d181 2
  391. d187 1
  392. @
  393.  
  394.  
  395. 1.6
  396. log
  397. @Changed compilation conditionals
  398. @
  399. text
  400. @d2 1
  401. a2 1
  402.  * $Header: arcio.c,v 1.5 88/06/01 17:58:29 hyc Locked $
  403. d126 2
  404. a127 2
  405.     fputc(number & 255, f);
  406.     fputc(number >> 8, f);    /* FIX? cast to (char) ? */
  407. d135 2
  408. a136 2
  409.     put_int(number & 0xFFFF, f);
  410.     put_int(number >> 16, f);    /* FIX? cat to (short) ? */
  411. d277 2
  412. a278 2
  413. /* FIX? cast size to (unsigned int)? */
  414.         cpy = fread(buf, sizeof(char), bufl < size ? bufl : size, f);
  415. @
  416.  
  417.  
  418. 1.5
  419. log
  420. @Merge ARC 5.21 changes
  421. @
  422. text
  423. @d2 1
  424. a2 1
  425.  * $Header: arcio.c,v 1.4 88/06/01 15:40:40 hyc Locked $
  426. d22 1
  427. a22 1
  428. #ifdef MTS
  429. d34 1
  430. a34 1
  431. #ifndef MSDOS
  432. d80 1
  433. a80 1
  434. #ifdef MTS
  435. d96 1
  436. a96 1
  437. #ifdef MSDOS
  438. d102 1
  439. a102 1
  440. #ifdef MTS
  441. d148 1
  442. a148 1
  443. #ifdef MSDOS
  444. d152 1
  445. a152 1
  446. #ifdef MTS
  447. d179 1
  448. a179 1
  449. #ifdef BSD
  450. d206 1
  451. a206 1
  452. #ifdef MSDOS
  453. @
  454.  
  455.  
  456. 1.4
  457. log
  458. @Merge Atari ST code
  459. @
  460. text
  461. @d2 1
  462. a2 1
  463.  * $Header: arcio.c,v 1.3 88/06/01 14:46:33 hyc Locked $
  464. d5 15
  465. a19 14
  466. /*
  467.  * ARC - Archive utility - ARCIO
  468.  * 
  469.  * Version 2.49, created on 07/25/86 at 16:44:23
  470.  * 
  471.  * (C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
  472.  * 
  473.  * By:  Thom Henderson
  474.  * 
  475.  * Description: This file contains the file I/O routines used to manipulate an
  476.  * archive.
  477.  * 
  478.  * Language: Computer Innovations Optimizing C86
  479.  */
  480. d63 3
  481. @
  482.  
  483.  
  484. 1.3
  485. log
  486. @Fixed declarations
  487. @
  488. text
  489. @d2 1
  490. a2 1
  491.  * $Header: arcio.c,v 1.5 88/04/19 01:39:46 hyc Exp $
  492. d101 1
  493. a101 2
  494.     hdr->size = (long) ((dummy[16] << 24) + (dummy[15] << 16) + (dummy[14] << 8)
  495.                 + dummy[13]);
  496. d105 1
  497. a105 2
  498.     hdr->length = (long) ((dummy[26] << 24) + (dummy[25] << 16)
  499.                   + (dummy[24] << 8) + dummy[23]);
  500. d123 1
  501. a123 1
  502.     fputc(number >> 8, f);
  503. d132 1
  504. a132 1
  505.     put_int(number >> 16, f);
  506. d173 1
  507. d266 1
  508. a266 1
  509.     bufl = 60000;
  510. d273 1
  511. @
  512.  
  513.  
  514. 1.2
  515. log
  516. @added support for squashing. (changed max hdrver from 8 to 9)
  517. @
  518. text
  519. @d2 1
  520. a2 20
  521.  * $Log:    arcio.c,v $
  522.  * Revision 1.1  88/04/11  17:59:43  hyc
  523.  * Initial revision
  524.  * 
  525.  * Revision 1.3  87/12/19  04:25:24  hyc
  526.  * As before, different location tho.
  527.  * 
  528.  * Revision 1.2  87/12/19  04:23:21  hyc
  529.  * Fix problem caused by indent(?) screwing up line boundaries...
  530.  * 
  531.  * Revision 1.1  87/12/19  04:21:23  hyc
  532.  * Initial revision
  533.  * 
  534.  * Revision 1.4  87/08/13  17:03:24  hyc
  535.  * Run thru the indent program...
  536.  *  Revision 1.3  87/07/21  11:41:29  hyc *** empty
  537.  * log message ***
  538.  * 
  539.  * Revision 1.2  87/07/21  08:19:45  hyc *** empty log message ***
  540.  * 
  541. d21 3
  542. d25 4
  543. a28 1
  544. INT
  545. d35 1
  546. a35 1
  547.     INT             i, j, k;
  548. d37 3
  549. a39 3
  550.     char            name[13];    /* filename buffer */
  551.     INT             try = 0;/* retry counter */
  552.     static INT      first = 1;    /* true only on first read */
  553. d46 1
  554. a46 1
  555.     if (fgetc(f) != 26) {    /* check archive validity */
  556. d53 1
  557. a53 1
  558.             if (fgetc(f) == 26) {
  559. d55 1
  560. a55 1
  561.                 if (hdrver >= 0 && hdrver <= 9)
  562. d74 2
  563. a75 2
  564.     if (hdrver > 9) {
  565.         fread(name, sizeof(char), 13, f);
  566. d97 1
  567. a97 1
  568.     for (i = 0; i < 13; hdr->name[i] = dummy[i], i++);
  569. d101 1
  570. a101 1
  571.     hdr->size = (LONG) ((dummy[16] << 24) + (dummy[15] << 16) + (dummy[14] << 8)
  572. d106 1
  573. a106 1
  574.     hdr->length = (LONG) ((dummy[26] << 24) + (dummy[25] << 16)
  575. d119 1
  576. d121 1
  577. a121 1
  578.     INT             number;
  579. d128 1
  580. d130 1
  581. a130 1
  582.     LONG            number;
  583. d137 1
  584. a137 1
  585. INT
  586. d142 1
  587. a142 1
  588.     fputc(26, f);        /* write out the mark of ARC */
  589. d153 1
  590. a153 1
  591.     fwrite(hdr->name, 1, 13, f);
  592. d170 1
  593. a170 1
  594. INT
  595. d257 1
  596. d260 1
  597. a260 1
  598.     LONG            size;    /* bytes to copy */
  599. a265 3
  600. #ifdef MTS
  601. #define MTEXT    0x0010
  602. #endif
  603. a274 4
  604. #ifdef MTS
  605.         if ((f->_fmode & MTEXT) && !image)
  606.             etoa(buf, cpy);
  607. #endif
  608. @
  609.  
  610.  
  611. 1.1
  612. log
  613. @Initial revision
  614. @
  615. text
  616. @d3 3
  617. d68 1
  618. a68 1
  619.                 if (hdrver >= 0 && hdrver <= 8)
  620. d87 1
  621. a87 1
  622.     if (hdrver > 8) {
  623. d102 1
  624. a102 1
  625.         hdr->length = hdr->size;    /* size is same when not *
  626. @
  627.