changeset 33:12cac756bc05 stable-0-5-5

enable -d option, but if not specified, don't generate a debug file
author carl
date Tue, 10 Jul 2007 20:23:55 -0700
parents c03974357771
children 07177825c91b
files src/debug.c src/define.h src/libpst.c src/pst2ldif.cpp src/readpst.c xml/libpst.in
diffstat 6 files changed, 376 insertions(+), 394 deletions(-) [+]
line wrap: on
line diff
--- a/src/debug.c	Tue Jul 10 18:18:38 2007 -0700
+++ b/src/debug.c	Tue Jul 10 20:23:55 2007 -0700
@@ -12,17 +12,18 @@
 #endif
 
 struct _debug_item {
-  int type;
-  char * function;
-  unsigned int line;
-  char * file;
-  char * text;
-  struct _debug_item *next;
+	int type;
+	char * function;
+	unsigned int line;
+	char * file;
+	char * text;
+	struct _debug_item *next;
 } *item_head=NULL, *item_tail=NULL, *item_ptr=NULL, *info_ptr=NULL, *temp_list=NULL;
 
+
 struct _debug_func {
-  char * name;
-  struct _debug_func *next;
+	char * name;
+	struct _debug_func *next;
 } *func_head=NULL, *func_ptr=NULL;
 
 
