home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 318 / utilsrc / arscan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  9.8 KB  |  308 lines

  1. /* Library function for scanning an archive file.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously
  32. and appropriately publish on each copy a valid copyright notice
  33. "Copyright (C) 1987 Free Software Foundation, Inc.", and include
  34. following the copyright notice a verbatim copy of the above disclaimer
  35. of warranty and of this License.
  36.  
  37.   2. You may modify your copy or copies of this source file or
  38. any portion of it, and copy and distribute such modifications under
  39. the terms of Paragraph 1 above, provided that you also do the following:
  40.  
  41.     a) cause the modified files to carry prominent notices stating
  42.     that you changed the files and the date of any change; and
  43.  
  44.     b) cause the whole of any work that you distribute or publish,
  45.     that in whole or in part contains or is a derivative of this
  46.     program or any part thereof, to be licensed at no charge to all
  47.     third parties on terms identical to those contained in this
  48.     License Agreement (except that you may choose to grant more extensive
  49.     warranty protection to some or all third parties, at your option).
  50.  
  51.     c) You may charge a distribution fee for the physical act of
  52.     transferring a copy, and you may at your option offer warranty
  53.     protection in exchange for a fee.
  54.  
  55. Mere aggregation of another unrelated program with this program (or its
  56. derivative) on a volume of a storage or distribution medium does not bring
  57. the other program under the scope of these terms.
  58.  
  59.   3. You may copy and distribute this program (or a portion or derivative
  60. of it, under Paragraph 2) in object code or executable form under the terms
  61. of Paragraphs 1 and 2 above provided that you also do one of the following:
  62.  
  63.     a) accompany it with the complete corresponding machine-readable
  64.     source code, which must be distributed under the terms of
  65.     Paragraphs 1 and 2 above; or,
  66.  
  67.     b) accompany it with a written offer, valid for at least three
  68.     years, to give any third party free (except for a nominal
  69.     shipping charge) a complete machine-readable copy of the
  70.     corresponding source code, to be distributed under the terms of
  71.     Paragraphs 1 and 2 above; or,
  72.  
  73.     c) accompany it with the information you received as to where the
  74.     corresponding source code may be obtained.  (This alternative is
  75.     allowed only for noncommercial distribution and only if you
  76.     received the program in object code or executable form alone.)
  77.  
  78. For an executable file, complete source code means all the source code for
  79. all modules it contains; but, as a special exception, it need not include
  80. source code for modules which are standard libraries that accompany the
  81. operating system on which the executable file runs.
  82.  
  83.   4. You may not copy, sublicense, distribute or transfer this program
  84. except as expressly provided under this License Agreement.  Any attempt
  85. otherwise to copy, sublicense, distribute or transfer this program is void and
  86. your rights to use the program under this License agreement shall be
  87. automatically terminated.  However, parties who have received computer
  88. software programs from you with this License Agreement will not have
  89. their licenses terminated so long as such parties remain in full compliance.
  90.  
  91.  In other words, you are welcome to use, share and improve this program.
  92.  You are forbidden to forbid anyone else to use, share and improve
  93.  what you give them.   Help stamp out software-hoarding!  */
  94.  
  95. #include <ar.h>
  96.  
  97. #ifdef atarist
  98.  
  99. #include <file.h>
  100. #include <types.h>
  101. #include <stat.h>
  102.  
  103. #else
  104.  
  105. #include <sys/file.h>
  106. #include <sys/types.h>
  107. #include <sys/stat.h>
  108.  
  109. #endif
  110.  
  111. /* Takes three arguments ARCHIVE, FUNCTION and ARG.
  112.  
  113.    Open the archive named ARCHIVE, find its members one by one,
  114.    and for each one call FUNCTION with the following arguments:
  115.      archive file descriptor for reading the data,
  116.      member name,
  117.      member header position in file,
  118.      member data position in file,
  119.      member data size,
  120.      member date,
  121.      member uid,
  122.      member gid,
  123.      member protection mode,
  124.      ARG.
  125.  
  126.    The descriptor is poised to read the data of the member
  127.    when FUNCTION is called.  It does not matter how much
  128.    data FUNCTION reads.
  129.  
  130.    If FUNCTION returns nonzero, we immediately return
  131.    what FUNCTION returned.
  132.  
  133.    Returns -1 if archive does not exist,
  134.    Returns -2 if archive has invalid format.
  135.    Returns 0 if have scanned successfully.  */
  136.  
  137. int
  138. ar_scan (archive, function, arg)
  139.      char *archive;
  140.      int (*function) ();
  141.      int arg;
  142. {
  143.   register int desc = open (archive, O_RDONLY, 0);
  144.   if (desc < 0)
  145.     {
  146.       return -1;
  147.     }
  148.   {
  149.     char buf[SARMAG];
  150.     register int nread = read (desc, buf, SARMAG);
  151.     if (nread != SARMAG || bcmp (buf, ARMAG, SARMAG))
  152.       {
  153.     close (desc);
  154.     return -2;
  155.       }
  156.   }
  157.  
  158.   /* Now find the members one by one.  */
  159.   {
  160.     register int member_offset = SARMAG;
  161.     while (1)
  162.       {
  163.     register int nread;
  164.     struct ar_hdr member_header;
  165.     char name [1 + sizeof member_header.ar_name];
  166.     int eltsize;
  167.     int eltmode;
  168.     int fnval;
  169.  
  170.     if (lseek (desc, member_offset, 0) < 0)
  171.       {
  172.         close (desc);
  173.         return -2;
  174.       }
  175.  
  176.     nread = read (desc, &member_header, sizeof (struct ar_hdr));
  177.     if (!nread) break;    /* No data left means end of file; that is ok */
  178.  
  179.     if (nread != sizeof (member_header)
  180.         || bcmp (member_header.ar_fmag, ARFMAG, 2))
  181.       {
  182.         close (desc);
  183.         return -2;
  184.       }
  185.     bcopy (member_header.ar_name, name, sizeof member_header.ar_name);
  186.     {
  187.       register char *p = name + sizeof member_header.ar_name;
  188.       while (p > name && *--p == ' ') *p = 0;
  189.     }
  190.  
  191.     sscanf (member_header.ar_mode, "%o", &eltmode);
  192.     eltsize = atoi (member_header.ar_size);
  193.  
  194.     fnval =
  195.       (*function) (desc, name, member_offset,
  196.                member_offset + sizeof (member_header), eltsize,
  197.                atoi (member_header.ar_date),
  198.                atoi (member_header.ar_uid),
  199.                atoi (member_header.ar_gid),
  200.                eltmode, arg);
  201.  
  202.     if (fnval)
  203.       {
  204.         close (desc);
  205.         return fnval;
  206.       }
  207.  
  208.     member_offset += sizeof (member_header) + eltsize;
  209.     if (member_offset & 1) member_offset++;
  210.       }
  211.   }
  212.  
  213.   close (desc);
  214.   return 0;
  215. }
  216.  
  217. static int
  218. ar_member_pos (desc, name, hdrpos, datapos, size, date, uid, gid, mode, mem)
  219.      int desc;
  220.      char *name;
  221.      int hdrpos, datapos, size, date, uid, gid, mode;
  222.      char *mem;
  223. {
  224.   if (strcmp (name, mem))
  225.     return 0;
  226.   return hdrpos;
  227. }
  228.  
  229. /* Set date of member MEMNAME in archive ARNAME to current time.
  230.    Returns 0 if successful,
  231.    -1 if file ARNAME does not exist,
  232.    -2 if not a valid archive,
  233.    -3 if other random system call error (including file read-only),
  234.    1 if valid but member MEMNAME does not exist.  */
  235.  
  236. ar_member_touch (arname, memname)
  237.      char *arname, *memname;
  238. {
  239.   register int pos = ar_scan (arname, ar_member_pos, memname);
  240.   register int fd;
  241.   struct ar_hdr ar_hdr;
  242.   register int i;
  243.   extern int errno;
  244.   struct stat statbuf;
  245.  
  246.   if (pos < 0)
  247.     return pos;
  248.   if (!pos)
  249.     return 1;
  250.  
  251.   fd = open (arname, O_RDWR, 0666);
  252.   if (fd < 0)
  253.     return -3;
  254.   /* Read in this member's header */
  255.   if (lseek (fd, pos, 0) < 0)
  256.     goto lose;
  257.   if (sizeof ar_hdr != read (fd, &ar_hdr, sizeof ar_hdr))
  258.     goto lose;
  259.   /* Write back the header, thus touching the archive file.  */
  260.   if (lseek (fd, pos, 0) < 0)
  261.     goto lose;
  262.   if (sizeof ar_hdr != write (fd, &ar_hdr, sizeof ar_hdr))
  263.     goto lose;
  264.   /* The file's mtime is the time we we want.  */
  265.   fstat (fd, &statbuf);
  266.   /* Advance member's time to that time */
  267.   for (i = 0; i < sizeof ar_hdr.ar_date; i++)
  268.     ar_hdr.ar_date[i] = ' ';
  269.   sprintf (ar_hdr.ar_date, "%d", statbuf.st_mtime);
  270.   /* Write back this member's header */
  271.   if (lseek (fd, pos, 0) < 0)
  272.     goto lose;
  273.   if (sizeof ar_hdr != write (fd, &ar_hdr, sizeof ar_hdr))
  274.     goto lose;
  275.   close (fd);
  276.   return 0;
  277.  
  278.  lose:
  279.   i = errno;
  280.   close (fd);
  281.   errno = i;
  282.   return -3;
  283. }
  284.  
  285. #ifdef TEST
  286.  
  287. int
  288. describe_member (desc, name, hdrpos, datapos, size, date, uid, gid, mode)
  289.      int desc;
  290.      char *name;
  291.      int hdrpos, datapos, size, date, uid, gid, mode;
  292. {
  293.   printf ("Member %s: %d bytes at %d (%d).\n", name, size, hdrpos, datapos);
  294.   printf ("  Date %s", ctime (&date));
  295.   printf ("  uid = %d, gid = %d, mode = 0%o.\n", uid, gid, mode);
  296.   return 0;
  297. }
  298.  
  299. main (argc, argv)
  300.      int argc;
  301.      char **argv;
  302. {
  303.   ar_scan (argv[1], describe_member);
  304.   return 0;
  305. }
  306.  
  307. #endif TEST
  308.