Karl MacMillan 6847e8
/* Copyright 2005, Tresys Technology */
Karl MacMillan 6847e8
Chris PeBenito 89ec23
#include <stdio.h>
Chris PeBenito 89ec23
Karl MacMillan 6847e8
typedef unsigned char bool_t;
Karl MacMillan 6847e8
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
 */
Karl MacMillan 6847e8
typedef struct file_context_node {
Karl MacMillan 6847e8
	char *regex;
Karl MacMillan 6847e8
	char *file_type;
Karl MacMillan 6847e8
	char *context;
Karl MacMillan 6847e8
	bool_t meta;
Karl MacMillan 6847e8
	int stem_len;
Karl MacMillan 6847e8
	int str_len;
Karl MacMillan 6847e8
	struct file_context_node *next;
Karl MacMillan 6847e8
} file_context_node_t;
Karl MacMillan 6847e8
Karl MacMillan 6847e8
void file_context_node_destroy(file_context_node_t *x)
Karl MacMillan 6847e8
{
Karl MacMillan 6847e8
	free(x->regex);
Karl MacMillan 6847e8
	free(x->file_type);
Karl MacMillan 6847e8
	free(x->context);
Karl MacMillan 6847e8
}
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
 */
Karl MacMillan 6847e8
typedef struct file_context_bucket {
Karl MacMillan 6847e8
	file_context_node_t *data;
Karl MacMillan 6847e8
	struct file_context_bucket *next;
Karl MacMillan 6847e8
} file_context_bucket_t;
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
 */
Karl MacMillan 6847e8
file_context_node_t *fc_merge(file_context_node_t *a,
Karl MacMillan 6847e8
				   file_context_node_t *b)