@@ -44,407 +45,400 @@
 #define MAX_MESSAGE_SIZE 4096
 
 void _pst_debug(char *fmt, ...) {
-  va_list ap;
-  va_start(ap,fmt);
-  vfprintf(stderr, fmt, ap);
-  va_end(ap);
+	va_list ap;
+	va_start(ap,fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
 }
 
+
 #define NUM_COL 30
 void _pst_debug_hexdump(FILE *out, unsigned char *buf, size_t size, int col) {
-  int off = 0, toff;
-  int count = 0;
-  
-  if (col == -1) {
-    col = NUM_COL;
-  }
-  fprintf(out, "\n");
-  while (off < size) {
-    fprintf(out, "%X\t:", off);
-    toff = off;
-    while (count < col && off < size) {
-      fprintf(out, "%02hhx ", buf[off]);
-      off++; count++;
-    }
-    off = toff;
-    while (count < col) {
-      // only happens at end of block to pad the text over to the text column
-      fprintf(out, "   ");
-      count++;
-    }
-    count = 0;
-    fprintf(out, ":");
-    while (count < col && off < size) {
-      fprintf(out, "%c", isgraph(buf[off])?buf[off]:'.');
-      off++; count ++;
-    }
+	int off = 0, toff;
+	int count = 0;
 
-    fprintf(out, "\n");
-    count=0;
-  }
+	if (!out) return;	// no file
+	if (col == -1) col = NUM_COL;
+	fprintf(out, "\n");
+	while (off < size) {
+		fprintf(out, "%X\t:", off);
+		toff = off;
+		while (count < col && off < size) {
+			fprintf(out, "%02hhx ", buf[off]);
+			off++; count++;
+		}
+		off = toff;
+		  while (count < col) {
+			// only happens at end of block to pad the text over to the text column
+			fprintf(out, "   ");
+			count++;
+		}
+		count = 0;
+		fprintf(out, ":");
+		while (count < col && off < size) {
+			fprintf(out, "%c", isgraph(buf[off])?buf[off]:'.');
+			off++; count ++;
+		}
 
-  fprintf(out, "\n");
+		fprintf(out, "\n");
+		count=0;
+	}
+
+	fprintf(out, "\n");
 }
 
-void _pst_debug_hexprint(char *data, int size) {
-  int i = 0;
-  while (i < size) {
-    fprintf(stderr, "%02hhX", data[i]);
-    i++;
-  }
-}
 
 FILE *debug_fp = NULL;
 unsigned int max_items=DEBUG_MAX_ITEMS, curr_items=0;
 
+
 void _debug_init(char* fname) {
-  unsigned char version = DEBUG_VERSION;
-  item_head = item_tail = NULL;
-  curr_items = 0;
-  if (debug_fp != NULL)
-    _debug_close();
-  if ((debug_fp = fopen(fname, "wb")) == NULL) {
-    fprintf(stderr, "Opening of file %s failed\n", fname);
-    exit(1);
-  }
-  fwrite(&version, 1, sizeof(char), debug_fp);
+	unsigned char version = DEBUG_VERSION;
+	item_head = item_tail = NULL;
+	curr_items = 0;
+	if (debug_fp) _debug_close();
+	if (!fname) return;
+	if ((debug_fp = fopen(fname, "wb")) == NULL) {
+	  fprintf(stderr, "Opening of file %s failed\n", fname);
+	  exit(1);
+	}
+	fwrite(&version, 1, sizeof(char), debug_fp);
 }
 
+
 // function must be called before _debug_msg. It sets up the
 // structure for the function that follows
 void _debug_msg_info(int line, char* file, int type) {
-  char *x;
-  if (debug_fp == NULL) {
-    fprintf(stderr, "debug_fp is NULL\n");
-    return;
-  }
-  info_ptr = (struct _debug_item*) xmalloc(sizeof(struct _debug_item));
-  info_ptr->type = type;
-  info_ptr->line = line;
-  x = (func_head==NULL?"No Function":func_head->name);
-  info_ptr->function = (char*) xmalloc(strlen(x)+1);
-  strcpy(info_ptr->function, x);
-  
-  info_ptr->file = (char*) xmalloc(strlen(file)+1);
-  strcpy(info_ptr->file, file);
-  
-  //put the current record on a temp linked list
-  info_ptr->next = temp_list;
-  temp_list = info_ptr; 
+	char *x;
+	if (!debug_fp) return;	// no file
+	info_ptr = (struct _debug_item*) xmalloc(sizeof(struct _debug_item));
+	info_ptr->type = type;
+	info_ptr->line = line;
+	x = (func_head==NULL?"No Function":func_head->name);
+	info_ptr->function = (char*) xmalloc(strlen(x)+1);
+	strcpy(info_ptr->function, x);
+
+	info_ptr->file = (char*) xmalloc(strlen(file)+1);
+	strcpy(info_ptr->file, file);
+
+	//put the current record on a temp linked list
+	info_ptr->next = temp_list;
+	temp_list = info_ptr;
 }
 
+
 void _debug_msg_text(char* fmt, ...) {
-  va_list ap;
-  int f, g;
-  char x[2];
-  struct _debug_item *temp;
-  if (debug_fp == NULL)
-    return;
-  va_start(ap, fmt);
-  // get the record off of the temp_list
-  info_ptr = temp_list;
-  if (info_ptr != NULL)
-    temp_list = info_ptr->next;
-  else {
-    fprintf(stderr, "NULL info_ptr. ERROR!!\n");
-    exit(-2);
-  }
-  // according to glibc 2.1, this should return the req. number of bytes for 
-  // the string
-#ifdef _WIN32
-  // vsnprintf trick doesn't work. must use function called _vscprintf
-  // cannot find much documentation about this on internet or anywhere.
-  // I assume it isn't a standard function, but only in VisualC++
-  f = _vscprintf(fmt, ap);
-#else
-  f = vsnprintf(x, 1, fmt, ap);
-#endif
-  va_end(ap);  // must be called after vsnprintf()
+	va_list ap;
+	int f, g;
+	char x[2];
+	struct _debug_item *temp;
+	if (!debug_fp) return;	// no file
+	va_start(ap, fmt);
+	// get the record off of the temp_list
+	info_ptr = temp_list;
+	if (info_ptr)
+		temp_list = info_ptr->next;
+	else {
+		fprintf(stderr, "NULL info_ptr. ERROR!!\n");
+		exit(-2);
+	}
+	// according to glibc 2.1, this should return the req. number of bytes for
+	// the string
+  #ifdef _WIN32
+	// vsnprintf trick doesn't work. must use function called _vscprintf
+	// cannot find much documentation about this on internet or anywhere.
+	// I assume it isn't a standard function, but only in VisualC++
+	f = _vscprintf(fmt, ap);
+  #else
+	f = vsnprintf(x, 1, fmt, ap);
+  #endif
+	va_end(ap);  // must be called after vsnprintf()
 
-  if (f > 0 && f < MAX_MESSAGE_SIZE) {
-    info_ptr->text = (char*) xmalloc(f+1);
-    va_start(ap, fmt);
-    if ((g = vsnprintf(info_ptr->text, f, fmt, ap)) == -1) {
-      fprintf(stderr, "_debug_msg: Dieing! vsnprintf returned -1 for format \"%s\"\n", fmt);
-      exit(-2);
-    }
-    va_end(ap);
-    info_ptr->text[g] = '\0';
-    if (f != g) {
-      fprintf(stderr, "_debug_msg: f != g\n");
-    }
-  } else if (f > 0) { // it is over the max_message_size then
-    f += strlen(info_ptr->file)+strlen(info_ptr->function);
-    temp = info_ptr;
-    _debug_write(); // dump the current messages
-    info_ptr = temp;
-    va_start(ap, fmt);
-    _debug_write_msg(info_ptr, fmt, &ap, f);
-    va_end(ap);
-    free(info_ptr->function);
-    free(info_ptr->file);
-    free(info_ptr);
-    info_ptr = NULL;
-    return;
-  } else {
-    fprintf(stderr, "_debug_msg: error getting requested size of debug message\n");
-    info_ptr->text = "ERROR Saving\n";
-  }
+	if (f > 0 && f < MAX_MESSAGE_SIZE) {
+		info_ptr->text = (char*) xmalloc(f+1);
+		va_start(ap, fmt);
+		if ((g = vsnprintf(info_ptr->text, f, fmt, ap)) == -1) {
+			fprintf(stderr, "_debug_msg: Dieing! vsnprintf returned -1 for format \"%s\"\n", fmt);
+			exit(-2);
+		}
+		va_end(ap);
+		info_ptr->text[g] = '\0';
+		if (f != g) {
+			fprintf(stderr, "_debug_msg: f != g\n");
+		}
+	} else if (f > 0) { // it is over the max_message_size then
+		f += strlen(info_ptr->file)+strlen(info_ptr->function);
+		temp = info_ptr;
+		_debug_write(); // dump the current messages
+		info_ptr = temp;
+		va_start(ap, fmt);
+		_debug_write_msg(info_ptr, fmt, &ap, f);
+		va_end(ap);
+		free(info_ptr->function);
+		free(info_ptr->file);
+		free(info_ptr);
+		info_ptr = NULL;
+		return;
+	} else {
+		fprintf(stderr, "_debug_msg: error getting requested size of debug message\n");
+		info_ptr->text = "ERROR Saving\n";
+	}
 
-  if (item_head == NULL)
-    item_head = info_ptr;
+	if (!item_head)
+	  item_head = info_ptr;
 
-  info_ptr->next = NULL;
-  if (item_tail != NULL)
-    item_tail->next = info_ptr;
-  item_tail = info_ptr;
+	info_ptr->next = NULL;
+	if (item_tail) item_tail->next = info_ptr;
+	item_tail = info_ptr;
 
-  if (++curr_items == max_items) {
-    // here we will jump off and save the contents
-    _debug_write();
-    info_ptr = NULL;
-  }
+	if (++curr_items == max_items) {
+		// here we will jump off and save the contents
+		_debug_write();
+		info_ptr = NULL;
+	}
 }
 
+
 void _debug_hexdump(unsigned char *x, int y, int cols) {
-  struct _debug_item *temp;
-  if (debug_fp == NULL)
-    return;
-  info_ptr = temp_list;
-  if (info_ptr != NULL)
-    temp_list = info_ptr->next;
-  temp = info_ptr;
-  _debug_write();
-  info_ptr = temp;
-  _debug_write_hex(info_ptr, x, y, cols);
-  free(info_ptr->function);
-  free(info_ptr->file);
-  free(info_ptr);
-  info_ptr = NULL;
+	struct _debug_item *temp;
+	if (!debug_fp) return;	// no file
+	info_ptr = temp_list;
+	if (info_ptr)
+	  temp_list = info_ptr->next;
+	temp = info_ptr;
+	_debug_write();
+	info_ptr = temp;
+	_debug_write_hex(info_ptr, x, y, cols);
+	free(info_ptr->function);
+	free(info_ptr->file);
+	free(info_ptr);
+	info_ptr = NULL;
 }
 
+
 void _debug_func(char *function) {
-  func_ptr = xmalloc (sizeof(struct _debug_func));
-  func_ptr->name = xmalloc(strlen(function)+1);
-  strcpy(func_ptr->name, function);
-  func_ptr->next = func_head;
-  func_head = func_ptr;
+	func_ptr = xmalloc (sizeof(struct _debug_func));
+	func_ptr->name = xmalloc(strlen(function)+1);
+	strcpy(func_ptr->name, function);
+	func_ptr->next = func_head;
+	func_head = func_ptr;
 }
 
+
 void _debug_func_ret() {
-  //remove the head item
-  func_ptr = func_head;
-  if (func_head != NULL) {
-    func_head = func_head->next;
-    free(func_ptr->name);
-    free(func_ptr);
-  } else {
-    DIE(("function list is empty!\n"));
-  }
+	//remove the head item
+	func_ptr = func_head;
+	if (func_head) {
+		func_head = func_head->next;
+		free(func_ptr->name);
+		free(func_ptr);
+	} else {
+		DIE(("function list is empty!\n"));
+	}
 }
 
+
 void _debug_close(void) {
-  _debug_write();
-  while (func_head != NULL) {
-    func_ptr = func_head;
-    func_head = func_head->next;
-    free(func_ptr->name);
-    free(func_ptr);
-  }
+	_debug_write();
+	while (func_head) {
+		func_ptr = func_head;
+		func_head = func_head->next;
+		free(func_ptr->name);
+		free(func_ptr);
+	}
 
-  if (debug_fp != NULL)
-    fclose(debug_fp);
-  debug_fp = NULL;
+	if (debug_fp) fclose(debug_fp);
+	debug_fp = NULL;
+}
 
-  if (func_head != NULL)
-    while (func_head != NULL) {
-      printf("function '%s' still on stack\n", func_head->name);
-      func_head = func_head->next;
-    }
-}
 
 void _debug_write() {
-  size_t size, ptr, funcname, filename, text, end;
-  char *buf = NULL, rec_type;
-  long index_pos = ftell (debug_fp), file_pos = index_pos;
-  // add 2. One for the pointer to the next index, 
-  // one for the count of this index
-  int index_size = ((curr_items+2) * sizeof(int));
-  int *index;
-  int index_ptr = 0;
-  struct _debug_file_rec_m mfile_rec;
-  struct _debug_file_rec_l lfile_rec;
+	size_t size, ptr, funcname, filename, text, end;
+	char *buf = NULL, rec_type;
+	if (!debug_fp) return;	// no file
+	long index_pos = ftell (debug_fp), file_pos = index_pos;
+	// add 2. One for the pointer to the next index,
+	// one for the count of this index
+	int index_size = ((curr_items+2) * sizeof(int));
+	int *index;
+	int index_ptr = 0;
+	struct _debug_file_rec_m mfile_rec;
+	struct _debug_file_rec_l lfile_rec;
 
-  if (curr_items == 0)
-    // no items to write.
-    return; 
-  index = (int*) xmalloc(index_size);
-  file_pos += index_size;
-  // write the index first, we will re-write it later, but
-  // we want to allocate the space
-  fwrite(index, index_size, 1, debug_fp);
-  index[index_ptr++] = curr_items;
+	if (curr_items == 0) return;	// no items to write.
+
+	index = (int*) xmalloc(index_size);
+	file_pos += index_size;
+	// write the index first, we will re-write it later, but
+	// we want to allocate the space
+	fwrite(index, index_size, 1, debug_fp);
+	index[index_ptr++] = curr_items;
 
-  item_ptr = item_head;
-  while (item_ptr != NULL) {
-    file_pos = ftell(debug_fp);
-    index[index_ptr++] = file_pos;
-    size = strlen(item_ptr->function)+strlen(item_ptr->file)+
-      strlen(item_ptr->text) + 3; //for the three \0s
-    if (buf) free(buf);
-    buf = xmalloc(size+1);
-    ptr = 0;
-    funcname=ptr;
-    ptr += sprintf(&(buf[ptr]), "%s", item_ptr->function)+1;
-    filename=ptr;
-    ptr += sprintf(&(buf[ptr]), "%s", item_ptr->file)+1;
-    text=ptr;
-    ptr += sprintf(&(buf[ptr]), "%s", item_ptr->text)+1;
-    end=ptr;
-    if (end > USHRT_MAX) { // bigger than can be stored in a short
-      rec_type = 'L';
-      fwrite(&rec_type, 1, sizeof(char), debug_fp);
-      lfile_rec.type = item_ptr->type;
-      lfile_rec.line = item_ptr->line;
-      lfile_rec.funcname = funcname;
-      lfile_rec.filename = filename;
-      lfile_rec.text = text;
-      lfile_rec.end = end;
-      fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
-    } else {
-      rec_type = 'M';
-      fwrite(&rec_type, 1, sizeof(char), debug_fp);
-      mfile_rec.type = item_ptr->type;
-      mfile_rec.line = item_ptr->line;
-      mfile_rec.funcname = funcname;
-      mfile_rec.filename = filename;
-      mfile_rec.text = text;
-      mfile_rec.end = end;
-      fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
-    }
-    fwrite(buf, 1, ptr, debug_fp);
-    if (buf) free(buf); buf = NULL;
-    item_head = item_ptr->next;
-    free(item_ptr->function);
-    free(item_ptr->file);
-    free(item_ptr->text);
-    free(item_ptr);
-    item_ptr = item_head;
-  }
-  curr_items = 0;
-  index[index_ptr] = ftell(debug_fp);
+	item_ptr = item_head;
+	while (item_ptr) {
+		file_pos = ftell(debug_fp);
+		index[index_ptr++] = file_pos;
+		size = strlen(item_ptr->function)+strlen(item_ptr->file)+
+		  strlen(item_ptr->text) + 3; //for the three \0s
+		if (buf) free(buf);
+		buf = xmalloc(size+1);
+		ptr = 0;
+		funcname=ptr;
+		ptr += sprintf(&(buf[ptr]), "%s", item_ptr->function)+1;
+		filename=ptr;
+		ptr += sprintf(&(buf[ptr]), "%s", item_ptr->file)+1;
+		text=ptr;
+		ptr += sprintf(&(buf[ptr]), "%s", item_ptr->text)+1;
+		end=ptr;
+		if (end > USHRT_MAX) { // bigger than can be stored in a short
+			rec_type = 'L';
+			fwrite(&rec_type, 1, sizeof(char), debug_fp);
+			lfile_rec.type = item_ptr->type;
+			lfile_rec.line = item_ptr->line;
+			lfile_rec.funcname = funcname;
+			lfile_rec.filename = filename;
+			lfile_rec.text = text;
+			lfile_rec.end = end;
+			fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
+		} else {
+			rec_type = 'M';
+			fwrite(&rec_type, 1, sizeof(char), debug_fp);
+			mfile_rec.type = item_ptr->type;
+			mfile_rec.line = item_ptr->line;
+			mfile_rec.funcname = funcname;
+			mfile_rec.filename = filename;
+			mfile_rec.text = text;
+			mfile_rec.end = end;
+			fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
+		}
+		fwrite(buf, 1, ptr, debug_fp);
+		if (buf) free(buf); buf = NULL;
+		item_head = item_ptr->next;
+		free(item_ptr->function);
+		free(item_ptr->file);
+		free(item_ptr->text);
+		free(item_ptr);
+		item_ptr = item_head;
+	}
+	curr_items = 0;
+	index[index_ptr] = ftell(debug_fp);
 
-  // we should now have a complete index
-  fseek(debug_fp, index_pos, SEEK_SET);
-  fwrite(index, index_size, 1, debug_fp);
-  fseek(debug_fp, 0, SEEK_END);
-  item_ptr = item_head = item_tail = NULL;
-  free(index);
-  if (buf) free(buf); buf = NULL;
+	// we should now have a complete index
+	fseek(debug_fp, index_pos, SEEK_SET);
+	fwrite(index, index_size, 1, debug_fp);
+	fseek(debug_fp, 0, SEEK_END);
+	item_ptr = item_head = item_tail = NULL;
+	free(index);
+	if (buf) free(buf); buf = NULL;
 }
 
