Chris PeBenito 89ec23
#include <stdio.h>
Chris PeBenito 89ec23
Chris PeBenito 89ec23
/* file_context_node
Chris PeBenito 89ec23
 * A node used in a linked list of file contexts.
Chris PeBenito 89ec23
 * Each node contains the regular expression, the type and 
Chris PeBenito 89ec23
 *  the context, as well as information about the regular
Chris PeBenito 89ec23
 *  expression. The regular expression data (meta, stem_len
Chris PeBenito 89ec23
 *  and str_len) can be filled in by using the fc_fill_data
Chris PeBenito 89ec23
 *  function after the regular expression has been loaded.
Chris PeBenito 89ec23
 * next points to the next node in the linked list.
Chris PeBenito 89ec23
 */
Chris PeBenito 89ec23
struct file_context_node { 
Chris PeBenito 89ec23
	char* regex;
Chris PeBenito 89ec23
	char* type;
Chris PeBenito 89ec23
        char* context;
Chris PeBenito 89ec23
        int meta;
Chris PeBenito 89ec23
        int stem_len;
Chris PeBenito 89ec23
        int str_len;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
        struct file_context_node* next;
Chris PeBenito 89ec23
};
Chris PeBenito 89ec23
Chris PeBenito 89ec23
Chris PeBenito 89ec23
Chris PeBenito 89ec23
/* file_context_bucket
Chris PeBenito 89ec23
 * A node used in a linked list of buckets that contain
Chris PeBenito 89ec23
 *  file_context_node's.
Chris PeBenito 89ec23
 * Each node contains a pointer to a file_context_node which
Chris PeBenito 89ec23
 *  is the header of its linked list. This linked list is the
Chris PeBenito 89ec23
 *  content of this bucket.
Chris PeBenito 89ec23
 * next points to the next bucket in the linked list.
Chris PeBenito 89ec23
 */
Chris PeBenito 89ec23
struct file_context_bucket {
Chris PeBenito 89ec23
	struct file_context_node* data;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	struct file_context_bucket* next;
Chris PeBenito 89ec23
};
Chris PeBenito 89ec23
Chris PeBenito 89ec23
Chris PeBenito 89ec23
Chris PeBenito 89ec23
/* fc_merge
Chris PeBenito 89ec23
 * Merges two sorted file context linked lists into one
Chris PeBenito 89ec23
 *  sorted one.
Chris PeBenito 89ec23
 * Pass two lists a and b, and after the completion of fc_merge,
Chris PeBenito 89ec23
 *  the final list is contained in a, and b is empty.
Chris PeBenito 89ec23
 */
