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