+
 void _debug_write_msg(struct _debug_item *item, char *fmt, va_list *ap, int size) {
-  struct _debug_file_rec_l lfile_rec;
-  struct _debug_file_rec_m mfile_rec;
-  unsigned char rec_type;
-  int index_size = 3 * sizeof(int);
-  int *index = malloc(index_size);
-  int index_pos, file_pos;
-  char zero='\0';
-  unsigned int end;
-  index[0] = 1; //only one item in this index
-  index_pos = ftell(debug_fp);
-  fwrite(index, index_size, 1, debug_fp);
+	struct _debug_file_rec_l lfile_rec;
+	struct _debug_file_rec_m mfile_rec;
+	unsigned char rec_type;
+	int index_size = 3 * sizeof(int);
+	int *index = malloc(index_size);
+	int index_pos, file_pos;
+	char zero='\0';
+	unsigned int end;
+	if (!debug_fp) return;	// no file
+	index[0] = 1; //only one item in this index
+	index_pos = ftell(debug_fp);
+	fwrite(index, index_size, 1, debug_fp);
+
+	index[1] = ftell(debug_fp);
 
-  index[1] = ftell(debug_fp);
-  
-  if (size > USHRT_MAX) { // bigger than can be stored in a short
-    rec_type = 'L';
-    fwrite(&rec_type, 1, sizeof(char), debug_fp);
-    lfile_rec.type = item->type;
-    lfile_rec.line = item->line;
-    lfile_rec.funcname = 0;
-    lfile_rec.filename = strlen(item->function)+1;
-    lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
-    fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
-  } else {
-    rec_type = 'M';
-    fwrite(&rec_type, 1, sizeof(char), debug_fp);
-    mfile_rec.type = item->type;
-    mfile_rec.line = item->line;
-    mfile_rec.funcname = 0;
-    mfile_rec.filename = strlen(item->function)+1;
-    mfile_rec.text = mfile_rec.filename+strlen(item->file)+1;
-    fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
-  }
-  file_pos = ftell(debug_fp);
-  fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
-  fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
-  vfprintf(debug_fp, fmt, *ap);
-  fwrite(&zero, 1, 1, debug_fp);
+	if (size > USHRT_MAX) { // bigger than can be stored in a short
+		rec_type = 'L';
+		fwrite(&rec_type, 1, sizeof(char), debug_fp);
+		lfile_rec.type = item->type;
+		lfile_rec.line = item->line;
+		lfile_rec.funcname = 0;
+		lfile_rec.filename = strlen(item->function)+1;
+		lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
+		fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
+	} else {
+		rec_type = 'M';
+		fwrite(&rec_type, 1, sizeof(char), debug_fp);
+		mfile_rec.type = item->type;
+		mfile_rec.line = item->line;
+		mfile_rec.funcname = 0;
+		mfile_rec.filename = strlen(item->function)+1;
+		mfile_rec.text = mfile_rec.filename+strlen(item->file)+1;
+		fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
+	}
+	file_pos = ftell(debug_fp);
+	fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
+	fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
+	vfprintf(debug_fp, fmt, *ap);
+	fwrite(&zero, 1, 1, debug_fp);
 
-  end = ftell(debug_fp)-file_pos;
+	end = ftell(debug_fp)-file_pos;
 
-  index[2] = ftell(debug_fp);
-  fseek(debug_fp, index_pos, SEEK_SET);
-  fwrite(index, index_size, 1, debug_fp);
-  if (size > USHRT_MAX) {
-    fwrite(&rec_type, 1, sizeof(char), debug_fp);
-    lfile_rec.end = end;
-    fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
-  } else {
-    fwrite(&rec_type, 1, sizeof(char), debug_fp);
-    mfile_rec.end = end;
-    fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
-  }
-  fseek(debug_fp, 0, SEEK_END);
-  // that should do it...
+	index[2] = ftell(debug_fp);
+	fseek(debug_fp, index_pos, SEEK_SET);
+	fwrite(index, index_size, 1, debug_fp);
+	if (size > USHRT_MAX) {
+		fwrite(&rec_type, 1, sizeof(char), debug_fp);
+		lfile_rec.end = end;
+		fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
+	} else {
+		fwrite(&rec_type, 1, sizeof(char), debug_fp);
+		mfile_rec.end = end;
+		fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
+	}
+	fseek(debug_fp, 0, SEEK_END);
+	// that should do it...
 }
 