Chris PeBenito 89ec23
struct file_context_node* fc_merge( struct file_context_node* a, struct file_context_node* b )
Chris PeBenito 89ec23
{
Chris PeBenito 89ec23
        struct file_context_node* a_current;
Chris PeBenito 89ec23
        struct file_context_node* b_current;
Chris PeBenito 89ec23
        struct file_context_node* temp;
Chris PeBenito 89ec23
        struct file_context_node* jumpto;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
        /* If a is a empty list, and b is not,
Chris PeBenito 89ec23
         *  set a as b and proceed to the end. */
Chris PeBenito 89ec23
        if( !a && b )
Chris PeBenito 89ec23
                a = b;
Chris PeBenito 89ec23
        /* If b is an empty list, leave a as it is. */
Chris PeBenito 89ec23
        else if( !b ) { }
Chris PeBenito 89ec23
        else {
Chris PeBenito 89ec23
                /* Make it so the list a has the lesser
Chris PeBenito 89ec23
                 *  first element always. */
Chris PeBenito 89ec23
                if( fc_compare( a, b ) == 1 ) {
Chris PeBenito 89ec23
                        temp = a;
Chris PeBenito 89ec23
                        a = b;
Chris PeBenito 89ec23
                        b = temp;
Chris PeBenito 89ec23
                }
Chris PeBenito 89ec23
                a_current = a;
Chris PeBenito 89ec23
                b_current = b;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
                /* Merge by inserting b's nodes inbetween a's nodes. */
Chris PeBenito 89ec23
                while( a_current->next && b_current ) {
Chris PeBenito 89ec23
                        jumpto = a_current->next;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
                        /* Insert b's nodes inbetween the current a node
Chris PeBenito 89ec23
                         *  and the next a node.*/
Chris PeBenito 89ec23
                        while( b_current && a_current->next &&
Chris PeBenito 89ec23
                               fc_compare( a_current->next, b_current) != -1 ) {                                temp = a_current->next;
Chris PeBenito 89ec23
                                a_current->next = b_current;
Chris PeBenito 89ec23
                                b_current = b_current->next;
Chris PeBenito 89ec23
                                a_current->next->next = temp;
Chris PeBenito 89ec23
                                a_current = a_current->next;
Chris PeBenito 89ec23
                        }
Chris PeBenito 89ec23
Chris PeBenito 89ec23
                        /* Skip all the inserted node from b to the
Chris PeBenito 89ec23
                         *  next node in the original a. */
Chris PeBenito 89ec23
                        a_current = jumpto;
Chris PeBenito 89ec23
                }
Chris PeBenito 89ec23
Chris PeBenito 89ec23
Chris PeBenito 89ec23
                /* if there is anything left in b to be inserted,
Chris PeBenito 89ec23
                   put it on the end */
Chris PeBenito 89ec23
                if( b_current ) {
Chris PeBenito 89ec23
                        a_current->next = b_current;
Chris PeBenito 89ec23
                }
Chris PeBenito 89ec23
        }
Chris PeBenito 89ec23
Chris PeBenito 89ec23
        b = NULL;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
        return a;
Chris PeBenito 89ec23
}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
Chris PeBenito 89ec23
Chris PeBenito 89ec23
/* fc_merge_sort
Chris PeBenito 89ec23
 * Sorts file contexts from least specific to more specific.
Chris PeBenito 89ec23
 * The bucket linked list is passed and after the completion
Chris PeBenito 89ec23
 *  of the fc_merge_sort function, there is only one bucket
Chris PeBenito 89ec23
 *  (pointed to by master) that contains a linked list
Chris PeBenito 89ec23
 *  of all the file contexts, in sorted order.
Chris PeBenito 89ec23
 * Explanation of the algorithm:
Chris PeBenito 89ec23
 *  The algorithm implemented in fc_merge_sort is an iterative
Chris PeBenito 89ec23
 *   implementation of merge sort.
Chris PeBenito 89ec23
 *  At first, each bucket has a linked list of file contexts
Chris PeBenito 89ec23
 *   that are 1 element each.
Chris PeBenito 89ec23
 *  Each pass, each odd numbered bucket is merged into the bucket
Chris PeBenito 89ec23
 *   before it. This halves the number of buckets each pass.
Chris PeBenito 89ec23
 *  It will continue passing over the buckets (as described above)
Chris PeBenito 89ec23
 *   until there is only  one bucket left, containing the list of
Chris PeBenito 89ec23
 *   file contexts, sorted.
Chris PeBenito 89ec23
 */
Chris PeBenito 89ec23
void fc_merge_sort( struct file_context_bucket* master )
Chris PeBenito 89ec23
{
Chris PeBenito 89ec23
        int i;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
        struct file_context_bucket* current;
Chris PeBenito 89ec23
        struct file_context_bucket* temp;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
        struct file_context_node* ncurrent;
Chris PeBenito 89ec23
        struct file_context_node* ntemp;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Loop until master is the only bucket left
Chris PeBenito 89ec23
         * so that this will stop when master contains
Chris PeBenito 89ec23
	 * the sorted list. */
Chris PeBenito 89ec23
        while( master->next ) {
Chris PeBenito 89ec23
                current = master;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		/* This loop merges buckets two-by-two. */
Chris PeBenito 89ec23
		while( current ) {
Chris PeBenito 89ec23
                        if( current->next ) {
Chris PeBenito 89ec23
				/* Merge the next one into the current one. */
Chris PeBenito 89ec23
                                current->data = fc_merge( current->data, current->next->data );
Chris PeBenito 89ec23
                        	/* remove the next bucket that is now empty. */
Chris PeBenito 89ec23
				temp = current->next;
Chris PeBenito 89ec23
                                current->next = current->next->next;
Chris PeBenito 89ec23
                                free( temp );
Chris PeBenito 89ec23
                        }
Chris PeBenito 89ec23
                        current = current->next;
Chris PeBenito 89ec23
                }
Chris PeBenito 89ec23
        }
Chris PeBenito 89ec23
}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
/* fc_compare
Chris PeBenito 89ec23
 * Compares two file contexts' regular expressions and returns:
Chris PeBenito 89ec23
 *    -1 if a is less specific than b
Chris PeBenito 89ec23
 *     0 if a and be are equally specific
Chris PeBenito 89ec23
 *     1 if a is more specific than b
Chris PeBenito 89ec23
 * The comparison is based on the following statements,
Chris PeBenito 89ec23
 *  in order from most important to least important, given a and b:
Chris PeBenito 89ec23
 *     If a is a regular expression and b is not,
Chris PeBenito 89ec23
 *      -> a is less specific than b.
Chris PeBenito 89ec23
 *     If a's stem length is shorter than b's stem length,
Chris PeBenito 89ec23
 *      -> a is less specific than b.
Chris PeBenito 89ec23
 *     If a's string length is shorter than b's string length,
Chris PeBenito 89ec23
 *      -> a is less specific than b.
Chris PeBenito 89ec23
 *     If a does not have a specified type and b does not,
Chris PeBenito 89ec23
 *      -> a is less specific than b.
Chris PeBenito 89ec23
 */
