home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / c / chop1292.zip / JOIN.C < prev    next >
C/C++ Source or Header  |  1992-12-18  |  16KB  |  506 lines

  1. /* This program joins the smaller files created by the CHOP program
  2.    back into the original input file
  3.  
  4.    USAGE: JOIN FILENAME [INPATH] -SWITCHES
  5.    Version 1.02.  Written by Edgar Swank using some code by W. J. Kennamer
  6.                   and released into the public domain.
  7.       Note: this source is for IBM/MS C Compiler.
  8.             Compile with HUGE memory model /AH.
  9.      FILENAME is any valid MS-DOS filename.  Wildcards are not supported.
  10.      This will be the single output filename.
  11.      Input file names will be FILENAME.1, FILENAME.2, etc.
  12.      Program will stop when FILENAME.n is not found, or when Esc is entered
  13.      in response to prompt from -w option.
  14.      Terminating <ctrl>Z is removed from Input files
  15.      UNLESS -b is specified.
  16.  
  17.      Terminating <ctrl>Z is added to Output files       <11/7/91>
  18.      ONLY if -e is specified AND -b is NOT specified.
  19.  
  20.      INPATH is an optional disk/pathname (A: or \path\ or a:\path\)
  21.      to get input files on a different disk and/or directory from output file.
  22.      SWITCHES must follow the FILENAME and INPATH parameters,
  23.        and may be entered in any order.  Switches may be combined.
  24.      Valid SWITCHES are:
  25.        -b  binary    ║ do not remove ^z from input files.
  26.        -e  eof       ║ add ^z to output file.
  27.                      ║ Note: may interfere with appending
  28.                      ║ to output file.
  29.        -w  wait      ║ wait before opening each input file.
  30.                      ║  can change input diskette if separate from output.
  31.  
  32. File pointer fp1 is the output file.  fp2 is always the input
  33.    file, though its name changes as new input files are
  34.    opened.
  35.  
  36. */
  37.  
  38. #include  <fcntl.h>
  39. #include  <dos.h>
  40. #include  <stdio.h>
  41. #include  <conio.h>
  42. #include  <ctype.h>
  43. #include  <string.h>
  44. #include  <malloc.h>
  45. #include  <stdlib.h>
  46.  
  47. #define VERSION "1.02"
  48. #define PROGNAME "JOIN"
  49. #define CTRL_Z   0x1a
  50. #define VOID     int
  51. #define CR       0x0d
  52. #define LF       0x0a
  53. #define ESC      0x1b
  54. #define OFF   0
  55. #define ON   1
  56.  
  57. int binary = OFF;                   /* do not remove EOF                */
  58. int wait   = OFF;                   /* Wait before each input file open */
  59. int weof   = OFF;                   /* Write EOF ^z to output file      */
  60. int c;                              /* current character                */
  61. int j;                              /* index                            */
  62. int i,u;
  63. long inpleft;                       /* Bytes input left to read         */
  64. long inplimit;                      /* Bytes input left to read low lim */
  65. long bfsiz;                         /* I/O buffer size                  */
  66. long bread;                         /* Bytes read into buffer           */
  67. long pbread;                        /* Bytes read this partition        */
  68. long bwrit;                         /* Bytes written from buffer        */
  69. long pbwrit;                        /* Bytes written this partition     */
  70.  
  71. long size = 0;                      /* partition value -- file size     */
  72. long byte_count = 0;                /* total bytes in original file     */
  73. long inpfz = 0;                     /* total bytes in original file     */
  74. long out_file_size = 0;             /* total bytes in output file       */
  75.  
  76. char *bfpa[20];                     /* I/O buffer pointer array         */
  77.  
  78. FILE *fp1,*fp2;                     /* fp1=output, fp2=input            */
  79. int derno,erno;
  80.  
  81.  int bfx;                           /* buffer array index               */
  82.  int num_input_files = 0;           /* no of files successfully read    */
  83.  int length;                        /* length of matching string        */
  84.  int int_size;                      /* integer for file size            */
  85.  int nmb;                           /* number of 16k blocks IO          */
  86.  unsigned int  w;
  87.  
  88.  char *p;                           /* pointer to switch string         */
  89.  char *period_p;                    /* location of period in filename   */
  90.  char *input_file;                  /* input file name                  */
  91.  char filename[80];                 /* original file name               */
  92.  char fnwork[80];                   /* workarea for file name           */
  93.  char outfilename[80];              /* output file name                 */
  94.  
  95.  char inpath[64];                   /* optional input disk/path         */
  96.  char line1[80];                    /* data buffer                      */
  97.  char line2[30];                    /* data buffer                      */
  98.  char *switches;                    /* string to hold switches          */
  99.  
  100.  VOID help();
  101.  VOID close_out();
  102.  VOID delete_files();
  103.  VOID block_read();
  104.  VOID block_write();
  105.  VOID copy_to_eol();
  106.  VOID construct_input_filename();
  107.  VOID Open_Next_Input_File();
  108.  VOID set_switches();
  109.  
  110. main(argc,argv)
  111. int argc;
  112. char *argv[];
  113.    {
  114.  
  115.  
  116.    /* test code to prints args **********
  117.    printf("ARGC=%d\n",argc);
  118.    for (j=0;j<argc ;j++ )
  119.    {
  120.    printf("ARGV[%d]=%s\n",j,argv[j]);
  121.    }
  122.    ***************************************/
  123.    bfsiz=0;
  124.    for (nmb=1;nmb<=20 ;nmb++ )
  125.    {
  126.    bfpa[nmb-1]=malloc(16384);
  127.    if (bfpa[nmb-1]==NULL)
  128.     {
  129.    break;
  130.     }
  131.     else
  132.     {
  133.    bfsiz=(long)nmb*16384;
  134.     }
  135.    } /*for (nmb=1;nmb<=10 ;nmb++ )*/
  136.    nmb--;
  137.    if (nmb==0)
  138.    {
  139.     printf ("Insufficient Memory for I/O Buffer.\n");
  140.     exit(8);
  141.    }
  142.    else
  143.     printf ("%ld (%u*16384) bytes allocated for I/O Buffers.\n",bfsiz,nmb);
  144.  
  145.  
  146.    if( argc > 4 || argc < 2 || strcmp(argv[1],"-?")==0
  147.                             || strcmp(argv[2],"-?")==0
  148.                             || strcmp(argv[3],"-?")==0 )
  149.       {
  150.       help();
  151.       exit(1);
  152.       }
  153.  
  154.    /* see which argument is the switch */
  155.    switches = line2;
  156.    strcpy(switches,"");
  157.    for (j=2;j<argc;j++)
  158.    {
  159.    if( argv[j][0]=='-')
  160.       {
  161.       strcpy(switches,argv[j]);
  162.       break;
  163.       }
  164.     } /*for*/
  165.     if (j>2)
  166.      strcpy(inpath,argv[2]);
  167.     else
  168.      inpath[0]=0;
  169.  
  170.      set_switches();
  171.  
  172.      strcpy(filename,argv[1]);
  173.      strcpy(outfilename,argv[1]);
  174.  
  175.      period_p = strchr(filename,'.');         /* locate the period, if any */
  176.  
  177.      if( period_p != NULL )
  178.         *period_p = '\0';                     /* chop off the period  */
  179.  
  180.      if (inpath[0]!=0)                       /* if inpath specified */
  181.        construct_input_filename();
  182.  
  183.      Open_Next_Input_File();
  184.  
  185.      if( (fp1 = fopen(argv[1],"wb") ) == NULL)
  186.         {
  187.         printf("Cannot open %s for output.\n",argv[1]);
  188.         printf("Exiting the program.\n");
  189.         exit(1);
  190.         }
  191.       else
  192.        printf ("Output file Open\n");
  193.  
  194.  
  195.    if (binary==ON)
  196.     inplimit=0;
  197.    else
  198.     inplimit=3;
  199.    bread=0;pbread=0;w=16384;
  200.    bwrit=0;pbwrit=0;bfx=0;
  201.    while (1)
  202.    {
  203.    while (inpleft>inplimit)
  204.     {
  205.       block_read();
  206.  
  207.       block_write();
  208.  
  209.          if( inpleft == 0 )
  210.             {
  211.             Open_Next_Input_File();
  212.             }
  213.        bfx=0;bread=0;w=16384;
  214.        bwrit=0;
  215.       } /*while (inpleft>inplimit)*/
  216.    while(1)
  217.     {
  218.       c = getc(fp2);
  219.  
  220.       if( c == EOF )
  221.          {
  222.          fclose(fp2);
  223.          break;
  224.          }
  225.      inpleft--;
  226.      if (binary==ON || c != CTRL_Z)
  227.      {
  228.       putc(c,fp1);
  229.      }
  230.     } /*while(1)*/
  231.    Open_Next_Input_File();
  232.    } /*while (1) (Exits thru Open-Next_Input_File)*/
  233.    } /*main(argc,argv)*/
  234. /***************************************************************************/
  235.   VOID Open_Next_Input_File()
  236.      {
  237.       (num_input_files)++;
  238.       if(num_input_files > 999)
  239.          {
  240.          printf("Too many files--%d.\nExiting program\n.",num_input_files);
  241.          exit(1);
  242.          }
  243.  
  244.       input_file = line1;                /* initialize input_file pointer */
  245.  
  246.       /* create the next filename */
  247.       sprintf(input_file,"%s.%-d",filename,num_input_files);
  248.       while (1)
  249.       {
  250.       if (wait==ON)
  251.        {
  252.        while (1)
  253.         {
  254.          printf("Ready to read File %s. \n\
  255. Press Return to continue or Esc to Stop\n",input_file);
  256.          c=getch();
  257.          if (c==ESC)
  258.           {
  259.            close_out(fp1,outfilename);
  260.            printf("%d files joined.\n",(num_input_files) - 1 );
  261.            delete_files();
  262.            exit(1);
  263.           } /* if (c==ESC)*/
  264.          if (c==CR) break;
  265.         } /* while (1)*/
  266.        } /* if (wait==ON)*/
  267.  
  268.       fp2 = fopen(input_file,"rb");
  269.       if (fp2 == NULL)
  270.          {
  271.           printf("Cannot open %s for input\n",input_file);
  272.           if (wait==OFF)
  273.            {
  274.             printf("Exiting the program.\n");
  275.             close_out(fp1,outfilename);
  276.             printf("%d files joined.\n",(num_input_files) - 1 );
  277.             delete_files();
  278.             exit(1);
  279.            } /* if (wait==OFF) */
  280.           else continue;
  281.          } /* if (fp2 == NULL) */
  282.       if( fseek(fp2,0L,2) == -1)            /* position at EOF */
  283.          {
  284.           printf("Error seeking end of input file %s.\n",input_file);
  285.           exit(1);
  286.          }
  287.       break;
  288.       } /* while (1) */
  289.  
  290.       inpfz = ftell(fp2);                   /* tell EOF  */
  291.       inpleft=inpfz;
  292.       rewind(fp2);                          /* back to beginning of file */
  293.       printf("  Input file %s size = %ld bytes\n",input_file,inpfz);
  294.      } /*VOID Open_Next_Input_File()*/
  295. /***************************************************************************/
  296.  VOID set_switches()
  297.   {
  298.    for( p = switches ; (p - switches) < strlen(switches)  ; p++ )
  299.       {
  300.        if( *p == 'b' )
  301.          {
  302.          binary = ON;
  303.          weof   = OFF;
  304.          }
  305.       else if ( *p == 'w')
  306.          {
  307.          wait = ON;
  308.          }
  309.       else if ( *p == 'e' && binary == OFF)
  310.          {
  311.          weof = ON;
  312.          }
  313.       } /*for( p = switches ...*/
  314.   } /*VOID set_switches()*/
  315. /***************************************************************************/
  316.  VOID construct_input_filename()
  317.     {
  318.      p=inpath+strlen(inpath)-1;           /* ensure inpath properly terminated*/
  319.      if (*p!='\\' && *p!=':')
  320.       {
  321.        *(p+1)='\\';
  322.        *(p+2)=0;
  323.       }
  324.  
  325.      if (filename[1]==':')                  /* strip disk/path from input filename*/
  326.       {
  327.       strcpy(fnwork,(filename+2));
  328.       strcpy(filename,fnwork);
  329.       }
  330.     for (p=filename+strlen(filename);p>=filename;p--)
  331.      {
  332.       if (*p=='\\')
  333.        {
  334.        strcpy(fnwork,p+1);
  335.        strcpy(filename,fnwork);
  336.        break;
  337.        } /*if*/
  338.      } /* for (p=filename+strlen(filename),p>=filename,p--)*/
  339.      if (strlen(inpath)+strlen(filename)<75)
  340.      {
  341.      strcpy(fnwork,inpath);
  342.      strcat(fnwork,filename);
  343.      strcpy(filename,fnwork);
  344.      }
  345.      else
  346.       {
  347.       printf("Syntax Error generating input filename\n");
  348.       exit(8);
  349.       }
  350.     } /*VOID construct_input_filename()*/
  351. /***************************************************************************/
  352. VOID block_read()
  353.    {
  354.       while (bread<bfsiz && inpleft>inplimit && w==16384)
  355.       {
  356.        if (bfsiz>=bread+16384 && inpleft>inplimit+16384)
  357.         {
  358.         w=fread(bfpa[bfx],1,16384,fp2);
  359.         }
  360.        else
  361.        if (inpleft-inplimit>bfsiz-bread)
  362.        {
  363.         w=fread(bfpa[bfx],1,(int)(bfsiz-bread),fp2);
  364.        }
  365.        else
  366.        {
  367.         w=fread(bfpa[bfx],1,(int)(inpleft-inplimit),fp2);
  368.        }
  369.        if (ferror(fp2))
  370.         {
  371.          printf("File read error.\n");
  372.          exit(8);
  373.         }
  374.        bfx++;
  375.        bread+=(long)w;
  376.        pbread+=(long)w;
  377.        inpleft-=(long)w;
  378.       } /* while (pbread<size && bread<bfsiz && inpleft>inplimit && w==16384)*/
  379.    }/* VOID block_read()*/
  380. /***************************************************************************/
  381.  VOID block_write()
  382.   {
  383.       bfx=0;w=16384;
  384.       bwrit=0;
  385.       while (bwrit<bread && w==16384)
  386.       {
  387.        if (bread>=bwrit+16384)
  388.        {
  389.         w=fwrite(bfpa[bfx],1,16384,fp1);
  390.        }
  391.        else
  392.         {
  393.         w=fwrite(bfpa[bfx],1,(int)(bread-bwrit),fp1);
  394.         }
  395.        if (ferror(fp1))
  396.         {
  397.          printf("File write error.\n");
  398.          exit(8);
  399.         } /*ferror(fp2)*/
  400.        bfx++;
  401.        bwrit+=(long)w;
  402.        pbwrit+=(long)w;
  403.        byte_count+=(long)w;
  404.       } /*while (bwrit<bread && w==16384)*/
  405.   } /*VOID block_write()*/
  406. /***************************************************************************/
  407. VOID help()
  408.    {
  409.    printf("Version %s -- 12/18/92\n",VERSION);
  410.    printf("\n");
  411.    printf("USAGE: JOIN FILENAME [INPATH] -SWITCHES\n");
  412.    printf("Written by Edgar Swank using some code by W. J. Kennamer\n");
  413.    printf("and released into the public domain.\n");
  414.    printf("  FILENAME is any valid MS-DOS filename.  Wildcards are not supported.\n");
  415.    printf("  This will be the single output filename.\n");
  416.    printf("  Input file names will be FILENAME.1, FILENAME.2, etc.\n");
  417.    printf("  Program will stop when FILENAME.n is not found, or when Esc is entered\n");
  418.    printf("  in response to prompt from -w option.\n");
  419.    printf("  Terminating <ctrl>Z is removed from Input files\n");
  420.    printf("  UNLESS -b is specified.\n");
  421.    printf("  Terminating <ctrl>Z is added to Output files\n");
  422.    printf("  ONLY if -e is specified AND -b is NOT specified.\n");
  423.    printf("  INPATH is an optional disk/pathname (A: or \\path\\ or a:\\path\\)\n");
  424.    printf("  to get input files on a different disk and/or directory from output file.\n");
  425.    printf("  SWITCHES must follow the FILENAME and INPATH parameters,\n");
  426.    printf("  and may be entered in any order.  Switches may be combined.\n");
  427.    printf("Press any key to continue:\n");
  428.     getchar();
  429.    printf("  Valid SWITCHES are:\n");
  430.    printf("╔═════════════════╤═══════════════════════════════════════════════╗\n");
  431.    printf("║   -b  binary    │ do not remove ^z from input files.            ║\n");
  432.    printf("╟─────────────────┼───────────────────────────────────────────────╢\n");
  433.    printf("║   -e  eof       │ add ^z to output file.                        ║\n");
  434.    printf("║                 │ Note: may interfere with appending            ║\n");
  435.    printf("║                 │ to output file later.                         ║\n");
  436.    printf("╟─────────────────┼───────────────────────────────────────────────╢\n");
  437.    printf("║   -w  wait      │ wait before opening each input file.          ║\n");
  438.    printf("║                 │  can change input diskette if separate from   ║\n");
  439.    printf("║                 │  output.                                      ║\n");
  440.    printf("╚═════════════════╧═══════════════════════════════════════════════╝\n");
  441.    }
  442.  
  443. /***************************************************************************/
  444.  
  445. VOID close_out(fp,filename)
  446.  
  447. FILE *fp;
  448. char *filename;                    /* output file name                 */
  449.  
  450.    {
  451.    long file_size;
  452.  
  453.    if (binary==OFF && weof==ON)
  454.    {
  455.    putc(CTRL_Z,fp);                /* terminate file with EOF mark    */
  456.    byte_count++;
  457.    }
  458.  
  459.    if( fseek(fp,0L,2) == -1 )         /* position at EOF                 */
  460.                 {
  461.                 printf("Error seeking end of output file.\n");
  462.                 exit(1);
  463.                 }
  464.    file_size = ftell(fp);          /* report EOF position             */
  465.    fclose(fp);
  466.    printf("Created %s -- %ld bytes\n" , filename , file_size);
  467.    if (binary==OFF && weof==ON)
  468.    {
  469.    printf("Warning: EOF added to output file. May interfere with appending.\n");
  470.    }
  471.    }
  472.  
  473. /***************************************************************************/
  474. VOID delete_files()
  475.  {
  476.   if (!wait)
  477.    {
  478.     Printf("Delete Input Files?(y/n)\n");
  479.     c=getche();
  480.     printf("\n");
  481.     if (c=='y' || c=='Y')
  482.      {
  483.       for (i=1;i<num_input_files;i++)
  484.        {
  485.         sprintf(input_file,"%s.%-d",filename,i);
  486.         u=unlink(input_file);
  487.         if (u==0)
  488.          {
  489.           printf("%s Deleted.\n",input_file);
  490.          }
  491.         else
  492.          {
  493.           derno=_doserrno;erno=errno;
  494.           printf("Error Deleting %s.\n",input_file);
  495.           printf ("DOS ERROR %4X errno %d %s\n",derno,erno,sys_errlist[erno]);
  496.          } /* endif */
  497.  
  498.        } /* endfor */
  499.      }
  500.     else
  501.      {
  502.      } /* endif */
  503.  
  504.    }
  505.  }
  506.