+
 void _debug_write_hex(struct _debug_item *item, unsigned char *buf, int size, int col) {
-  struct _debug_file_rec_l lfile_rec;
-  unsigned char rec_type;
-  int index_size = 3 * sizeof(int);
-  int *index = malloc(index_size);
-  int index_pos, file_pos;
-  char zero='\0';
-  index[0] = 1; // only one item in this index run
-  index_pos = ftell(debug_fp);
-  fwrite(index, index_size, 1, debug_fp);
-  index[1] = ftell(debug_fp);
-
-  // always use the long
-  rec_type = 'L';
-  fwrite(&rec_type, 1, sizeof(char), debug_fp);
-  lfile_rec.type = item->type;
-  lfile_rec.line = item->line;
-  lfile_rec.funcname = 0;
-  lfile_rec.filename = strlen(item->function)+1;
-  lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
-  fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
+	struct _debug_file_rec_l lfile_rec;
+	unsigned char rec_type;
+	int index_size = 3 * sizeof(int);
+	int *index = malloc(index_size);
+	int index_pos, file_pos;
+	char zero='\0';
+	if (!debug_fp) return;	// no file
+	index[0] = 1; // only one item in this index run
+	index_pos = ftell(debug_fp);
+	fwrite(index, index_size, 1, debug_fp);
+	index[1] = ftell(debug_fp);
 
-  file_pos = ftell(debug_fp);
-  fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
-  fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
-  
-  _pst_debug_hexdump(debug_fp, buf, size, col);
-  fwrite(&zero, 1, 1, debug_fp);
-  lfile_rec.end = ftell(debug_fp)-file_pos;
+	// always use the long
+	rec_type = 'L';
+	fwrite(&rec_type, 1, sizeof(char), debug_fp);
+	lfile_rec.type = item->type;
+	lfile_rec.line = item->line;
+	lfile_rec.funcname = 0;
+	lfile_rec.filename = strlen(item->function)+1;
+	lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
+	fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
 
-  index[2] = ftell(debug_fp);
-  fseek(debug_fp, index_pos, SEEK_SET);
-  fwrite(index, index_size, 1, debug_fp);
-  fwrite(&rec_type, 1, sizeof(char), debug_fp);
-  fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
-  fseek(debug_fp, 0, SEEK_END);
-}
-  
-void * xmalloc(size_t size) {
-  void *mem = malloc(size);
-  if (mem == NULL) {
-    fprintf(stderr, "xMalloc: Out Of memory [req: %ld]\n", (long)size);
-    exit(1);
-  }
-  return mem;
+	file_pos = ftell(debug_fp);
+	fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
+	fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
+
+	_pst_debug_hexdump(debug_fp, buf, size, col);
+	fwrite(&zero, 1, 1, debug_fp);
+	lfile_rec.end = ftell(debug_fp)-file_pos;
+
+	index[2] = ftell(debug_fp);
+	fseek(debug_fp, index_pos, SEEK_SET);
+	fwrite(index, index_size, 1, debug_fp);
+	fwrite(&rec_type, 1, sizeof(char), debug_fp);
+	fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
+	fseek(debug_fp, 0, SEEK_END);
 }
 