Chris PeBenito 89ec23
int fc_compare( struct file_context_node* a, struct file_context_node* b )
Chris PeBenito 89ec23
{
Chris PeBenito 89ec23
	/* Check to see if either a or b have meta characters
Chris PeBenito 89ec23
         *  and the other doesn't. */
Chris PeBenito 89ec23
        if( a->meta && !b->meta )
Chris PeBenito 89ec23
                return -1;
Chris PeBenito 89ec23
        if( b->meta && !a->meta )
Chris PeBenito 89ec23
                return 1;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Check to see if either a or b have a shorter stem
Chris PeBenito 89ec23
         *  length than the other. */
Chris PeBenito 89ec23
        if( a->stem_len < b->stem_len )
Chris PeBenito 89ec23
                return -1;
Chris PeBenito 89ec23
        if( b->stem_len < a->stem_len )
Chris PeBenito 89ec23
                return 1;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Check to see if either a or b have a shorter string
Chris PeBenito 89ec23
         *  length than the other. */
Chris PeBenito 89ec23
        if( a->str_len < b->str_len )
Chris PeBenito 89ec23
                return -1;
Chris PeBenito 89ec23
        if( b->str_len < b->str_len )
Chris PeBenito 89ec23
                return 1;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Check to see if either a or b has a specified type
Chris PeBenito 89ec23
         *  and the other doesn't. */
Chris PeBenito 89ec23
        if( !a->type && b->type )
Chris PeBenito 89ec23
                return -1;
Chris PeBenito 89ec23
        if( !b->type && a->type )
Chris PeBenito 89ec23
                return 1;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* If none of the above conditions were satisfied, 
Chris PeBenito 89ec23
         * then a and b are equally specific. */
Chris PeBenito 89ec23
        return 0;
Chris PeBenito 89ec23
}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
Chris PeBenito 89ec23
Chris PeBenito 89ec23
/* fc_fill_data
Chris PeBenito 89ec23
 * This processes a regular expression in a file context
Chris PeBenito 89ec23
 *  and sets the data held in file_context_node, namely
Chris PeBenito 89ec23
 *  meta, str_len and stem_len. 
Chris PeBenito 89ec23
 * The following changes are made to fc_node after the
Chris PeBenito 89ec23
 *  the completion of the function:
Chris PeBenito 89ec23
 *     fc_node->meta =		1 if regex has a meta character,
Chris PeBenito 89ec23
 *                       	0 if not.
Chris PeBenito 89ec23
 *     fc_node->str_len =	The string length of the regular
Chris PeBenito 89ec23
 *                               expression.
Chris PeBenito 89ec23
 *     fc_node->stem_len = 	The number of characters up until
Chris PeBenito 89ec23
 *				 the first meta character.
Chris PeBenito 89ec23
 */
