Chris PeBenito 17de1b7
/* Copyright 2005, Tresys Technology 
Chris PeBenito 17de1b7
 * 
Chris PeBenito 17de1b7
 * Some parts of this came from matchpathcon.c in libselinux
Chris PeBenito 17de1b7
 */
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
/* PURPOSE OF THIS PROGRAM
Chris PeBenito 17de1b7
 * The original setfiles sorting algorithm did not take into 
Chris PeBenito 17de1b7
 * account regular expression specificity. With the current 
Chris PeBenito 17de1b7
 * strict and targeted policies this is not an issue because 
Chris PeBenito 17de1b7
 * the file contexts are partially hand sorted and concatenated 
Chris PeBenito 17de1b7
 * in the right order so that the matches are generally correct.
Chris PeBenito 17de1b7
 * The way reference policy and loadable policy modules handle
Chris PeBenito 17de1b7
 * file contexts makes them come out in an unpredictable order
Chris PeBenito 17de1b7
 * and therefore setfiles (or this standalone tool) need to sort
Chris PeBenito 17de1b7
 * the regular expressions in a deterministic and stable way.
Chris PeBenito 17de1b7
 */
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
#define BUF_SIZE 4096;
Chris PeBenito 17de1b7
#define _GNU_SOURCE
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
#include <stdio.h>
Chris PeBenito 17de1b7
#include <stdlib.h>
Chris PeBenito 17de1b7
#include <string.h>
Chris PeBenito 17de1b7
#include <ctype.h>
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
typedef unsigned char bool_t;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
/* file_context_node
Chris PeBenito 17de1b7
 * A node used in a linked list of file contexts.c
Chris PeBenito 17de1b7
 * Each node contains the regular expression, the type and 
Chris PeBenito 17de1b7
 *  the context, as well as information about the regular
Chris PeBenito 17de1b7
 *  expression. The regular expression data (meta, stem_len
Chris PeBenito 17de1b7
 *  and str_len) can be filled in by using the fc_fill_data
Chris PeBenito 17de1b7
 *  function after the regular expression has been loaded.
Chris PeBenito 17de1b7
 * next points to the next node in the linked list.
Chris PeBenito 17de1b7
 */
Chris PeBenito 17de1b7
typedef struct file_context_node {
Chris PeBenito 17de1b7
	char *path;
Chris PeBenito 17de1b7
	char *file_type;
Chris PeBenito 17de1b7
	char *context;
Chris PeBenito 17de1b7
	bool_t meta;
Chris PeBenito 17de1b7
	int stem_len;
Chris PeBenito 17de1b7
	int str_len;
Chris PeBenito 17de1b7
	struct file_context_node *next;
Chris PeBenito 17de1b7
} file_context_node_t;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
void file_context_node_destroy(file_context_node_t *x)
Chris PeBenito 17de1b7
{
Chris PeBenito 17de1b7
	free(x->path);
Chris PeBenito 17de1b7
	free(x->file_type);
Chris PeBenito 17de1b7
	free(x->context);
Chris PeBenito 17de1b7
}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
/* file_context_bucket
Chris PeBenito 17de1b7
 * A node used in a linked list of buckets that contain
Chris PeBenito 17de1b7
 *  file_context_node's.
Chris PeBenito 17de1b7
 * Each node contains a pointer to a file_context_node which
Chris PeBenito 17de1b7
 *  is the header of its linked list. This linked list is the
Chris PeBenito 17de1b7
 *  content of this bucket.
Chris PeBenito 17de1b7
 * next points to the next bucket in the linked list.
Chris PeBenito 17de1b7
 */
Chris PeBenito 17de1b7
typedef struct file_context_bucket {
Chris PeBenito 17de1b7
	file_context_node_t *data;
Chris PeBenito 17de1b7
	struct file_context_bucket *next;
Chris PeBenito 17de1b7
} file_context_bucket_t;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
/* fc_compare
Chris PeBenito 17de1b7
 * Compares two file contexts' regular expressions and returns:
Chris PeBenito 17de1b7
 *    -1 if a is less specific than b
Chris PeBenito 17de1b7
 *     0 if a and be are equally specific
Chris PeBenito 17de1b7
 *     1 if a is more specific than b
Chris PeBenito 17de1b7
 * The comparison is based on the following statements,
Chris PeBenito 17de1b7
 *  in order from most important to least important, given a and b:
Chris PeBenito 17de1b7
 *     If a is a regular expression and b is not,
Chris PeBenito 17de1b7
 *      -> a is less specific than b.
Chris PeBenito 17de1b7
 *     If a's stem length is shorter than b's stem length,
Chris PeBenito 17de1b7
 *      -> a is less specific than b.
Chris PeBenito 17de1b7
 *     If a's string length is shorter than b's string length,
Chris PeBenito 17de1b7
 *      -> a is less specific than b.
Chris PeBenito 17de1b7
 *     If a does not have a specified type and b does not,
Chris PeBenito 17de1b7
 *      -> a is less specific than b.
Chris PeBenito 17de1b7
 */
