home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / util / cli / splitter / splitter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-21  |  7.9 KB  |  331 lines

  1. /*
  2.  *  Splitter
  3.  *
  4.  *  A small program to split files in several protions with a defined
  5.  *  size or number and join them together again.
  6.  *
  7.  *  Version 1.0 on 18-Oct-1993   Fully reliable version.
  8.  *  Version 1.1 on 18-Oct-1993   Replaced fclose()/fopen() by rewind().
  9.  *  Version 1.2 on 24-Nov-1993   Works now with MSDOS systems;
  10.  *                               file buffer extended.
  11.  *  Version 1.21 on 25-Nov-1993  Removed a small bug in the output.
  12.  *
  13.  *
  14.  *  Usage: Splitter <file> [<spath>] [-j] [-p n] [-s n] [-d]
  15.  *
  16.  *   file  the file to split
  17.  *   spath optional path of the splitted parts
  18.  *   -j    join splitted parts
  19.  *   -p    split into n portions
  20.  *   -s    split into protions of n bytes size each
  21.  *   -d    MSDOS option, uses extension for marking and "\" for paths
  22.  *
  23.  *
  24.  *  DISCLAIMER: As usual, I can't take any responsibility for anything
  25.  *              that might happen to you or your computer when you use
  26.  *              this program.
  27.  *
  28.  *  This is freeware, i.e. you may give this program to anyone you like
  29.  *  and upload it to any mailbox, bulletin board or software server as
  30.  *  long as you leave this file as it is. If you have any ideas on how
  31.  *  to advance the reliability or funtionality of this program, please
  32.  *  let me know. But you may not use this code for anything commercial
  33.  *  nor are you allowed to distibute any changed version on your self.
  34.  *
  35.  */
  36.  
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40.  
  41. #define info "\nSplitter V1.21 on 11/25/1993\n\
  42. (c)1993 by Martin Schlodder\n\n\
  43. Usage: Splitter <file> [<dest>] [-j] [-p n] [-s n] [-d]\n\
  44.  file  the file to split\n\
  45.  spath optional path of the splitted parts\n\
  46.  -j    join splitted path\n\
  47.  -p    split into n portions\n\
  48.  -s    split into protions of n bytes size each\n\
  49.  -d    MSDOS option, uses extension for marking and \"\\\" for paths\n\n\
  50. Address:\n\
  51.  Martin Schlodder\n\
  52.  Uhlandstr. 18\n\
  53.  D 72336 Balingen\n\n\
  54. Internet:\n\
  55.  schlodder@student.uni-tuebingen.de\n"
  56. #define nofile "\nPlease give me the name of the file you want me to split.\n"
  57. #define lessarg "\nPlease give me at least the filename and one argument,\n\
  58. or shall i guess what to do?\n"
  59. #define errinparg "\nPlease give me the number of protions in the form of '-p 7' or '-s12'.\n"
  60. #define errinsarg "\nPlease give me the size of the protions in the form '-s 12000' or '-s880000'.\n"
  61. #define wrongarg "\nI don't know this option.\n"
  62. #define noarg "\nPlease give me one (or more) of the three options '-j', '-p' or '-s'.\n"
  63. #define wrongfile "\nCouldn't open file.\n"
  64. #define nooutfile "\nCouldn't open output file!\n"
  65. #define seekerr "\nCouldn't split file because i couldn't ascertain the file's size!\n"
  66. #define fileempty "\nFile not splitted. Why should i split an ampty file?\n"
  67.  
  68. #ifndef SEEK_END
  69. #define SEEK_END 2
  70. #endif
  71.  
  72. char sfile[257],dfile[257],ext[257];
  73. char *version="$VER: Splitter 1.21 (25.11.93)   ©1993 by Martin Schlodder";
  74. char tmp[257];
  75. short fbuf[16384];
  76.  
  77. main(int argc,char **argv){
  78.   FILE *sfh=NULL,*dfh=NULL;
  79.   char *filename=0,*pathname=0;
  80.   char *name;
  81.   int portions=0,i;
  82.   long size=0,j;
  83.   long dos=0;
  84.   long join=0;
  85.   int c=0;
  86.  
  87.   switch(argc){
  88.   case 1:
  89.     puts(info);
  90.     exit(5);
  91.   case 2:
  92.     if(strchr(argv[1],'?')){
  93.       puts(info);
  94.       exit(0);
  95.     }
  96.     else if(strstr(argv[1],"-h")){
  97.       puts(info);
  98.       exit(0);
  99.     }
  100.     puts(lessarg);
  101.     exit(5);
  102.   }
  103.  
  104.   for(i=1;i<argc;++i){
  105.     if(argv[i][0]!='-'){
  106.       if(!filename)
  107.     filename=argv[i];
  108.       else
  109.     pathname=argv[i];
  110.     }else
  111.       switch(argv[i][1]){
  112.       case 'j':
  113.     join=1;
  114.     break;
  115.       case 'p':
  116.     if(argv[i][2]!=0){
  117.       if(!(portions=atoi(argv[i]+2))){
  118.         puts(errinparg);
  119.         exit(5);
  120.       }
  121.     }
  122.     else if(!(portions=atoi(argv[++i]))){
  123.       puts(errinparg);
  124.       exit(5);
  125.     }
  126.     break;
  127.       case 's':
  128.     if(argv[i][2]!=0){
  129.       if(!(size=atol(argv[i]+2))){
  130.         puts(errinsarg);
  131.         exit(5);
  132.       }
  133.     }
  134.     else if(!(size=atol(argv[++i]))){
  135.       puts(errinsarg);
  136.       exit(5);
  137.     }
  138.     break;
  139.       case 'd':
  140.     dos=1;
  141.     break;
  142.       default:
  143.     puts(wrongarg);
  144.     exit(5);
  145.       }
  146.   }
  147.  
  148.   if(!filename){
  149.     puts(nofile);
  150.     exit(5);
  151.   }
  152.   strcpy(sfile,filename);
  153.   i=0;
  154.   if(pathname){
  155.     strcpy(dfile,pathname);
  156.     i=strlen(dfile);
  157.     if(i&&(dfile[i-1]!=':'))
  158.       if(dos){
  159.     if(dfile[i-1]!='\\')
  160.       dfile[i++]='\\';
  161.       }else
  162.     if(dfile[i-1]!='/')
  163.       dfile[i++]='/';
  164.     if(dos)
  165.       name=(char *)strrchr(sfile,'\\');
  166.     else
  167.       name=(char *)strrchr(sfile,'/');
  168.     if(!name)
  169.       name=(char *)strchr(sfile,':');
  170.     if(name)
  171.       strcpy(dfile+i,name+1);
  172.     else
  173.       strcpy(dfile+i,sfile);
  174.   }else
  175.     strcpy(dfile,sfile);
  176.   ext[0]=0;
  177.   if(name=(char *)strrchr(dfile,'.')){
  178.     if(name>strrchr(dfile,':')){
  179.       if(dos)
  180.     i=(name>(char *)strrchr(dfile,'\\'));
  181.       else
  182.     i=(name>(char *)strrchr(dfile,'/'));
  183.       if(i){
  184.     if(!dos)
  185.       strcpy(ext,name);
  186.     *name=0;
  187.       }
  188.     }
  189.   }
  190.   
  191.   if(!(join||size||portions)){
  192.     puts(noarg);
  193.     exit(5);
  194.   }
  195.  
  196.   if(!join){
  197.     if(size){
  198.       if(!(sfh=fopen(sfile,"rb"))){
  199.     puts(wrongfile);
  200.     exit(5);
  201.       }
  202.       setvbuf(sfh,(char *)fbuf,_IOFBF,sizeof(fbuf));
  203.       if(portions){
  204.     for(i=1;(i<=portions)&&c!=EOF;++i){
  205.       if(dos)
  206.         sprintf(tmp,"%s.%d",dfile,i);
  207.       else
  208.         sprintf(tmp,"%s_%d%s",dfile,i,ext);
  209.       if(!(dfh=fopen(tmp,"wb"))){
  210.         fclose(sfh);
  211.         puts(nooutfile);
  212.         exit(20);
  213.       }
  214.       for(j=0;(j<size)&&c!=EOF;++j){
  215.         c=fgetc(sfh);
  216.         if(c!=EOF)
  217.           fputc((char)c,dfh);
  218.       }
  219.       fclose(dfh);
  220.     }
  221.     if(feof(sfh))
  222.       j--;
  223.     if(i==2)
  224.       printf("Splitted file %s into one portion with %ld Bytes.\n",sfile,j);
  225.     else if(j==size)
  226.       printf("Splitted file %s into %d portions with %ld Bytes.\n",sfile,i-1,size);
  227.     else
  228.       printf("Splitted file %s into\n  - %d portion%s with %ld Bytes and\n  - one portion with %ld Bytes.\n",sfile,i-2,(i==3)?"":"s",size,j);
  229.       }else{
  230.     for(i=1;c!=EOF;++i){
  231.       if(dos)
  232.         sprintf(tmp,"%s.%d",dfile,i);
  233.       else
  234.         sprintf(tmp,"%s_%d%s",dfile,i,ext);
  235.       if(!(dfh=fopen(tmp,"wb"))){
  236.         fclose(sfh);
  237.         puts(nooutfile);
  238.         exit(20);
  239.       }
  240.       for(j=0;(j<size)&&c!=EOF;++j){
  241.         c=fgetc(sfh);
  242.         if(c!=EOF)
  243.           fputc((char)c,dfh);
  244.       }
  245.       fclose(dfh);
  246.     }
  247.     if(i==2)
  248.       printf("Splitted file %s into one portion with %ld Bytes.\n",sfile,j-1);
  249.     else
  250.       printf("Splitted file %s into\n  - %d portion%s with %ld Bytes and\n  - one portion with %ld Bytes.\n",sfile,i-2,(i==3)?"":"s",size,j-1);
  251.       }
  252.       fclose(sfh);
  253.     }else{
  254.       if(!(sfh=fopen(sfile,"rb"))){
  255.     puts(wrongfile);
  256.     exit(5);
  257.       }
  258.       setvbuf(sfh,(char *)fbuf,_IOFBF,sizeof(fbuf));
  259.       if(fseek(sfh,0L,SEEK_END)){
  260.     puts(seekerr);
  261.     exit(20);
  262.       }
  263.       if(!(size=ftell(sfh))){
  264.     puts(fileempty);
  265.     exit(5);
  266.       }
  267.       rewind(sfh);
  268.       size=(size+portions)/portions;
  269.       for(i=1;i<=portions;++i){
  270.     if(dos)
  271.       sprintf(tmp,"%s.%d",dfile,i);
  272.     else
  273.       sprintf(tmp,"%s_%d%s",dfile,i,ext);
  274.     if(!(dfh=fopen(tmp,"wb"))){
  275.       fclose(sfh);
  276.       puts(nooutfile);
  277.       exit(20);
  278.     }
  279.     for(j=0;(j<size)&&c!=EOF;++j){
  280.       c=fgetc(sfh);
  281.       if(c!=EOF)
  282.         fputc((char)c,dfh);
  283.     }
  284.     fclose(dfh);
  285.       }
  286.       fclose(sfh);
  287.       j--;
  288.       if(portions==1)
  289.     printf("Splitted file %s into one portion with %ld Bytes.\n",sfile,j);
  290.       else if(j==size)
  291.     printf("Splitted file %s into %d portions with %ld Bytes.\n",sfile,portions,size);
  292.       else
  293.     printf("Splitted file %s into\n  - %d portion%s with %ld Bytes and\n  - one portion with %ld Bytes.\n",sfile,portions-1,(portions==2)?"":"s",size,j);
  294.     }
  295.   }else{
  296.     if(!(sfh=fopen(sfile,"wb"))){
  297.       puts(wrongfile);
  298.       exit(5);
  299.     }
  300.     printf("Joined ");
  301.     setvbuf(sfh,(char *)fbuf,_IOFBF,sizeof(fbuf));
  302.     for(i=1;i>0;++i){
  303.       if(dos)
  304.     sprintf(tmp,"%s.%d",dfile,i);
  305.       else
  306.     sprintf(tmp,"%s_%d%s",dfile,i,ext);
  307.       if(!(dfh=fopen(tmp,"rb"))){
  308.     fclose(sfh);
  309.     i=-1;
  310.       }
  311.       else{
  312.     printf("'%s' ",tmp);
  313.     if(fseek(dfh,0L,SEEK_END)){
  314.       puts(seekerr);
  315.       exit(20);
  316.     }
  317.     if(!(size=ftell(dfh))){
  318.       puts(fileempty);
  319.       exit(5);
  320.     }
  321.     rewind(dfh);
  322.     for(j=0;(j<size);++j)
  323.       fputc(fgetc(dfh),sfh);
  324.     fclose(dfh);
  325.       }
  326.     }
  327.     fclose(sfh);
  328.     printf("to '%s'.\n",sfile);
  329.   }
  330. }
  331.