home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / util / conv / LSFileCut.lha / filecut.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-18  |  1.6 KB  |  86 lines

  1. #include    <stdio.h>
  2. #include    <string.h>
  3. #include    <stdlib.h>
  4. #include    <clib/dos_protos.h>
  5.  
  6. #define    BUFFSIZE 200000UL
  7.  
  8. void    main(int argc, char *argv[])
  9. {
  10.     BPTR    fhi, fho;
  11.     long    pos,bytes;
  12.     char    buffer[BUFFSIZE];
  13.     long    allbytes=0;
  14.     long    fr=0;
  15.     
  16.     if(argc<5)
  17.     {
  18.         printf("usage:\n");
  19.         printf("      LSFileCut <source filename> <target filename> <position> <bytes>\n");
  20.         exit(0);
  21.     }
  22.     
  23.  
  24.     if(strchr(argv[3],'x')!=NULL) sscanf(argv[3],"%x",&pos);
  25.     else                              pos=atol(argv[3]);
  26.     if(strchr(argv[4],'x')!=NULL) sscanf(argv[4],"%x",&bytes);
  27.     else                              bytes=atol(argv[4]);
  28.  
  29.     printf("FileCut v1.05 by LouiSe 1999\n\n");
  30.     printf("Input file   : %s\n",argv[1]);
  31.     printf("Output file  : %s\n",argv[2]);
  32.     printf("Cut position : %lu\n",pos);
  33.     printf("Bytes to Cut : %lu\n",bytes);
  34.  
  35.     if((fhi=Open(argv[1],MODE_OLDFILE))==NULL)
  36.     {
  37.         printf("*** couldn't open input file: []\n",argv[1]);
  38.         exit(-1);
  39.     }
  40.     else
  41.         {
  42.             if((fho=Open(argv[2],MODE_NEWFILE))==NULL)
  43.             {
  44.                 printf("*** couldn't open output file: []\n",argv[2]);
  45.                 exit(-2);
  46.             }
  47.             else
  48.                 {
  49.                     allbytes=0;
  50.                     Seek(fhi,pos,OFFSET_BEGINNING);
  51.                     while(1)
  52.                     {
  53.                         fr=Read(fhi,buffer,sizeof(buffer));
  54.                         if(fr<=0) break;
  55.                         allbytes=allbytes+fr;
  56.                         // printf("fr=%lu, allbytes=%lu\n",fr,allbytes);
  57.  
  58.                         if(allbytes<bytes)
  59.                         {
  60.                             Write(fho,buffer,fr);
  61.                         }
  62.                         else
  63.                             {
  64.                                 if(fr>bytes)
  65.                                 {
  66.                                     Write(fho,buffer,bytes);
  67.                                 }
  68.                                 else
  69.                                     {
  70.                                         Write(fho,buffer,allbytes-bytes);
  71.                                     }
  72.                                     break;
  73.                             }
  74.                     }
  75.                     
  76.                     Close(fhi);
  77.                     Close(fho);
  78.                 }
  79.     
  80.         }
  81.  
  82. }
  83.  
  84.  
  85.  
  86.