Chris PeBenito 17de1b7
int fc_compare(file_context_node_t *a, file_context_node_t *b)
Chris PeBenito 17de1b7
{
Chris PeBenito 17de1b7
	/* Check to see if either a or b have meta characters
Chris PeBenito 17de1b7
	 *  and the other doesn't. */
Chris PeBenito 17de1b7
	if (a->meta && !b->meta)
Chris PeBenito 17de1b7
		return -1;
Chris PeBenito 17de1b7
	if (b->meta && !a->meta)
Chris PeBenito 17de1b7
		return 1;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Check to see if either a or b have a shorter stem
Chris PeBenito 17de1b7
	 *  length than the other. */
Chris PeBenito 17de1b7
	if (a->stem_len < b->stem_len)
Chris PeBenito 17de1b7
		return -1;
Chris PeBenito 17de1b7
	if (b->stem_len < a->stem_len)
Chris PeBenito 17de1b7
		return 1;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Check to see if either a or b have a shorter string
Chris PeBenito 17de1b7
	 *  length than the other. */
Chris PeBenito 17de1b7
	if (a->str_len < b->str_len)
Chris PeBenito 17de1b7
		return -1;
Chris PeBenito 17de1b7
	if (b->str_len < a->str_len)
Chris PeBenito 17de1b7
		return 1;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Check to see if either a or b has a specified type
Chris PeBenito 17de1b7
	 *  and the other doesn't. */
Chris PeBenito 17de1b7
	if (!a->file_type && b->file_type)
Chris PeBenito 17de1b7
		return -1;
Chris PeBenito 17de1b7
	if (!b->file_type && a->file_type)
Chris PeBenito 17de1b7
		return 1;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* If none of the above conditions were satisfied, 
Chris PeBenito 17de1b7
	 * then a and b are equally specific. */
Chris PeBenito 17de1b7
	return 0;
Chris PeBenito 17de1b7
}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
/* fc_merge
Chris PeBenito 17de1b7
 * Merges two sorted file context linked lists into one
Chris PeBenito 17de1b7
 *  sorted one.
Chris PeBenito 17de1b7
 * Pass two lists a and b, and after the completion of fc_merge,
Chris PeBenito 17de1b7
 *  the final list is contained in a, and b is empty.
Chris PeBenito 17de1b7
 */
Chris PeBenito 17de1b7
file_context_node_t *fc_merge(file_context_node_t *a,
Chris PeBenito 17de1b7
				   file_context_node_t *b)