Chris PeBenito 89ec23
{
Karl MacMillan 6847e8
	file_context_node_t *a_current;
Karl MacMillan 6847e8
	file_context_node_t *b_current;
Karl MacMillan 6847e8
	file_context_node_t *temp;
Karl MacMillan 6847e8
	file_context_node_t *jumpto;
Karl MacMillan 6847e8
Karl MacMillan 6847e8
	/* If a is a empty list, and b is not,
Karl MacMillan 6847e8
	 *  set a as b and proceed to the end. */
Karl MacMillan 6847e8
	if (!a && b)
Karl MacMillan 6847e8
		a = b;
Karl MacMillan 6847e8
	/* If b is an empty list, leave a as it is. */
Karl MacMillan 6847e8
	else if (!b) {
Karl MacMillan 6847e8
	} else {
Karl MacMillan 6847e8
		/* Make it so the list a has the lesser
Karl MacMillan 6847e8
		 *  first element always. */
Karl MacMillan 6847e8
		if (fc_compare(a, b) == 1) {
Karl MacMillan 6847e8
			temp = a;
Karl MacMillan 6847e8
			a = b;
Karl MacMillan 6847e8
			b = temp;
Karl MacMillan 6847e8
		}
Karl MacMillan 6847e8
		a_current = a;
Karl MacMillan 6847e8
		b_current = b;
Karl MacMillan 6847e8
Karl MacMillan 6847e8
		/* Merge by inserting b's nodes inbetween a's nodes. */
Karl MacMillan 6847e8
		while (a_current->next && b_current) {
Karl MacMillan 6847e8
			jumpto = a_current->next;
Karl MacMillan 6847e8
Karl MacMillan 6847e8
			/* Insert b's nodes inbetween the current a node
Karl MacMillan 6847e8
			 *  and the next a node.*/
Karl MacMillan 6847e8
			while (b_current && a_current->next &&
Karl MacMillan 6847e8
			       fc_compare(a_current->next,
Karl MacMillan 6847e8
					  b_current) != -1) {
Karl MacMillan 6847e8
				temp = a_current->next;
Karl MacMillan 6847e8
				a_current->next = b_current;
Karl MacMillan 6847e8
				b_current = b_current->next;
Karl MacMillan 6847e8
				a_current->next->next = temp;
Karl MacMillan 6847e8
				a_current = a_current->next;
Karl MacMillan 6847e8
			}
Karl MacMillan 6847e8
Karl MacMillan 6847e8
			/* Skip all the inserted node from b to the
Karl MacMillan 6847e8
			 *  next node in the original a. */
Karl MacMillan 6847e8
			a_current = jumpto;
Karl MacMillan 6847e8
		}
Karl MacMillan 6847e8
Karl MacMillan 6847e8
Karl MacMillan 6847e8
		/* if there is anything left in b to be inserted,
Karl MacMillan 6847e8
		   put it on the end */
Karl MacMillan 6847e8
		if (b_current) {
Karl MacMillan 6847e8
			a_current->next = b_current;
Karl MacMillan 6847e8
		}
Karl MacMillan 6847e8
	}
Karl MacMillan 6847e8
Karl MacMillan 6847e8
	b = NULL;
Karl MacMillan 6847e8
Karl MacMillan 6847e8
	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
 */
Karl MacMillan 6847e8
void fc_merge_sort(file_context_bucket_t *master)
Chris PeBenito 89ec23
{
Karl MacMillan 6847e8
	int i;
Chris PeBenito 89ec23
Karl MacMillan 6847e8
	file_context_bucket_t *current;
Karl MacMillan 6847e8
	file_context_bucket_t *temp;
Chris PeBenito 89ec23
Karl MacMillan 6847e8
	file_context_node_t *ncurrent;
Karl MacMillan 6847e8
	file_context_node_t *ntemp;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Loop until master is the only bucket left
Karl MacMillan 6847e8
	 * so that this will stop when master contains
Chris PeBenito 89ec23
	 * the sorted list. */
Karl MacMillan 6847e8
	while (master->next) {
Karl MacMillan 6847e8
		current = master;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		/* This loop merges buckets two-by-two. */
Karl MacMillan 6847e8
		while (current) {
Karl MacMillan 6847e8
			if (current->next) {
Chris PeBenito 89ec23
				/* Merge the next one into the current one. */
Karl MacMillan 6847e8
				current->data =
Karl MacMillan 6847e8
				    fc_merge(current->data,
Karl MacMillan 6847e8
					     current->next->data);
Karl MacMillan 6847e8
				/* remove the next bucket that is now empty. */
Chris PeBenito 89ec23
				temp = current->next;
Karl MacMillan 6847e8
				current->next = current->next->next;
Karl MacMillan 6847e8
				free(temp);
Karl MacMillan 6847e8
			}
Karl MacMillan 6847e8
			current = current->next;
Karl MacMillan 6847e8
		}
Karl MacMillan 6847e8
	}
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
 */
Karl MacMillan 6847e8
int fc_compare(file_context_node_t *a, file_context_node_t *b)
Chris PeBenito 89ec23
{
Chris PeBenito 89ec23
	/* Check to see if either a or b have meta characters
Karl MacMillan 6847e8
	 *  and the other doesn't. */
Karl MacMillan 6847e8
	if (a->meta && !b->meta)
Karl MacMillan 6847e8
		return -1;
Karl MacMillan 6847e8
	if (b->meta && !a->meta)
Karl MacMillan 6847e8
		return 1;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Check to see if either a or b have a shorter stem
Karl MacMillan 6847e8
	 *  length than the other. */
Karl MacMillan 6847e8
	if (a->stem_len < b->stem_len)
Karl MacMillan 6847e8
		return -1;
Karl MacMillan 6847e8
	if (b->stem_len < a->stem_len)
Karl MacMillan 6847e8
		return 1;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Check to see if either a or b have a shorter string
Karl MacMillan 6847e8
	 *  length than the other. */
Karl MacMillan 6847e8
	if (a->str_len < b->str_len)
Karl MacMillan 6847e8
		return -1;
Karl MacMillan 6847e8
	if (b->str_len < b->str_len)
Karl MacMillan 6847e8
		return 1;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Check to see if either a or b has a specified type
Karl MacMillan 6847e8
	 *  and the other doesn't. */
Karl MacMillan 6847e8
	if (!a->type && b->type)
Karl MacMillan 6847e8
		return -1;
Karl MacMillan 6847e8
	if (!b->type && a->type)
Karl MacMillan 6847e8
		return 1;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* If none of the above conditions were satisfied, 
Karl MacMillan 6847e8
	 * then a and b are equally specific. */
Karl MacMillan 6847e8
	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
 */
Karl MacMillan 6847e8
void fc_fill_data(file_context_node_t *fc_node)
Chris PeBenito 89ec23
{
Karl MacMillan 6847e8
	int c = 0;
Chris PeBenito 89ec23
Karl MacMillan 6847e8
	fc_node->meta = 0;
Karl MacMillan 6847e8
	fc_node->stem_len = 0;
Karl MacMillan 6847e8
	fc_node->str_len = 0;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Process until the string termination character
Karl MacMillan 6847e8
	 *  has been reached.
Chris PeBenito 89ec23
	 * Note: this while loop has been adapted from
Karl MacMillan 6847e8
	 *  spec_hasMetaChars in matchpathcon.c from
Karl MacMillan 6847e8
	 *  libselinux-1.22. */
Karl MacMillan 6847e8
	while (fc_node->regex[c] != 0) {
Karl MacMillan 6847e8
		switch (fc_node->regex[c]) {
Karl MacMillan 6847e8
		case '.':
Karl MacMillan 6847e8
		case '^':
Karl MacMillan 6847e8
		case '$':
Karl MacMillan 6847e8
		case '?':
Karl MacMillan 6847e8
		case '*':
Karl MacMillan 6847e8
		case '+':
Karl MacMillan 6847e8
		case '|':
Karl MacMillan 6847e8
		case '[':
Karl MacMillan 6847e8
		case '(':
Karl MacMillan 6847e8
		case '{':
Karl MacMillan 6847e8
			/* If a meta character is found,
Karl MacMillan 6847e8
			 *  set meta to one */
Karl MacMillan 6847e8
			fc_node->meta = 1;
Karl MacMillan 6847e8
			break;
Karl MacMillan 6847e8
		case '\\':
Karl MacMillan 6847e8
			/* If a escape character is found,
Karl MacMillan 6847e8
			 *  skip the next character. */
Karl MacMillan 6847e8
			c++;
Karl MacMillan 6847e8
		default:
Karl MacMillan 6847e8
			/* If no meta character has been found yet,
Karl MacMillan 6847e8
			 *  add one to the stem length. */
Karl MacMillan 6847e8
			if (!fc_node->meta)
Karl MacMillan 6847e8
				fc_node->stem_len++;
Karl MacMillan 6847e8
			break;
Karl MacMillan 6847e8
		}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		fc_node->str_len++;
Karl MacMillan 6847e8
		c++;
Karl MacMillan 6847e8
	}
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
 */
Karl MacMillan 6847e8
int main(int argc, char *argv[])
Chris PeBenito 89ec23
{
Karl MacMillan 6847e8
	int lines;
Karl MacMillan 6847e8
	size_t start, finish, regex_len;
Karl MacMillan 6847e8
	size_t line_len, i, j;
Karl MacMillan 6847e8
	char *str, *input_name, *output_name, *line_buf;
Karl MacMillan 6847e8
	
Karl MacMillan 6847e8
	file_context_node_t *temp;
Karl MacMillan 6847e8
	file_context_node_t *head;
Karl MacMillan 6847e8
	file_context_node_t *current;
Karl MacMillan 6847e8
	file_context_node_t *array;
Karl MacMillan 6847e8
	file_context_bucket_t *master;
Karl MacMillan 6847e8
	file_context_bucket_t *bcurrent;
Karl MacMillan 6847e8
Karl MacMillan 6847e8
	FILE *in_file, *out_file;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Check for the correct number of command line arguments. */
Karl MacMillan 6847e8
	if (argc != 3) {
Karl MacMillan 6847e8
		fprintf(stderr, "Error: invalid number of command line arguments.\n");
Karl MacMillan 6847e8
		return 1;
Chris PeBenito 89ec23
	}
Karl MacMillan 6847e8
	
Karl MacMillan 6847e8
	input_name = argv[1];
Karl MacMillan 6847e8
	output_name = argv[2];
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	i = j = lines = 0;
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Make sure to have a terminating character, always. */
Karl MacMillan 6847e8
	line_buf[BUF_SIZE - 1] = '\0';
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Open the input file. */
Karl MacMillan 6847e8
	if (!(in_file = fopen(input_name), "r")) {
Karl MacMillan 6847e8
		fprintf(stderr, "Error: failure opening input file for read.\n");
Karl MacMillan 6847e8
		return 1;
Chris PeBenito 89ec23
	}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Parse the file into a file_context linked list. */
Karl MacMillan 6847e8
	buf = NULL;
Karl MacMillan 6847e8
	while (getline(&line_buf, &line_len, in_file) {
Chris PeBenito 89ec23
		/* Get rid of whitespace from the front of the line. */
Karl MacMillan 6847e8
		for (i = 0; i < line_len; i++) {
Karl MacMillan 6847e8
			if (line_buf[i] != ' ' || line_buf[i] != '\t')
Karl MacMillan 6847e8
				break;
Karl MacMillan 6847e8
		}
Karl MacMillan 6847e8
		
Karl MacMillan 6847e8
		if (i >= line_len)
Karl MacMillan 6847e8
			continue;
Chris PeBenito 89ec23
		/* Check if the line isn't empty and isn't a comment */
Karl MacMillan 6847e8
		if (line_buf[i] == '#')
Karl MacMillan 6847e8
			continue;
Karl MacMillan 6847e8
		
Karl MacMillan 6847e8
		/* We have a valid line - allocate a new node. */
Karl MacMillan 6847e8
		temp = (file_context_node_t *)malloc(sizeof(file_context_node_t));
Karl MacMillan 6847e8
		if (!temp) {
Karl MacMillan 6847e8
			fprintf(stderr, "Error: failure allocating memory.\n");
Karl MacMillan 6847e8
			return 1;
Karl MacMillan 6847e8
		}
Karl MacMillan 6847e8
		memset(temp, 0, sizeof(file_context_node_t));
Chris PeBenito 89ec23
Karl MacMillan 6847e8
		/* Parse out the regular expression from the line. */
Karl MacMillan 6847e8
		start = i;
Karl MacMillan 6847e8
		while (i < line_len && (line_buf[i] != ' ' || line_buf[i] != '\t'))
Karl MacMillan 6847e8
			i++;
Karl MacMillan 6847e8
		finish = i;
Karl MacMillan 6847e8
		
Karl MacMillan 6847e8
		regex_len = start - finish;
Karl MacMillan 6847e8
		
Karl MacMillan 6847e8
		if (regex_len == 0) {
Karl MacMillan 6847e8
			file_context_node_destroy(temp);
Karl MacMillan 6847e8
			free(temp);
Karl MacMillan 6847e8
			continue;
Karl MacMillan 6847e8
		}
Chris PeBenito 89ec23
Karl MacMillan 6847e8
		temp->path = strndup(&line_buf[start], regex_len);
Karl MacMillan 6847e8
		if (!temp->path) {
Karl MacMillan 6847e8
			file_context_node_destroy(temp);
Karl MacMillan 6847e8
			free(temp);
Karl MacMillan 6847e8
			fprintf(stderr, "Memory error\n");
Karl MacMillan 6847e8
			return 1;
Karl MacMillan 6847e8
		}
Chris PeBenito 89ec23
Karl MacMillan 6847e8
		/* Get rid of whitespace after the regular expression. */
Karl MacMillan 6847e8
		for (; i < line_len; i++) {
Karl MacMillan 6847e8
			if (line_buf[i] != ' ' || line_buf[i] != '\t')
Karl MacMillan 6847e8
				break;
Karl MacMillan 6847e8
		}
Karl MacMillan 6847e8
		
Karl MacMillan 6847e8
		if (i == line_len) {
Karl MacMillan 6847e8
			file_context_node_destroy(temp);
Karl MacMillan 6847e8
			free(temp);
Karl MacMillan 6847e8
			continue;
Karl MacMillan 6847e8
		}
Chris PeBenito 89ec23
Karl MacMillan 6847e8
		/* Parse out the type from the line (if it 
Karl MacMillan 6847e8
			*  is there). */
Karl MacMillan 6847e8
		if (line_buf[i] == '-') {
Karl MacMillan 6847e8
			
Karl MacMillan 6847e8
			temp->type = (char *)malloc(sizeof(char) * 3);
Karl MacMillan 6847e8
			if (!(temp->type)) {
Karl MacMillan 6847e8
				fprintf(stderr, "Error: failure allocating memory.\n");
Karl MacMillan 6847e8
				return 1;
Chris PeBenito 89ec23
			}
Chris PeBenito 89ec23
Karl MacMillan 6847e8
			/* Fill the type into the array. */
Karl MacMillan 6847e8
			temp->type[0] = line_buf[i];
Karl MacMillan 6847e8
			temp->type[1] = line_buf[i + 1];
Karl MacMillan 6847e8
			i += 2;
Karl MacMillan 6847e8
			temp->type[2] = 0;
Chris PeBenito 89ec23
Karl MacMillan 6847e8
			/* Get rid of whitespace after the type. */
Karl MacMillan 6847e8
			while (line_buf[i] <= ' ')
Karl MacMillan 6847e8
				i++;
Karl MacMillan 6847e8
		}
Chris PeBenito 89ec23
Karl MacMillan 6847e8
		/* Parse out the context from the line. */
Karl MacMillan 6847e8
		start = i;
Karl MacMillan 6847e8
		while (line_buf[i] > ' ')
Karl MacMillan 6847e8
			i++;
Karl MacMillan 6847e8
		finish = i;
Karl MacMillan 6847e8
Karl MacMillan 6847e8
		/* Allocate a character array to hold the context. */
Karl MacMillan 6847e8
		temp->context =
Karl MacMillan 6847e8
			(char *) malloc(sizeof(char) *
Karl MacMillan 6847e8
					(finish - start + 1));
Karl MacMillan 6847e8
		if (!(temp->context)) {
Karl MacMillan 6847e8
			printf
Karl MacMillan 6847e8
				("Error: failure allocating memory.\n");
Karl MacMillan 6847e8
			return -1;
Chris PeBenito 89ec23
		}
Karl MacMillan 6847e8
		temp->context[0] = 0;
Karl MacMillan 6847e8
Karl MacMillan 6847e8
		/* Fill the context array. */
Karl MacMillan 6847e8
		temp->context[(finish - start)] = 0;
Karl MacMillan 6847e8
		for (j = 0; j < finish - start; j++) {
Karl MacMillan 6847e8
			temp->context[j] = line_buf[j + start];
Karl MacMillan 6847e8
		}
Karl MacMillan 6847e8
Karl MacMillan 6847e8
		/* Set all the data about the regular
Karl MacMillan 6847e8
			*  expression. */
Karl MacMillan 6847e8
		fc_fill_data(temp);
Karl MacMillan 6847e8
Karl MacMillan 6847e8
		/* Link this line of code at the end of
Karl MacMillan 6847e8
			*  the linked list. */
Karl MacMillan 6847e8
		current->next = temp;
Karl MacMillan 6847e8
		current = current->next;
Karl MacMillan 6847e8
		lines++;
Chris PeBenito 89ec23
	}
Karl MacMillan 6847e8
	free(buf);
Karl MacMillan 6847e8
	fclose(path);
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Create the bucket linked list from the earlier linked list. */
Chris PeBenito 89ec23
	current = head->next;
Karl MacMillan 6847e8
	bcurrent = master =
Karl MacMillan 6847e8
	    (file_context_bucket_t *)
Karl MacMillan 6847e8
	    malloc(sizeof(file_context_bucket_t));
Chris PeBenito 89ec23
	/* Go until all the nodes have been put in individual buckets. */
Karl MacMillan 6847e8
	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. */
Karl MacMillan 6847e8
		if (current) {
Karl MacMillan 6847e8
			bcurrent->next =
Karl MacMillan 6847e8
			    (file_context_bucket_t *)
Karl MacMillan 6847e8
			    malloc(sizeof(file_context_bucket_t));
Karl MacMillan 6847e8
			if (!(bcurrent->next)) {
Karl MacMillan 6847e8
				printf
Karl MacMillan 6847e8
				    ("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
Karl MacMillan 6847e8
			 *  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. */
Karl MacMillan 6847e8
	fc_merge_sort(master);
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	/* Open the output file. */
Karl MacMillan 6847e8
	if (!(path = fopen(argv[2], "w"))) {
Karl MacMillan 6847e8
		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;
Karl MacMillan 6847e8
	while (current) {
Chris PeBenito 89ec23
		/* Output the regular expression. */
Chris PeBenito 89ec23
		i = 0;
Karl MacMillan 6847e8
		while (current->regex[i] != 0) {
Karl MacMillan 6847e8
			fprintf(path, "%c", current->regex[i]);
Chris PeBenito 89ec23
			i++;
Chris PeBenito 89ec23
		}
Karl MacMillan 6847e8
		fprintf(path, "\t");
Karl MacMillan 6847e8
Chris PeBenito 89ec23
		/* Output the type, if there is one. */
Karl MacMillan 6847e8
		if (current->type) {
Chris PeBenito 89ec23
			i = 0;
Karl MacMillan 6847e8
			while (current->type[i] != 0) {
Karl MacMillan 6847e8
				fprintf(path, "%c", current->type[i]);
Chris PeBenito 89ec23
				i++;
Chris PeBenito 89ec23
			}
Karl MacMillan 6847e8
			fprintf(path, "\t");
Chris PeBenito 89ec23
		}
Chris PeBenito 89ec23
Chris PeBenito 89ec23
		/* Output the context. */
Chris PeBenito 89ec23
		i = 0;
Karl MacMillan 6847e8
		while (current->context[i] != 0) {
Karl MacMillan 6847e8
			fprintf(path, "%c", current->context[i]);
Chris PeBenito 89ec23
			i++;
Chris PeBenito 89ec23
		}
Karl MacMillan 6847e8
		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
Karl MacMillan 6847e8
		free(temp->regex);
Karl MacMillan 6847e8
		if (temp->type)
Karl MacMillan 6847e8
			free(temp->type);
Karl MacMillan 6847e8
		free(temp->context);
Karl MacMillan 6847e8
		free(temp);
Chris PeBenito 89ec23
	}
Karl MacMillan 6847e8
	free(master);
Chris PeBenito 89ec23
Karl MacMillan 6847e8
	fclose(path);
Chris PeBenito 89ec23
Chris PeBenito 89ec23
	return 0;
Chris PeBenito 89ec23
}