Chris PeBenito 89ec23
void fc_fill_data( struct file_context_node* fc_node )
Chris PeBenito 89ec23
{
Chris PeBenito 89ec23
        int c = 0;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
        fc_node->meta = 0;
Chris PeBenito 89ec23
        fc_node->stem_len = 0;
Chris PeBenito 89ec23
        fc_node->str_len = 0;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Process until the string termination character
Chris PeBenito 89ec23
         *  has been reached.
Chris PeBenito 89ec23
	 * Note: this while loop has been adapted from
Chris PeBenito 89ec23
         *  spec_hasMetaChars in matchpathcon.c from
Chris PeBenito 89ec23
         *  libselinux-1.22. */
Chris PeBenito 89ec23
        while( fc_node->regex[c] != 0 ) {
Chris PeBenito 89ec23
                switch( fc_node->regex[c] ) {
Chris PeBenito 89ec23
                        case '.':
Chris PeBenito 89ec23
                        case '^':
Chris PeBenito 89ec23
                        case '$':
Chris PeBenito 89ec23
                        case '?':
Chris PeBenito 89ec23
                        case '*':
Chris PeBenito 89ec23
                        case '+':
Chris PeBenito 89ec23
                        case '|':
Chris PeBenito 89ec23
                        case '[':
Chris PeBenito 89ec23
                        case '(':
Chris PeBenito 89ec23
                        case '{':
Chris PeBenito 89ec23
            			/* If a meta character is found,
Chris PeBenito 89ec23
                                 *  set meta to one */
Chris PeBenito 89ec23
		                fc_node->meta = 1;
Chris PeBenito 89ec23
                                break;
Chris PeBenito 89ec23
                        case '\\':
Chris PeBenito 89ec23
				/* If a escape character is found,
Chris PeBenito 89ec23
                                 *  skip the next character. */
Chris PeBenito 89ec23
                                c++;
Chris PeBenito 89ec23
                        default:
Chris PeBenito 89ec23
				/* If no meta character has been found yet,
Chris PeBenito 89ec23
				 *  add one to the stem length. */
Chris PeBenito 89ec23
                                if( !fc_node->meta ) fc_node->stem_len++;
Chris PeBenito 89ec23
                                break;
Chris PeBenito 89ec23
                }
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		fc_node->str_len++;
Chris PeBenito 89ec23
                c++;
Chris PeBenito 89ec23
        }
Chris PeBenito 89ec23
}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
/* main
Chris PeBenito 89ec23
 * This program takes in two arguments, the input filename and the
Chris PeBenito 89ec23
 *  output filename. The input file should be syntactically correct.
Chris PeBenito 89ec23
 * Overall what is done in the main is read in the file and store each
Chris PeBenito 89ec23
 *  line of code, sort it, then output it to the output file.
Chris PeBenito 89ec23
 */