+
+void * xmalloc(size_t size) {
+	void *mem = malloc(size);
+	if (!mem) {
+		fprintf(stderr, "xMalloc: Out Of memory [req: %ld]\n", (long)size);
+		exit(1);
+	}
+	return mem;
+}
+
--- a/src/define.h	Tue Jul 10 18:18:38 2007 -0700
+++ b/src/define.h	Tue Jul 10 20:23:55 2007 -0700
@@ -12,7 +12,7 @@
 #ifndef DEFINEH_H
 #define DEFINEH_H
 
-#ifdef DEBUG_ALL
+//	#ifdef DEBUG_ALL
 #define DEBUG_MODE_GEN
 #define DEBUGPRINT
 #define DEBUG_MODE_WARN
@@ -24,8 +24,7 @@
 #define DEBUG_MODE_INFO
 #define DEBUG_MODE_HEXDUMP
 #define DEBUG_MODE_FUNC
-//#define DEBUG_MODE_DECRYPT
-#endif
+//	#endif
 
 //number of items to save in memory between writes
 #define DEBUG_MAX_ITEMS 0
--- a/src/libpst.c	Tue Jul 10 18:18:38 2007 -0700
+++ b/src/libpst.c	Tue Jul 10 20:23:55 2007 -0700
@@ -13,8 +13,8 @@
 #include <wchar.h>
 
 #include <errno.h>
