|
Chris PeBenito |
dc5daf |
/* Copyright 2005, Tresys Technology
|
|
Chris PeBenito |
dc5daf |
*
|
|
Chris PeBenito |
dc5daf |
* Some parts of this came from matchpathcon.c in libselinux
|
|
Chris PeBenito |
dc5daf |
*/
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
/* PURPOSE OF THIS PROGRAM
|
|
Chris PeBenito |
dc5daf |
* The original setfiles sorting algorithm did not take into
|
|
Chris PeBenito |
dc5daf |
* account regular expression specificity. With the current
|
|
Chris PeBenito |
dc5daf |
* strict and targeted policies this is not an issue because
|
|
Chris PeBenito |
dc5daf |
* the file contexts are partially hand sorted and concatenated
|
|
Chris PeBenito |
dc5daf |
* in the right order so that the matches are generally correct.
|
|
Chris PeBenito |
dc5daf |
* The way reference policy and loadable policy modules handle
|
|
Chris PeBenito |
dc5daf |
* file contexts makes them come out in an unpredictable order
|
|
Chris PeBenito |
dc5daf |
* and therefore setfiles (or this standalone tool) need to sort
|
|
Chris PeBenito |
dc5daf |
* the regular expressions in a deterministic and stable way.
|
|
Chris PeBenito |
dc5daf |
*/
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
#define BUF_SIZE 4096;
|
|
Chris PeBenito |
dc5daf |
#define _GNU_SOURCE
|
|
Karl MacMillan |
6847e8 |
|
|
Chris PeBenito |
89ec23 |
#include <stdio.h>
|
|
Chris PeBenito |
dc5daf |
#include <stdlib.h>
|
|
Chris PeBenito |
dc5daf |
#include <string.h>
|
|
Chris PeBenito |
dc5daf |
#include <ctype.h>
|
|
Chris PeBenito |
89ec23 |
|
|
Karl MacMillan |
6847e8 |
typedef unsigned char bool_t;
|
|
Karl MacMillan |
6847e8 |
|
|
Chris PeBenito |
89ec23 |
/* file_context_node
|
|
Chris PeBenito |
dc5daf |
* A node used in a linked list of file contexts.c
|
|
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 {
|
|
Chris PeBenito |
dc5daf |
char *path;
|
|
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 |
{
|
|
Chris PeBenito |
dc5daf |
free(x->path);
|
|
Karl MacMillan |
6847e8 |
free(x->file_type);
|
|
Karl MacMillan |
6847e8 |
free(x->context);
|
|
Karl MacMillan |
6847e8 |
}
|
|
Chris PeBenito |
89ec23 |
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
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 |
dc5daf |
/* fc_compare
|
|
Chris PeBenito |
dc5daf |
* Compares two file contexts' regular expressions and returns:
|
|
Chris PeBenito |
dc5daf |
* -1 if a is less specific than b
|
|
Chris PeBenito |
dc5daf |
* 0 if a and be are equally specific
|
|
Chris PeBenito |
dc5daf |
* 1 if a is more specific than b
|
|
Chris PeBenito |
dc5daf |
* The comparison is based on the following statements,
|
|
Chris PeBenito |
dc5daf |
* in order from most important to least important, given a and b:
|
|
Chris PeBenito |
dc5daf |
* If a is a regular expression and b is not,
|
|
Chris PeBenito |
dc5daf |
* -> a is less specific than b.
|
|
Chris PeBenito |
dc5daf |
* If a's stem length is shorter than b's stem length,
|
|
Chris PeBenito |
dc5daf |
* -> a is less specific than b.
|
|
Chris PeBenito |
dc5daf |
* If a's string length is shorter than b's string length,
|
|
Chris PeBenito |
dc5daf |
* -> a is less specific than b.
|
|
Chris PeBenito |
dc5daf |
* If a does not have a specified type and b does not,
|
|
Chris PeBenito |
dc5daf |
* -> a is less specific than b.
|
|
Chris PeBenito |
dc5daf |
*/
|
|
Chris PeBenito |
dc5daf |
int fc_compare(file_context_node_t *a, file_context_node_t *b)
|
|
Chris PeBenito |
dc5daf |
{
|
|
Chris PeBenito |
dc5daf |
/* Check to see if either a or b have meta characters
|
|
Chris PeBenito |
dc5daf |
* and the other doesn't. */
|
|
Chris PeBenito |
dc5daf |
if (a->meta && !b->meta)
|
|
Chris PeBenito |
dc5daf |
return -1;
|
|
Chris PeBenito |
dc5daf |
if (b->meta && !a->meta)
|
|
Chris PeBenito |
dc5daf |
return 1;
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
/* Check to see if either a or b have a shorter stem
|
|
Chris PeBenito |
dc5daf |
* length than the other. */
|
|
Chris PeBenito |
dc5daf |
if (a->stem_len < b->stem_len)
|
|
Chris PeBenito |
dc5daf |
return -1;
|
|
Chris PeBenito |
dc5daf |
if (b->stem_len < a->stem_len)
|
|
Chris PeBenito |
dc5daf |
return 1;
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
/* Check to see if either a or b have a shorter string
|
|
Chris PeBenito |
dc5daf |
* length than the other. */
|
|
Chris PeBenito |
dc5daf |
if (a->str_len < b->str_len)
|
|
Chris PeBenito |
dc5daf |
return -1;
|
|
Chris PeBenito |
cd8fa4 |
if (b->str_len < a->str_len)
|
|
Chris PeBenito |
dc5daf |
return 1;
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
/* Check to see if either a or b has a specified type
|
|
Chris PeBenito |
dc5daf |
* and the other doesn't. */
|
|
Chris PeBenito |
dc5daf |
if (!a->file_type && b->file_type)
|
|
Chris PeBenito |
dc5daf |
return -1;
|
|
Chris PeBenito |
dc5daf |
if (!b->file_type && a->file_type)
|
|
Chris PeBenito |
dc5daf |
return 1;
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
/* If none of the above conditions were satisfied,
|
|
Chris PeBenito |
dc5daf |
* then a and b are equally specific. */
|
|
Chris PeBenito |
dc5daf |
return 0;
|
|
Chris PeBenito |
dc5daf |
}
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
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 |
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
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 |
|
|
Chris PeBenito |
dc5daf |
/* Merge by inserting b's nodes in between a's nodes. */
|
|
Karl MacMillan |
6847e8 |
while (a_current->next && b_current) {
|
|
Karl MacMillan |
6847e8 |
jumpto = a_current->next;
|
|
Karl MacMillan |
6847e8 |
|
|
Chris PeBenito |
dc5daf |
/* Insert b's nodes in between 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) {
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
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 |
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 |
{
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
89ec23 |
|
|
Karl MacMillan |
6847e8 |
file_context_bucket_t *current;
|
|
Karl MacMillan |
6847e8 |
file_context_bucket_t *temp;
|
|
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) {
|
|
Chris PeBenito |
dc5daf |
|
|
Karl MacMillan |
6847e8 |
if (current->next) {
|
|
Chris PeBenito |
dc5daf |
|
|
Karl MacMillan |
6847e8 |
current->data =
|
|
Karl MacMillan |
6847e8 |
fc_merge(current->data,
|
|
Karl MacMillan |
6847e8 |
current->next->data);
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
89ec23 |
temp = current->next;
|
|
Karl MacMillan |
6847e8 |
current->next = current->next->next;
|
|
Chris PeBenito |
dc5daf |
|
|
Karl MacMillan |
6847e8 |
free(temp);
|
|
Chris PeBenito |
dc5daf |
|
|
Karl MacMillan |
6847e8 |
}
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
Karl MacMillan |
6847e8 |
current = current->next;
|
|
Karl MacMillan |
6847e8 |
}
|
|
Karl MacMillan |
6847e8 |
}
|
|
Chris PeBenito |
89ec23 |
|
|
Chris PeBenito |
89ec23 |
|
|
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 |
dc5daf |
* fc_node->meta = 1 if path has a meta character, 0 if not.
|
|
Chris PeBenito |
dc5daf |
* fc_node->str_len = The string length of the entire path
|
|
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. */
|
|
Chris PeBenito |
dc5daf |
while (fc_node->path[c] != '\0') {
|
|
Chris PeBenito |
dc5daf |
switch (fc_node->path[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;
|
|
Chris PeBenito |
dc5daf |
size_t start, finish, regex_len, context_len;
|
|
Chris PeBenito |
dc5daf |
size_t line_len, buf_len, i, j;
|
|
Chris PeBenito |
dc5daf |
char *input_name, *output_name, *line_buf;
|
|
Chris PeBenito |
dc5daf |
|
|
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_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 |
dc5daf |
|
|
Chris PeBenito |
89ec23 |
/* Check for the correct number of command line arguments. */
|
|
Karl MacMillan |
6847e8 |
if (argc != 3) {
|
|
Chris PeBenito |
dc5daf |
fprintf(stderr, "Usage: %s <infile> <outfile>\n",argv[0]);
|
|
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 |
/* Open the input file. */
|
|
Chris PeBenito |
dc5daf |
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 |
dc5daf |
/* Initialize the head of the linked list. */
|
|
Chris PeBenito |
dc5daf |
head = current = (file_context_node_t*)malloc(sizeof(file_context_node_t));
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
89ec23 |
/* Parse the file into a file_context linked list. */
|
|
Chris PeBenito |
dc5daf |
line_buf = NULL;
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
while ( getline(&line_buf, &buf_len, in_file) != -1 ){
|
|
Chris PeBenito |
dc5daf |
line_len = strlen(line_buf);
|
|
Chris PeBenito |
dc5daf |
if( line_len == 0 || line_len == 1)
|
|
Chris PeBenito |
dc5daf |
continue;
|
|
Chris PeBenito |
89ec23 |
/* Get rid of whitespace from the front of the line. */
|
|
Karl MacMillan |
6847e8 |
for (i = 0; i < line_len; i++) {
|
|
Chris PeBenito |
dc5daf |
if (!isspace(line_buf[i]))
|
|
Karl MacMillan |
6847e8 |
break;
|
|
Karl MacMillan |
6847e8 |
}
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
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;
|
|
Chris PeBenito |
dc5daf |
|
|
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 |
}
|
|
Chris PeBenito |
dc5daf |
temp->next = NULL;
|
|
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;
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
while (i < line_len && (!isspace(line_buf[i])))
|
|
Karl MacMillan |
6847e8 |
i++;
|
|
Karl MacMillan |
6847e8 |
finish = i;
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
regex_len = finish - start;
|
|
Chris PeBenito |
dc5daf |
|
|
Karl MacMillan |
6847e8 |
if (regex_len == 0) {
|
|
Karl MacMillan |
6847e8 |
file_context_node_destroy(temp);
|
|
Karl MacMillan |
6847e8 |
free(temp);
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
Karl MacMillan |
6847e8 |
continue;
|
|
Karl MacMillan |
6847e8 |
}
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
temp->path = (char*)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);
|
|
Chris PeBenito |
dc5daf |
fprintf(stderr, "Error: failure allocating memory.\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++) {
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
if (!isspace(line_buf[i]))
|
|
Karl MacMillan |
6847e8 |
break;
|
|
Chris PeBenito |
dc5daf |
}
|
|
Chris PeBenito |
dc5daf |
|
|
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] == '-') {
|
|
Chris PeBenito |
dc5daf |
temp->file_type = (char *)malloc(sizeof(char) * 3);
|
|
Chris PeBenito |
dc5daf |
if (!(temp->file_type)) {
|
|
Karl MacMillan |
6847e8 |
fprintf(stderr, "Error: failure allocating memory.\n");
|
|
Karl MacMillan |
6847e8 |
return 1;
|
|
Chris PeBenito |
89ec23 |
}
|
|
Chris PeBenito |
89ec23 |
|
|
Chris PeBenito |
dc5daf |
if( i + 2 >= line_len ) {
|
|
Chris PeBenito |
dc5daf |
file_context_node_destroy(temp);
|
|
Chris PeBenito |
dc5daf |
free(temp);
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
continue;
|
|
Chris PeBenito |
dc5daf |
}
|
|
Chris PeBenito |
dc5daf |
|
|
Karl MacMillan |
6847e8 |
/* Fill the type into the array. */
|
|
Chris PeBenito |
dc5daf |
temp->file_type[0] = line_buf[i];
|
|
Chris PeBenito |
dc5daf |
temp->file_type[1] = line_buf[i + 1];
|
|
Karl MacMillan |
6847e8 |
i += 2;
|
|
Chris PeBenito |
dc5daf |
temp->file_type[2] = 0;
|
|
Chris PeBenito |
89ec23 |
|
|
Karl MacMillan |
6847e8 |
/* Get rid of whitespace after the type. */
|
|
Chris PeBenito |
dc5daf |
for (; i < line_len; i++) {
|
|
Chris PeBenito |
dc5daf |
if (!isspace(line_buf[i]))
|
|
Chris PeBenito |
dc5daf |
break;
|
|
Chris PeBenito |
dc5daf |
}
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
if (i == line_len) {
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
file_context_node_destroy(temp);
|
|
Chris PeBenito |
dc5daf |
free(temp);
|
|
Chris PeBenito |
dc5daf |
continue;
|
|
Chris PeBenito |
dc5daf |
}
|
|
Karl MacMillan |
6847e8 |
}
|
|
Chris PeBenito |
89ec23 |
|
|
Karl MacMillan |
6847e8 |
/* Parse out the context from the line. */
|
|
Karl MacMillan |
6847e8 |
start = i;
|
|
Chris PeBenito |
dc5daf |
while (i < line_len && (!isspace(line_buf[i])))
|
|
Karl MacMillan |
6847e8 |
i++;
|
|
Karl MacMillan |
6847e8 |
finish = i;
|
|
Karl MacMillan |
6847e8 |
|
|
Chris PeBenito |
dc5daf |
context_len = finish - start;
|
|
Karl MacMillan |
6847e8 |
|
|
Chris PeBenito |
dc5daf |
temp->context = (char*)strndup(&line_buf[start], context_len);
|
|
Chris PeBenito |
dc5daf |
if (!temp->context) {
|
|
Chris PeBenito |
dc5daf |
file_context_node_destroy(temp);
|
|
Chris PeBenito |
dc5daf |
free(temp);
|
|
Chris PeBenito |
dc5daf |
fprintf(stderr, "Error: failure allocating memory.\n");
|
|
Chris PeBenito |
dc5daf |
return 1;
|
|
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 |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
dc5daf |
free(line_buf);
|
|
Chris PeBenito |
dc5daf |
line_buf = NULL;
|
|
Chris PeBenito |
89ec23 |
}
|
|
Chris PeBenito |
dc5daf |
fclose(in_file);
|
|
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 |
dc5daf |
|
|
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 |
dc5daf |
|
|
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. */
|
|
Chris PeBenito |
dc5daf |
if (!(out_file = 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 |
dc5daf |
/* Output the path. */
|
|
Chris PeBenito |
dc5daf |
fprintf(out_file, "%s\t\t", current->path);
|
|
Karl MacMillan |
6847e8 |
|
|
Chris PeBenito |
89ec23 |
/* Output the type, if there is one. */
|
|
Chris PeBenito |
dc5daf |
if (current->file_type) {
|
|
Chris PeBenito |
dc5daf |
fprintf(out_file, "%s\t", current->file_type);
|
|
Chris PeBenito |
89ec23 |
}
|
|
Chris PeBenito |
89ec23 |
|
|
Chris PeBenito |
89ec23 |
/* Output the context. */
|
|
Chris PeBenito |
dc5daf |
fprintf(out_file, "%s\n", current->context);
|
|
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 |
dc5daf |
file_context_node_destroy(temp);
|
|
Karl MacMillan |
6847e8 |
free(temp);
|
|
Chris PeBenito |
dc5daf |
|
|
Chris PeBenito |
89ec23 |
}
|
|
Karl MacMillan |
6847e8 |
free(master);
|
|
Chris PeBenito |
89ec23 |
|
|
Chris PeBenito |
dc5daf |
fclose(out_file);
|
|
Chris PeBenito |
89ec23 |
|
|
Chris PeBenito |
89ec23 |
return 0;
|
|
Chris PeBenito |
89ec23 |
}
|