Chris PeBenito 89ec23
int main( int argc, char *argv[])
Chris PeBenito 89ec23
{
Chris PeBenito 89ec23
	int i, j, lines;
Chris PeBenito 89ec23
	int start, finish;
Chris PeBenito 89ec23
	char* str;
Chris PeBenito 89ec23
	struct file_context_node* temp;
Chris PeBenito 89ec23
	struct file_context_node* head;
Chris PeBenito 89ec23
	struct file_context_node* current;
Chris PeBenito 89ec23
	struct file_context_node* array; 
Chris PeBenito 89ec23
	struct file_context_bucket* master;
Chris PeBenito 89ec23
	struct file_context_bucket* bcurrent;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	FILE *path;
Chris PeBenito 89ec23
	char line_buf[ 127 ];
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Check for the correct number of command line arguments. */
Chris PeBenito 89ec23
	if( argc != 3 ) {
Chris PeBenito 89ec23
		printf( "Error: invalid number of command line arguments.\n" );
Chris PeBenito 89ec23
		return -1;
Chris PeBenito 89ec23
	}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	i = j = lines = 0;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Allocate the head of the file_context linked list. */
Chris PeBenito 89ec23
	if( !( current = head = (struct file_context_node*)malloc( sizeof( struct file_context_node ) ) ) ) {
Chris PeBenito 89ec23
		printf( "Error: failure allocating memory.\n" );
Chris PeBenito 89ec23
		return -1;
Chris PeBenito 89ec23
	}
Chris PeBenito 89ec23
	
Chris PeBenito 89ec23
	/* Make sure to have a terminating character, always. */
Chris PeBenito 89ec23
	line_buf[127] = 0;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Open the input file. */
Chris PeBenito 89ec23
	if( !( path = fopen( argv[1], "r" ) ) ) {
Chris PeBenito 89ec23
		printf( "Error: failure opening input file for read.\n" );
Chris PeBenito 89ec23
		return -1;
Chris PeBenito 89ec23
	}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Parse the file into a file_context linked list. */
Chris PeBenito 89ec23
	while( fgets( line_buf, 126, path ) != NULL ) {
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		/* Get rid of whitespace from the front of the line. */
Chris PeBenito 89ec23
		i = 0;
Chris PeBenito 89ec23
		while( line_buf[i] && line_buf[i] <= 32 ) i++;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		/* Check if the line isn't empty and isn't a comment */
Chris PeBenito 89ec23
		if( line_buf[i] && line_buf[i] != '#' ) {
Chris PeBenito 89ec23
			/* Allocate a new node. */
Chris PeBenito 89ec23
			temp = (struct file_context_node*)malloc( sizeof( struct file_context_node ) );
Chris PeBenito 89ec23
			if( !temp ) {
Chris PeBenito 89ec23
				printf( "Error: failure allocating memory.\n" );
Chris PeBenito 89ec23
				return -1;
Chris PeBenito 89ec23
			}
Chris PeBenito 89ec23
			temp->next = NULL;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			/* Parse out the regular expression from the line. */
Chris PeBenito 89ec23
			start = i;
Chris PeBenito 89ec23
			while( line_buf[i] > 32 )i++;
Chris PeBenito 89ec23
			finish = i;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			/* Allocate a character array to hold the regular
Chris PeBenito 89ec23
 			 *  expression. */
Chris PeBenito 89ec23
			temp->regex = (char*)malloc( sizeof( char ) * ( finish - start + 1) );
Chris PeBenito 89ec23
			if( !( temp->regex ) ) {
Chris PeBenito 89ec23
				printf( "Error: failure allocating memory.\n" );
Chris PeBenito 89ec23
				return -1;
Chris PeBenito 89ec23
			}
Chris PeBenito 89ec23
			temp->regex[0] = 0;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			/* Fill the regular expression array. */
Chris PeBenito 89ec23
			temp->regex[ ( finish - start ) ] = 0;
Chris PeBenito 89ec23
			for( j = 0; j < finish - start; j++ ) {
Chris PeBenito 89ec23
				temp->regex[j] = line_buf[j + start];
Chris PeBenito 89ec23
			}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			/* Get rid of whitespace after the regular
Chris PeBenito 89ec23
                         *  expression. */
Chris PeBenito 89ec23
			while( line_buf[i] <= 32 ) i++;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			/* Parse out the type from the line (if it 
Chris PeBenito 89ec23
                         *  is there). */
Chris PeBenito 89ec23
			if( line_buf[i] == '-' ) {
Chris PeBenito 89ec23
				/* Allocate a character array to
Chris PeBenito 89ec23
                                 *  hold the type. */
Chris PeBenito 89ec23
				temp->type = (char*)malloc( sizeof( char ) * 3 );
Chris PeBenito 89ec23
				if( !( temp->type ) ) {
Chris PeBenito 89ec23
					printf( "Error: failure allocating memory.\n" );
Chris PeBenito 89ec23
					return -1;
Chris PeBenito 89ec23
				}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
				/* Fill the type into the array. */
Chris PeBenito 89ec23
				temp->type[0] = line_buf[i];
Chris PeBenito 89ec23
				temp->type[1] = line_buf[i + 1];
Chris PeBenito 89ec23
				i += 2;
Chris PeBenito 89ec23
				temp->type[2] = 0;
Chris PeBenito 89ec23
				
Chris PeBenito 89ec23
				/* Get rid of whitespace after the type. */
Chris PeBenito 89ec23
				while( line_buf[i] <= 32 ) i++;
Chris PeBenito 89ec23
			}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			/* Parse out the context from the line. */
Chris PeBenito 89ec23
                        start = i;
Chris PeBenito 89ec23
                        while( line_buf[i] > 32 ) i++;
Chris PeBenito 89ec23
                        finish = i;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			/* Allocate a character array to hold the context. */
Chris PeBenito 89ec23
                        temp->context = (char*)malloc( sizeof( char ) * ( finish - start + 1 ) );
Chris PeBenito 89ec23
			if( !( temp->context ) ) {
Chris PeBenito 89ec23
				printf( "Error: failure allocating memory.\n" );
Chris PeBenito 89ec23
				return -1;
Chris PeBenito 89ec23
			}
Chris PeBenito 89ec23
			temp->context[0] = 0;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			/* Fill the context array. */
Chris PeBenito 89ec23
                        temp->context[ ( finish - start ) ] = 0;
Chris PeBenito 89ec23
                        for( j = 0; j < finish - start; j++ ) {
Chris PeBenito 89ec23
                                temp->context[j] = line_buf[j + start];
Chris PeBenito 89ec23
                        }
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			/* Set all the data about the regular
Chris PeBenito 89ec23
                         *  expression. */
Chris PeBenito 89ec23
			fc_fill_data( temp );
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			/* Link this line of code at the end of
Chris PeBenito 89ec23
                         *  the linked list. */
Chris PeBenito 89ec23
			current->next = temp;
Chris PeBenito 89ec23
			current = current->next;
Chris PeBenito 89ec23
			lines++;
Chris PeBenito 89ec23
		}
Chris PeBenito 89ec23
	}
Chris PeBenito 89ec23
	fclose( path );
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Create the bucket linked list from the earlier linked list. */
Chris PeBenito 89ec23
	current = head->next;
Chris PeBenito 89ec23
	bcurrent = master = (struct file_context_bucket*)malloc( sizeof( struct file_context_bucket ) );
Chris PeBenito 89ec23
	/* Go until all the nodes have been put in individual buckets. */
Chris PeBenito 89ec23
	while( current ) {
Chris PeBenito 89ec23
		/* Copy over the file context line into the bucket. */
Chris PeBenito 89ec23
		bcurrent->data = current;
Chris PeBenito 89ec23
		current = current->next;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		/* Detatch the node in the bucket from the old list. */
Chris PeBenito 89ec23
		bcurrent->data->next = NULL;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		/* If there should be another bucket, put one at the end. */
Chris PeBenito 89ec23
		if( current ) {
Chris PeBenito 89ec23
			bcurrent->next = (struct file_context_bucket*) malloc( sizeof( struct file_context_bucket ) );
Chris PeBenito 89ec23
			if( !( bcurrent->next ) ) {
Chris PeBenito 89ec23
				printf( "Error: failure allocating memory.\n" );
Chris PeBenito 89ec23
				return -1;
Chris PeBenito 89ec23
			}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			/* Make sure the new bucket thinks it's the end of the
Chris PeBenito 89ec23
                         *  list. */
Chris PeBenito 89ec23
			bcurrent->next->next = NULL;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
			bcurrent = bcurrent->next;
Chris PeBenito 89ec23
		}
Chris PeBenito 89ec23
	}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Sort the bucket list. */
Chris PeBenito 89ec23
	fc_merge_sort( master );
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Open the output file. */
Chris PeBenito 89ec23
	if( !(path = fopen( argv[2], "w" ) ) ) {
Chris PeBenito 89ec23
		printf( "Error: failure opening output file for write.\n" );
Chris PeBenito 89ec23
		return -1;
Chris PeBenito 89ec23
	}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Output the sorted file_context linked list to the output file. */
Chris PeBenito 89ec23
	current = master->data;
Chris PeBenito 89ec23
	while( current ) {
Chris PeBenito 89ec23
		/* Output the regular expression. */
Chris PeBenito 89ec23
		i = 0;
Chris PeBenito 89ec23
		while( current->regex[i] != 0 ) {
Chris PeBenito 89ec23
			fprintf( path, "%c", current->regex[i] );
Chris PeBenito 89ec23
			i++;
Chris PeBenito 89ec23
		}
Chris PeBenito 89ec23
		fprintf( path, "\t" );
Chris PeBenito 89ec23
		
Chris PeBenito 89ec23
		/* Output the type, if there is one. */
Chris PeBenito 89ec23
		if( current->type ) {
Chris PeBenito 89ec23
			i = 0;
Chris PeBenito 89ec23
			while( current->type[i] != 0 ) {
Chris PeBenito 89ec23
				fprintf( path, "%c", current->type[i] );
Chris PeBenito 89ec23
				i++;
Chris PeBenito 89ec23
			}
Chris PeBenito 89ec23
			fprintf( path, "\t" ); 	
Chris PeBenito 89ec23
		}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		/* Output the context. */
Chris PeBenito 89ec23
		i = 0;
Chris PeBenito 89ec23
		while( current->context[i] != 0 ) {
Chris PeBenito 89ec23
			fprintf( path, "%c", current->context[i] );
Chris PeBenito 89ec23
			i++;
Chris PeBenito 89ec23
		}
Chris PeBenito 89ec23
		fprintf( path, "\n" );
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		/* Remove the node. */
Chris PeBenito 89ec23
		temp = current;
Chris PeBenito 89ec23
		current = current->next;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		free( temp->regex );
Chris PeBenito 89ec23
		if( temp->type)
Chris PeBenito 89ec23
			free( temp->type );
Chris PeBenito 89ec23
		free( temp->context );		
Chris PeBenito 89ec23
		free( temp );
Chris PeBenito 89ec23
	}
Chris PeBenito 89ec23
	free( master );
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	fclose( path );
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	return 0;
Chris PeBenito 89ec23
}
Chris PeBenito 89ec23