-#include <sys/stat.h> //mkdir
-#include <fcntl.h> // for Win32 definition of _O_BINARY
+#include <sys/stat.h>	// mkdir
+#include <fcntl.h>		// for Win32 definition of _O_BINARY
 #include "define.h"
 #include "libstrfunc.h"
 
@@ -24,28 +24,9 @@
 # include <unistd.h>
 #endif //ifdef _MSC_VER
 
-//#include <endian.h>
-//#include <byteswap.h>
-
 #include "libpst.h"
 #include "timeconv.h"
 
-//#ifdef _MSC_VER
-//#include "windows.h"
-//#define WARN printf
-//#define DEBUG_INFO printf
-//#define DEBUG_EMAIL printf
-//#define DEBUG_READ printf
-//#define DEBUG_DECRYPT printf
-//#define DEBUG_CODE printf
-//#define DEBUG_INDEX printf
-//#define DEBUG_WARN printf
-//#define DEBUG printf
-//
-//#define LE32_CPU(x) {}
-//#define LE16_CPU(x) {}
-//#endif // _MSC_VER
-
 #define FILE_SIZE_POINTER 0xA8
 #define INDEX_POINTER	  0xC4
 #define SECOND_POINTER	  0xBC
--- a/src/pst2ldif.cpp	Tue Jul 10 18:18:38 2007 -0700
+++ b/src/pst2ldif.cpp	Tue Jul 10 20:23:55 2007 -0700
@@ -362,10 +362,11 @@
 	char *fname = NULL;
 	char *temp = NULL;		  //temporary char pointer
 	char c;