Chris PeBenito 17de1b7
{
Chris PeBenito 17de1b7
	file_context_node_t *a_current;
Chris PeBenito 17de1b7
	file_context_node_t *b_current;
Chris PeBenito 17de1b7
	file_context_node_t *temp;
Chris PeBenito 17de1b7
	file_context_node_t *jumpto;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* If a is a empty list, and b is not,
Chris PeBenito 17de1b7
	 *  set a as b and proceed to the end. */
Chris PeBenito 17de1b7
	if (!a && b)
Chris PeBenito 17de1b7
		a = b;
Chris PeBenito 17de1b7
	/* If b is an empty list, leave a as it is. */
Chris PeBenito 17de1b7
	else if (!b) {
Chris PeBenito 17de1b7
	} else {
Chris PeBenito 17de1b7
		/* Make it so the list a has the lesser
Chris PeBenito 17de1b7
		 *  first element always. */
Chris PeBenito 17de1b7
		if (fc_compare(a, b) == 1) {
Chris PeBenito 17de1b7
			temp = a;
Chris PeBenito 17de1b7
			a = b;
Chris PeBenito 17de1b7
			b = temp;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
		a_current = a;
Chris PeBenito 17de1b7
		b_current = b;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* Merge by inserting b's nodes in between a's nodes. */
Chris PeBenito 17de1b7
		while (a_current->next && b_current) {
Chris PeBenito 17de1b7
			jumpto = a_current->next;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			/* Insert b's nodes in between the current a node
Chris PeBenito 17de1b7
			 *  and the next a node.*/
Chris PeBenito 17de1b7
			while (b_current && a_current->next &&
Chris PeBenito 17de1b7
			       fc_compare(a_current->next,
Chris PeBenito 17de1b7
					  b_current) != -1) {
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
				temp = a_current->next;
Chris PeBenito 17de1b7
				a_current->next = b_current;
Chris PeBenito 17de1b7
				b_current = b_current->next;
Chris PeBenito 17de1b7
				a_current->next->next = temp;
Chris PeBenito 17de1b7
				a_current = a_current->next;
Chris PeBenito 17de1b7
			}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			/* Skip all the inserted node from b to the
Chris PeBenito 17de1b7
			 *  next node in the original a. */
Chris PeBenito 17de1b7
			a_current = jumpto;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* if there is anything left in b to be inserted,
Chris PeBenito 17de1b7
		   put it on the end */
Chris PeBenito 17de1b7
		if (b_current) {
Chris PeBenito 17de1b7
			a_current->next = b_current;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
	}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	return a;
Chris PeBenito 17de1b7
}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
/* fc_merge_sort
Chris PeBenito 17de1b7
 * Sorts file contexts from least specific to more specific.
Chris PeBenito 17de1b7
 * The bucket linked list is passed and after the completion
Chris PeBenito 17de1b7
 *  of the fc_merge_sort function, there is only one bucket
Chris PeBenito 17de1b7
 *  (pointed to by master) that contains a linked list
Chris PeBenito 17de1b7
 *  of all the file contexts, in sorted order.
Chris PeBenito 17de1b7
 * Explanation of the algorithm:
Chris PeBenito 17de1b7
 *  The algorithm implemented in fc_merge_sort is an iterative
Chris PeBenito 17de1b7
 *   implementation of merge sort.
Chris PeBenito 17de1b7
 *  At first, each bucket has a linked list of file contexts
Chris PeBenito 17de1b7
 *   that are 1 element each.
Chris PeBenito 17de1b7
 *  Each pass, each odd numbered bucket is merged into the bucket
Chris PeBenito 17de1b7
 *   before it. This halves the number of buckets each pass.
Chris PeBenito 17de1b7
 *  It will continue passing over the buckets (as described above)
Chris PeBenito 17de1b7
 *   until there is only  one bucket left, containing the list of
Chris PeBenito 17de1b7
 *   file contexts, sorted.
Chris PeBenito 17de1b7
 */
Chris PeBenito 17de1b7
void fc_merge_sort(file_context_bucket_t *master)
Chris PeBenito 17de1b7
{
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	file_context_bucket_t *current;
Chris PeBenito 17de1b7
	file_context_bucket_t *temp;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Loop until master is the only bucket left
Chris PeBenito 17de1b7
	 * so that this will stop when master contains
Chris PeBenito 17de1b7
	 * the sorted list. */
Chris PeBenito 17de1b7
	while (master->next) {
Chris PeBenito 17de1b7
		current = master;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* This loop merges buckets two-by-two. */
Chris PeBenito 17de1b7
		while (current) {
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			if (current->next) {
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
				current->data =
Chris PeBenito 17de1b7
				    fc_merge(current->data,
Chris PeBenito 17de1b7
					     current->next->data);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
				temp = current->next;
Chris PeBenito 17de1b7
				current->next = current->next->next;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
				free(temp);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			current = current->next;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
	}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
/* fc_fill_data
Chris PeBenito 17de1b7
 * This processes a regular expression in a file context
Chris PeBenito 17de1b7
 *  and sets the data held in file_context_node, namely
Chris PeBenito 17de1b7
 *  meta, str_len and stem_len. 
Chris PeBenito 17de1b7
 * The following changes are made to fc_node after the
Chris PeBenito 17de1b7
 *  the completion of the function:
Chris PeBenito 17de1b7
 *     fc_node->meta =		1 if path has a meta character, 0 if not.
Chris PeBenito 17de1b7
 *     fc_node->str_len =	The string length of the entire path
Chris PeBenito 17de1b7
 *     fc_node->stem_len = 	The number of characters up until
Chris PeBenito 17de1b7
 *				 the first meta character.
Chris PeBenito 17de1b7
 */
Chris PeBenito 17de1b7
void fc_fill_data(file_context_node_t *fc_node)
Chris PeBenito 17de1b7
{
Chris PeBenito 17de1b7
	int c = 0;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	fc_node->meta = 0;
Chris PeBenito 17de1b7
	fc_node->stem_len = 0;
Chris PeBenito 17de1b7
	fc_node->str_len = 0;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Process until the string termination character
Chris PeBenito 17de1b7
	 *  has been reached.
Chris PeBenito 17de1b7
	 * Note: this while loop has been adapted from
Chris PeBenito 17de1b7
	 *  spec_hasMetaChars in matchpathcon.c from
Chris PeBenito 17de1b7
	 *  libselinux-1.22. */
Chris PeBenito 17de1b7
	while (fc_node->path[c] != '\0') {
Chris PeBenito 17de1b7
		switch (fc_node->path[c]) {
Chris PeBenito 17de1b7
		case '.':
Chris PeBenito 17de1b7
		case '^':
Chris PeBenito 17de1b7
		case '$':
Chris PeBenito 17de1b7
		case '?':
Chris PeBenito 17de1b7
		case '*':
Chris PeBenito 17de1b7
		case '+':
Chris PeBenito 17de1b7
		case '|':
Chris PeBenito 17de1b7
		case '[':
Chris PeBenito 17de1b7
		case '(':
Chris PeBenito 17de1b7
		case '{':
Chris PeBenito 17de1b7
			/* If a meta character is found,
Chris PeBenito 17de1b7
			 *  set meta to one */
Chris PeBenito 17de1b7
			fc_node->meta = 1;
Chris PeBenito 17de1b7
			break;
Chris PeBenito 17de1b7
		case '\\':
Chris PeBenito 17de1b7
			/* If a escape character is found,
Chris PeBenito 17de1b7
			 *  skip the next character. */
Chris PeBenito 17de1b7
			c++;
Chris PeBenito 17de1b7
		default:
Chris PeBenito 17de1b7
			/* If no meta character has been found yet,
Chris PeBenito 17de1b7
			 *  add one to the stem length. */
Chris PeBenito 17de1b7
			if (!fc_node->meta)
Chris PeBenito 17de1b7
				fc_node->stem_len++;
Chris PeBenito 17de1b7
			break;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		fc_node->str_len++;
Chris PeBenito 17de1b7
		c++;
Chris PeBenito 17de1b7
	}
Chris PeBenito 17de1b7
}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
/* main
Chris PeBenito 17de1b7
 * This program takes in two arguments, the input filename and the
Chris PeBenito 17de1b7
 *  output filename. The input file should be syntactically correct.
Chris PeBenito 17de1b7
 * Overall what is done in the main is read in the file and store each
Chris PeBenito 17de1b7
 *  line of code, sort it, then output it to the output file.
Chris PeBenito 17de1b7
 */
Chris PeBenito 17de1b7
int main(int argc, char *argv[])
Chris PeBenito 17de1b7
{
Chris PeBenito 17de1b7
	int lines;
Chris PeBenito 17de1b7
	size_t start, finish, regex_len, context_len;
Chris PeBenito 17de1b7
	size_t line_len, buf_len, i, j;
Chris PeBenito 17de1b7
	char *input_name, *output_name, *line_buf;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	file_context_node_t *temp;
Chris PeBenito 17de1b7
	file_context_node_t *head;
Chris PeBenito 17de1b7
	file_context_node_t *current;
Chris PeBenito 17de1b7
	file_context_bucket_t *master;
Chris PeBenito 17de1b7
	file_context_bucket_t *bcurrent;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	FILE *in_file, *out_file;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Check for the correct number of command line arguments. */
Chris PeBenito 17de1b7
	if (argc != 3) {
Chris PeBenito 17de1b7
		fprintf(stderr, "Usage: %s <infile> <outfile>\n",argv[0]);
Chris PeBenito 17de1b7
		return 1;
Chris PeBenito 17de1b7
	}
Chris PeBenito 17de1b7
	
Chris PeBenito 17de1b7
	input_name = argv[1];
Chris PeBenito 17de1b7
	output_name = argv[2];
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	i = j = lines = 0;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Open the input file. */
Chris PeBenito 17de1b7
	if (!(in_file = fopen(input_name, "r"))) {
Chris PeBenito 17de1b7
		fprintf(stderr, "Error: failure opening input file for read.\n");
Chris PeBenito 17de1b7
		return 1;
Chris PeBenito 17de1b7
	}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Initialize the head of the linked list. */
Chris PeBenito 17de1b7
	head = current = (file_context_node_t*)malloc(sizeof(file_context_node_t));
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Parse the file into a file_context linked list. */
Chris PeBenito 17de1b7
	line_buf = NULL;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	while ( getline(&line_buf, &buf_len, in_file) != -1 ){
Chris PeBenito 17de1b7
		line_len = strlen(line_buf);
Chris PeBenito 17de1b7
		if( line_len == 0 || line_len == 1)
Chris PeBenito 17de1b7
			continue;
Chris PeBenito 17de1b7
		/* Get rid of whitespace from the front of the line. */
Chris PeBenito 17de1b7
		for (i = 0; i < line_len; i++) {
Chris PeBenito 17de1b7
			if (!isspace(line_buf[i]))
Chris PeBenito 17de1b7
				break;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		if (i >= line_len)
Chris PeBenito 17de1b7
			continue;
Chris PeBenito 17de1b7
		/* Check if the line isn't empty and isn't a comment */
Chris PeBenito 17de1b7
		if (line_buf[i] == '#')
Chris PeBenito 17de1b7
			continue;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* We have a valid line - allocate a new node. */
Chris PeBenito 17de1b7
		temp = (file_context_node_t *)malloc(sizeof(file_context_node_t));
Chris PeBenito 17de1b7
		if (!temp) {
Chris PeBenito 17de1b7
			fprintf(stderr, "Error: failure allocating memory.\n");
Chris PeBenito 17de1b7
			return 1;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
		temp->next = NULL;
Chris PeBenito 17de1b7
		memset(temp, 0, sizeof(file_context_node_t));
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* Parse out the regular expression from the line. */
Chris PeBenito 17de1b7
		start = i;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		while (i < line_len && (!isspace(line_buf[i])))
Chris PeBenito 17de1b7
			i++;
Chris PeBenito 17de1b7
		finish = i;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		regex_len = finish - start;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		if (regex_len == 0) {
Chris PeBenito 17de1b7
			file_context_node_destroy(temp);
Chris PeBenito 17de1b7
			free(temp);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			continue;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
		
Chris PeBenito 17de1b7
		temp->path = (char*)strndup(&line_buf[start], regex_len);
Chris PeBenito 17de1b7
		if (!temp->path) {
Chris PeBenito 17de1b7
			file_context_node_destroy(temp);
Chris PeBenito 17de1b7
			free(temp);
Chris PeBenito 17de1b7
			fprintf(stderr, "Error: failure allocating memory.\n");
Chris PeBenito 17de1b7
			return 1;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* Get rid of whitespace after the regular expression. */
Chris PeBenito 17de1b7
		for (; i < line_len; i++) {
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			if (!isspace(line_buf[i]))
Chris PeBenito 17de1b7
				break;
Chris PeBenito 17de1b7
		}	
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		if (i == line_len) {
Chris PeBenito 17de1b7
			file_context_node_destroy(temp);
Chris PeBenito 17de1b7
			free(temp);
Chris PeBenito 17de1b7
			continue;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* Parse out the type from the line (if it 
Chris PeBenito 17de1b7
			*  is there). */
Chris PeBenito 17de1b7
		if (line_buf[i] == '-') {
Chris PeBenito 17de1b7
			temp->file_type = (char *)malloc(sizeof(char) * 3);
Chris PeBenito 17de1b7
			if (!(temp->file_type)) {
Chris PeBenito 17de1b7
				fprintf(stderr, "Error: failure allocating memory.\n");
Chris PeBenito 17de1b7
				return 1;
Chris PeBenito 17de1b7
			}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			if( i + 2 >= line_len ) {
Chris PeBenito 17de1b7
				file_context_node_destroy(temp);
Chris PeBenito 17de1b7
				free(temp);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
				continue;
Chris PeBenito 17de1b7
			}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			/* Fill the type into the array. */
Chris PeBenito 17de1b7
			temp->file_type[0] = line_buf[i];
Chris PeBenito 17de1b7
			temp->file_type[1] = line_buf[i + 1];
Chris PeBenito 17de1b7
			i += 2;
Chris PeBenito 17de1b7
			temp->file_type[2] = 0;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			/* Get rid of whitespace after the type. */
Chris PeBenito 17de1b7
			for (; i < line_len; i++) {
Chris PeBenito 17de1b7
				if (!isspace(line_buf[i]))
Chris PeBenito 17de1b7
					break;
Chris PeBenito 17de1b7
			}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			if (i == line_len) {
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
				file_context_node_destroy(temp);
Chris PeBenito 17de1b7
				free(temp);
Chris PeBenito 17de1b7
				continue;
Chris PeBenito 17de1b7
			}
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* Parse out the context from the line. */
Chris PeBenito 17de1b7
		start = i;
Chris PeBenito 17de1b7
		while (i < line_len && (!isspace(line_buf[i])))
Chris PeBenito 17de1b7
			i++;
Chris PeBenito 17de1b7
		finish = i;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		context_len = finish - start;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		temp->context = (char*)strndup(&line_buf[start], context_len);
Chris PeBenito 17de1b7
		if (!temp->context) {
Chris PeBenito 17de1b7
			file_context_node_destroy(temp);
Chris PeBenito 17de1b7
			free(temp);
Chris PeBenito 17de1b7
			fprintf(stderr, "Error: failure allocating memory.\n");
Chris PeBenito 17de1b7
			return 1;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* Set all the data about the regular
Chris PeBenito 17de1b7
			*  expression. */
Chris PeBenito 17de1b7
		fc_fill_data(temp);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* Link this line of code at the end of
Chris PeBenito 17de1b7
			*  the linked list. */
Chris PeBenito 17de1b7
		current->next = temp;
Chris PeBenito 17de1b7
		current = current->next;
Chris PeBenito 17de1b7
		lines++;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		free(line_buf);
Chris PeBenito 17de1b7
		line_buf = NULL;
Chris PeBenito 17de1b7
	}
Chris PeBenito 17de1b7
	fclose(in_file);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Create the bucket linked list from the earlier linked list. */
Chris PeBenito 17de1b7
	current = head->next;
Chris PeBenito 17de1b7
	bcurrent = master =
Chris PeBenito 17de1b7
	    (file_context_bucket_t *)
Chris PeBenito 17de1b7
	    malloc(sizeof(file_context_bucket_t));
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Go until all the nodes have been put in individual buckets. */
Chris PeBenito 17de1b7
	while (current) {
Chris PeBenito 17de1b7
		/* Copy over the file context line into the bucket. */
Chris PeBenito 17de1b7
		bcurrent->data = current;
Chris PeBenito 17de1b7
		current = current->next;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* Detatch the node in the bucket from the old list. */
Chris PeBenito 17de1b7
		bcurrent->data->next = NULL;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* If there should be another bucket, put one at the end. */
Chris PeBenito 17de1b7
		if (current) {
Chris PeBenito 17de1b7
			bcurrent->next =
Chris PeBenito 17de1b7
			    (file_context_bucket_t *)
Chris PeBenito 17de1b7
			    malloc(sizeof(file_context_bucket_t));
Chris PeBenito 17de1b7
			if (!(bcurrent->next)) {
Chris PeBenito 17de1b7
				printf
Chris PeBenito 17de1b7
				    ("Error: failure allocating memory.\n");
Chris PeBenito 17de1b7
				return -1;
Chris PeBenito 17de1b7
			}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			/* Make sure the new bucket thinks it's the end of the
Chris PeBenito 17de1b7
			 *  list. */
Chris PeBenito 17de1b7
			bcurrent->next->next = NULL;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
			bcurrent = bcurrent->next;
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Sort the bucket list. */
Chris PeBenito 17de1b7
	fc_merge_sort(master);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Open the output file. */
Chris PeBenito 17de1b7
	if (!(out_file = fopen(argv[2], "w"))) {
Chris PeBenito 17de1b7
		printf("Error: failure opening output file for write.\n");
Chris PeBenito 17de1b7
		return -1;
Chris PeBenito 17de1b7
	}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	/* Output the sorted file_context linked list to the output file. */
Chris PeBenito 17de1b7
	current = master->data;
Chris PeBenito 17de1b7
	while (current) {
Chris PeBenito 17de1b7
		/* Output the path. */
Chris PeBenito 17de1b7
		fprintf(out_file, "%s\t\t", current->path);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* Output the type, if there is one. */
Chris PeBenito 17de1b7
		if (current->file_type) {
Chris PeBenito 17de1b7
			fprintf(out_file, "%s\t", current->file_type);
Chris PeBenito 17de1b7
		}
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* Output the context. */
Chris PeBenito 17de1b7
		fprintf(out_file, "%s\n", current->context);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		/* Remove the node. */
Chris PeBenito 17de1b7
		temp = current;
Chris PeBenito 17de1b7
		current = current->next;
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
		file_context_node_destroy(temp);
Chris PeBenito 17de1b7
		free(temp);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	}
Chris PeBenito 17de1b7
	free(master);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	fclose(out_file);
Chris PeBenito 17de1b7
Chris PeBenito 17de1b7
	return 0;
Chris PeBenito 17de1b7
}