/***********************************************************************/
/* Author: Suzanne Miller Dorney                                       */
/* File: lab5.c                                                        */
/* Class:  CPSC-202 lab section 1 		                       */
/* Project:  Lab 4                                                     */
/* Purpose:  This program will Scramble or Unscramble a text file.     */
/***********************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>

/* Constants */
#define MAX_SIZE 1024

int main( int argc, char *argv[] )
{
  char  direction;            /* direction of scrambling                 */
  int   numPasses;            /* number of passes through file           */
  int   i;                    /* loop control variable                   */
  int   groupSize;            /* number of chars to flip on current pass */   
  int   inFD;                 /* input file descriptor                   */
  int   outFD;                /* output file descriptor                  */
  char  group[MAX_SIZE];      /* array to store bytes read in from file  */
  int   numChars;             /* number of characters actually read in   */
  char  tempChar;             /* temporary character used for flipping   */


  /* initial check on the number of command line arguments */
  if ( argc &lt; 5 )
  {
    printf( "Invalid program initiation. Please enter the direction of scrambling,\n" );
    printf( "(S or U )the number of passes to be made through the file, the name of \n" );
    printf( "the input file and the output filename on the command line.\n" );
    printf( "Program terminated, please try again.\n" );
    exit( 1 );
  }

  /* set the scrambling direction */
  direction = toupper( argv[1][0] );

  if ( direction != 'S' && direction != 'U' )
  {
    printf( "Invalid scrambling direction.  Please enter an S or U\n" );
    printf( "Program terminated, please try again.\n" );
    exit( 1 );
  }

  /* set the number of passes */
  numPasses = atoi( argv[2] );

  /* open the input and output files */
  if ( ( inFD = open( argv[3], O_RDONLY ) ) &lt; 0 )
  {
    printf( "ERROR: could not open file: %s\n", argv[3] );
    return -1;
  }

  if ( ( outFD = open( argv[4], O_RDWR | O_CREAT | O_TRUNC, 0664 ) ) &lt; 0 )
  {
    printf( "ERROR: could not open file: %s\n", argv[4] );
    return -1;
  }

  /* copy input file to output file */
  while ( ( numChars = read( inFD, group, MAX_SIZE ) ) == MAX_SIZE )
    write( outFD, group, numChars );
  write( outFD, group, numChars );
  close( inFD );

  for ( i=0; i&lt;numPasses; i++ )
  {
     if ( toupper( direction ) == 'S' )
       groupSize = 2 * ( i+1 );
     else
       groupSize = 2 * ( numPasses-i );

     lseek( outFD, 0, SEEK_SET );
     while ( ( numChars = read( outFD, group, groupSize ) ) == groupSize )
     {
       tempChar = group[0];
       group[0] = group[groupSize-1];
       group[groupSize-1] = tempChar;
       lseek( outFD, -groupSize, SEEK_CUR );
       write( outFD, group, groupSize );
     }
     lseek( outFD, -numChars, SEEK_CUR );
     write( outFD, group, numChars ); 
  }

  close( outFD );
  return 0;
}
