From: vpc1156@tamsun.tamu.edu (Victor Paul Campano)
Subject: Help me with this code?
Organization: Texas A&M University, College Station
Date: Tue, 28 Jul 1992 21:59:44 GMT

Hello.  I wrote the following code to combine several binary files together
into an uncompressed archive and extract them later.  It works, except that
executable files that get archived and then de-archived dont want to execute
anymore.  Obviously this is some aspect of AmigaDOS I'm just not familiar
with...

Could someone help me?

Thanks,

Thom

***************************************************************************
I am posting this for my friend, Thom, who lost USENET access recently.
Replys to him may be made though this net address.
***************************************************************************


#include <exec/types.h>
#include <stdio.h>
#include <exec/exec.h>
#include <exec/memory.h>
#include <fcntl.h>


#define	BUFFER_SIZE	1024

void main(argc,argv)
int argc;
char *argv[];
{

int input,output,i,j;
long size;
char filename[80],data[BUFFER_SIZE];

if (argc != 3 || (toupper(argv[1][0]) != 'A' &&
		  toupper(argv[1][0]) != 'L' &&
		  toupper(argv[1][0]) != 'X')) {
   printf("USAGE:combine [alx] file1\n\n");
   exit(30);
   }

if (toupper(argv[1][0]) == 'A') {  /* add files to one big file */

   if ((output=(int)open(argv[2], O_WRONLY | O_CREAT | O_APPEND))==-1) {
      printf("USAGE:combine [alx] file1\n\n");
      exit(30);
      }
   
   while(TRUE) {
      scanf("%s",filename);

      if ((input=(int)open(filename, O_RDONLY))==-1)

         break;


      write(output,filename,strlen(filename) + 1);  /* write filename + null */
      lseek(input,0,2);   /* goto bottom,  */
      size = tell(input); /* and get size. */      
      lseek(input,0,0);   /* goto top.  */
      write(output,(char *)&size,4); /* write out size of file */
      while (size > 0) {
         if (size > BUFFER_SIZE) {
            read(input,data,BUFFER_SIZE);
            write(output,data,BUFFER_SIZE);
	    size -= BUFFER_SIZE;
            }
         else {
            read(input,data,size);
            write(output,data,size);
	    size -= size;
            }
         }
      close(output);
      }
   close(input);
   }
else if (toupper(argv[1][0]) == 'X') {		/* split files out of archive */

   if ((input=(int)open(argv[2], O_RDONLY))==-1) {
      printf("USAGE:combine [alx] file1\n\n");
      exit(30);
      }
   
   while(TRUE) {
      i = j = 0;
      do {  /* get the next filename in the archive */
         if (read(input,&(filename[i]),1) < 1)
	    goto exit_loop1;
         } while (filename[i++] != 0);

      if ((output=(int)open(filename, O_TRUNC | O_WRONLY | O_CREAT))==-1)
         break;

      read(input,(char *) &size,4);  /* read in length */

      while (size > 0) {
         if (size > BUFFER_SIZE) {
            read(input,data,BUFFER_SIZE);
            write(output,data,BUFFER_SIZE);
	    size -= BUFFER_SIZE;
            }
         else {
            read(input,data,size);
            write(output,data,size);
	    size -= size;
            }
         }
      close(output);
      }
   exit_loop1:
   close(input);
   }

else {		/* show files in archive */

   if ((input=(int)open(argv[2], O_RDONLY))==-1) {
      printf("USAGE:combine [alx] file1\n\n");
      exit(30);
      }

   printf("  Filename                                    Size\n");
   while(TRUE) {
      i = j = 0;
      do {  /* get the next filename in the archive */
         if (read(input,&(filename[i]),1) < 1)
	    goto exit_loop2;
         } while (filename[i++] != 0);


      read(input,(char *) &size,4);  /* read in length */

      printf("%-45.45s %-d\n",filename,size);

      lseek(input,size,1); /* skip to next entry */

      }
   exit_loop2:
   close(input);
   }




}