Index: trunk/doc/Makefile
===================================================================
--- trunk/doc/Makefile	(revision 37)
+++ trunk/doc/Makefile	(revision 38)
@@ -1,7 +1,11 @@
 # $Id$
 
-default:
+BUILD_FILES=$(BUILD_DOC)/man/man1/reglookup.1.gz
+
+default: $(BUILD_FILES)
+
+$(BUILD_DOC)/man/man1/reglookup.1.gz: man/man1/reglookup.1.gz
 	mkdir -p $(BUILD_DOC)/man/man1
-	cp man/man1/reglookup.1.gz $(BUILD_DOC)/man/man1/reglookup.1.gz
+	cp man/man1/reglookup.1.gz $@
 
 install:
Index: trunk/include/void_stack.h
===================================================================
--- trunk/include/void_stack.h	(revision 37)
+++ trunk/include/void_stack.h	(revision 38)
@@ -37,5 +37,8 @@
 
 void_stack* void_stack_new(unsigned short max_size);
+void_stack* void_stack_copy(const void_stack* v);
+void_stack* void_stack_copy_reverse(const void_stack* v);
 void void_stack_destroy(void_stack* stack);
+void void_stack_destroy_deep(void_stack* stack);
 unsigned short void_stack_size(void_stack* stack);
 void* void_stack_pop(void_stack* stack);
Index: trunk/lib/void_stack.c
===================================================================
--- trunk/lib/void_stack.c	(revision 37)
+++ trunk/lib/void_stack.c	(revision 38)
@@ -50,5 +50,49 @@
 
 
+void_stack* void_stack_copy(const void_stack* v)
+{
+  unsigned int i;
+  void_stack* ret_val;
+  if(v == NULL)
+    return NULL;
+
+  ret_val = void_stack_new(v->max_size);
+  if(ret_val == NULL)
+    return NULL;
+
+  for(i = 0; i < v->top; i++)
+    ret_val->elements[i] = v->elements[i];
+  ret_val->top = v->top;
+
+  return ret_val;
+}
+
+
+void_stack* void_stack_copy_reverse(const void_stack* v)
+{
+  unsigned int i;
+  void_stack* ret_val;
+  if(v == NULL)
+    return NULL;
+
+  ret_val = void_stack_new(v->max_size);
+  if(ret_val == NULL)
+    return NULL;
+
+  for(i = 0; i < v->top; i++)
+    ret_val->elements[i] = v->elements[v->top-i-1];
+  ret_val->top = v->top;
+
+  return ret_val;
+}
+
+
 void void_stack_destroy(void_stack* stack)
+{
+  free(stack);
+}
+
+
+void void_stack_destroy_deep(void_stack* stack)
 {
   unsigned short i;
Index: trunk/src/reglookup.c
===================================================================
--- trunk/src/reglookup.c	(revision 37)
+++ trunk/src/reglookup.c	(revision 38)
@@ -28,25 +28,47 @@
 #include "../include/void_stack.h"
 
-/* XXX: this needs to be rewritten to malloc each resulting string, instead of
- *      altering them in place
- */
+
+void bailOut(int code, char* message)
+{
+  fprintf(stderr, message);
+  exit(code);
+}
+
+
 void_stack* path2Stack(const char* s)
 {
-  void_stack* ret_val = void_stack_new(1024);
+  void_stack* ret_val;
+  void_stack* rev_ret = void_stack_new(1024);
+  const char* cur = s;
   char* next = NULL;
-  char* cur;
+  char* copy;
+
+  if (rev_ret == NULL)
+    return NULL;
   if (s == NULL)
-    return ret_val;
-  else
-    cur = strdup(s);
-
-  while((next = strrchr(cur, '/')) != NULL)
-  {
-    next[0] = '\0';
-    if(strlen(next+1) > 0)
-      void_stack_push(ret_val, next+1);
+    return rev_ret;
+  
+  while((next = strchr(cur, '/')) != NULL)
+  {
+    if ((next-cur) > 0)
+    {
+      copy = (char*)malloc((next-cur+1)*sizeof(char));
+      if(copy == NULL)
+	bailOut(2, "ERROR: Memory allocation problem.\n");
+	  
+      memcpy(copy, cur, next-cur);
+      copy[next-cur] = '\0';
+      void_stack_push(rev_ret, copy);
+    }
+    cur = next+1;
   }
   if(strlen(cur) > 0)
-    void_stack_push(ret_val, cur);
+  {
+    copy = strdup(cur);
+    void_stack_push(rev_ret, copy);
+  }
+
+  ret_val = void_stack_copy_reverse(rev_ret);
+  void_stack_destroy(rev_ret);
 
   return ret_val;
@@ -284,7 +306,6 @@
   if(argc < 2)
   {
-    printf("ERROR: Requires 1 argument.\n");
     usage();
-    exit(1);
+    bailOut(1, "ERROR: Requires 1 argument.\n");
   }
   
@@ -295,13 +316,10 @@
       if(++argi > argc)
       {
-	fprintf(stderr, "ERROR: '-f' option requires parameter.\n");
 	usage();
-	exit(1);
+	bailOut(1, "ERROR: '-f' option requires parameter.\n");
       }
       if((prefix_filter = strdup(argv[argi])) == NULL)
-      {
-	fprintf(stderr, "ERROR: Memory allocation problem.\n");
-	exit(2);
-      }
+	bailOut(2, "ERROR: Memory allocation problem.\n");
+
       prefix_filter_enabled = true;
     }
@@ -310,13 +328,10 @@
       if(++argi > argc)
       {
-	fprintf(stderr, "ERROR: '-t' option requires parameter.\n");
 	usage();
-	exit(1);
+	bailOut(1, "ERROR: '-t' option requires parameter.\n");
       }
       if((prefix_filter = strdup(argv[argi])) == NULL)
-      {
-	fprintf(stderr, "ERROR: Memory allocation problem.\n");
-	exit(2);
-      }
+	bailOut(2, "ERROR: Memory allocation problem.\n");
+
       type_filter_enabled = true;
     }
@@ -327,15 +342,12 @@
     else if (argv[argi][0] == '-')
     {
+      usage();
       fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
-      usage();
-      exit(1);
+      bailOut(1, "");
     }
     else
     {
       if((registry_file = strdup(argv[argi])) == NULL)
-      {
-	fprintf(stderr, "ERROR: Memory allocation problem.\n");
-	exit(2);
-      }      
+	bailOut(2, "ERROR: Memory allocation problem.\n");
     }
   }
@@ -345,6 +357,7 @@
   {
     fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
-    exit(1);
-  }
+    bailOut(3, "");
+  }
+
   root = regfio_rootkey(f);
   nk_stack = void_stack_new(1024);
@@ -361,14 +374,11 @@
 	fprintf(stderr, "WARNING: specified path not found.\n");
       else if(retr_path_ret != 0)
-	fprintf(stderr, "ERROR:\n");
+	bailOut(4, "ERROR:\n");
     }
   }
   else
-  {
-    fprintf(stderr, "ERROR: Memory allocation problem.\n");
-    exit(2);
-  }
-  void_stack_destroy(nk_stack);
-
+    bailOut(2, "ERROR: Memory allocation problem.\n");
+
+  void_stack_destroy_deep(nk_stack);
   regfio_close(f);
 
