Source

mdiscprep / mdiscprep.c

/*
 Multiple media/disc splitter
 Copyright (C) 2012 Daybo Logic
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. Neither the name of the project nor the names of its contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.
*/
/*-------------------------------------------------------------------------*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /*HAVE_CONFIG_H*/

#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/*-------------------------------------------------------------------------*/
#define DEFAULT_CHUNK_SIZE_K (4488 * 1024)

static bool ProcessOptions(int ArgC, char **ArgV);
int main(int argc, char *argv[]);
/*-------------------------------------------------------------------------*/
static unsigned int DebugLevel;
static char *InputDirectory;
static unsigned long int ChunkSizeK;
/*-------------------------------------------------------------------------*/
static bool ProcessOptions(int ArgC, char **ArgV)
{
	int c;

	while ( (c = getopt(ArgC, ArgV, "D:d:s:")) != -1 ) {
		switch ( c ) {
			case 'D' : {
				DebugLevel = (unsigned int)(abs(atoi(optarg)));
				break;
			}
			case 'd' : {
				InputDirectory = strdup(optarg);
				break;
			}
			case 's' : {
				char *strCpy = strdup(optarg);
				char *pch = strCpy; /* Place pointer at start of string */
				int sep = 0;
				do {
					if ( !isdigit(*pch) ) { /* Non-digit separator */
						sep = *pch; /* Save this separator */
						*pch = '\0'; /* Truncate copy of string, so just digits remain */
						break; /* No need to examine further */
					}
					pch++; /* Move on to next character in string */
				} while ( *pch != '\0' ); /* Until end of string */
				ChunkSizeK = atol(strCpy);
				free(strCpy); /* No longer need the temporary, modifiable option copy */
				if ( sep ) {
					sep = tolower(sep); /* Simpify our char comparisons */
					if ( sep == 'm' )
						ChunkSizeK *= 1024;
					else if ( sep == 'g' )
						ChunkSizeK *= ( 1024*1024 );
				} else { /* Bytes, divide by 1024 for kilobytes */
					ChunkSizeK /= 1024;
				}
				if ( DebugLevel )
					fprintf(stderr, "Per-chunk size set to %u KB\n", ChunkSizeK);

				break;
			}
			default : {
				return false;
			}
		}
	}

	return true;
}
/*-------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
	if ( !ProcessOptions(argc, argv) ) return EXIT_FAILURE; /* Bail, if command-line arguments are bad */
	if ( DebugLevel ) fprintf(stderr, "Debug verbosity set to %u\n", DebugLevel);

	if ( !ChunkSizeK ) {
		ChunkSizeK = DEFAULT_CHUNK_SIZE_K;
		printf("Warning: No chunk size set, defaulting to %u KB\n", ChunkSizeK);
	}

	if ( InputDirectory ) {
		if ( DebugLevel ) fprintf(stderr, "Input directory set to %s\n", InputDirectory);
	} else {
		fputs("ERROR: Input directory (-d) not specified.\n", stderr);
		return EXIT_FAILURE;
	}
	free(InputDirectory);
	return EXIT_SUCCESS;
}
/*-------------------------------------------------------------------------*/