+	char *d_log = NULL;
 	prog_name = argv[0];
 	pst_item *item = NULL;
 
-	while ((c = getopt(argc, argv, "b:c:Vh"))!= -1) {
+	while ((c = getopt(argc, argv, "b:c:d:Vh"))!= -1) {
 		switch (c) {
 		case 'b':
 			ldap_base = optarg;
@@ -379,6 +380,9 @@
 		case 'c':
 			ldap_class = optarg;
 			break;
+		case 'd':
+			d_log = optarg;
+			break;
 		case 'h':
 			usage();
 			exit(0);
@@ -402,9 +406,11 @@
 	}
 
 	#ifdef DEBUG_ALL
-		DEBUG_INIT("pst2ldif.log");
-		DEBUG_REGISTER_CLOSE();
+		// force a log file
+		if (!d_log) d_log = "pst2ldif.log";
 	#endif
+	DEBUG_INIT(d_log);
+	DEBUG_REGISTER_CLOSE();
 	DEBUG_ENT("main");
 	RET_DERROR(pst_open(&pstfile, fname, "r"), 1, ("Error opening File\n"));
 	RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n"));
--- a/src/readpst.c	Tue Jul 10 18:18:38 2007 -0700
+++ b/src/readpst.c	Tue Jul 10 20:23:55 2007 -0700
@@ -202,13 +202,11 @@
 	}
 
 #ifdef DEBUG_ALL
-	// initialize log file
-	if (!d_log)
-		d_log = "readpst.log";
+	// force a log file
+	if (!d_log) d_log = "readpst.log";
+#endif // defined DEBUG_ALL
 	DEBUG_INIT(d_log);
 	DEBUG_REGISTER_CLOSE();
-#endif // defined DEBUG_ALL
-
 	DEBUG_ENT("main");
 
 	if (argc > optind) {
@@ -275,10 +273,6 @@
 		item = NULL;
 	}
 
-	/*	if ((item = _pst_parse_item(&pstfile, d_ptr)) == NULL || item->folder == NULL) {
-		DEBUG_MAIN(("main: Could not get \"Top Of Personal Folder\" record\n"));
-		return -2;
-		}*/
 	d_ptr = d_ptr->child; // do the children of TOPF
 
 	if (output_mode != OUTPUT_QUIET) printf("Processing items...\n");
@@ -328,8 +322,7 @@
 					skip_child = 1;
 				} else {
 					DEBUG_MAIN(("main: Folder has NO children. Creating directory, and closing again\n"));
-					if (output_mode != OUTPUT_QUIET)
-						printf("\tNo items to process in folder \"%s\", should have been %i\n", f->dname, f->stored_count);
+					if (output_mode != OUTPUT_QUIET) printf("\tNo items to process in folder \"%s\", should have been %i\n", f->dname, f->stored_count);
 					head = f->next;
 					if (f->output)
 						fclose(f->output);
--- a/xml/libpst.in	Tue Jul 10 18:18:38 2007 -0700
+++ b/xml/libpst.in	Tue Jul 10 20:23:55 2007 -0700
@@ -79,7 +79,7 @@
                 <varlistentry>
                     <term>-d <replaceable class="parameter">debug-file</replaceable></term>
                     <listitem><para>
-                        Specify name of debug log file. Defaults to "readpst.log". The
+                        Specify name of debug log file. The
                         log file is not an ascii file, it is a binary file readable
                         by <command>readpstlog</command>.
                     </para></listitem>
@@ -395,6 +395,7 @@
                 <arg><option>-V</option></arg>
                 <arg><option>-b <replaceable class="parameter">ldap-base</replaceable></option></arg>
                 <arg><option>-c <replaceable class="parameter">class</replaceable></option></arg>
+                <arg><option>-d <replaceable class="parameter">debug-file</replaceable></option></arg>
                 <arg choice='plain'>pstfilename</arg>
             </cmdsynopsis>
         </refsynopsisdiv>
@@ -429,6 +430,14 @@
                         contain the ldap attributes given below.
                     </para></listitem>
                 </varlistentry>
+                <varlistentry>
+                    <term>-d <replaceable class="parameter">debug-file</replaceable></term>
+                    <listitem><para>
+                        Specify name of debug log file. The
+                        log file is not an ascii file, it is a binary file readable
+                        by <command>readpstlog</command>.
+                    </para></listitem>
+                </varlistentry>
             </variablelist>
         </refsect1>