# HG changeset patch # User meillo@marmaro.de # Date 1205660453 -3600 # Node ID 6279e5b14d9ea0f39c7b6aa8efd97d85c2a2fe1c # Parent 6e46b106c33477f32562212e8cd9fdc04ef9a4ee added error handling for fopen and malloc; code cleanups diff -r 6e46b106c334 -r 6279e5b14d9e actions.c --- a/actions.c Sun Mar 02 16:43:07 2008 +0100 +++ b/actions.c Sun Mar 16 10:40:53 2008 +0100 @@ -66,10 +66,8 @@ struct Node* tp; node->value = 0; - tp = node->down; - while (tp != NULL) { + for (tp = node->down; tp != NULL; tp = tp->right) { node->value += action(tp); - tp = tp->right; } return node->value; } @@ -102,6 +100,7 @@ unsigned char action_times(struct Node* node) { unsigned char i; + for (i = 0; i < node->value; i++) { insertLast(node, copyTree(node->down)); } diff -r 6e46b106c334 -r 6279e5b14d9e baum.1 --- a/baum.1 Sun Mar 02 16:43:07 2008 +0100 +++ b/baum.1 Sun Mar 16 10:40:53 2008 +0100 @@ -136,6 +136,10 @@ a node has not the required amount of sons .TP +.BI 10 +unable to open input file, unable to allocate memory, or something similar + +.TP .BI 126 invalid command line options diff -r 6e46b106c334 -r 6279e5b14d9e baum.c --- a/baum.c Sun Mar 02 16:43:07 2008 +0100 +++ b/baum.c Sun Mar 16 10:40:53 2008 +0100 @@ -44,6 +44,10 @@ struct Node* newNode(char* name, unsigned char value) { struct Node* node; node = (struct Node*) malloc(sizeof(struct Node)); + if (node == NULL) { + perror("unable to allocate memory"); + exit(10); + } strcpy(node->name, name); node->value = value; node->right = 0; @@ -91,12 +95,13 @@ /* print */ void printNode(struct Node* node, int level) { - if (node != NULL) { - while (level-- > 0) { - fprintf(stderr, "\t"); - } - fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value); + if (node == NULL) { + return; } + while (level-- > 0) { + fprintf(stderr, "\t"); + } + fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value); } void printTree(struct Node* root, int level) { @@ -117,6 +122,10 @@ struct Stackitem* tmp; struct Stackitem* new; new = (struct Stackitem*) malloc(sizeof(struct Stackitem)); + if (new == NULL) { + perror("unable to allocate memory"); + exit(10); + } new->node = node; tmp = stack; stack = new; @@ -154,9 +163,13 @@ last_indent = -1; last_node = NULL; file = fopen(filename, "r"); + if (file == NULL) { + perror("unable to open file"); + exit(10); + } while ((c = getc(file)) != EOF) { - if (c == '#') { /* comment */ + if (c == '#') { /* comment */ while ((c = getc(file)) != '\n') { } }