[cowdancer] 01/01: Reformat everything with the help of clang-format

James Clarke jrtc27 at moszumanska.debian.org
Tue Jan 17 23:44:31 UTC 2017


This is an automated email from the git hooks/post-receive script.

jrtc27 pushed a commit to branch wip/reformat
in repository cowdancer.

commit bf0c8685e508d4e54c0dff95a1dd7df863c85ff0
Author: James Clarke <jrtc27 at debian.org>
Date:   Tue Jan 17 23:33:21 2017 +0000

    Reformat everything with the help of clang-format
    
    It sometimes does stupid things, so don't blindly run clang-format.
    Check the formatting is sensible, and fix up by hand if needed.
---
 .clang-format                |   91 ++
 benchmarking/time-output.c   |   67 +-
 cow-shell.c                  |  176 ++-
 cowbuilder.c                 | 1067 +++++++++---------
 cowbuilder_util.c            |   67 +-
 cowbuilder_util.h            |    5 +-
 cowdancer-ilistcreate.c      |   23 +-
 cowdancer-ilistdump.c        |   89 +-
 cowdancer.c                  | 1165 ++++++++++----------
 file.c                       |  266 +++--
 file.h                       |    9 +-
 forkexec.c                   |  224 ++--
 ilist.h                      |   26 +-
 ilistcreate.c                |  204 ++--
 initrd/qemuinit.c            |  247 ++---
 log.c                        |  287 +++--
 log.h                        |   30 +-
 main.c                       |    7 +-
 parameter.c                  | 1490 ++++++++++++-------------
 parameter.h                  |  175 ++-
 qemuarch.c                   |  234 ++--
 qemuarch.h                   |   16 +-
 qemubuilder.c                | 2504 ++++++++++++++++++++----------------------
 qemuipsanitize.c             |  107 +-
 qemuipsanitize.h             |    5 +-
 test_cowbuilder.c            |   47 +-
 test_cowbuilder_util.c       |   85 +-
 test_file.c                  |   89 +-
 test_forkexec.c              |   47 +-
 test_ilistcreate.c           |    9 +-
 test_parameter.c             |   69 +-
 test_qemuarch.c              |   80 +-
 test_qemuipsanitize.c        |   58 +-
 tests/002_test_open.c        |   36 +-
 tests/003_test_fopen.c       |   26 +-
 tests/004_test_fopen64.c     |   26 +-
 tests/012_test_chown.c       |   47 +-
 tests/013_test_chmod.c       |   29 +-
 tests/015_test_ilistdelete.c |   83 +-
 tests/016_memleakcheck.c     |  136 +--
 tests/018_testcxx.cc         |    7 +-
 tests/021_test_open.c        |   19 +-
 tests/022_kernel_waitpid.c   |   39 +-
 43 files changed, 4546 insertions(+), 4967 deletions(-)

diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..fe39d88
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,91 @@
+---
+Language:        Cpp
+# BasedOnStyle:  LLVM
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlinesLeft: false
+AlignOperands:   true
+AlignTrailingComments: true
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: false
+BinPackParameters: false
+BraceWrapping:
+  AfterClass:      false
+  AfterControlStatement: false
+  AfterEnum:       false
+  AfterFunction:   false
+  AfterNamespace:  false
+  AfterObjCDeclaration: false
+  AfterStruct:     false
+  AfterUnion:      false
+  BeforeCatch:     false
+  BeforeElse:      false
+  IndentBraces:    false
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Attach
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializersBeforeComma: false
+BreakStringLiterals: false
+ColumnLimit:     80
+CommentPragmas:  '^ IWYU pragma:'
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DerivePointerAlignment: false
+DisableFormat:   false
+ExperimentalAutoDetectBinPacking: false
+ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
+IncludeCategories:
+  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
+    Priority:        2
+  - Regex:           '^(<|"(gtest|isl|json)/)'
+    Priority:        3
+  - Regex:           '.*'
+    Priority:        1
+IndentCaseLabels: true
+IndentWidth:     4
+IndentWrappedFunctionNames: false
+KeepEmptyLinesAtTheStartOfBlocks: true
+MacroBlockBegin: ''
+MacroBlockEnd:   ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBlockIndentWidth: 4
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: true
+PenaltyBreakBeforeFirstCallParameter: 19
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakString: 1000
+PenaltyExcessCharacter: 1000000
+PenaltyReturnTypeOnItsOwnLine: 60
+PointerAlignment: Right
+ReflowComments:  false
+SortIncludes:    false
+SpaceAfterCStyleCast: false
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeParens: ControlStatements
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles:  false
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard:        Cpp11
+TabWidth:        4
+UseTab:          Always
+...
+
diff --git a/benchmarking/time-output.c b/benchmarking/time-output.c
old mode 100755
new mode 100644
index 46738f0..6244daa
--- a/benchmarking/time-output.c
+++ b/benchmarking/time-output.c
@@ -13,46 +13,37 @@
 #include <string.h>
 #include <termios.h>
 
-void fix_terminal() 
-{
-  struct termios t; 
-
-  if (isatty(1)) 
-    {
-      tcgetattr(1, &t); 
-      t.c_lflag |= ECHO;
-      tcsetattr(1, TCSANOW, &t); 
-    }
+void fix_terminal() {
+	struct termios t;
+
+	if (isatty(1)) {
+		tcgetattr(1, &t);
+		t.c_lflag |= ECHO;
+		tcsetattr(1, TCSANOW, &t);
+	}
 }
 
-int main(int argc, char** argv)
-{
-  time_t start = time(NULL);
-  char b[4096];
-  FILE* f = NULL;  
-
-  if (argc == 2) 
-    {
-      printf("Output tee to extra file %s\n", argv[1]);
-      f = fopen(argv[1], "w");
-    }
-  
-  while(fgets(b,sizeof(b),stdin)!=0)
-    {
-      printf("[%i] %s", 
-	     (int)difftime(time(NULL), start), 
-	     b);
-      fflush(stdout);
-      if (f) 
-	{
-	  fprintf(f, "[%i] %s", 
-		  (int)difftime(time(NULL), start), 
-		  b);
-	  fflush(f);
+int main(int argc, char **argv) {
+	time_t start = time(NULL);
+	char b[4096];
+	FILE *f = NULL;
+
+	if (argc == 2) {
+		printf("Output tee to extra file %s\n", argv[1]);
+		f = fopen(argv[1], "w");
+	}
+
+	while (fgets(b, sizeof(b), stdin) != 0) {
+		printf("[%i] %s", (int)difftime(time(NULL), start), b);
+		fflush(stdout);
+		if (f) {
+			fprintf(f, "[%i] %s", (int)difftime(time(NULL), start), b);
+			fflush(f);
+		}
 	}
-    }
 
-  if (f) fclose(f);
-  fix_terminal();
-  return 0;
+	if (f)
+		fclose(f);
+	fix_terminal();
+	return 0;
 }
diff --git a/cow-shell.c b/cow-shell.c
old mode 100755
new mode 100644
index bc964a4..9e9f6c1
--- a/cow-shell.c
+++ b/cow-shell.c
@@ -17,120 +17,100 @@
 #include "ilist.h"
 #include "log.h"
 
-const char* ilist_PRGNAME="cow-shell";
+const char *ilist_PRGNAME = "cow-shell";
 
-static const char* ilistpath="./.ilist";
+static const char *ilistpath = "./.ilist";
 
 /*
  * remove ilist after use.
  */
-static void ilist_deleter(const char* ilistfile)
-{
-  if(fork()==0)
-    {
-      /* I am the child process */
-
-      pid_t parent_pid=getppid();
-      if (daemon(0,1) < 0)
-	{
-	  log_perror("cow-shell daemon");
-	  exit (-1);
+static void ilist_deleter(const char *ilistfile) {
+	if (fork() == 0) {
+		/* I am the child process */
+
+		pid_t parent_pid = getppid();
+		if (daemon(0, 1) < 0) {
+			log_perror("cow-shell daemon");
+			exit(-1);
+		}
+		while (kill(parent_pid, 0) >= 0) {
+			sleep(1);
+		}
+		if (unlink(ilistfile) == -1) {
+			log_perror("cow-shell unlink .ilist");
+			exit(1);
+		}
+		exit(0);
 	}
-      while (kill(parent_pid,0) >= 0)
-	{
-	  sleep(1);
-	}
-      if (unlink(ilistfile)==-1)
-	{
-	  log_perror("cow-shell unlink .ilist");
-	  exit(1);
-	}
-      exit(0);
-    }
 }
 
 /**
  * Set environment variables.
  */
 static void set_env_vars() {
-  char* buf;
-
-  // For sending down as environment variable, use a canonicalized version.
-  char* canonicalized_ilistpath=canonicalize_file_name(ilistpath);
-  setenv("COWDANCER_ILISTFILE",
-	 canonicalized_ilistpath,1);
-  unsetenv("COWDANCER_IGNORE");
-  free(canonicalized_ilistpath);
-
-  asprintf(&buf, "%s%s%s",
-	   getenv("LD_PRELOAD")?:"",
-	   getenv("LD_PRELOAD")?" ":"",
-	   getenv("COWDANCER_SO") ?: COWDANCER_SO);
-  setenv("LD_PRELOAD", buf, 1);
-  free(buf);
+	char *buf;
+
+	// For sending down as environment variable, use a canonicalized version.
+	char *canonicalized_ilistpath = canonicalize_file_name(ilistpath);
+	setenv("COWDANCER_ILISTFILE", canonicalized_ilistpath, 1);
+	unsetenv("COWDANCER_IGNORE");
+	free(canonicalized_ilistpath);
+
+	asprintf(&buf,
+			 "%s%s%s",
+			 getenv("LD_PRELOAD") ?: "",
+			 getenv("LD_PRELOAD") ? " " : "",
+			 getenv("COWDANCER_SO") ?: COWDANCER_SO);
+	setenv("LD_PRELOAD", buf, 1);
+	free(buf);
 }
 
 /* give me a command-line to exec,
    and I will cow-keep what's under this directory. */
-int main(int ac, char** av)
-{
-  struct stat st;
-  int cowdancer_reuse;
-
-  cowdancer_reuse=getenv("COWDANCER_REUSE") &&
-    !strcmp(getenv("COWDANCER_REUSE"),"yes") ;
-
-  if (cowdancer_reuse && !stat(ilistpath, &st))
-    {
-      /* if reuse flag is on and file already exists
-	 do nothing */
-    }
-  else
-    {
-      if (unlink(ilistpath)==-1)
-	{
-	  if (errno == ENOENT)
-	    {
-	      /* expected */
-	    }
-	  else
-	    {
-	      log_perror("cow-shell: unlink of .ilist failed");
-	      return 1;
-	    }
+int main(int ac, char **av) {
+	struct stat st;
+	int cowdancer_reuse;
+
+	cowdancer_reuse =
+		getenv("COWDANCER_REUSE") && !strcmp(getenv("COWDANCER_REUSE"), "yes");
+
+	if (cowdancer_reuse && !stat(ilistpath, &st)) {
+		/* if reuse flag is on and file already exists
+		   do nothing */
+	} else {
+		if (unlink(ilistpath) == -1) {
+			if (errno == ENOENT) {
+				/* expected */
+			} else {
+				log_perror("cow-shell: unlink of .ilist failed");
+				return 1;
+			}
+		}
+		if (ilistcreate(ilistpath, NULL)) {
+			ilist_outofmemory(".ilist creation failed");
+			return 1;
+		}
+	}
+
+	set_env_vars();
+
+	if (!cowdancer_reuse) {
+		/* if reuse flag is not on, remove the file */
+		ilist_deleter(ilistpath);
 	}
-      if(ilistcreate(ilistpath, NULL))
-	{
-	  ilist_outofmemory(".ilist creation failed");
-	  return 1;
+
+	if (ac > 1)
+		execvp(av[1], av + 1);
+	else {
+		const char *myshell = getenv("SHELL") ?: "/bin/sh";
+		log_printf(log_info, "Invoking %s", myshell);
+
+		execlp(myshell, myshell, NULL);
+		log_perror("cow-shell: exec");
+
+		log_printf(log_warn, "Falling back to /bin/sh");
+		execlp("/bin/sh", "/bin/sh", NULL);
 	}
-    }
-
-  set_env_vars();
-
-  if (!cowdancer_reuse)
-    {
-      /* if reuse flag is not on, remove the file */
-      ilist_deleter(ilistpath);
-    }
-
-  if (ac>1)
-    execvp(av[1], av+1);
-  else
-    {
-      const char* myshell=getenv("SHELL")?:"/bin/sh";
-      log_printf(log_info, "Invoking %s", myshell);
-
-      execlp(myshell,
-	     myshell,
-	     NULL);
-      log_perror("cow-shell: exec");
-
-      log_printf(log_warn, "Falling back to /bin/sh");
-      execlp("/bin/sh",
-	     "/bin/sh",
-	     NULL);
-    }
-  log_perror("cow-shell: exec");
-  return 1;
+	log_perror("cow-shell: exec");
+	return 1;
 }
diff --git a/cowbuilder.c b/cowbuilder.c
old mode 100755
new mode 100644
index 3288697..9d3ada9
--- a/cowbuilder.c
+++ b/cowbuilder.c
@@ -51,8 +51,7 @@ will show the list of files changed.
 #include "ilist.h"
 #include "parameter.h"
 
-const char* ilist_PRGNAME="cowbuilder";
-
+const char *ilist_PRGNAME = "cowbuilder";
 
 /**
    remove a directory.
@@ -61,9 +60,8 @@ const char* ilist_PRGNAME="cowbuilder";
 
    @return 0 on success
  */
-static int rmrf(const char* path)
-{
-  return forkexeclp("rm", "rm", "-rf", path, NULL);
+static int rmrf(const char *path) {
+	return forkexeclp("rm", "rm", "-rf", path, NULL);
 }
 
 /**
@@ -71,37 +69,34 @@ static int rmrf(const char* path)
 
    @return 0 on success
  */
-static int rmrf_check_mount(const char* path)
-{
-  /*
+static int rmrf_check_mount(const char *path) {
+	/*
    * A directory bind-mounted into path will be cleaned out by rmrf().
    * To avoid that potential disaster we want to detect if there's
    * something mounted under path before deleting it.
    */
 
-  char* canonical_path = strdupa(path);
+	char *canonical_path = strdupa(path);
 
-  canonicalize_doubleslash(path, canonical_path);
-  if (check_mountpoint(canonical_path)) {
-    return 1;
-  }
+	canonicalize_doubleslash(path, canonical_path);
+	if (check_mountpoint(canonical_path)) {
+		return 1;
+	}
 
-  return rmrf(path);
+	return rmrf(path);
 }
 
 /**
  * @return .ilist path as malloc()ed string, or NULL on error.
  */
-static char* get_ilistfile_path(const struct pbuilderconfig* pc)
-{
-  char *ilistfile;
-  if (0>asprintf(&ilistfile, "%s/.ilist", pc->buildplace))
-    {
-      /* outofmemory */
-      log_printf(log_error, "cowdancer: out of memory.");
-      return NULL;
-    }
-  return ilistfile;
+static char *get_ilistfile_path(const struct pbuilderconfig *pc) {
+	char *ilistfile;
+	if (0 > asprintf(&ilistfile, "%s/.ilist", pc->buildplace)) {
+		/* outofmemory */
+		log_printf(log_error, "cowdancer: out of memory.");
+		return NULL;
+	}
+	return ilistfile;
 }
 
 /**
@@ -109,87 +104,90 @@ static char* get_ilistfile_path(const struct pbuilderconfig* pc)
 
    return 0 on success, 1 on fail.
  */
-int break_cowlink(const char* s)
-{
-  char *backup_file;
-  struct stat buf;
-  int pid;
-  int status;
-
-  if(lstat(s, &buf))
-    return 1;			/* if stat fails, the file probably
+int break_cowlink(const char *s) {
+	char *backup_file;
+	struct stat buf;
+	int pid;
+	int status;
+
+	if (lstat(s, &buf))
+		return 1; /* if stat fails, the file probably
 				   doesn't exist; return, success */
-  if (S_ISLNK(buf.st_mode))
-    return 1;			/* ignore symbollic link */
-
-  if((buf.st_nlink > 1) && 	/* it is hardlinked */
-     S_ISREG(buf.st_mode))
-    {
-      if (asprintf(&backup_file, "%sXXXXXX", s)==-1)
-	{
-	  ilist_outofmemory("out of memory in break_cowlink");
-	  return 1;
-	}
-      switch(pid=fork())
-	{
-	case 0:
-	  /* child process, run cp */
-	  putenv("COWDANCER_IGNORE=yes");
-	  execl("/bin/cp", "/bin/cp", "-a", s, backup_file, NULL);
-	  log_perror("execl:cp:");
-	  exit(EXIT_FAILURE);
-	case -1:
-	  /* error condition in fork(); something is really wrong */
-	  ilist_outofmemory("out of memory in check_inode_and_copy, 2");
-	  break;
-	default:
-	  /* parent process, waiting for cp -a to terminate */
-	  if(-1==waitpid(pid, &status, 0))
-	    {
-	      log_perror("waitpid:cp");
-	      log_printf(log_error, "%s: unexpected waitpid error when waiting for process %i with status %x",
-		      ilist_PRGNAME, pid, status);
-	      goto error_spm;
-	    }
-
-	  if (!WIFEXITED(status))
-	    {
-	      /* something unexpected */
-	      log_printf(log_error, "%s: unexpected WIFEXITED status in waitpid: %x", ilist_PRGNAME,
-		      (int)status);
-	      goto error_spm;
-	    }
-	  else if (WEXITSTATUS(status))
-	    {
-	      /* cp -a failed */
-	      log_printf(log_error, "%s: cp -a failed for %s", ilist_PRGNAME, backup_file);
-	      goto error_spm;
-	    }
-	  /* when cp -a succeeded, overwrite the target file from the temporary file with rename */
-	  else if (-1==rename(backup_file, s))
-	    {
-	      log_perror("file overwrite with rename");
-	      log_printf(log_error, "while trying rename of %s to %s",  backup_file, s);
-	      goto error_spm;
-	    }
-	}
-    }
-  return 0;
-
- error_spm:
-  free(backup_file);
-  return 1;
+	if (S_ISLNK(buf.st_mode))
+		return 1; /* ignore symbollic link */
+
+	if ((buf.st_nlink > 1) && /* it is hardlinked */
+		S_ISREG(buf.st_mode)) {
+		if (asprintf(&backup_file, "%sXXXXXX", s) == -1) {
+			ilist_outofmemory("out of memory in break_cowlink");
+			return 1;
+		}
+		switch (pid = fork()) {
+			case 0:
+				/* child process, run cp */
+				putenv("COWDANCER_IGNORE=yes");
+				execl("/bin/cp", "/bin/cp", "-a", s, backup_file, NULL);
+				log_perror("execl:cp:");
+				exit(EXIT_FAILURE);
+			case -1:
+				/* error condition in fork(); something is really wrong */
+				ilist_outofmemory("out of memory in check_inode_and_copy, 2");
+				break;
+			default:
+				/* parent process, waiting for cp -a to terminate */
+				if (-1 == waitpid(pid, &status, 0)) {
+					log_perror("waitpid:cp");
+					log_printf(
+						log_error,
+						"%s: unexpected waitpid error when waiting for process %i with status %x",
+						ilist_PRGNAME,
+						pid,
+						status);
+					goto error_spm;
+				}
+
+				if (!WIFEXITED(status)) {
+					/* something unexpected */
+					log_printf(log_error,
+							   "%s: unexpected WIFEXITED status in waitpid: %x",
+							   ilist_PRGNAME,
+							   (int)status);
+					goto error_spm;
+				} else if (WEXITSTATUS(status)) {
+					/* cp -a failed */
+					log_printf(log_error,
+							   "%s: cp -a failed for %s",
+							   ilist_PRGNAME,
+							   backup_file);
+					goto error_spm;
+				}
+				/* when cp -a succeeded, overwrite the target file from the temporary file with rename */
+				else if (-1 == rename(backup_file, s)) {
+					log_perror("file overwrite with rename");
+					log_printf(log_error,
+							   "while trying rename of %s to %s",
+							   backup_file,
+							   s);
+					goto error_spm;
+				}
+		}
+	}
+	return 0;
+
+error_spm:
+	free(backup_file);
+	return 1;
 }
 
 /**
    Convenience function to call break_cowlink with basepath prepended.
  */
-static void break_cowlink_inside_chroot(const char* basepath, const char* filepath)
-{
-  char* filename;
-  asprintf(&filename, "%s%s", basepath, filepath);
-  break_cowlink(filename);
-  free(filename);
+static void break_cowlink_inside_chroot(const char *basepath,
+										const char *filepath) {
+	char *filename;
+	asprintf(&filename, "%s%s", basepath, filepath);
+	break_cowlink(filename);
+	free(filename);
 }
 
 /**
@@ -198,243 +196,220 @@ static void break_cowlink_inside_chroot(const char* basepath, const char* filepa
 
    @return 0 on success, 1 on failure.
  */
-static int cpbuilder_internal_cowcopy(const struct pbuilderconfig* pc)
-{
-  char *ilistfile;
-
-  log_printf(log_info, "Copying COW directory");
-  if (0!=rmrf_check_mount(pc->buildplace))
-    return 1;
-  if (0!=forkexeclp("cp", "cp", "-al", pc->basepath, pc->buildplace, NULL))
-    return 1;
-
-  /* ilist file handling.
-     Delete existing ilist file if it exists, because I use COWDANCER_REUSE */
-  if(!(ilistfile=get_ilistfile_path(pc)))
-    {
-      /* outofmemory */
-      log_printf(log_error, "cowdancer: out of memory.");
-      return 1;
-    }
-  if (unlink(ilistfile))
-    {
-      /* if there was no ilist file in the beginning, that's not a
-	 problem.
-       */
-      log_printf(log_info, "unlink for ilistfile %s failed, it didn't exist?", ilistfile);
-    }
-  else
-    {
-      log_printf(log_info, "removed stale ilistfile %s", ilistfile);
-    }
-  free(ilistfile);
-
-  /* Non-hardlinkable file support
-   */
-  break_cowlink_inside_chroot(pc->buildplace, "/var/cache/debconf/config.dat");
-  break_cowlink_inside_chroot(pc->buildplace, "/var/cache/debconf/templates.dat");
-  break_cowlink_inside_chroot(pc->buildplace, "/var/cache/debconf/passwords.dat");
+static int cpbuilder_internal_cowcopy(const struct pbuilderconfig *pc) {
+	char *ilistfile;
+
+	log_printf(log_info, "Copying COW directory");
+	if (0 != rmrf_check_mount(pc->buildplace))
+		return 1;
+	if (0 != forkexeclp("cp", "cp", "-al", pc->basepath, pc->buildplace, NULL))
+		return 1;
+
+	/* ilist file handling.
+	   Delete existing ilist file if it exists, because I use COWDANCER_REUSE */
+	if (!(ilistfile = get_ilistfile_path(pc))) {
+		/* outofmemory */
+		log_printf(log_error, "cowdancer: out of memory.");
+		return 1;
+	}
+	if (unlink(ilistfile)) {
+		/* if there was no ilist file in the beginning, that's not a
+		   problem. */
+		log_printf(log_info,
+				   "unlink for ilistfile %s failed, it didn't exist?",
+				   ilistfile);
+	} else {
+		log_printf(log_info, "removed stale ilistfile %s", ilistfile);
+	}
+	free(ilistfile);
+
+	/* Non-hardlinkable file support */
+	break_cowlink_inside_chroot(pc->buildplace,
+								"/var/cache/debconf/config.dat");
+	break_cowlink_inside_chroot(pc->buildplace,
+								"/var/cache/debconf/templates.dat");
+	break_cowlink_inside_chroot(pc->buildplace,
+								"/var/cache/debconf/passwords.dat");
 
-  return 0;
+	return 0;
 }
 
 /*
    @return 0 on success, 1 on failure.
  */
-static int cpbuilder_internal_cleancow(const struct pbuilderconfig* pc)
-{
-  log_printf(log_info, "Cleaning COW directory");
+static int cpbuilder_internal_cleancow(const struct pbuilderconfig *pc) {
+	log_printf(log_info, "Cleaning COW directory");
 
-  if (!pc->debug && 0!=rmrf_check_mount(pc->buildplace))
-    return 1;
-  return 0;
+	if (!pc->debug && 0 != rmrf_check_mount(pc->buildplace))
+		return 1;
+	return 0;
 }
 
-int cpbuilder_check_config(const struct pbuilderconfig* pc)
-{
-  return 0;
+int cpbuilder_check_config(const struct pbuilderconfig *pc) {
+	return 0;
 }
 
 /*
  * @return: return code of pbuilder, or <0 on failure
  */
-int cpbuilder_build(const struct pbuilderconfig* pc, const char* dscfile_)
-{
-  const char* dscfile=canonicalize_file_name(dscfile_);
-  char* ilistfile;
-  char* buf_chroot=NULL;
-  int ret;
-  char * prevdir;
-
-  if (!dscfile)
-    {
-      log_printf(log_error, "File not found: %s", dscfile_);
-      return 1;
-    }
-
-  if (cpbuilder_internal_cowcopy(pc))
-    {
-      log_printf(log_error, "Failed cowcopy.");
-      return 1;
-    }
-
-
-  if (0>asprintf(&buf_chroot, "chroot %s cow-shell", pc->buildplace))
-    {
-      /* outofmemory */
-      log_printf(log_error, "cowdancer: out of memory.");
-      return -1;
-    }
-
-  if(!(ilistfile=get_ilistfile_path(pc)))
-    {
-      /* outofmemory */
-      log_printf(log_error, "cowdancer: out of memory.");
-      return 1;
-    }
-
-  prevdir=get_current_dir_name();
-  chdir(pc->buildplace);
-
-  if (forkexeclp("chroot",
-		 "chroot",
-		 pc->buildplace,
-		 "cowdancer-ilistcreate",
-		 "/.ilist",
-		 "find . -xdev -path ./home -prune -o \\( \\( -type l -o -type f \\) -a -links +1 -print0 \\) | xargs -0 stat --format '%d %i '",
-		 NULL))
-    {
-      /* if there was an error, back off to manual form */
-
-      /* but first, try etch-workaround */
-      if (pc->debian_etch_workaround)
-	{
-	  log_printf(log_warn, "Trying my backup method for working with Debian Etch chroots.");
-	  if (forkexeclp("chroot",
-			 "chroot",
-			 pc->buildplace,
-			 "cow-shell",
-			 "/bin/true",
-			 NULL))
-	    {
-	      /* I failed, what can I do? noooo */
-	      log_printf(log_error, "failed running Debian Etch compatibility backup plan, I'm in panic, eek.");
-	      return 1;
-	    }
-	}
-      else
-	{
-	  /*
+int cpbuilder_build(const struct pbuilderconfig *pc, const char *dscfile_) {
+	const char *dscfile = canonicalize_file_name(dscfile_);
+	char *ilistfile;
+	char *buf_chroot = NULL;
+	int ret;
+	char *prevdir;
+
+	if (!dscfile) {
+		log_printf(log_error, "File not found: %s", dscfile_);
+		return 1;
+	}
+
+	if (cpbuilder_internal_cowcopy(pc)) {
+		log_printf(log_error, "Failed cowcopy.");
+		return 1;
+	}
+
+	if (0 > asprintf(&buf_chroot, "chroot %s cow-shell", pc->buildplace)) {
+		/* outofmemory */
+		log_printf(log_error, "cowdancer: out of memory.");
+		return -1;
+	}
+
+	if (!(ilistfile = get_ilistfile_path(pc))) {
+		/* outofmemory */
+		log_printf(log_error, "cowdancer: out of memory.");
+		return 1;
+	}
+
+	prevdir = get_current_dir_name();
+	chdir(pc->buildplace);
+
+	if (forkexeclp(
+			"chroot",
+			"chroot",
+			pc->buildplace,
+			"cowdancer-ilistcreate",
+			"/.ilist",
+			"find . -xdev -path ./home -prune -o \\( \\( -type l -o -type f \\) -a -links +1 -print0 \\) | xargs -0 stat --format '%d %i '",
+			NULL)) {
+		/* if there was an error, back off to manual form */
+
+		/* but first, try etch-workaround */
+		if (pc->debian_etch_workaround) {
+			log_printf(
+				log_warn,
+				"Trying my backup method for working with Debian Etch chroots.");
+			if (forkexeclp("chroot",
+						   "chroot",
+						   pc->buildplace,
+						   "cow-shell",
+						   "/bin/true",
+						   NULL)) {
+				/* I failed, what can I do? noooo */
+				log_printf(
+					log_error,
+					"failed running Debian Etch compatibility backup plan, I'm in panic, eek.");
+				return 1;
+			}
+		} else {
+			/*
 	     try the normal backup method
 
 	     It probably doesn't work but try it anyway.
 	   */
-	  log_printf(log_warn, "cowdancer-ilistcreate failed to run within chroot, falling back to old method");
-	  ilistcreate(ilistfile,
-		      "find . -xdev -path ./home -prune -o \\( \\( -type l -o -type f \\) -a -links +1 -print0 \\) | xargs -0 stat --format '%d %i '");
-	}
-    }
-  chdir(prevdir);
-  free(prevdir);
-
-  log_printf(log_info, "Invoking pbuilder");
-  pbuildercommandline[1]="build";
-  PBUILDER_ADD_PARAM("--buildplace");
-  PBUILDER_ADD_PARAM(pc->buildplace);
-  if (pc->buildresult)
-    {
-      PBUILDER_ADD_PARAM("--buildresult");
-      PBUILDER_ADD_PARAM(pc->buildresult);
-    }
-  if (pc->arch)
-    {
-      PBUILDER_ADD_PARAM("--architecture");
-      PBUILDER_ADD_PARAM(pc->arch);
-    }
-  if (pc->extrapackages)
-    {
-      PBUILDER_ADD_PARAM("--extrapackages");
-      PBUILDER_ADD_PARAM(pc->extrapackages);
-    }
-  if (pc->binary_arch)
-    {
-      PBUILDER_ADD_PARAM("--binary-arch");
-    }
-  else if (pc->binary_indep)
-    {
-      PBUILDER_ADD_PARAM("--binary-indep");
-    }
-  PBUILDER_ADD_PARAM("--no-targz");
-  PBUILDER_ADD_PARAM("--internal-chrootexec");
-  PBUILDER_ADD_PARAM(buf_chroot);
-  PBUILDER_ADD_PARAM(strdup(dscfile));
-  PBUILDER_ADD_PARAM(NULL);
-  ret=forkexecvp(pbuildercommandline);
-  free(buf_chroot);
-  if (ret < 128)
-    cpbuilder_internal_cleancow(pc);
-  else
-    log_printf(log_warn, "pbuilder build aborted, not cleaning");
-
-  free(ilistfile);
-  return ret;
+			log_printf(
+				log_warn,
+				"cowdancer-ilistcreate failed to run within chroot, falling back to old method");
+			ilistcreate(
+				ilistfile,
+				"find . -xdev -path ./home -prune -o \\( \\( -type l -o -type f \\) -a -links +1 -print0 \\) | xargs -0 stat --format '%d %i '");
+		}
+	}
+	chdir(prevdir);
+	free(prevdir);
+
+	log_printf(log_info, "Invoking pbuilder");
+	pbuildercommandline[1] = "build";
+	PBUILDER_ADD_PARAM("--buildplace");
+	PBUILDER_ADD_PARAM(pc->buildplace);
+	if (pc->buildresult) {
+		PBUILDER_ADD_PARAM("--buildresult");
+		PBUILDER_ADD_PARAM(pc->buildresult);
+	}
+	if (pc->arch) {
+		PBUILDER_ADD_PARAM("--architecture");
+		PBUILDER_ADD_PARAM(pc->arch);
+	}
+	if (pc->extrapackages) {
+		PBUILDER_ADD_PARAM("--extrapackages");
+		PBUILDER_ADD_PARAM(pc->extrapackages);
+	}
+	if (pc->binary_arch) {
+		PBUILDER_ADD_PARAM("--binary-arch");
+	} else if (pc->binary_indep) {
+		PBUILDER_ADD_PARAM("--binary-indep");
+	}
+	PBUILDER_ADD_PARAM("--no-targz");
+	PBUILDER_ADD_PARAM("--internal-chrootexec");
+	PBUILDER_ADD_PARAM(buf_chroot);
+	PBUILDER_ADD_PARAM(strdup(dscfile));
+	PBUILDER_ADD_PARAM(NULL);
+	ret = forkexecvp(pbuildercommandline);
+	free(buf_chroot);
+	if (ret < 128)
+		cpbuilder_internal_cleancow(pc);
+	else
+		log_printf(log_warn, "pbuilder build aborted, not cleaning");
+
+	free(ilistfile);
+	return ret;
 }
 
-int cpbuilder_create(const struct pbuilderconfig* pc)
-{
-  int ret;
-
-  mkdir(pc->basepath,0777);
-  log_printf(log_info, "Invoking pbuilder");
-
-  pbuildercommandline[1]="create";
-  PBUILDER_ADD_PARAM("--buildplace");
-  PBUILDER_ADD_PARAM(pc->basepath);
-  if (pc->mirror)
-    {
-      PBUILDER_ADD_PARAM("--mirror");
-      PBUILDER_ADD_PARAM(pc->mirror);
-    }
-  if (pc->arch)
-    {
-      PBUILDER_ADD_PARAM("--architecture");
-      PBUILDER_ADD_PARAM(pc->arch);
-    }
-  PBUILDER_ADD_PARAM("--distribution");
-  PBUILDER_ADD_PARAM(pc->distribution);
-  PBUILDER_ADD_PARAM("--no-targz");
-  PBUILDER_ADD_PARAM("--extrapackages");
-  if (pc->extrapackages)
-    {
-      char *extrapackages;
-      if (0>asprintf(&extrapackages, "%s cowdancer", pc->extrapackages))
-	{
-	  log_printf(log_error, "cowdancer: out of memory.");
-	  ret = 1;
-	  goto out;
-	}
-      PBUILDER_ADD_PARAM(extrapackages);
-    }
-  else
-    {
-      PBUILDER_ADD_PARAM("cowdancer");
-    }
-  PBUILDER_ADD_PARAM(NULL);
-  ret=forkexecvp(pbuildercommandline);
+int cpbuilder_create(const struct pbuilderconfig *pc) {
+	int ret;
+
+	mkdir(pc->basepath, 0777);
+	log_printf(log_info, "Invoking pbuilder");
+
+	pbuildercommandline[1] = "create";
+	PBUILDER_ADD_PARAM("--buildplace");
+	PBUILDER_ADD_PARAM(pc->basepath);
+	if (pc->mirror) {
+		PBUILDER_ADD_PARAM("--mirror");
+		PBUILDER_ADD_PARAM(pc->mirror);
+	}
+	if (pc->arch) {
+		PBUILDER_ADD_PARAM("--architecture");
+		PBUILDER_ADD_PARAM(pc->arch);
+	}
+	PBUILDER_ADD_PARAM("--distribution");
+	PBUILDER_ADD_PARAM(pc->distribution);
+	PBUILDER_ADD_PARAM("--no-targz");
+	PBUILDER_ADD_PARAM("--extrapackages");
+	if (pc->extrapackages) {
+		char *extrapackages;
+		if (0 > asprintf(&extrapackages, "%s cowdancer", pc->extrapackages)) {
+			log_printf(log_error, "cowdancer: out of memory.");
+			ret = 1;
+			goto out;
+		}
+		PBUILDER_ADD_PARAM(extrapackages);
+	} else {
+		PBUILDER_ADD_PARAM("cowdancer");
+	}
+	PBUILDER_ADD_PARAM(NULL);
+	ret = forkexecvp(pbuildercommandline);
 
 out:
-  if (ret)
-    {
-      log_printf(log_error, "pbuilder create failed");
-      if (!pc->debug && 0!=rmrf_check_mount(pc->basepath))
-	{
-	  log_printf(log_error, "Could not remove original tree");
-	}
-    }
-  return ret;
+	if (ret) {
+		log_printf(log_error, "pbuilder create failed");
+		if (!pc->debug && 0 != rmrf_check_mount(pc->basepath)) {
+			log_printf(log_error, "Could not remove original tree");
+		}
+	}
+	return ret;
 }
 
-
 /*
   mv basepath basepath-
   mv buildplace basepath
@@ -442,88 +417,83 @@ out:
 
   return 0 on success, nonzero on error.
  */
-int cpbuilder_internal_saveupdate(const struct pbuilderconfig* pc)
-{
-  int ret=0;
-  char* buf_tmpfile=NULL;
-
-  if (0>asprintf(&buf_tmpfile, "%s-%i-tmp", pc->buildplace, (int)getpid()))
-    {
-      /* outofmemory */
-      return -1;
-    }
-  log_printf(log_info, "Moving work directory [%s] to final location [%s] and cleaning up old copy",
-	 pc->buildplace, pc->basepath);
-
-  if(-1==rename(pc->basepath, buf_tmpfile))
-    {
-      log_perror("rename");
-      ret=-1;
-      goto out;
-    }
-  if(-1==rename(pc->buildplace, pc->basepath))
-    {
-      log_perror("rename");
-      ret=-1;
-      goto out;
-    }
-  if (0!=rmrf_check_mount(buf_tmpfile))
-    {
-      log_printf(log_error, "Could not remove original tree");
-      ret=-1;
-      goto out;
-    }
- out:
-  free(buf_tmpfile);
-  return ret;
+int cpbuilder_internal_saveupdate(const struct pbuilderconfig *pc) {
+	int ret = 0;
+	char *buf_tmpfile = NULL;
+
+	if (0 >
+		asprintf(&buf_tmpfile, "%s-%i-tmp", pc->buildplace, (int)getpid())) {
+		/* outofmemory */
+		return -1;
+	}
+	log_printf(
+		log_info,
+		"Moving work directory [%s] to final location [%s] and cleaning up old copy",
+		pc->buildplace,
+		pc->basepath);
+
+	if (-1 == rename(pc->basepath, buf_tmpfile)) {
+		log_perror("rename");
+		ret = -1;
+		goto out;
+	}
+	if (-1 == rename(pc->buildplace, pc->basepath)) {
+		log_perror("rename");
+		ret = -1;
+		goto out;
+	}
+	if (0 != rmrf_check_mount(buf_tmpfile)) {
+		log_printf(log_error, "Could not remove original tree");
+		ret = -1;
+		goto out;
+	}
+out:
+	free(buf_tmpfile);
+	return ret;
 }
 
+int cpbuilder_login(const struct pbuilderconfig *pc) {
+	char *buf_chroot;
+	int ret;
+
+	if (cpbuilder_internal_cowcopy(pc)) {
+		log_printf(log_error, "Failed cowcopy.");
+		return 1;
+	}
 
-int cpbuilder_login(const struct pbuilderconfig* pc)
-{
-  char *buf_chroot;
-  int ret;
-
-  if (cpbuilder_internal_cowcopy(pc))
-    {
-      log_printf(log_error, "Failed cowcopy.");
-      return 1;
-    }
-
-  /* this is the option passed to internal-chrootexec */
-  if (0>asprintf(&buf_chroot, "chroot %s cow-shell", pc->buildplace))
-    {
-      /* outofmemory */
-      return -1;
-    }
-  log_printf(log_info, "Invoking pbuilder");
-  pbuildercommandline[1]="login";
-  PBUILDER_ADD_PARAM("--buildplace");
-  PBUILDER_ADD_PARAM(pc->buildplace);
-  PBUILDER_ADD_PARAM("--no-targz");
-  PBUILDER_ADD_PARAM("--internal-chrootexec");
-  PBUILDER_ADD_PARAM(buf_chroot);
-  PBUILDER_ADD_PARAM(NULL);
-  ret=forkexecvp(pbuildercommandline);
-  free(buf_chroot);
-  if (ret < 128)
-    {
-      if (pc->save_after_login)
-	{
-	  if (0!=forkexeclp("chroot", "chroot", pc->buildplace, "apt-get", "clean", NULL))
-	    ret=-1;
-	  if (cpbuilder_internal_saveupdate(pc))
-	    ret=-1;
-	}
-      else
-	{
-	  cpbuilder_internal_cleancow(pc);
-	}
-    }
-  else
-    log_printf(log_warn, "pbuilder login aborted, not cleaning");
-
-  return ret;
+	/* this is the option passed to internal-chrootexec */
+	if (0 > asprintf(&buf_chroot, "chroot %s cow-shell", pc->buildplace)) {
+		/* outofmemory */
+		return -1;
+	}
+	log_printf(log_info, "Invoking pbuilder");
+	pbuildercommandline[1] = "login";
+	PBUILDER_ADD_PARAM("--buildplace");
+	PBUILDER_ADD_PARAM(pc->buildplace);
+	PBUILDER_ADD_PARAM("--no-targz");
+	PBUILDER_ADD_PARAM("--internal-chrootexec");
+	PBUILDER_ADD_PARAM(buf_chroot);
+	PBUILDER_ADD_PARAM(NULL);
+	ret = forkexecvp(pbuildercommandline);
+	free(buf_chroot);
+	if (ret < 128) {
+		if (pc->save_after_login) {
+			if (0 != forkexeclp("chroot",
+								"chroot",
+								pc->buildplace,
+								"apt-get",
+								"clean",
+								NULL))
+				ret = -1;
+			if (cpbuilder_internal_saveupdate(pc))
+				ret = -1;
+		} else {
+			cpbuilder_internal_cleancow(pc);
+		}
+	} else
+		log_printf(log_warn, "pbuilder login aborted, not cleaning");
+
+	return ret;
 }
 
 /*
@@ -531,77 +501,69 @@ int cpbuilder_login(const struct pbuilderconfig* pc)
 Mostly a copy of pbuilder login, executes a script.
 
  */
-int cpbuilder_execute(const struct pbuilderconfig* pc, char** av)
-{
-  char *buf_chroot;
-  int ret;
-  int i=0;
-
-  if (cpbuilder_internal_cowcopy(pc))
-    {
-      log_printf(log_error, "Failed cowcopy.");
-      return 1;
-    }
-
-  if (0>asprintf(&buf_chroot, "chroot %s cow-shell", pc->buildplace))
-    {
-      /* outofmemory */
-      return -1;
-    }
-  log_printf(log_info, "Invoking pbuilder");
-  pbuildercommandline[1]="execute";
-  PBUILDER_ADD_PARAM("--buildplace");
-  PBUILDER_ADD_PARAM(pc->buildplace);
-  PBUILDER_ADD_PARAM("--no-targz");
-  PBUILDER_ADD_PARAM("--internal-chrootexec");
-  PBUILDER_ADD_PARAM(buf_chroot);
-
-  /* add all additional parameters */
-  while(av[i])
-    {
-      PBUILDER_ADD_PARAM(av[i]);
-      i++;
-    }
-  PBUILDER_ADD_PARAM(NULL);
-  ret=forkexecvp(pbuildercommandline);
-  free(buf_chroot);
-  if (ret < 128)
-    {
-      if (pc->save_after_login)
-	{
-	  if (0!=forkexeclp("chroot", "chroot", pc->buildplace, "apt-get", "clean", NULL))
-	    ret=-1;
-	  if (cpbuilder_internal_saveupdate(pc))
-	    ret=-1;
-	}
-      else
-	{
-	  cpbuilder_internal_cleancow(pc);
-	}
-    }
-  else
-    log_printf(log_warn, "pbuilder execute aborted, not cleaning");
-
-  return ret;
-}
+int cpbuilder_execute(const struct pbuilderconfig *pc, char **av) {
+	char *buf_chroot;
+	int ret;
+	int i = 0;
+
+	if (cpbuilder_internal_cowcopy(pc)) {
+		log_printf(log_error, "Failed cowcopy.");
+		return 1;
+	}
 
+	if (0 > asprintf(&buf_chroot, "chroot %s cow-shell", pc->buildplace)) {
+		/* outofmemory */
+		return -1;
+	}
+	log_printf(log_info, "Invoking pbuilder");
+	pbuildercommandline[1] = "execute";
+	PBUILDER_ADD_PARAM("--buildplace");
+	PBUILDER_ADD_PARAM(pc->buildplace);
+	PBUILDER_ADD_PARAM("--no-targz");
+	PBUILDER_ADD_PARAM("--internal-chrootexec");
+	PBUILDER_ADD_PARAM(buf_chroot);
+
+	/* add all additional parameters */
+	while (av[i]) {
+		PBUILDER_ADD_PARAM(av[i]);
+		i++;
+	}
+	PBUILDER_ADD_PARAM(NULL);
+	ret = forkexecvp(pbuildercommandline);
+	free(buf_chroot);
+	if (ret < 128) {
+		if (pc->save_after_login) {
+			if (0 != forkexeclp("chroot",
+								"chroot",
+								pc->buildplace,
+								"apt-get",
+								"clean",
+								NULL))
+				ret = -1;
+			if (cpbuilder_internal_saveupdate(pc))
+				ret = -1;
+		} else {
+			cpbuilder_internal_cleancow(pc);
+		}
+	} else
+		log_printf(log_warn, "pbuilder execute aborted, not cleaning");
+
+	return ret;
+}
 
 /*
    find matching parameter, and check if it's set.
    @returns found=1, not found=0.
  */
-int find_matching_param(const char* option)
-{
-  int i;
-  int found=0;
-  for (i=0; i<offset; ++i)
-    {
-      if (!strcmp(pbuildercommandline[i], option))
-	{
-	  found=1;
-	}
-    }
-  return found;
+int find_matching_param(const char *option) {
+	int i;
+	int found = 0;
+	for (i = 0; i < offset; ++i) {
+		if (!strcmp(pbuildercommandline[i], option)) {
+			found = 1;
+		}
+	}
+	return found;
 }
 
 /**
@@ -609,12 +571,11 @@ int find_matching_param(const char* option)
 
    @return 0 on success, other values on failure.
  */
-int cpbuilder_update(const struct pbuilderconfig* pc)
-{
-  char * buf_chroot;
-  int ret;
+int cpbuilder_update(const struct pbuilderconfig *pc) {
+	char *buf_chroot;
+	int ret;
 
-  /*
+	/*
      I want the following actions done:
      NEW=buildplace OLD=basepath
 
@@ -627,107 +588,97 @@ int cpbuilder_update(const struct pbuilderconfig* pc)
      But if you ignore that part, it's really simple.
   */
 
-  /* This simple process will not disrupt already-running pbuilder-cow session,
-     but it will not work with new pbuilder-cow sessions until this process finishes,
-     and any failure to pbuilder update will leave crap.
-  */
+	/* This simple process will not disrupt already-running pbuilder-cow session,
+	   but it will not work with new pbuilder-cow sessions until this process finishes,
+	   and any failure to pbuilder update will leave crap. */
 
-  if (0>asprintf(&buf_chroot,
-		 (pc->no_cowdancer_update)?"chroot %s":	/* try to not use cow-shell, when
+	if (0 > asprintf(&buf_chroot,
+					 (pc->no_cowdancer_update)
+						 ? "chroot %s"
+						 : /* try to not use cow-shell, when
 							   no-cowdancer-update option is used */
-		 "chroot %s cow-shell",
-		 pc->buildplace))
-    {
-      /* if outofmemory, die. */
-      log_printf(log_error, "Out of memory.");
-      return -1;
-    }
-
-  if (cpbuilder_internal_cowcopy(pc))
-    {
-      log_printf(log_error, "Failed cowcopy.");
-      return 1;
-    }
-
-  log_printf(log_info, "Invoking pbuilder");
-
-  pbuildercommandline[1]="update";
-  PBUILDER_ADD_PARAM("--buildplace");
-  PBUILDER_ADD_PARAM(pc->buildplace);
-  if (find_matching_param("--override-config"))
-    {
-      if (pc->mirror)
-	{
-	  PBUILDER_ADD_PARAM("--mirror");
-	  PBUILDER_ADD_PARAM(pc->mirror);
-	}
-      PBUILDER_ADD_PARAM("--distribution");
-      PBUILDER_ADD_PARAM(pc->distribution);
-    }
-  if (pc->extrapackages)
-    {
-      PBUILDER_ADD_PARAM("--extrapackages");
-      PBUILDER_ADD_PARAM(pc->extrapackages);
-    }
-  PBUILDER_ADD_PARAM("--no-targz");
-  PBUILDER_ADD_PARAM("--internal-chrootexec");
-  PBUILDER_ADD_PARAM(buf_chroot);
-  PBUILDER_ADD_PARAM(NULL);
-
-  ret=forkexecvp(pbuildercommandline);
-  if (ret)
-    {
-      if (ret < 128)
-	{
-	  log_printf(log_error, "pbuilder update failed");
-
-	  if (!pc->no_cowdancer_update)
-	    {
-	      /* advise the user to try this option first */
-	      log_printf(log_error, "could not update with cowdancer, try --no-cowdancer-update option");
-	    }
-
-	  if (!pc->debug && 0!=rmrf_check_mount(pc->buildplace))
-	    {
-	      log_printf(log_error, "Could not remove original tree");
-	    }
-	}
-      else
-	log_printf(log_warn, "pbuilder update aborted, not cleaning");
-
-      /* either way, I don't want to touch the original tree */
-      goto out;
-
-    }
-  log_printf(log_info, "removing %s working copy", ilist_PRGNAME);
-  cpbuilder_internal_saveupdate(pc);
- out:
-  free(buf_chroot);
-  return ret;
+						 "chroot %s cow-shell",
+					 pc->buildplace)) {
+		/* if outofmemory, die. */
+		log_printf(log_error, "Out of memory.");
+		return -1;
+	}
+
+	if (cpbuilder_internal_cowcopy(pc)) {
+		log_printf(log_error, "Failed cowcopy.");
+		return 1;
+	}
+
+	log_printf(log_info, "Invoking pbuilder");
+
+	pbuildercommandline[1] = "update";
+	PBUILDER_ADD_PARAM("--buildplace");
+	PBUILDER_ADD_PARAM(pc->buildplace);
+	if (find_matching_param("--override-config")) {
+		if (pc->mirror) {
+			PBUILDER_ADD_PARAM("--mirror");
+			PBUILDER_ADD_PARAM(pc->mirror);
+		}
+		PBUILDER_ADD_PARAM("--distribution");
+		PBUILDER_ADD_PARAM(pc->distribution);
+	}
+	if (pc->extrapackages) {
+		PBUILDER_ADD_PARAM("--extrapackages");
+		PBUILDER_ADD_PARAM(pc->extrapackages);
+	}
+	PBUILDER_ADD_PARAM("--no-targz");
+	PBUILDER_ADD_PARAM("--internal-chrootexec");
+	PBUILDER_ADD_PARAM(buf_chroot);
+	PBUILDER_ADD_PARAM(NULL);
+
+	ret = forkexecvp(pbuildercommandline);
+	if (ret) {
+		if (ret < 128) {
+			log_printf(log_error, "pbuilder update failed");
+
+			if (!pc->no_cowdancer_update) {
+				/* advise the user to try this option first */
+				log_printf(
+					log_error,
+					"could not update with cowdancer, try --no-cowdancer-update option");
+			}
+
+			if (!pc->debug && 0 != rmrf_check_mount(pc->buildplace)) {
+				log_printf(log_error, "Could not remove original tree");
+			}
+		} else
+			log_printf(log_warn, "pbuilder update aborted, not cleaning");
+
+		/* either way, I don't want to touch the original tree */
+		goto out;
+	}
+	log_printf(log_info, "removing %s working copy", ilist_PRGNAME);
+	cpbuilder_internal_saveupdate(pc);
+out:
+	free(buf_chroot);
+	return ret;
 }
 
-int cpbuilder_help(void)
-{
-  printf("%s [operation] [options]\n"
-	 "operation:\n"
-	 " --build\n"
-	 " --create\n"
-	 " --update\n"
-	 " --login\n"
-	 " --execute\n"
-	 " --help\n"
-	 " --dumpconfig\n"
-	 "options:\n"
-	 " --basepath:\n"
-	 " --buildplace:\n"
-	 " --distribution:\n"
-	 " ... and other pbuilder options\n", ilist_PRGNAME
-	 );
-  return 0;
+int cpbuilder_help(void) {
+	printf("%s [operation] [options]\n"
+		   "operation:\n"
+		   " --build\n"
+		   " --create\n"
+		   " --update\n"
+		   " --login\n"
+		   " --execute\n"
+		   " --help\n"
+		   " --dumpconfig\n"
+		   "options:\n"
+		   " --basepath:\n"
+		   " --buildplace:\n"
+		   " --distribution:\n"
+		   " ... and other pbuilder options\n",
+		   ilist_PRGNAME);
+	return 0;
 }
 
-int app_main(int ac, char** av)
-{
-  setenv("COWDANCER_REUSE","yes",1);
-  return parse_parameter(ac, av, "cow");
+int app_main(int ac, char **av) {
+	setenv("COWDANCER_REUSE", "yes", 1);
+	return parse_parameter(ac, av, "cow");
 }
diff --git a/cowbuilder_util.c b/cowbuilder_util.c
index 5f8c4a5..36d1dfd 100644
--- a/cowbuilder_util.c
+++ b/cowbuilder_util.c
@@ -9,47 +9,42 @@
 /**
  * @return 0 if not mounted, 1 if something under the mountpoint is mounted.
  */
-int check_mountpoint(const char* mountpoint) {
-  /* Check if a subdirectory under buildplace is still mounted, which may happen when there's a stale bind mount or whatnot. */
-  FILE *mtab = NULL;
-  struct mntent * part = NULL;
-  if ((mtab = setmntent("/etc/mtab", "r")) != NULL)
-    {
-      while ((part = getmntent(mtab) ) != NULL)
-        {
-          if ( part->mnt_fsname != NULL )
-            {
-              if (strstr(part->mnt_dir, mountpoint) == part->mnt_dir)
-                {
-                  log_printf(log_error,
-                      "Something (%s) is still mounted under %s; unmount and remove %s manually",
-                      part->mnt_dir, mountpoint, mountpoint);
-                  endmntent(mtab);
-                  return 1;
-                }
-            }
-        }
-      endmntent(mtab);
-    }
-  return 0;
+int check_mountpoint(const char *mountpoint) {
+	/* Check if a subdirectory under buildplace is still mounted, which may happen when there's a stale bind mount or whatnot. */
+	FILE *mtab = NULL;
+	struct mntent *part = NULL;
+	if ((mtab = setmntent("/etc/mtab", "r")) != NULL) {
+		while ((part = getmntent(mtab)) != NULL) {
+			if (part->mnt_fsname != NULL) {
+				if (strstr(part->mnt_dir, mountpoint) == part->mnt_dir) {
+					log_printf(
+						log_error,
+						"Something (%s) is still mounted under %s; unmount and remove %s manually",
+						part->mnt_dir,
+						mountpoint,
+						mountpoint);
+					endmntent(mtab);
+					return 1;
+				}
+			}
+		}
+		endmntent(mtab);
+	}
+	return 0;
 }
 
 /**
  * Remove double slash "//" from pc->buildplace and use "/"
  * dest needs to be large enough to hold buildplace string.
  */
-void canonicalize_doubleslash(const char* buildplace,
-			      char* dest) {
-  char prev=0;
-  int i, j;
-  for (i = 0, j = 0; buildplace[i]; ++i)
-    {
-      if (prev == '/' && buildplace[i] == '/')
-	{
-	  continue;
+void canonicalize_doubleslash(const char *buildplace, char *dest) {
+	char prev = 0;
+	int i, j;
+	for (i = 0, j = 0; buildplace[i]; ++i) {
+		if (prev == '/' && buildplace[i] == '/') {
+			continue;
+		}
+		prev = dest[j++] = buildplace[i];
 	}
-      prev = dest[j++] = buildplace[i];
-    }
-  dest[j] = '\0';
+	dest[j] = '\0';
 }
-
diff --git a/cowbuilder_util.h b/cowbuilder_util.h
index 416a812..8ccdfbd 100644
--- a/cowbuilder_util.h
+++ b/cowbuilder_util.h
@@ -1,8 +1,7 @@
 #ifndef COWBUILDER_UTIL_H
 #define COWBUILDER_UTIL_H
-int check_mountpoint(const char* mountpoint);
+int check_mountpoint(const char *mountpoint);
 
-void canonicalize_doubleslash(const char* buildplace,
-			      char* dest);
+void canonicalize_doubleslash(const char *buildplace, char *dest);
 
 #endif
diff --git a/cowdancer-ilistcreate.c b/cowdancer-ilistcreate.c
old mode 100755
new mode 100644
index f2e4954..1be6b40
--- a/cowdancer-ilistcreate.c
+++ b/cowdancer-ilistcreate.c
@@ -24,20 +24,19 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include "ilist.h"
-const char* ilist_PRGNAME="cowdancer-ilistcreate";
+const char *ilist_PRGNAME = "cowdancer-ilistcreate";
 
-int main(int argc, char** argv)
-{
-  /*
+int main(int argc, char **argv) {
+	/*
     Wrapper to invoke ilistcreate.
    */
-  if (argc != 3)
-    {
-      fprintf (stderr,
-	       "%s ilist-path find-option\n\n\tcreate .ilist file for use with cow-shell\n\n",
-	       argv[0]);
-      return 1;
-    }
+	if (argc != 3) {
+		fprintf(
+			stderr,
+			"%s ilist-path find-option\n\n\tcreate .ilist file for use with cow-shell\n\n",
+			argv[0]);
+		return 1;
+	}
 
-  return ilistcreate(argv[1], argv[2]);
+	return ilistcreate(argv[1], argv[2]);
 }
diff --git a/cowdancer-ilistdump.c b/cowdancer-ilistdump.c
old mode 100755
new mode 100644
index abb00a3..ab8d8d3
--- a/cowdancer-ilistdump.c
+++ b/cowdancer-ilistdump.c
@@ -10,50 +10,47 @@
 #include "ilist.h"
 #include "log.h"
 
-const char* ilist_PRGNAME="cowdancer-ilistdump";
-
-int main(int argc, char** argv)
-{
-  struct ilist_struct s;
-  struct ilist_header h;
-
-  FILE*f;
-  
-  if (argc != 2)
-    {
-      fprintf (stderr,
-	       "%s ilist-path \n\n\tdump contents of .ilist file\n\n",
-	       argv[0]);
-      return 1;
-    }
-  
-  fprintf (stderr, "ilist_struct size on this architecture: %i\n", (int)sizeof(struct ilist_struct));
-
-  f=fopen (argv[1], "r");
-  if (!f)
-    {
-      log_printf(log_error,
-	  "%s: cannot open file %s",
-	  argv[0], argv[1]);
-      return 1;
-    }
-  
-  fread(&h, sizeof(struct ilist_header), 1, f);
-  fprintf(stderr, 
-	  "Signature: %x (expect %x)\n"
-	  "Revision: %i (expect %i)\n"
-	  "Struct size: %i\n\n",
-	  h.ilistsig, ILISTSIG, 
-	  h.revision, ILISTREVISION, 
-	  h.ilist_struct_size
-	  );
-  
-  while(fread(&s, sizeof(struct ilist_struct), 1, f))
-    {
-      printf ("%li %li\n", (long)s.dev, (long)s.inode);
-    }
-
-  fclose(f);
-  
-  return 0;
+const char *ilist_PRGNAME = "cowdancer-ilistdump";
+
+int main(int argc, char **argv) {
+	struct ilist_struct s;
+	struct ilist_header h;
+
+	FILE *f;
+
+	if (argc != 2) {
+		fprintf(stderr,
+				"%s ilist-path \n\n\tdump contents of .ilist file\n\n",
+				argv[0]);
+		return 1;
+	}
+
+	fprintf(stderr,
+			"ilist_struct size on this architecture: %i\n",
+			(int)sizeof(struct ilist_struct));
+
+	f = fopen(argv[1], "r");
+	if (!f) {
+		log_printf(log_error, "%s: cannot open file %s", argv[0], argv[1]);
+		return 1;
+	}
+
+	fread(&h, sizeof(struct ilist_header), 1, f);
+	fprintf(stderr,
+			"Signature: %x (expect %x)\n"
+			"Revision: %i (expect %i)\n"
+			"Struct size: %i\n\n",
+			h.ilistsig,
+			ILISTSIG,
+			h.revision,
+			ILISTREVISION,
+			h.ilist_struct_size);
+
+	while (fread(&s, sizeof(struct ilist_struct), 1, f)) {
+		printf("%li %li\n", (long)s.dev, (long)s.inode);
+	}
+
+	fclose(f);
+
+	return 0;
 }
diff --git a/cowdancer.c b/cowdancer.c
index 61e5018..14f637e 100644
--- a/cowdancer.c
+++ b/cowdancer.c
@@ -26,15 +26,15 @@
    larger. */
 typedef __typeof__(+(mode_t)0) promoted_mode_t;
 
-const char* ilist_PRGNAME="cowdancer";
+const char *ilist_PRGNAME = "cowdancer";
 
 /* libc functions. */
 static int (*origlibc_open)(const char *, int, ...) = NULL;
 static int (*origlibc_open64)(const char *, int, ...) = NULL;
 static int (*origlibc_creat)(const char *, mode_t) = NULL;
 static int (*origlibc_creat64)(const char *, mode_t) = NULL;
-static FILE* (*origlibc_fopen)(const char *, const char*) = NULL;
-static FILE* (*origlibc_fopen64)(const char *, const char*) = NULL;
+static FILE *(*origlibc_fopen)(const char *, const char *) = NULL;
+static FILE *(*origlibc_fopen64)(const char *, const char *) = NULL;
 
 static int (*origlibc_chown)(const char *, uid_t, gid_t) = NULL;
 static int (*origlibc_fchown)(int fd, uid_t, gid_t) = NULL;
@@ -43,115 +43,126 @@ static int (*origlibc_chmod)(const char *, mode_t) = NULL;
 static int (*origlibc_fchmod)(int fd, mode_t) = NULL;
 static int (*origlibc_flock)(int fd, int) = NULL;
 
-static struct ilist_struct* ilist=NULL;
-static long ilist_len=0;
+static struct ilist_struct *ilist = NULL;
+static long ilist_len = 0;
 
 /*
    verify the header content
  */
-static int verify_ilist_header(struct ilist_header header)
-{
-  if (header.revision != ILISTREVISION ||
-      header.ilistsig != ILISTSIG ||
-      header.ilist_struct_size != sizeof (struct ilist_struct))
-    {
-      ilist_outofmemory(".ilist header unexpected");
-      return 1;
-    }
-  return 0;
+static int verify_ilist_header(struct ilist_header header) {
+	if (header.revision != ILISTREVISION || header.ilistsig != ILISTSIG ||
+		header.ilist_struct_size != sizeof(struct ilist_struct)) {
+		ilist_outofmemory(".ilist header unexpected");
+		return 1;
+	}
+	return 0;
 }
 
 /* load ilist file
 
    @return 1 on error, 0 on success */
-static int load_ilist(void)
-{
-  FILE* f=0;
-  int fd=0;
-  struct stat stbuf;
-  struct ilist_struct* local_ilist=NULL;
-  struct ilist_header header;
-  long local_ilist_len=0;
-
-  if (!getenv("COWDANCER_ILISTFILE"))
-    {
-      log_printf(log_error, "env var COWDANCER_ILISTFILE not defined");
-      return 1;
-    }
-
-  if (-1==(fd=origlibc_open(getenv("COWDANCER_ILISTFILE"),O_RDONLY,0)))
-    {
-      log_printf(log_error, "%s: cannot open ilistfile %s", ilist_PRGNAME, getenv("COWDANCER_ILISTFILE"));
-      return 1;
-    }
-  if (-1==fstat(fd,&stbuf))
-    {
-      log_printf(log_error, "%s: cannot fstat ilistfile %s", ilist_PRGNAME, getenv("COWDANCER_ILISTFILE"));
-      return 1;
-    }
-
-  local_ilist_len=(stbuf.st_size - sizeof (struct ilist_header)) / sizeof(struct ilist_struct);
-
-  if (stbuf.st_size != (sizeof(struct ilist_struct) * local_ilist_len + sizeof (struct ilist_header)))
-    {
-      log_printf(log_error, "%s: .ilist size: %li", ilist_PRGNAME, (long)stbuf.st_size);
-      ilist_outofmemory(".ilist size unexpected");
-      return 1;
-    }
-
-  if (((void*)-1)==
-      (local_ilist=mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE,
-		  fd, 0)))
-    {
-      log_perror("mmap failed, failback to other method");
-      /* fall back to non-mmap method. */
-      if (NULL==(f=fdopen(fd, "r")))
-	{
-	  log_printf(log_error, "%s: cannot fdopen ilistfile %s", ilist_PRGNAME, getenv("COWDANCER_ILISTFILE"));
-	  return 1;
+static int load_ilist(void) {
+	FILE *f = 0;
+	int fd = 0;
+	struct stat stbuf;
+	struct ilist_struct *local_ilist = NULL;
+	struct ilist_header header;
+	long local_ilist_len = 0;
+
+	if (!getenv("COWDANCER_ILISTFILE")) {
+		log_printf(log_error, "env var COWDANCER_ILISTFILE not defined");
+		return 1;
 	}
 
-      if (NULL==(local_ilist=malloc(stbuf.st_size)))
-	{
-	  log_printf(log_error, "%s: out of memory while trying to allocate memory for ilist", ilist_PRGNAME);
-	  return 1;
-	}
-      fread(&header, sizeof(struct ilist_header), 1, f);
-      if(verify_ilist_header(header)) return 1;
-      fread(local_ilist, sizeof(struct ilist_struct), local_ilist_len, f);
-      fclose(f);
-    }
-  else
-    {
-      if (verify_ilist_header(*(struct ilist_header *)local_ilist)) return 1;
-      local_ilist=(void*)local_ilist+sizeof (struct ilist_header);
-      close(fd);
-    }
-
-  /* commit, this should really be atomic,
-     but I don't want to have a lock here.
-     Pray that same result will result in multiple invocations,
-     and ask for luck. It shouldn't change the result very much if it was called multiple times.
-  */
-  sched_yield();
-  /* Do I need to make them volatile? I want the following assignment to happen
-     at this exact timing, to make this quasi-lock-free logic to be remotely successful. */
-  ilist_len = local_ilist_len;
-  ilist = local_ilist;
+	if (-1 ==
+		(fd = origlibc_open(getenv("COWDANCER_ILISTFILE"), O_RDONLY, 0))) {
+		log_printf(log_error,
+				   "%s: cannot open ilistfile %s",
+				   ilist_PRGNAME,
+				   getenv("COWDANCER_ILISTFILE"));
+		return 1;
+	}
+	if (-1 == fstat(fd, &stbuf)) {
+		log_printf(log_error,
+				   "%s: cannot fstat ilistfile %s",
+				   ilist_PRGNAME,
+				   getenv("COWDANCER_ILISTFILE"));
+		return 1;
+	}
 
-  return 0;
+	local_ilist_len = (stbuf.st_size - sizeof(struct ilist_header)) /
+					  sizeof(struct ilist_struct);
+
+	if (stbuf.st_size != (sizeof(struct ilist_struct) * local_ilist_len +
+						  sizeof(struct ilist_header))) {
+		log_printf(log_error,
+				   "%s: .ilist size: %li",
+				   ilist_PRGNAME,
+				   (long)stbuf.st_size);
+		ilist_outofmemory(".ilist size unexpected");
+		return 1;
+	}
+
+	if (((void *)-1) ==
+		(local_ilist =
+			 mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0))) {
+		log_perror("mmap failed, failback to other method");
+		/* fall back to non-mmap method. */
+		if (NULL == (f = fdopen(fd, "r"))) {
+			log_printf(log_error,
+					   "%s: cannot fdopen ilistfile %s",
+					   ilist_PRGNAME,
+					   getenv("COWDANCER_ILISTFILE"));
+			return 1;
+		}
+
+		if (NULL == (local_ilist = malloc(stbuf.st_size))) {
+			log_printf(
+				log_error,
+				"%s: out of memory while trying to allocate memory for ilist",
+				ilist_PRGNAME);
+			return 1;
+		}
+		fread(&header, sizeof(struct ilist_header), 1, f);
+		if (verify_ilist_header(header))
+			return 1;
+		fread(local_ilist, sizeof(struct ilist_struct), local_ilist_len, f);
+		fclose(f);
+	} else {
+		if (verify_ilist_header(*(struct ilist_header *)local_ilist))
+			return 1;
+		local_ilist = (void *)local_ilist + sizeof(struct ilist_header);
+		close(fd);
+	}
+
+	/* commit, this should really be atomic,
+	   but I don't want to have a lock here.
+	   Pray that same result will result in multiple invocations,
+	   and ask for luck. It shouldn't change the result very much if it was
+	   called multiple times. */
+	sched_yield();
+	/* Do I need to make them volatile? I want the following assignment
+	 * to happen at this exact timing, to make this quasi-lock-free
+	 * logic to be remotely successful. */
+	ilist_len = local_ilist_len;
+	ilist = local_ilist;
+
+	return 0;
 }
 
-static void debug_cowdancer (const char * s)
-{
-  if (getenv("COWDANCER_DEBUG"))
-    log_printf(log_debug | log_always_print, "%s: DEBUG %s", ilist_PRGNAME, s);
+static void debug_cowdancer(const char *s) {
+	if (getenv("COWDANCER_DEBUG"))
+		log_printf(
+			log_debug | log_always_print, "%s: DEBUG %s", ilist_PRGNAME, s);
 }
 
-static void debug_cowdancer_2 (const char * s, const char*e)
-{
-  if (getenv("COWDANCER_DEBUG"))
-    log_printf(log_debug | log_always_print, "%s: DEBUG %s:%s", ilist_PRGNAME, s, e);
+static void debug_cowdancer_2(const char *s, const char *e) {
+	if (getenv("COWDANCER_DEBUG"))
+		log_printf(log_debug | log_always_print,
+				   "%s: DEBUG %s:%s",
+				   ilist_PRGNAME,
+				   s,
+				   e);
 }
 
 /**
@@ -159,103 +170,89 @@ static void debug_cowdancer_2 (const char * s, const char*e)
 
     @return 1 on error
  */
-__attribute__ ((warn_unused_result))
-static int initialize_functions ()
-{
-  static volatile int initialized = 0;
-  /* this code is quasi-reentrant;
-     shouldn't suffer too much if it is called multiple times. */
-
-  /* this code is __unlikely__ to be true
-     It is initialized==0 only for the first time
-     so should be !initialized==0 most of the time.
-  */
-  if (__builtin_expect(!initialized,0))
-    {
-      log_use_colors use_colors = log_get_use_colors_unresolved();
-      /* Temporarily disable auto detection, since ncurses calls fopen, which
-       * leads to initialize_functions calling itself. */
-      if (use_colors == log_use_colors_auto)
-	log_set_use_colors(log_use_colors_no);
-
-      initialized = 1;
-      origlibc_open = dlsym(RTLD_NEXT, "open");
-      origlibc_open64 = dlsym(RTLD_NEXT, "open64");
-      origlibc_creat = dlsym(RTLD_NEXT, "creat");
-      origlibc_creat64 = dlsym(RTLD_NEXT, "creat64");
-      origlibc_fopen = dlsym(RTLD_NEXT, "fopen64");
-      origlibc_fopen64 = dlsym(RTLD_NEXT, "fopen64");
-      dlerror();
-      if (!(origlibc_chown = dlvsym(RTLD_NEXT, "chown", "GLIBC_2.1")))
-	{
-	  /* I should really check dlerror, but due to a possible bug in glibc,
-	     dlerror doesn't seem to work at all.
-	   */
-	  const char* d=dlerror();
-	  if(!d)
-	    {
-	      debug_cowdancer("dlerror does not return anything, chown returned NULL but OK");
-	      /* success */
-	    }
-	  else
-	    {
-	      debug_cowdancer(d);
-	    }
-
-
-	  /* fallback to loading unversioned symbol doing it anyway
-	     since glibc does not seem to set dlerror on dlsym failure.
-	  */
-	  origlibc_chown = dlsym(RTLD_NEXT, "chown");
-
-	}
-      origlibc_fchown = dlsym(RTLD_NEXT, "fchown");
-      origlibc_lchown = dlsym(RTLD_NEXT, "lchown");
-      origlibc_chmod = dlsym(RTLD_NEXT, "chmod");
-      origlibc_fchmod = dlsym(RTLD_NEXT, "fchmod");
-      origlibc_flock = dlsym(RTLD_NEXT, "flock");
-
-      if (getenv("COWDANCER_DEBUG"))
-	{
-	  log_printf(log_debug | log_always_print,
-		     "chown:%p lchown:%p", origlibc_chown, origlibc_lchown);
+__attribute__((warn_unused_result)) static int initialize_functions() {
+	static volatile int initialized = 0;
+	/* this code is quasi-reentrant;
+	   shouldn't suffer too much if it is called multiple times. */
+
+	/* this code is __unlikely__ to be true
+	   It is initialized==0 only for the first time
+	   so should be !initialized==0 most of the time. */
+	if (__builtin_expect(!initialized, 0)) {
+		log_use_colors use_colors = log_get_use_colors_unresolved();
+		/* Temporarily disable auto detection, since ncurses calls fopen, which
+		 * leads to initialize_functions calling itself. */
+		if (use_colors == log_use_colors_auto)
+			log_set_use_colors(log_use_colors_no);
+
+		initialized = 1;
+		origlibc_open = dlsym(RTLD_NEXT, "open");
+		origlibc_open64 = dlsym(RTLD_NEXT, "open64");
+		origlibc_creat = dlsym(RTLD_NEXT, "creat");
+		origlibc_creat64 = dlsym(RTLD_NEXT, "creat64");
+		origlibc_fopen = dlsym(RTLD_NEXT, "fopen64");
+		origlibc_fopen64 = dlsym(RTLD_NEXT, "fopen64");
+		dlerror();
+		if (!(origlibc_chown = dlvsym(RTLD_NEXT, "chown", "GLIBC_2.1"))) {
+			/* I should really check dlerror, but due to a possible bug
+			 * in glibc, dlerror doesn't seem to work at all. */
+			const char *d = dlerror();
+			if (!d) {
+				debug_cowdancer(
+					"dlerror does not return anything, chown returned NULL but OK");
+				/* success */
+			} else {
+				debug_cowdancer(d);
+			}
+
+			/* fallback to loading unversioned symbol doing it anyway
+			 * since glibc does not seem to set dlerror on dlsym failure
+			 */
+			origlibc_chown = dlsym(RTLD_NEXT, "chown");
+		}
+		origlibc_fchown = dlsym(RTLD_NEXT, "fchown");
+		origlibc_lchown = dlsym(RTLD_NEXT, "lchown");
+		origlibc_chmod = dlsym(RTLD_NEXT, "chmod");
+		origlibc_fchmod = dlsym(RTLD_NEXT, "fchmod");
+		origlibc_flock = dlsym(RTLD_NEXT, "flock");
+
+		if (getenv("COWDANCER_DEBUG")) {
+			log_printf(log_debug | log_always_print,
+					   "chown:%p lchown:%p",
+					   origlibc_chown,
+					   origlibc_lchown);
+		}
+
+		/* load the ilist */
+		if (!ilist) {
+			if (load_ilist()) {
+				initialized = 0;
+				return 1;
+			}
+			initialized = 2;
+			debug_cowdancer("Initialization successfully finished.");
+		}
+		log_set_use_colors(use_colors);
 	}
-
-      /* load the ilist */
-      if (!ilist)
-	{
-	  if (load_ilist())
-	    {
-	      initialized = 0;
-	      return 1;
-	    }
-	  initialized = 2;
-	  debug_cowdancer ("Initialization successfully finished.");
-	}
-      log_set_use_colors(use_colors);
-    }
-  /*
+	/*
      Wait until somebody else finishes his job
      This is very unlikely
   */
-  while (__builtin_expect(initialized == 1,0))
-    sched_yield();
+	while (__builtin_expect(initialized == 1, 0))
+		sched_yield();
 
-  if (initialized==0)
-    return 1;			/* that somebody else failed */
-  else
-    return 0;
+	if (initialized == 0)
+		return 1; /* that somebody else failed */
+	else
+		return 0;
 }
 
 /* the constructor for this library */
-__attribute__ ((constructor))
-     void ctor()
-{
-  if (initialize_functions())
-    {
-      log_printf(log_error,
-          "%s: Fatal, initialize_functions failed", ilist_PRGNAME);
-    }
+__attribute__((constructor)) void ctor() {
+	if (initialize_functions()) {
+		log_printf(
+			log_error, "%s: Fatal, initialize_functions failed", ilist_PRGNAME);
+	}
 }
 
 /**
@@ -267,337 +264,317 @@ __attribute__ ((constructor))
 
    @return 1 on failure, 0 on success
 */
-__attribute__ ((warn_unused_result))
-static int check_inode_and_copy(const char* s, int canonicalize)
-{
-  struct ilist_struct search_target;
-  char* canonical=NULL;/* the canonical filename, the filename of the protected inode and the newly generated*/
-  struct stat buf;
-  char* backup_file=NULL;/* filename of backup file, the new file with new inode that will be used for future writes */
-  int ret;
-  pid_t pid;
-  int status;
-  sigset_t newmask, omask;
-
-  debug_cowdancer_2("DEBUG: test for", s);
-  if(lstat(s, &buf))
-    return 0;			/* if stat fails, the file probably
+__attribute__((warn_unused_result)) static int
+check_inode_and_copy(const char *s, int canonicalize) {
+	struct ilist_struct search_target;
+	char *canonical =
+		NULL; /* the canonical filename, the filename of the protected inode and the newly generated*/
+	struct stat buf;
+	char *backup_file =
+		NULL; /* filename of backup file, the new file with new inode that will be used for future writes */
+	int ret;
+	pid_t pid;
+	int status;
+	sigset_t newmask, omask;
+
+	debug_cowdancer_2("DEBUG: test for", s);
+	if (lstat(s, &buf))
+		return 0; /* if stat fails, the file probably
 				   doesn't exist; return, success */
-  if (canonicalize && S_ISLNK(buf.st_mode))
-    {
-      /* for symbollic link, canonicalize and get the real filename */
-      if (!(canonical=canonicalize_file_name(s)))
-	return 0;			/* if canonicalize_file_name fails,
+	if (canonicalize && S_ISLNK(buf.st_mode)) {
+		/* for symbollic link, canonicalize and get the real filename */
+		if (!(canonical = canonicalize_file_name(s)))
+			return 0; /* if canonicalize_file_name fails,
 					   the file probably doesn't exist. */
 
-      if(stat(canonical, &buf))	/* if I can't stat this file, I don't think
+		if (stat(canonical, &buf)) /* if I can't stat this file, I don't think
 				   I can write to it; ignore */
-	return 0;
-    }
-  else
-    canonical=strdup(s);
-
-  memset(&search_target, 0, sizeof(search_target));
-  search_target.inode = buf.st_ino;
-  search_target.dev = buf.st_dev;
-
-  //do some hardcore debugging here:
-  if (getenv("COWDANCER_DEBUG"))
-    {
-      log_printf(log_debug | log_always_print,
-		 "ciac: s:%s=canonical:%s nlink:%i reg:%i lnk:%i match:%p", s, canonical, (int)buf.st_nlink, S_ISREG(buf.st_mode), S_ISLNK(buf.st_mode),
-		 bsearch(&search_target, ilist, ilist_len,
-			 sizeof(search_target), compare_ilist));
-    }
-
-  if((buf.st_nlink > 1) && 	/* it is hardlinked */
-     (S_ISREG(buf.st_mode) ||
-      S_ISLNK(buf.st_mode)) && 	/* it is a regular file or a symbollic link */
-     bsearch(&search_target, ilist, ilist_len,
-	     sizeof(search_target), compare_ilist)) /* and a match is
+			return 0;
+	} else
+		canonical = strdup(s);
+
+	memset(&search_target, 0, sizeof(search_target));
+	search_target.inode = buf.st_ino;
+	search_target.dev = buf.st_dev;
+
+	//do some hardcore debugging here:
+	if (getenv("COWDANCER_DEBUG")) {
+		log_printf(log_debug | log_always_print,
+				   "ciac: s:%s=canonical:%s nlink:%i reg:%i lnk:%i match:%p",
+				   s,
+				   canonical,
+				   (int)buf.st_nlink,
+				   S_ISREG(buf.st_mode),
+				   S_ISLNK(buf.st_mode),
+				   bsearch(&search_target,
+						   ilist,
+						   ilist_len,
+						   sizeof(search_target),
+						   compare_ilist));
+	}
+
+	if ((buf.st_nlink > 1) && /* it is hardlinked */
+		(S_ISREG(buf.st_mode) ||
+		 S_ISLNK(buf.st_mode)) && /* it is a regular file or a symbollic link */
+		bsearch(&search_target,
+				ilist,
+				ilist_len,
+				sizeof(search_target),
+				compare_ilist)) /* and a match is
 						       found in ilist
 						       file */
-    {
-      /* There is a file that needs to be protected,
-	 Copy-on-write hardlink files.
-
-	 we copy the file first to a backup place, and then mv the
-	 backup to the original file, to break the hardlink.
-      */
-
-      if (asprintf(&backup_file, "%sXXXXXX", canonical)==-1)
 	{
-	  ilist_outofmemory("out of memory in check_inode_and_copy, 1");
-	  goto error_canonical;
-	}
+		/* There is a file that needs to be protected,
+		 * Copy-on-write hardlink files.
+		 *
+		 * we copy the file first to a backup place, and then mv the
+		 * backup to the original file, to break the hardlink.
+		 */
+
+		if (asprintf(&backup_file, "%sXXXXXX", canonical) == -1) {
+			ilist_outofmemory("out of memory in check_inode_and_copy, 1");
+			goto error_canonical;
+		}
+
+		close(ret = mkstemp(backup_file));
+		if (ret == -1) {
+			log_perror("mkstemp");
+			goto error_buf;
+		}
+
+		/* mask the SIGCHLD signal, I want waitpid to work.  */
+		sigemptyset(&newmask);
+		sigaddset(&newmask, SIGCHLD);
+		if (sigprocmask(SIG_BLOCK, &newmask, &omask) < 0) {
+			log_perror("sigprocmask");
+			goto error_buf;
+		}
+
+		/* let cp do the task,
+		 * it probably knows about filesystem details more than I do.
+		 *
+		 * forking here is really difficult; I should rewrite this code
+		 * to not fork/exec. Signal handling and process waiting is too
+		 * unreliable.
+		 */
+		switch (pid = fork()) {
+			case 0:
+				/* child process, run cp */
+				putenv("COWDANCER_IGNORE=yes");
+				sigprocmask(SIG_SETMASK,
+							&omask,
+							(sigset_t *)NULL); /* unmask SIGCHLD signal */
+				execl("/bin/cp", "/bin/cp", "-a", canonical, backup_file, NULL);
+				log_perror("execl:cp:");
+				exit(EXIT_FAILURE);
+			case -1:
+				/* error condition in fork(); something is really wrong */
+				ilist_outofmemory("out of memory in check_inode_and_copy, 2");
+				goto error_spm;
+			default:
+				/* parent process, waiting for cp -a to terminate */
+				if (-1 == waitpid(pid, &status, 0)) {
+					log_perror("waitpid:cp");
+					log_printf(
+						log_error,
+						"%s: unexpected waitpid error when waiting for process %i with status %x",
+						ilist_PRGNAME,
+						pid,
+						status);
+					goto error_spm;
+				}
+
+				if (!WIFEXITED(status)) {
+					/* something unexpected */
+					log_printf(log_error,
+							   "%s: unexpected WIFEXITED status in waitpid: %x",
+							   ilist_PRGNAME,
+							   (int)status);
+					goto error_spm;
+				} else if (WEXITSTATUS(status)) {
+					/* cp -a failed */
+					log_printf(log_error,
+							   "%s: cp -a failed for %s",
+							   ilist_PRGNAME,
+							   backup_file);
+					goto error_spm;
+				}
+				/* when cp -a succeeded, overwrite the target file from the temporary file with rename */
+				else if (-1 == rename(backup_file, canonical)) {
+					log_perror("file overwrite with rename");
+					log_printf(log_error,
+							   "%s: while trying rename of %s to %s",
+							   ilist_PRGNAME,
+							   canonical,
+							   backup_file);
+					goto error_spm;
+				}
+		}
+		free(backup_file);
+		sigprocmask(SIG_SETMASK, &omask, (sigset_t *)NULL);
+	} else
+		debug_cowdancer_2("DEBUG: did not match ", s);
+
+	free(canonical);
+	return 0;
 
-      close(ret=mkstemp(backup_file));
-      if (ret==-1)
-	{
-	  log_perror("mkstemp");
-	  goto error_buf;
-	}
+/* error-processing routine. */
+error_spm:
+	sigprocmask(SIG_SETMASK, &omask, (sigset_t *)NULL);
+error_buf:
+	free(backup_file);
+error_canonical:
+	free(canonical);
+	return 1;
+}
 
-      /* mask the SIGCHLD signal, I want waitpid to work.
-       */
-      sigemptyset (&newmask);
-      sigaddset (&newmask, SIGCHLD);
-      if (sigprocmask (SIG_BLOCK, &newmask, &omask) < 0)
-	{
-	  log_perror("sigprocmask");
-	  goto error_buf;
+int open(const char *a, int flags, ...) {
+	int fd;
+	mode_t mode;
+	va_list args;
+	va_start(args, flags);
+	mode = (mode_t)va_arg(args, promoted_mode_t);
+	va_end(args);
+	if (initialize_functions()) {
+		errno = ENOMEM;
+		return -1;
 	}
 
-      /* let cp do the task,
-	 it probably knows about filesystem details more than I do.
-
-	 forking here is really difficult; I should rewrite this code
-	 to not fork/exec. Signal handling and process waiting is too
-	 unreliable.
-      */
-      switch(pid=fork())
-	{
-	case 0:
-	  /* child process, run cp */
-	  putenv("COWDANCER_IGNORE=yes");
-	  sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL); /* unmask SIGCHLD signal */
-	  execl("/bin/cp", "/bin/cp", "-a", canonical, backup_file, NULL);
-	  log_perror("execl:cp:");
-	  exit(EXIT_FAILURE);
-	case -1:
-	  /* error condition in fork(); something is really wrong */
-	  ilist_outofmemory("out of memory in check_inode_and_copy, 2");
-	  goto error_spm;
-	default:
-	  /* parent process, waiting for cp -a to terminate */
-	  if(-1==waitpid(pid, &status, 0))
-	    {
-	      log_perror("waitpid:cp");
-	      log_printf(log_error, "%s: unexpected waitpid error when waiting for process %i with status %x",
-		  ilist_PRGNAME, pid, status);
-	      goto error_spm;
-	    }
-
-	  if (!WIFEXITED(status))
-	    {
-	      /* something unexpected */
-	      log_printf(log_error, "%s: unexpected WIFEXITED status in waitpid: %x", ilist_PRGNAME,
-		  (int)status);
-	      goto error_spm;
-	    }
-	  else if (WEXITSTATUS(status))
-	    {
-	      /* cp -a failed */
-	      log_printf(log_error, "%s: cp -a failed for %s", ilist_PRGNAME, backup_file);
-	      goto error_spm;
-	    }
-	  /* when cp -a succeeded, overwrite the target file from the temporary file with rename */
-	  else if (-1==rename(backup_file, canonical))
-	    {
-	      log_perror("file overwrite with rename");
-	      log_printf(log_error, "%s: while trying rename of %s to %s",  ilist_PRGNAME, canonical, backup_file);
-	      goto error_spm;
-	    }
-	}
-      free(backup_file);
-      sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);
-    }
-  else
-    debug_cowdancer_2("DEBUG: did not match ", s);
-
-  free(canonical);
-  return 0;
-
-  /* error-processing routine. */
- error_spm:
-  sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);
- error_buf:
-  free(backup_file);
- error_canonical:
-  free(canonical);
-  return 1;
-}
-
-int open(const char * a, int flags, ...)
-{
-  int fd;
-  mode_t mode;
-  va_list args;
-  va_start(args, flags);
-  mode = (mode_t) va_arg(args, promoted_mode_t);
-  va_end(args);
-  if (initialize_functions())
-    {
-      errno=ENOMEM;
-      return -1;
-    }
-
-  if(!getenv("COWDANCER_IGNORE"))
-    {
-      debug_cowdancer_2 ("open", a);
-      switch(flags & O_ACCMODE)
-	{
-	case O_WRONLY:
-	case O_RDWR:
-	  if (check_inode_and_copy(a,1))
-	    {
-	      errno=ENOMEM;
-	      return -1;
-	    }
-	  break;
-	}
-    }
-  fd = origlibc_open (a, flags, mode);
-  return fd;
+	if (!getenv("COWDANCER_IGNORE")) {
+		debug_cowdancer_2("open", a);
+		switch (flags & O_ACCMODE) {
+			case O_WRONLY:
+			case O_RDWR:
+				if (check_inode_and_copy(a, 1)) {
+					errno = ENOMEM;
+					return -1;
+				}
+				break;
+		}
+	}
+	fd = origlibc_open(a, flags, mode);
+	return fd;
 }
 
-int open64(const char * a, int flags, ...)
-{
-  int fd;
-  mode_t mode;
-  va_list args;
-  va_start(args, flags);
-  mode = (mode_t) va_arg(args, promoted_mode_t);
-  va_end(args);
-  if (initialize_functions())
-    {
-      errno=ENOMEM;
-      return -1;
-    }
-  if(!getenv("COWDANCER_IGNORE"))
-    {
-      debug_cowdancer_2 ("open64", a);
-      switch(flags & O_ACCMODE)
-	{
-	case O_WRONLY:
-	case O_RDWR:
-	  if (check_inode_and_copy(a,1))
-	    {
-	      errno=ENOMEM;
-	      return -1;
-	    }
-	  break;
-	}
-    }
-  fd = origlibc_open64 (a, flags, mode);
-  return fd;
+int open64(const char *a, int flags, ...) {
+	int fd;
+	mode_t mode;
+	va_list args;
+	va_start(args, flags);
+	mode = (mode_t)va_arg(args, promoted_mode_t);
+	va_end(args);
+	if (initialize_functions()) {
+		errno = ENOMEM;
+		return -1;
+	}
+	if (!getenv("COWDANCER_IGNORE")) {
+		debug_cowdancer_2("open64", a);
+		switch (flags & O_ACCMODE) {
+			case O_WRONLY:
+			case O_RDWR:
+				if (check_inode_and_copy(a, 1)) {
+					errno = ENOMEM;
+					return -1;
+				}
+				break;
+		}
+	}
+	fd = origlibc_open64(a, flags, mode);
+	return fd;
 }
 
-int creat(const char * a, mode_t mode)
-{
-  int fd;
-  if (initialize_functions())
-    {
-      errno=ENOMEM;
-      return -1;
-    }
-  if(!getenv("COWDANCER_IGNORE"))
-    {
-      debug_cowdancer_2 ("creat", a);
-      if (check_inode_and_copy(a,1))
-	{
-	  errno=ENOMEM;
-	  return -1;
+int creat(const char *a, mode_t mode) {
+	int fd;
+	if (initialize_functions()) {
+		errno = ENOMEM;
+		return -1;
 	}
-    }
-  fd = origlibc_creat (a, mode);
-  return fd;
+	if (!getenv("COWDANCER_IGNORE")) {
+		debug_cowdancer_2("creat", a);
+		if (check_inode_and_copy(a, 1)) {
+			errno = ENOMEM;
+			return -1;
+		}
+	}
+	fd = origlibc_creat(a, mode);
+	return fd;
 }
 
-int creat64(const char * a, mode_t mode)
-{
-  int fd;
-  if (initialize_functions())
-    {
-      errno=ENOMEM;
-      return -1;
-    }
-  if(!getenv("COWDANCER_IGNORE"))
-    {
-      debug_cowdancer_2 ("creat64", a);
-      if (check_inode_and_copy(a,1))
-	    {
-	      errno=ENOMEM;
-	      return -1;
-	    }
-    }
-  fd = origlibc_creat64 (a, mode);
-  return fd;
+int creat64(const char *a, mode_t mode) {
+	int fd;
+	if (initialize_functions()) {
+		errno = ENOMEM;
+		return -1;
+	}
+	if (!getenv("COWDANCER_IGNORE")) {
+		debug_cowdancer_2("creat64", a);
+		if (check_inode_and_copy(a, 1)) {
+			errno = ENOMEM;
+			return -1;
+		}
+	}
+	fd = origlibc_creat64(a, mode);
+	return fd;
 }
 
-static int likely_fopen_write(const char *t)
-{
-  /* checks if it is likely to be a write fopen */
-  return strspn(t, "aw+");
+static int likely_fopen_write(const char *t) {
+	/* checks if it is likely to be a write fopen */
+	return strspn(t, "aw+");
 }
 
 #undef fopen
-FILE* fopen(const char* s, const char* t)
-{
-  FILE *f;
-  if (initialize_functions())
-    {
-      errno=ENOMEM;
-      return NULL;
-    }
-  if(!getenv("COWDANCER_IGNORE")&&
-     likely_fopen_write(t))
-    {
-      debug_cowdancer_2 ("fopen", s);
-      if (check_inode_and_copy(s,1))
-	{
-	  errno=ENOMEM;
-	  return NULL;
+FILE *fopen(const char *s, const char *t) {
+	FILE *f;
+	if (initialize_functions()) {
+		errno = ENOMEM;
+		return NULL;
+	}
+	if (!getenv("COWDANCER_IGNORE") && likely_fopen_write(t)) {
+		debug_cowdancer_2("fopen", s);
+		if (check_inode_and_copy(s, 1)) {
+			errno = ENOMEM;
+			return NULL;
+		}
 	}
-    }
-  f = origlibc_fopen (s, t);
-  return f;
+	f = origlibc_fopen(s, t);
+	return f;
 }
 
 #undef fopen64
-FILE* fopen64(const char* s, const char* t)
-{
-  FILE *f;
-  if(initialize_functions())
-    {
-      errno=ENOMEM;
-      return NULL;
-    }
-  if(!getenv("COWDANCER_IGNORE")&&
-     likely_fopen_write(t))
-    {
-      debug_cowdancer_2 ("fopen64", s);
-      if(check_inode_and_copy(s,1))
-	{
-	  errno=ENOMEM;
-	  return NULL;
+FILE *fopen64(const char *s, const char *t) {
+	FILE *f;
+	if (initialize_functions()) {
+		errno = ENOMEM;
+		return NULL;
+	}
+	if (!getenv("COWDANCER_IGNORE") && likely_fopen_write(t)) {
+		debug_cowdancer_2("fopen64", s);
+		if (check_inode_and_copy(s, 1)) {
+			errno = ENOMEM;
+			return NULL;
+		}
 	}
-    }
-  f = origlibc_fopen64(s, t);
-  return f;
+	f = origlibc_fopen64(s, t);
+	return f;
 }
 
 #undef chown
-int chown(const char* s, uid_t u, gid_t g)
-{
-  int ret;
-  if(initialize_functions())
-    {
-      errno=ENOMEM;
-      return -1;
-    }
-  if(!getenv("COWDANCER_IGNORE"))
-    {
-      debug_cowdancer_2 ("chown", s);
-      if(check_inode_and_copy(s,1))
-	{
-	  errno=ENOMEM;
-	  return -1;
+int chown(const char *s, uid_t u, gid_t g) {
+	int ret;
+	if (initialize_functions()) {
+		errno = ENOMEM;
+		return -1;
+	}
+	if (!getenv("COWDANCER_IGNORE")) {
+		debug_cowdancer_2("chown", s);
+		if (check_inode_and_copy(s, 1)) {
+			errno = ENOMEM;
+			return -1;
+		}
 	}
-    }
-  ret = origlibc_chown(s, u, g);
-  debug_cowdancer_2 ("end-chown", s);
-  return ret;
+	ret = origlibc_chown(s, u, g);
+	debug_cowdancer_2("end-chown", s);
+	return ret;
 }
 
 /* Check out file descriptor
@@ -607,142 +584,128 @@ int chown(const char* s, uid_t u, gid_t g)
  * @return 1 on failure, 0 on success
  *
  */
-static int check_fd_inode_and_warn(int fd, const char* operation)
-{
-  struct stat buf;
-  struct ilist_struct search_target;
-
-  fstat(fd, &buf);
-  memset(&search_target, 0, sizeof(search_target));
-  search_target.inode = buf.st_ino;
-  search_target.dev = buf.st_dev;
-  if(bsearch(&search_target, ilist, ilist_len,
-	     sizeof(search_target), compare_ilist) &&
-     S_ISREG(buf.st_mode))
-    {
-      /* Someone opened file read-only, and called
-	 fchown/fchmod/flock; I don't really know how to do
-	 salvation in that case, since the original filename is
-	 probably not available, and file is already open.
-
-	 If there is any better way, I'd like to know.
-       */
-      log_printf(log_warn,
-          "cowdancer: unsupported operation %s, read-only open and fchown/fchmod/flock are not supported: tried opening dev:inode of %li:%li",
-          operation, (long)buf.st_dev, (long)buf.st_ino);
-      /* emit a warning and do not fail,
-	 if you want to make it fail, add a return 1;
-	 apt seems to want to use this operation; thus apt will always fail.
-       */
-    }
-  return 0;
+static int check_fd_inode_and_warn(int fd, const char *operation) {
+	struct stat buf;
+	struct ilist_struct search_target;
+
+	fstat(fd, &buf);
+	memset(&search_target, 0, sizeof(search_target));
+	search_target.inode = buf.st_ino;
+	search_target.dev = buf.st_dev;
+	if (bsearch(&search_target,
+				ilist,
+				ilist_len,
+				sizeof(search_target),
+				compare_ilist) &&
+		S_ISREG(buf.st_mode)) {
+		/* Someone opened file read-only, and called
+		 * fchown/fchmod/flock; I don't really know how to do
+		 * salvation in that case, since the original filename is
+		 * probably not available, and file is already open.
+		 *
+		 * If there is any better way, I'd like to know.
+		 */
+		log_printf(
+			log_warn,
+			"cowdancer: unsupported operation %s, read-only open and fchown/fchmod/flock are not supported: tried opening dev:inode of %li:%li",
+			operation,
+			(long)buf.st_dev,
+			(long)buf.st_ino);
+		/* emit a warning and do not fail,
+		 * if you want to make it fail, add a return 1;
+		 * apt seems to want to use this operation; thus apt will
+		 * always fail.
+		 */
+	}
+	return 0;
 }
 
 #undef fchown
-int fchown(int fd, uid_t u, gid_t g)
-{
-  int ret;
-  if(initialize_functions())
-    return -1;
-  if(!getenv("COWDANCER_IGNORE"))
-    {
-      debug_cowdancer ("fchown");
-      if (check_fd_inode_and_warn(fd, "fchown"))
-	{
-	  errno=ENOMEM;
-	  return -1;
+int fchown(int fd, uid_t u, gid_t g) {
+	int ret;
+	if (initialize_functions())
+		return -1;
+	if (!getenv("COWDANCER_IGNORE")) {
+		debug_cowdancer("fchown");
+		if (check_fd_inode_and_warn(fd, "fchown")) {
+			errno = ENOMEM;
+			return -1;
+		}
 	}
-    }
-  ret = origlibc_fchown(fd, u, g);
-  return ret;
+	ret = origlibc_fchown(fd, u, g);
+	return ret;
 }
 
 #undef lchown
 /* chown that does not follow the symbollic links. */
-int lchown(const char* s, uid_t u, gid_t g)
-{
-  int ret;
-  if(initialize_functions())
-    {
-      errno=ENOMEM;
-      return -1;
-    }
-  if(!getenv("COWDANCER_IGNORE"))
-    {
-      debug_cowdancer_2 ("lchown", s);
-      if (check_inode_and_copy(s,0))
-	{
-	  errno=ENOMEM;
-	  return -1;
+int lchown(const char *s, uid_t u, gid_t g) {
+	int ret;
+	if (initialize_functions()) {
+		errno = ENOMEM;
+		return -1;
 	}
-    }
-  ret = origlibc_lchown(s, u, g);
-  debug_cowdancer_2 ("end-lchown", s);
-  return ret;
+	if (!getenv("COWDANCER_IGNORE")) {
+		debug_cowdancer_2("lchown", s);
+		if (check_inode_and_copy(s, 0)) {
+			errno = ENOMEM;
+			return -1;
+		}
+	}
+	ret = origlibc_lchown(s, u, g);
+	debug_cowdancer_2("end-lchown", s);
+	return ret;
 }
 
 #undef chmod
-int chmod(const char* s, mode_t mode)
-{
-  int ret;
-  if(initialize_functions())
-    {
-      errno=ENOMEM;
-      return -1;
-    }
-  if(!getenv("COWDANCER_IGNORE"))
-    {
-      debug_cowdancer_2 ("chmod", s);
-      if (check_inode_and_copy(s,1))
-	{
-	  errno=ENOMEM;
-	  return -1;
+int chmod(const char *s, mode_t mode) {
+	int ret;
+	if (initialize_functions()) {
+		errno = ENOMEM;
+		return -1;
 	}
-    }
-  ret = origlibc_chmod(s, mode);
-  return ret;
+	if (!getenv("COWDANCER_IGNORE")) {
+		debug_cowdancer_2("chmod", s);
+		if (check_inode_and_copy(s, 1)) {
+			errno = ENOMEM;
+			return -1;
+		}
+	}
+	ret = origlibc_chmod(s, mode);
+	return ret;
 }
 
 #undef fchmod
-int fchmod(int fd, mode_t mode)
-{
-  int ret;
-  if(initialize_functions())
-    {
-      errno=ENOMEM;
-      return -1;
-    }
-  if(!getenv("COWDANCER_IGNORE"))
-    {
-      debug_cowdancer ("fchmod");
-      if (check_fd_inode_and_warn(fd, "fchmod"))
-	{
-	  errno=ENOMEM;
-	  return -1;
+int fchmod(int fd, mode_t mode) {
+	int ret;
+	if (initialize_functions()) {
+		errno = ENOMEM;
+		return -1;
 	}
-    }
-  ret = origlibc_fchmod(fd, mode);
-  return ret;
+	if (!getenv("COWDANCER_IGNORE")) {
+		debug_cowdancer("fchmod");
+		if (check_fd_inode_and_warn(fd, "fchmod")) {
+			errno = ENOMEM;
+			return -1;
+		}
+	}
+	ret = origlibc_fchmod(fd, mode);
+	return ret;
 }
 
 #undef flock
-int flock(int fd, int operation)
-{
-  int ret;
-  if(initialize_functions())
-    {
-      errno=ENOMEM;
-      return -1;
-    }
-  if(!getenv("COWDANCER_IGNORE"))
-    {
-      debug_cowdancer ("flock");
-      if (check_fd_inode_and_warn(fd, "flock"))
-	{
-	  errno=ENOMEM;
-	  return -1;
+int flock(int fd, int operation) {
+	int ret;
+	if (initialize_functions()) {
+		errno = ENOMEM;
+		return -1;
+	}
+	if (!getenv("COWDANCER_IGNORE")) {
+		debug_cowdancer("flock");
+		if (check_fd_inode_and_warn(fd, "flock")) {
+			errno = ENOMEM;
+			return -1;
+		}
 	}
-    }
-  ret = origlibc_flock(fd, operation);
-  return ret;
+	ret = origlibc_flock(fd, operation);
+	return ret;
 }
diff --git a/file.c b/file.c
index 0255185..8a557d1 100644
--- a/file.c
+++ b/file.c
@@ -45,77 +45,64 @@
  *
  * returns 0 on success, -1 on failure.
  */
-int copy_file(const char*orig, const char*dest)
-{
-  const int buffer_size=4096;
-  char *buf=malloc(buffer_size);
-  int ret=-1;
-  int fin=-1;
-  int fout=-1;
-  size_t count;
-  struct stat st;
-
-  if (!buf)
-    {
-      log_printf(log_error, "Out of memory");
-      goto out;
-    }
-
-  if (-1==(fin=open(orig,O_RDONLY)))
-    {
-      log_perror("file copy: open for read");
-      goto out;
-    }
-  if (-1==(fout=creat(dest,0666)))
-    {
-      log_perror("file copy: open for write");
-      goto out;
-    }
-  while((count=read(fin, buf, buffer_size))>0)
-    {
-      if (-1==write(fout, buf, count))
-	{			/* error */
-	  log_perror("file copy: write");
-	  log_printf(log_error, "%i", fin);
-	  log_printf(log_error, "%i %i", fout, (int)count);
-	  goto out;
-	}
-    }
-  if(count==-1)
-    {
-      log_perror("file copy: read ");
-      goto out;
-    }
-  if (-1==close(fin) ||
-      -1==close(fout))
-    {
-      log_perror("file copy: close ");
-      goto out;
-    }
-
-  if (0>stat(orig, &st))
-    {
-      log_printf(log_error,
-	      "Error calling stat '%s': %s",
-	      orig,
-	      strerror(errno));
-      goto out;
-    }
-
-  if (chmod(dest, st.st_mode))
-    {
-      log_printf(log_error,
-	      "Error calling chmod('%s' 0%llo): %s",
-	      orig,
-	      (unsigned long long) st.st_mode,
-	      strerror(errno));
-      goto out;
-    }
-
-  ret=0;
- out:
-  free(buf);
-  return ret;
+int copy_file(const char *orig, const char *dest) {
+	const int buffer_size = 4096;
+	char *buf = malloc(buffer_size);
+	int ret = -1;
+	int fin = -1;
+	int fout = -1;
+	size_t count;
+	struct stat st;
+
+	if (!buf) {
+		log_printf(log_error, "Out of memory");
+		goto out;
+	}
+
+	if (-1 == (fin = open(orig, O_RDONLY))) {
+		log_perror("file copy: open for read");
+		goto out;
+	}
+	if (-1 == (fout = creat(dest, 0666))) {
+		log_perror("file copy: open for write");
+		goto out;
+	}
+	while ((count = read(fin, buf, buffer_size)) > 0) {
+		if (-1 == write(fout, buf, count)) { /* error */
+			log_perror("file copy: write");
+			log_printf(log_error, "%i", fin);
+			log_printf(log_error, "%i %i", fout, (int)count);
+			goto out;
+		}
+	}
+	if (count == -1) {
+		log_perror("file copy: read ");
+		goto out;
+	}
+	if (-1 == close(fin) || -1 == close(fout)) {
+		log_perror("file copy: close ");
+		goto out;
+	}
+
+	if (0 > stat(orig, &st)) {
+		log_printf(
+			log_error, "Error calling stat '%s': %s", orig, strerror(errno));
+		goto out;
+	}
+
+	if (chmod(dest, st.st_mode)) {
+		log_printf(log_error,
+				   "Error calling chmod('%s' 0%llo): %s",
+				   orig,
+				   (unsigned long long)st.st_mode,
+				   strerror(errno));
+		goto out;
+	}
+
+	ret = 0;
+out:
+	free(buf);
+	return ret;
 }
 
 /**
@@ -123,81 +110,74 @@ int copy_file(const char*orig, const char*dest)
 
    returns 0 on success, 1 on fail.
  */
-int create_sparse_file(const char* filename, unsigned long int size)
-{
-  int fd=creat(filename, 0660);
-  if (-1==fd)
-    {
-      log_perror("creat");
-      log_printf(log_error, "Could not create %s", filename);
-      return 1;
-    }
-
-  const off_t seeksize = 1 << 30;	/* try with 30-bit seeks (1GB / seek) */
-  assert(size > 1);
-  size--;
-  if (-1==lseek(fd, 0, SEEK_SET))
-    {
-	log_perror("initial lseek");
-	return 1;
-    }
-
-  while(size > seeksize) {
-    if (-1==lseek(fd, seeksize, SEEK_CUR))
-      {
-	log_perror("intermediate lseek");
-	return 1;
-      }
-    size -= seeksize;
-  }
-  if (-1==lseek(fd, size - 1, SEEK_CUR))
-    {
-      log_perror("final lseek");
-      return 1;
-    }
-
-  if (-1==write(fd, "", 1))		/* A string consists of \0, write 0 to end of file */
-    {
-      log_perror("write");
-      return 1;
-    }
-
-  if (-1==close(fd))
-    {
-      log_perror("close");
-      return 1;
-    }
-  return 0;
+int create_sparse_file(const char *filename, unsigned long int size) {
+	int fd = creat(filename, 0660);
+	if (-1 == fd) {
+		log_perror("creat");
+		log_printf(log_error, "Could not create %s", filename);
+		return 1;
+	}
+
+	const off_t seeksize = 1 << 30; /* try with 30-bit seeks (1GB / seek) */
+	assert(size > 1);
+	size--;
+	if (-1 == lseek(fd, 0, SEEK_SET)) {
+		log_perror("initial lseek");
+		return 1;
+	}
+
+	while (size > seeksize) {
+		if (-1 == lseek(fd, seeksize, SEEK_CUR)) {
+			log_perror("intermediate lseek");
+			return 1;
+		}
+		size -= seeksize;
+	}
+	if (-1 == lseek(fd, size - 1, SEEK_CUR)) {
+		log_perror("final lseek");
+		return 1;
+	}
+
+	if (-1 ==
+		write(fd, "", 1)) /* A string consists of \0, write 0 to end of file */
+	{
+		log_perror("write");
+		return 1;
+	}
+
+	if (-1 == close(fd)) {
+		log_perror("close");
+		return 1;
+	}
+	return 0;
 }
 
 /* mknod with prepended pathname
    return -1 on failure.
  */
-int mknod_inside_chroot(const char* chroot, const char* pathname, mode_t mode, dev_t dev)
-{
-  char* p = alloca(strlen(chroot)+strlen(pathname)+2);
-  int ret;
-
-  if (!p)
-    {
-      log_printf(log_error, "error on alloca");
-      return -1;
-    }
-
-  if (-1==sprintf(p, "%s/%s", chroot, pathname))
-    {
-      log_printf(log_error, "error on sprintf");
-      return -1;
-    }
-
-  ret=mknod(p, mode, dev);
-
-  if (ret == -1)
-    {
-      /* output the error message for debug, but ignore it here. */
-      log_perror(p);
-    }
-
-  return ret;
-}
+int mknod_inside_chroot(const char *chroot,
+						const char *pathname,
+						mode_t mode,
+						dev_t dev) {
+	char *p = alloca(strlen(chroot) + strlen(pathname) + 2);
+	int ret;
+
+	if (!p) {
+		log_printf(log_error, "error on alloca");
+		return -1;
+	}
+
+	if (-1 == sprintf(p, "%s/%s", chroot, pathname)) {
+		log_printf(log_error, "error on sprintf");
+		return -1;
+	}
+
+	ret = mknod(p, mode, dev);
 
+	if (ret == -1) {
+		/* output the error message for debug, but ignore it here. */
+		log_perror(p);
+	}
+
+	return ret;
+}
diff --git a/file.h b/file.h
index 2f7c3fd..7707f99 100644
--- a/file.h
+++ b/file.h
@@ -22,7 +22,10 @@
 
 #ifndef __FILE_H__
 #define __FILE_H__
-int copy_file(const char*orig, const char*dest);
-int create_sparse_file(const char* filename, unsigned long int size);
-int mknod_inside_chroot(const char* chroot, const char* pathname, mode_t mode, dev_t dev);
+int copy_file(const char *orig, const char *dest);
+int create_sparse_file(const char *filename, unsigned long int size);
+int mknod_inside_chroot(const char *chroot,
+						const char *pathname,
+						mode_t mode,
+						dev_t dev);
 #endif
diff --git a/forkexec.c b/forkexec.c
index db89842..8baa70e 100644
--- a/forkexec.c
+++ b/forkexec.c
@@ -31,58 +31,56 @@
 
   @return < 0 for failure, exit code for other cases.
  */
-int forkexecvp (char *const argv[])
-{
-  int ret;
-  pid_t pid;
-  int status;
-
-  /* DEBUG: */
-  {
-    int i=0;
-    log_begin(log_info);
-    log_middle(log_info, "forking:");
-
-    for (i = 0; argv[i]; ++i)
-      {
-	if (strchr(argv[i], ' '))
-	  log_middle(log_info, " '%s'", argv[i]);
-	else
-	  log_middle(log_info, " %s", argv[i]);
-      }
-    log_end(log_info);
-  }
-
-  fflush(NULL);
-
-  switch(pid=fork())
-    {
-    case 0:
-      execvp(argv[0], (char*const*)argv);
-      log_perror("execvp");
-      log_printf(log_error, "Could not execute %s", argv[0]);
-      exit(EXIT_FAILURE);
-    case -1:
-      /* error condition in fork(); something is really wrong */
-      log_perror("fork");
-      return -1;
-    default:
-      /* parent process, waiting for termination */
-      if (-1==waitpid(pid, &status, 0))
+int forkexecvp(char *const argv[]) {
+	int ret;
+	pid_t pid;
+	int status;
+
+	/* DEBUG: */
 	{
-	  log_perror("waitpid");
-	  log_printf(log_error, "Unexpected waitpid error when waiting for process %i with status %x",
-		  pid, status);
-	  return -1;
+		int i = 0;
+		log_begin(log_info);
+		log_middle(log_info, "forking:");
+
+		for (i = 0; argv[i]; ++i) {
+			if (strchr(argv[i], ' '))
+				log_middle(log_info, " '%s'", argv[i]);
+			else
+				log_middle(log_info, " %s", argv[i]);
+		}
+		log_end(log_info);
 	}
-      if (!WIFEXITED(status))
-	{
-	  /* something unexpected */
-	  return -1;
+
+	fflush(NULL);
+
+	switch (pid = fork()) {
+		case 0:
+			execvp(argv[0], (char *const *)argv);
+			log_perror("execvp");
+			log_printf(log_error, "Could not execute %s", argv[0]);
+			exit(EXIT_FAILURE);
+		case -1:
+			/* error condition in fork(); something is really wrong */
+			log_perror("fork");
+			return -1;
+		default:
+			/* parent process, waiting for termination */
+			if (-1 == waitpid(pid, &status, 0)) {
+				log_perror("waitpid");
+				log_printf(
+					log_error,
+					"Unexpected waitpid error when waiting for process %i with status %x",
+					pid,
+					status);
+				return -1;
+			}
+			if (!WIFEXITED(status)) {
+				/* something unexpected */
+				return -1;
+			}
+			ret = WEXITSTATUS(status);
 	}
-      ret = WEXITSTATUS(status);
-    }
-  return ret;
+	return ret;
 }
 
 /*
@@ -95,74 +93,64 @@ int forkexecvp (char *const argv[])
 
   @return < 0 for failure, exit code for other cases.
  */
-int
-forkexeclp (const char *path, const char *arg0, ...)
-{
-  int i, ret;
-  va_list args;
-  const char *argv[1024];
-  pid_t pid;
-  int status;
-
-  va_start(args, arg0);
-  argv[0] = arg0;
-  i = 1;
-
-  log_begin(log_info);
-  if (strchr(argv[0], ' '))
-    log_middle(log_info, "forking: '%s'", argv[0]); /* debug message */
-  else
-    log_middle(log_info, "forking: %s", argv[0]);   /* debug message */
-
-  do
-    {
-      argv[i] = va_arg(args, const char *);
-      if (argv[i])
-	{
-	  if (strchr(argv[i], ' '))
-	    log_middle(log_info, " '%s'", argv[i]);    /* debug message */
-	  else
-	    log_middle(log_info, " %s", argv[i]);      /* debug message */
-	}
-
-      if ( i >= 1023 )
-	{
-	  return -1;
-	}
-
-    }
-  while (argv[i++] != NULL);
-  va_end (args);
-
-  log_end(log_info);				  /* debug message */
-
-  fflush(NULL);
-
-  switch(pid=fork())
-    {
-    case 0:
-      execvp(path, (char*const*)argv);
-      log_perror("pbuilder: execlp");
-      log_printf(log_error, "Could not execute %s", path);
-      exit(EXIT_FAILURE);
-    case -1:
-      /* error condition in fork(); something is really wrong */
-      log_perror("pbuilder: fork");
-      return -1;
-    default:
-      /* parent process, waiting for termination */
-      if (-1==waitpid(pid, &status, 0))
-	{
-	  log_perror("waitpid");
-	  return -1;
-	}
-      if (!WIFEXITED(status))
-	{
-	  /* something unexpected */
-	  return -1;
+int forkexeclp(const char *path, const char *arg0, ...) {
+	int i, ret;
+	va_list args;
+	const char *argv[1024];
+	pid_t pid;
+	int status;
+
+	va_start(args, arg0);
+	argv[0] = arg0;
+	i = 1;
+
+	log_begin(log_info);
+	if (strchr(argv[0], ' '))
+		log_middle(log_info, "forking: '%s'", argv[0]); /* debug message */
+	else
+		log_middle(log_info, "forking: %s", argv[0]); /* debug message */
+
+	do {
+		argv[i] = va_arg(args, const char *);
+		if (argv[i]) {
+			if (strchr(argv[i], ' '))
+				log_middle(log_info, " '%s'", argv[i]); /* debug message */
+			else
+				log_middle(log_info, " %s", argv[i]); /* debug message */
+		}
+
+		if (i >= 1023) {
+			return -1;
+		}
+
+	} while (argv[i++] != NULL);
+	va_end(args);
+
+	log_end(log_info); /* debug message */
+
+	fflush(NULL);
+
+	switch (pid = fork()) {
+		case 0:
+			execvp(path, (char *const *)argv);
+			log_perror("pbuilder: execlp");
+			log_printf(log_error, "Could not execute %s", path);
+			exit(EXIT_FAILURE);
+		case -1:
+			/* error condition in fork(); something is really wrong */
+			log_perror("pbuilder: fork");
+			return -1;
+		default:
+			/* parent process, waiting for termination */
+			if (-1 == waitpid(pid, &status, 0)) {
+				log_perror("waitpid");
+				return -1;
+			}
+			if (!WIFEXITED(status)) {
+				/* something unexpected */
+				return -1;
+			}
+			ret = WEXITSTATUS(status);
 	}
-      ret = WEXITSTATUS(status);
-    }
-  return ret;
+	return ret;
 }
-
diff --git a/ilist.h b/ilist.h
index eb15919..51a8be2 100644
--- a/ilist.h
+++ b/ilist.h
@@ -6,21 +6,19 @@
 /* 'COWD' in the host-order */
 #define ILISTSIG 0x4f434457
 
-struct ilist_header
-{
-  int ilistsig;
-  int revision;
-  int ilist_struct_size;
-  int dummy;
+struct ilist_header {
+	int ilistsig;
+	int revision;
+	int ilist_struct_size;
+	int dummy;
 };
 
-struct ilist_struct
-{
-  dev_t dev;
-  ino_t inode;
+struct ilist_struct {
+	dev_t dev;
+	ino_t inode;
 };
 
-void ilist_outofmemory(const char* msg);
-int ilistcreate(const char* ilistpath, const char* findcommandline);
-int compare_ilist (const void *a, const void *b);
-extern const char* ilist_PRGNAME;
+void ilist_outofmemory(const char *msg);
+int ilistcreate(const char *ilistpath, const char *findcommandline);
+int compare_ilist(const void *a, const void *b);
+extern const char *ilist_PRGNAME;
diff --git a/ilistcreate.c b/ilistcreate.c
old mode 100755
new mode 100644
index 31ba143..2492e6c
--- a/ilistcreate.c
+++ b/ilistcreate.c
@@ -12,134 +12,114 @@
 #include "log.h"
 
 /* Output error message. You need to process the error result. */
-void ilist_outofmemory(const char* msg)
-{
-  log_printf(log_error, "%s: %s", ilist_PRGNAME, msg);
+void ilist_outofmemory(const char *msg) {
+	log_printf(log_error, "%s: %s", ilist_PRGNAME, msg);
 }
 
 /* return 1 on error, 0 on success */
-int ilistcreate(const char* ilistpath, const char* findcommandline)
-{
-  int i=0;
-  long dev, ino;
-  FILE* inf;
-  FILE* outf;
-  struct ilist_struct* ilist=NULL;
-  struct ilist_header header=
-    {
-      ILISTSIG,
-      ILISTREVISION,
-      sizeof (struct ilist_struct),
-      0
-    };
-  long ilist_len=0;
-  if(!findcommandline)
-    findcommandline="find . -xdev \\( -type l -o -type f \\) -a -links +1 -print0 | xargs -0 stat --format '%d %i '";
-
-  if (!(ilist=calloc(2000,sizeof(struct ilist_struct))))
-    {
-      ilist_outofmemory("memory allocation failed");
-      return 1;
-    }
-  ilist_len=2000;
-  if (NULL==(inf=popen(findcommandline, "r")))
-    {
-      ilist_outofmemory("popen find failed");
-      return 1;
-    }
-
-  while (fscanf(inf,"%li %li", &dev, &ino)>0)
-    {
-      (ilist+i)->dev=(dev_t)dev;
-      (ilist+i)->inode=(ino_t)ino;
-
-      if (getenv("COWDANCER_DEBUG"))
-	printf("%li %li \n ", (long int)dev, (long int)ino);
-
-      i++;
-      if (i>=ilist_len)
-	{
-	  ilist=realloc(ilist, (ilist_len*=2)*sizeof(struct ilist_struct));
-	  if (!ilist)
-	    {
-	      ilist_outofmemory("realloc failed");
-	      pclose(inf);
-	      return 1;
-	    }
+int ilistcreate(const char *ilistpath, const char *findcommandline) {
+	int i = 0;
+	long dev, ino;
+	FILE *inf;
+	FILE *outf;
+	struct ilist_struct *ilist = NULL;
+	struct ilist_header header = {
+		ILISTSIG, ILISTREVISION, sizeof(struct ilist_struct), 0};
+	long ilist_len = 0;
+	if (!findcommandline)
+		findcommandline =
+			"find . -xdev \\( -type l -o -type f \\) -a -links +1 -print0 | xargs -0 stat --format '%d %i '";
+
+	if (!(ilist = calloc(2000, sizeof(struct ilist_struct)))) {
+		ilist_outofmemory("memory allocation failed");
+		return 1;
 	}
-    }
-  ilist_len=i;
-  if (pclose(inf))
-    {
-      ilist_outofmemory("pclose returned non-zero, probably the directory contains no hardlinked file, don't bother using cow-shell here.");
-      return 1;
-    }
-
-  /* sort the ilist */
-  qsort(ilist, ilist_len, sizeof(struct ilist_struct), compare_ilist);
-
-  /* write out the ilist file */
-  if (NULL==(outf=fopen(ilistpath,"w")))
-    {
-      ilist_outofmemory("cannot open .ilist file");
-      return 1;
-    }
-
-
-  if(1 != fwrite(&header, sizeof(struct ilist_header), 1, outf))
-    {
-      ilist_outofmemory("failed writing header to .ilist file");
-      return 1;
-    }
-
-  if (ilist_len != fwrite(ilist, sizeof(struct ilist_struct), ilist_len, outf))
-    {
-      ilist_outofmemory("failed writing to .ilist file");
-      return 1;
-    }
-
-  if (fclose (outf))
-    {
-      ilist_outofmemory("error flushing to .ilist file");
-      return 1;
-    }
-  return 0;
-}
+	ilist_len = 2000;
+	if (NULL == (inf = popen(findcommandline, "r"))) {
+		ilist_outofmemory("popen find failed");
+		return 1;
+	}
+
+	while (fscanf(inf, "%li %li", &dev, &ino) > 0) {
+		(ilist + i)->dev = (dev_t)dev;
+		(ilist + i)->inode = (ino_t)ino;
+
+		if (getenv("COWDANCER_DEBUG"))
+			printf("%li %li \n ", (long int)dev, (long int)ino);
+
+		i++;
+		if (i >= ilist_len) {
+			ilist =
+				realloc(ilist, (ilist_len *= 2) * sizeof(struct ilist_struct));
+			if (!ilist) {
+				ilist_outofmemory("realloc failed");
+				pclose(inf);
+				return 1;
+			}
+		}
+	}
+	ilist_len = i;
+	if (pclose(inf)) {
+		ilist_outofmemory(
+			"pclose returned non-zero, probably the directory contains no hardlinked file, don't bother using cow-shell here.");
+		return 1;
+	}
+
+	/* sort the ilist */
+	qsort(ilist, ilist_len, sizeof(struct ilist_struct), compare_ilist);
 
+	/* write out the ilist file */
+	if (NULL == (outf = fopen(ilistpath, "w"))) {
+		ilist_outofmemory("cannot open .ilist file");
+		return 1;
+	}
+
+	if (1 != fwrite(&header, sizeof(struct ilist_header), 1, outf)) {
+		ilist_outofmemory("failed writing header to .ilist file");
+		return 1;
+	}
+
+	if (ilist_len !=
+		fwrite(ilist, sizeof(struct ilist_struct), ilist_len, outf)) {
+		ilist_outofmemory("failed writing to .ilist file");
+		return 1;
+	}
+
+	if (fclose(outf)) {
+		ilist_outofmemory("error flushing to .ilist file");
+		return 1;
+	}
+	return 0;
+}
 
 /* comparison function for qsort/bsearch of ilist
  */
-int
-compare_ilist (const void *a, const void *b)
-{
-  const struct ilist_struct * ilista = (const struct ilist_struct*) a;
-  const struct ilist_struct * ilistb = (const struct ilist_struct*) b;
-  int ret;
-
-  ret = ilista->inode - ilistb->inode;
-  if (!ret)
-    {
-      ret = ilista->dev - ilistb->dev;
-    }
-  return ret;
+int compare_ilist(const void *a, const void *b) {
+	const struct ilist_struct *ilista = (const struct ilist_struct *)a;
+	const struct ilist_struct *ilistb = (const struct ilist_struct *)b;
+	int ret;
+
+	ret = ilista->inode - ilistb->inode;
+	if (!ret) {
+		ret = ilista->dev - ilistb->dev;
+	}
+	return ret;
 }
 
 #ifdef ILISTCREATEBENCH
 /* test code for performance tuning */
-const char* ilist_PRGNAME="testbench";
+const char *ilist_PRGNAME = "testbench";
 
-int
-main()
-{
-  int i;
+int main() {
+	int i;
 
-  if (-1==chdir("/home/dancer/shared/git/linux-2.6/"))
-    exit (1);
+	if (-1 == chdir("/home/dancer/shared/git/linux-2.6/"))
+		exit(1);
 
-  for(i=0; i<100; ++i)
-    ilistcreate("/home/dancer/shared/git/linux-2.6/.ilist", NULL);
+	for (i = 0; i < 100; ++i)
+		ilistcreate("/home/dancer/shared/git/linux-2.6/.ilist", NULL);
 
-  exit (0);
+	exit(0);
 }
 
 #endif
diff --git a/initrd/qemuinit.c b/initrd/qemuinit.c
index 2ff3731..9b9cd0c 100644
--- a/initrd/qemuinit.c
+++ b/initrd/qemuinit.c
@@ -25,161 +25,146 @@
 #define ROOT_MOUNTPOINT "/root"
 
 /* global variable to hold /proc/cmdline */
-char* proc_cmdline = NULL;
-
-void read_proc_cmdline()
-{
-  int fd = open("/proc/cmdline", O_RDONLY);
-  char buf[BUFSIZ];
-  int len=read(fd, buf, sizeof(buf)-1);
-  buf[len]=0;			/* make sure it's NULL-terminated */
-  proc_cmdline=strdup(buf);
-  close(fd);
+char *proc_cmdline = NULL;
+
+void read_proc_cmdline() {
+	int fd = open("/proc/cmdline", O_RDONLY);
+	char buf[BUFSIZ];
+	int len = read(fd, buf, sizeof(buf) - 1);
+	buf[len] = 0; /* make sure it's NULL-terminated */
+	proc_cmdline = strdup(buf);
+	close(fd);
 }
 
 /* parse the command-line option, and return a pointer to
 space-delimited option string */
-const char* parse_option(const char* option)
-{
-  const char* s = proc_cmdline;
-  do {
-    if (!strncmp(option, s, strlen(option)) &&
-	s[strlen(option)] == '=')
-      return s;
-  } while((s=strchr(s, ' ')));
-  return NULL;
+const char *parse_option(const char *option) {
+	const char *s = proc_cmdline;
+	do {
+		if (!strncmp(option, s, strlen(option)) && s[strlen(option)] == '=')
+			return s;
+	} while ((s = strchr(s, ' ')));
+	return NULL;
 }
 
 /*
    Return a strdup string, optionally NULL-terminating if it's
    space-delimited.
  */
-char* strdup_spacedelimit(const char* str)
-{
-  char* s = strdup(str);
-  char* space = strchr(str, ' ');
-  if (space) *space = 0;
-  return s;
+char *strdup_spacedelimit(const char *str) {
+	char *s = strdup(str);
+	char *space = strchr(str, ' ');
+	if (space)
+		*space = 0;
+	return s;
 }
 
 /*
    insert module and display error message when insmod returns an
    error.
  */
-int insmod(const char* module)
-{
-  int ret=forkexeclp("/bin/insmod", "/bin/insmod", module, NULL);
-  if (ret)
-    {
-      fprintf(stderr, "W: insmod of %s returned %i\n",
-	      module,
-	      ret);
-    }
-  return ret;
-
+int insmod(const char *module) {
+	int ret = forkexeclp("/bin/insmod", "/bin/insmod", module, NULL);
+	if (ret) {
+		fprintf(stderr, "W: insmod of %s returned %i\n", module, ret);
+	}
+	return ret;
 }
 
-int main(int argc, char** argv)
-{
-  printf(" -> qemu-pbuilder first-stage(initrd version)\n");
-
-  umask(S_IWGRP | S_IWOTH);
-
-  chdir("/");
-  setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 1);
-  setenv("PBUILDER_MOUNTPOINT", PBUILDER_MOUNTPOINT, 1);
-
-  mkdir("/root", 0777);		/* mountpoint */
-  mkdir("/proc", 0777);
-  mkdir("/dev", 0777);
-  mkdir("/dev/pts", 0777);
-
-  if (mount("/proc", "/proc",
-	    "proc", MS_MGC_VAL, NULL) == -1)
-    {
-      perror("mount proc");
-    }
-
-  if (mount("/dev/pts", "/dev/pts",
-	    "devpts", MS_MGC_VAL, NULL) == -1)
-    {
-      perror("mount devpts");
-    }
-
-  read_proc_cmdline();
-
-  /* ext3 driver */
-  insmod("lib/modules/linux-live/kernel/drivers/ide/ide-core.ko");
-  insmod("lib/modules/linux-live/kernel/drivers/ide/ide-disk.ko");
-  insmod("lib/modules/linux-live/kernel/drivers/ide/ide-generic.ko");
-  insmod("lib/modules/linux-live/kernel/fs/mbcache.ko");
-  insmod("lib/modules/linux-live/kernel/fs/jbd/jbd.ko");
-  insmod("lib/modules/linux-live/kernel/fs/ext2/ext2.ko");
-  insmod("lib/modules/linux-live/kernel/fs/ext3/ext3.ko");
-  sleep(1);
-
-  /* ne2k driver */
-  insmod("lib/modules/linux-live/kernel/drivers/net/8390.ko");
-  insmod("lib/modules/linux-live/kernel/drivers/net/ne2k-pci.ko");
-  /* ARM would need different driver... smc */
-
-  if (copy_file("/proc/mounts", "/etc/mtab"))
-    {
-      fprintf(stderr, "Cannot copy file /proc/mounts to /etc/mtab.\n");
-    }
-
-  mknod_inside_chroot("", "dev/sda", S_IFBLK | 0660, makedev(8, 0));
-  mknod_inside_chroot("", "dev/sdb", S_IFBLK | 0660, makedev(8, 16));
-  mknod_inside_chroot("", "dev/hda", S_IFBLK | 0660, makedev(3, 0));
-  mknod_inside_chroot("", "dev/hdb", S_IFBLK | 0660, makedev(3, 64));
-
-  if (mount("/dev/sda", ROOT_MOUNTPOINT, "ext3",
-	     MS_MGC_VAL, NULL) == -1)
-    {
-      perror("mount root device try sda");
-      if (mount("/dev/hda", ROOT_MOUNTPOINT, "ext3",
-		MS_MGC_VAL, NULL) == -1)
-	{
-	  perror("mount root device try hda");
+int main(int argc, char **argv) {
+	printf(" -> qemu-pbuilder first-stage(initrd version)\n");
+
+	umask(S_IWGRP | S_IWOTH);
+
+	chdir("/");
+	setenv("PATH",
+		   "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
+		   1);
+	setenv("PBUILDER_MOUNTPOINT", PBUILDER_MOUNTPOINT, 1);
+
+	mkdir("/root", 0777); /* mountpoint */
+	mkdir("/proc", 0777);
+	mkdir("/dev", 0777);
+	mkdir("/dev/pts", 0777);
+
+	if (mount("/proc", "/proc", "proc", MS_MGC_VAL, NULL) == -1) {
+		perror("mount proc");
 	}
-    }
-
-  if (mount("/dev/sdb",
-	    ROOT_MOUNTPOINT PBUILDER_MOUNTPOINT,
-	    "ext3", MS_MGC_VAL, NULL) == -1)
-    {
-      perror("mount command device try sdb");
-      if (mount("/dev/hdb",
-		ROOT_MOUNTPOINT PBUILDER_MOUNTPOINT,
-		"ext3", MS_MGC_VAL, NULL) == -1)
-	{
-	   perror("mount command device try hdb");
+
+	if (mount("/dev/pts", "/dev/pts", "devpts", MS_MGC_VAL, NULL) == -1) {
+		perror("mount devpts");
 	}
-    }
 
-  /* debug shell to see what's there. */
-  printf("command-line inside initrd, exit to continue\n");
-  forkexeclp("/bin/sh", "/bin/sh", NULL);
+	read_proc_cmdline();
+
+	/* ext3 driver */
+	insmod("lib/modules/linux-live/kernel/drivers/ide/ide-core.ko");
+	insmod("lib/modules/linux-live/kernel/drivers/ide/ide-disk.ko");
+	insmod("lib/modules/linux-live/kernel/drivers/ide/ide-generic.ko");
+	insmod("lib/modules/linux-live/kernel/fs/mbcache.ko");
+	insmod("lib/modules/linux-live/kernel/fs/jbd/jbd.ko");
+	insmod("lib/modules/linux-live/kernel/fs/ext2/ext2.ko");
+	insmod("lib/modules/linux-live/kernel/fs/ext3/ext3.ko");
+	sleep(1);
+
+	/* ne2k driver */
+	insmod("lib/modules/linux-live/kernel/drivers/net/8390.ko");
+	insmod("lib/modules/linux-live/kernel/drivers/net/ne2k-pci.ko");
+	/* ARM would need different driver... smc */
+
+	if (copy_file("/proc/mounts", "/etc/mtab")) {
+		fprintf(stderr, "Cannot copy file /proc/mounts to /etc/mtab.\n");
+	}
 
-  if (chroot("root/"))
-    {
-      perror("chroot");
-    }
+	mknod_inside_chroot("", "dev/sda", S_IFBLK | 0660, makedev(8, 0));
+	mknod_inside_chroot("", "dev/sdb", S_IFBLK | 0660, makedev(8, 16));
+	mknod_inside_chroot("", "dev/hda", S_IFBLK | 0660, makedev(3, 0));
+	mknod_inside_chroot("", "dev/hdb", S_IFBLK | 0660, makedev(3, 64));
+
+	if (mount("/dev/sda", ROOT_MOUNTPOINT, "ext3", MS_MGC_VAL, NULL) == -1) {
+		perror("mount root device try sda");
+		if (mount("/dev/hda", ROOT_MOUNTPOINT, "ext3", MS_MGC_VAL, NULL) ==
+			-1) {
+			perror("mount root device try hda");
+		}
+	}
+
+	if (mount("/dev/sdb",
+			  ROOT_MOUNTPOINT PBUILDER_MOUNTPOINT,
+			  "ext3",
+			  MS_MGC_VAL,
+			  NULL) == -1) {
+		perror("mount command device try sdb");
+		if (mount("/dev/hdb",
+				  ROOT_MOUNTPOINT PBUILDER_MOUNTPOINT,
+				  "ext3",
+				  MS_MGC_VAL,
+				  NULL) == -1) {
+			perror("mount command device try hdb");
+		}
+	}
+
+	/* debug shell to see what's there. */
+	printf("command-line inside initrd, exit to continue\n");
+	forkexeclp("/bin/sh", "/bin/sh", NULL);
 
-  if (chdir("/"))
-    {
-      perror("chdir");
-    }
+	if (chroot("root/")) {
+		perror("chroot");
+	}
+
+	if (chdir("/")) {
+		perror("chdir");
+	}
 
-  forkexeclp("bin/sh", "bin/sh", PBUILDER_MOUNTPOINT "/pbuilder-run", NULL);
+	forkexeclp("bin/sh", "bin/sh", PBUILDER_MOUNTPOINT "/pbuilder-run", NULL);
 
-  /* debug shell inside chroot. */
-  printf("command-line inside chroot, exit to continue\n");
-  forkexeclp("bin/sh", "bin/sh", NULL);
+	/* debug shell inside chroot. */
+	printf("command-line inside chroot, exit to continue\n");
+	forkexeclp("bin/sh", "bin/sh", NULL);
 
-  /* turn the machine off */
-  sync();
-  sync();
-  reboot(RB_POWER_OFF);
-  return 0;
+	/* turn the machine off */
+	sync();
+	sync();
+	reboot(RB_POWER_OFF);
+	return 0;
 }
diff --git a/log.c b/log.c
index 388a4b5..a9b3eda 100644
--- a/log.c
+++ b/log.c
@@ -31,10 +31,10 @@
 #include <curses.h>
 #include <term.h>
 
-static const char *red="\033[0;31m";
-static const char *yellow="\033[1;33m";
-static const char *blue="\033[0;34m";
-static const char *reset="\033[0m";
+static const char *red = "\033[0;31m";
+static const char *yellow = "\033[1;33m";
+static const char *blue = "\033[0;34m";
+static const char *reset = "\033[0m";
 
 static int called_setupterm = 0;
 static log_level filter_level = log_info;
@@ -42,198 +42,177 @@ static log_use_colors use_colors = log_use_colors_auto;
 /* Same as use_colors, but stays as auto even when auto has been resolved */
 static log_use_colors use_colors_orig = log_use_colors_auto;
 
-static int term_supports_colors(void)
-{
-  /* Don't use log_printf - potential for infinite mutual recursion... */
+static int term_supports_colors(void) {
+	/* Don't use log_printf - potential for infinite mutual recursion... */
 
-  int colors;
-  int erret;
-  const char *term;
+	int colors;
+	int erret;
+	const char *term;
 
-  term = getenv("TERM");
+	term = getenv("TERM");
 
-  if (!term || !*term)
-    return 0;
+	if (!term || !*term)
+		return 0;
 
-  if (!called_setupterm)
-    {
-      if (setupterm(term, STDOUT_FILENO, &erret) != OK && erret <= 0)
-	{
-	  fprintf(stderr, "E: Error calling setupterm: %d\n", erret);
-	  return 0;
-	}
+	if (!called_setupterm) {
+		if (setupterm(term, STDOUT_FILENO, &erret) != OK && erret <= 0) {
+			fprintf(stderr, "E: Error calling setupterm: %d\n", erret);
+			return 0;
+		}
 
-      called_setupterm = 1;
-    }
+		called_setupterm = 1;
+	}
 
-  colors = tigetnum("colors");
+	colors = tigetnum("colors");
 
-  /* colors is -1 on error, which will be treated as not supported */
-  return colors >= 8;
+	/* colors is -1 on error, which will be treated as not supported */
+	return colors >= 8;
 }
 
-static FILE *file_for_level(log_level level)
-{
-  switch (level & LOG_LEVEL_MASK) {
-    case log_debug:
-    case log_info:
-      return stdout;
-    case log_warn:
-    case log_error:
-    default:
-      return stderr;
-  }
+static FILE *file_for_level(log_level level) {
+	switch (level & LOG_LEVEL_MASK) {
+		case log_debug:
+		case log_info:
+			return stdout;
+		case log_warn:
+		case log_error:
+		default:
+			return stderr;
+	}
 }
 
-log_level log_get_filter_level(void)
-{
-  return filter_level;
+log_level log_get_filter_level(void) {
+	return filter_level;
 }
 
-void log_set_filter_level(log_level filter_level_new)
-{
-  filter_level = filter_level_new;
+void log_set_filter_level(log_level filter_level_new) {
+	filter_level = filter_level_new;
 }
 
-log_use_colors log_get_use_colors(void)
-{
-  if (use_colors == log_use_colors_auto)
-    {
-      if (term_supports_colors())
-	use_colors = log_use_colors_yes;
-      else
-	use_colors = log_use_colors_no;
-    }
-
-  return use_colors;
+log_use_colors log_get_use_colors(void) {
+	if (use_colors == log_use_colors_auto) {
+		if (term_supports_colors())
+			use_colors = log_use_colors_yes;
+		else
+			use_colors = log_use_colors_no;
+	}
+
+	return use_colors;
 }
 
-log_use_colors log_get_use_colors_unresolved(void)
-{
-  return use_colors_orig;
+log_use_colors log_get_use_colors_unresolved(void) {
+	return use_colors_orig;
 }
 
-void log_set_use_colors(log_use_colors use_colors_new)
-{
-  use_colors = use_colors_orig = use_colors_new;
+void log_set_use_colors(log_use_colors use_colors_new) {
+	use_colors = use_colors_orig = use_colors_new;
 }
 
-void log_perror(const char *s)
-{
-  if (s != NULL && *s)
-    log_printf(log_error, "%s: %s", s, strerror(errno));
-  else
-    log_printf(log_error, "%s", strerror(errno));
+void log_perror(const char *s) {
+	if (s != NULL && *s)
+		log_printf(log_error, "%s: %s", s, strerror(errno));
+	else
+		log_printf(log_error, "%s", strerror(errno));
 }
 
-void log_printf(log_level level, const char *format, ...)
-{
-  va_list args;
+void log_printf(log_level level, const char *format, ...) {
+	va_list args;
 
-  if (level < filter_level)
-    return;
+	if (level < filter_level)
+		return;
 
-  log_begin(level);
+	log_begin(level);
 
-  va_start(args, format);
-  log_vmiddle(level, format, args);
-  va_end(args);
+	va_start(args, format);
+	log_vmiddle(level, format, args);
+	va_end(args);
 
-  log_end(level);
+	log_end(level);
 }
 
-void log_begin(log_level level)
-{
-  const char *color_str;
-  FILE *file;
-  const char *level_str;
-
-  if (level < filter_level)
-    return;
-
-  file = file_for_level(level);
-
-  if (use_colors == log_use_colors_auto)
-    {
-      if (term_supports_colors())
-	use_colors = log_use_colors_yes;
-      else
-	use_colors = log_use_colors_no;
-    }
-
-  switch (level & LOG_LEVEL_MASK) {
-    case log_debug:
-      color_str = blue;
-      level_str = "D";
-      break;
-    case log_info:
-      color_str = reset;
-      level_str = "I";
-      break;
-    case log_warn:
-      color_str = yellow;
-      level_str = "W";
-      break;
-    case log_error:
-      color_str = red;
-      level_str = "E";
-      break;
-    default:
-      color_str = red;
-      level_str = "?";
-      break;
-  }
-
-  if (use_colors == log_use_colors_yes)
-  {
-    fprintf(file, "%s", color_str);
-  }
-
-  fprintf(file, "%s: ", level_str);
+void log_begin(log_level level) {
+	const char *color_str;
+	FILE *file;
+	const char *level_str;
+
+	if (level < filter_level)
+		return;
+
+	file = file_for_level(level);
+
+	if (use_colors == log_use_colors_auto) {
+		if (term_supports_colors())
+			use_colors = log_use_colors_yes;
+		else
+			use_colors = log_use_colors_no;
+	}
+
+	switch (level & LOG_LEVEL_MASK) {
+		case log_debug:
+			color_str = blue;
+			level_str = "D";
+			break;
+		case log_info:
+			color_str = reset;
+			level_str = "I";
+			break;
+		case log_warn:
+			color_str = yellow;
+			level_str = "W";
+			break;
+		case log_error:
+			color_str = red;
+			level_str = "E";
+			break;
+		default:
+			color_str = red;
+			level_str = "?";
+			break;
+	}
+
+	if (use_colors == log_use_colors_yes) {
+		fprintf(file, "%s", color_str);
+	}
+
+	fprintf(file, "%s: ", level_str);
 }
 
-void log_middle(log_level level, const char *format, ...)
-{
-  va_list args;
-  FILE *file;
+void log_middle(log_level level, const char *format, ...) {
+	va_list args;
+	FILE *file;
 
-  if (level < filter_level)
-    return;
+	if (level < filter_level)
+		return;
 
-  file = file_for_level(level);
+	file = file_for_level(level);
 
-  va_start(args, format);
-  vfprintf(file, format, args);
-  va_end(args);
+	va_start(args, format);
+	vfprintf(file, format, args);
+	va_end(args);
 }
 
-void log_vmiddle(log_level level, const char *format, va_list args)
-{
-  FILE *file;
+void log_vmiddle(log_level level, const char *format, va_list args) {
+	FILE *file;
 
-  if (level < filter_level)
-    return;
+	if (level < filter_level)
+		return;
 
-  file = file_for_level(level);
+	file = file_for_level(level);
 
-  vfprintf(file, format, args);
+	vfprintf(file, format, args);
 }
 
-void log_end(log_level level)
-{
-  FILE *file;
+void log_end(log_level level) {
+	FILE *file;
 
-  if (level < filter_level)
-    return;
+	if (level < filter_level)
+		return;
 
-  file = file_for_level(level);
+	file = file_for_level(level);
 
-  if (use_colors == log_use_colors_yes)
-    {
-      fprintf(file, "%s\n", reset);
-    }
-  else
-    {
-      fprintf(file, "\n");
-    }
+	if (use_colors == log_use_colors_yes) {
+		fprintf(file, "%s\n", reset);
+	} else {
+		fprintf(file, "\n");
+	}
 }
diff --git a/log.h b/log.h
index 8956b1a..df96ed4 100644
--- a/log.h
+++ b/log.h
@@ -25,20 +25,20 @@
 #include <stdio.h>
 
 typedef enum log_level {
-  log_debug = 0,
-  log_info = 1,
-  log_warn = 2,
-  log_error = 3,
-  /* Do not use on its own - OR it in */
-  log_always_print = 0x100
+	log_debug = 0,
+	log_info = 1,
+	log_warn = 2,
+	log_error = 3,
+	/* Do not use on its own - OR it in */
+	log_always_print = 0x100
 } log_level;
 
 #define LOG_LEVEL_MASK 0xff
 
 typedef enum log_use_colors {
-  log_use_colors_auto,
-  log_use_colors_no,
-  log_use_colors_yes
+	log_use_colors_auto,
+	log_use_colors_no,
+	log_use_colors_yes
 } log_use_colors;
 
 log_level log_get_filter_level(void);
@@ -53,16 +53,16 @@ void log_set_use_colors(log_use_colors use_colors_new);
 
 void log_perror(const char *s);
 
-__attribute__((format(printf, 2, 3)))
-void log_printf(log_level level, const char *format, ...);
+__attribute__((format(printf, 2, 3))) void
+log_printf(log_level level, const char *format, ...);
 
 void log_begin(log_level level);
 
-__attribute__((format(printf, 2, 3)))
-void log_middle(log_level level, const char *format, ...);
+__attribute__((format(printf, 2, 3))) void
+log_middle(log_level level, const char *format, ...);
 
-__attribute__((format(printf, 2, 0)))
-void log_vmiddle(log_level level, const char *format, va_list args);
+__attribute__((format(printf, 2, 0))) void
+log_vmiddle(log_level level, const char *format, va_list args);
 
 void log_end(log_level level);
 
diff --git a/main.c b/main.c
index 1fdf1c1..ffcfb33 100644
--- a/main.c
+++ b/main.c
@@ -2,9 +2,8 @@
    Just calls app_main. Makes easier for testing individual functions.
  */
 
-int app_main(int ac, char** av);
+int app_main(int ac, char **av);
 
-int main(int ac, char** av)
-{
-  return app_main(ac, av);
+int main(int ac, char **av) {
+	return app_main(ac, av);
 }
diff --git a/parameter.c b/parameter.c
index 6f2039f..0e7c9f3 100644
--- a/parameter.c
+++ b/parameter.c
@@ -30,8 +30,8 @@
 #include <assert.h>
 
 struct pbuilder_operation_arg {
-  const char* arg;
-  enum pbuilder_operation operation;
+	const char *arg;
+	enum pbuilder_operation operation;
 };
 
 /*
@@ -46,8 +46,8 @@ The last-command will be
 PBUILDER_ADD_PARAM(NULL);
 
  */
-char* pbuildercommandline[MAXPBUILDERCOMMANDLINE];
-int offset=2;
+char *pbuildercommandline[MAXPBUILDERCOMMANDLINE];
+int offset = 2;
 
 /*
 
@@ -64,20 +64,19 @@ The last-command will be
 DEBOOTSTRAP_ADD_PARAM(NULL);
 
  */
-char* debootstrap_command_line[MAX_DEBOOTSTRAP_COMMAND_LINE];
+char *debootstrap_command_line[MAX_DEBOOTSTRAP_COMMAND_LINE];
 int debootstrap_param_offset = 4;
 
 /*
    get size of Null Terminated array of strings
  */
-int size_of_ntarray(char ** buf)
-{
-  int i = 0;
-  while(buf[i]) {
-    ++i;
-    assert(i < MAX_CUSTOM_FILES);
-  }
-  return i;
+int size_of_ntarray(char **buf) {
+	int i = 0;
+	while (buf[i]) {
+		++i;
+		assert(i < MAX_CUSTOM_FILES);
+	}
+	return i;
 }
 
 /**
@@ -86,39 +85,31 @@ int size_of_ntarray(char ** buf)
  *
  ... for ', ''"'"
  */
-char* strdup_strip_quote(const char* p) {
-  if (*p == 0) {
-    return strdup("");
-  }
-
-  size_t len = strlen(p);
-  if (*p == '\'' && p[len-1] == '\'') {
-    char* ret = strdup(p+1);
-    assert(strlen(ret) == len - 1);
-    ret[len-2] = 0;
-    return ret;
-  }
-  return strdup(p);
+char *strdup_strip_quote(const char *p) {
+	if (*p == 0) {
+		return strdup("");
+	}
+
+	size_t len = strlen(p);
+	if (*p == '\'' && p[len - 1] == '\'') {
+		char *ret = strdup(p + 1);
+		assert(strlen(ret) == len - 1);
+		ret[len - 2] = 0;
+		return ret;
+	}
+	return strdup(p);
 }
 
-void parse_set_log_level(const char *str)
-{
-  if (!strcmp(str, "D"))
-  {
-    log_set_filter_level(log_debug);
-  }
-  else if (!strcmp(str, "I"))
-  {
-    log_set_filter_level(log_info);
-  }
-  else if (!strcmp(str, "W"))
-  {
-    log_set_filter_level(log_warn);
-  }
-  else if (!strcmp(str, "E"))
-  {
-    log_set_filter_level(log_error);
-  }
+void parse_set_log_level(const char *str) {
+	if (!strcmp(str, "D")) {
+		log_set_filter_level(log_debug);
+	} else if (!strcmp(str, "I")) {
+		log_set_filter_level(log_info);
+	} else if (!strcmp(str, "W")) {
+		log_set_filter_level(log_warn);
+	} else if (!strcmp(str, "E")) {
+		log_set_filter_level(log_error);
+	}
 }
 
 /**
@@ -132,784 +123,701 @@ void parse_set_log_level(const char *str)
  *   1 = file not found
  *   2 = syntax error
  */
-int load_config_file(const char* config, pbuilderconfig* pc)
-{
-  char *s;
-  FILE* f;
-
-  char* buf=NULL;
-  size_t bufsiz=0;
-  char* delim;
-  int result;
-
-  asprintf(&s, "env bash -c 'set -e ; . %s; set ' 2>&1", config);
-  f=popen(s, "r");
-  if( NULL == f )
-    return -1;
-  while ( (0 == feof(f)) && (getline(&buf,&bufsiz,f)>0) )
-    {
-      if (strrchr(buf,'\n'))
-	{
-	  *(strrchr(buf,'\n'))=0;
-	}
-
-      if ((delim=strchr(buf,'=')))
-	{
-	  /* assuming config entry */
-	  *(delim++)=0;
-	  if (!strcmp(buf, "LOGLEVEL"))
-	    {
-	      parse_set_log_level(delim);
-	    }
-	  else if (!strcmp(buf, "USECOLORS"))
-	    {
-	      if (!strcmp(delim, "auto"))
-		{
-		  log_set_use_colors(log_use_colors_auto);
-		}
-	      else if (!strcmp(delim, "no"))
-		{
-		  log_set_use_colors(log_use_colors_no);
-		}
-	      else if (!strcmp(delim, "yes"))
-		{
-		  log_set_use_colors(log_use_colors_yes);
+int load_config_file(const char *config, pbuilderconfig *pc) {
+	char *s;
+	FILE *f;
+
+	char *buf = NULL;
+	size_t bufsiz = 0;
+	char *delim;
+	int result;
+
+	asprintf(&s, "env bash -c 'set -e ; . %s; set ' 2>&1", config);
+	f = popen(s, "r");
+	if (NULL == f)
+		return -1;
+	while ((0 == feof(f)) && (getline(&buf, &bufsiz, f) > 0)) {
+		if (strrchr(buf, '\n')) {
+			*(strrchr(buf, '\n')) = 0;
 		}
-	    }
-	  else if (!strcmp(buf, "MIRRORSITE"))
-	    {
-	      pc->mirror=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "BUILDRESULT"))
-	    {
-	      pc->buildresult=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "DISTRIBUTION"))
-	    {
-	      pc->distribution=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "KERNEL_IMAGE"))
-	    {
-	      pc->kernel_image=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "INITRD"))
-	    {
-	      pc->initrd=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "MEMORY_MEGS"))
-	    {
-	      pc->memory_megs=atoi(delim);
-	    }
-	  else if (!strcmp(buf, "ARCHITECTURE"))
-	    {
-	      pc->arch=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "ARCH"))
-	    {
-	      pc->arch=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "BASEPATH"))
-	    {
-	      pc->basepath=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "BUILDPLACE"))
-	    {
-	      pc->buildplace=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "COMPONENTS"))
-	    {
-	      pc->components=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "EXTRAPACKAGES"))
-	    {
-	      pc->extrapackages=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "OTHERMIRROR"))
-	    {
-	      pc->othermirror=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "SMP"))
-	    {
-	      pc->smp=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "HOOKDIR"))
-	    {
-	      pc->hookdir=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "DEBBUILDOPTS"))
-	    {
-	      pc->debbuildopts=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "BINARY_ARCH"))
-	    {
-	      if (!strcmp(delim, "binary"))
-		{
-		  pc->binary_arch=1;
-		  pc->binary_indep=0;
-		}
-	      else if (!strcmp(delim, "all"))
-		{
-		  pc->binary_arch=0;
-		  pc->binary_indep=1;
+
+		if ((delim = strchr(buf, '='))) {
+			/* assuming config entry */
+			*(delim++) = 0;
+			if (!strcmp(buf, "LOGLEVEL")) {
+				parse_set_log_level(delim);
+			} else if (!strcmp(buf, "USECOLORS")) {
+				if (!strcmp(delim, "auto")) {
+					log_set_use_colors(log_use_colors_auto);
+				} else if (!strcmp(delim, "no")) {
+					log_set_use_colors(log_use_colors_no);
+				} else if (!strcmp(delim, "yes")) {
+					log_set_use_colors(log_use_colors_yes);
+				}
+			} else if (!strcmp(buf, "MIRRORSITE")) {
+				pc->mirror = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "BUILDRESULT")) {
+				pc->buildresult = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "DISTRIBUTION")) {
+				pc->distribution = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "KERNEL_IMAGE")) {
+				pc->kernel_image = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "INITRD")) {
+				pc->initrd = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "MEMORY_MEGS")) {
+				pc->memory_megs = atoi(delim);
+			} else if (!strcmp(buf, "ARCHITECTURE")) {
+				pc->arch = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "ARCH")) {
+				pc->arch = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "BASEPATH")) {
+				pc->basepath = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "BUILDPLACE")) {
+				pc->buildplace = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "COMPONENTS")) {
+				pc->components = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "EXTRAPACKAGES")) {
+				pc->extrapackages = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "OTHERMIRROR")) {
+				pc->othermirror = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "SMP")) {
+				pc->smp = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "HOOKDIR")) {
+				pc->hookdir = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "DEBBUILDOPTS")) {
+				pc->debbuildopts = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "BINARY_ARCH")) {
+				if (!strcmp(delim, "binary")) {
+					pc->binary_arch = 1;
+					pc->binary_indep = 0;
+				} else if (!strcmp(delim, "all")) {
+					pc->binary_arch = 0;
+					pc->binary_indep = 1;
+				}
+			} else if (!strcmp(buf, "NO_COWDANCER_UPDATE")) {
+				pc->no_cowdancer_update = 1;
+			} else if (!strcmp(buf, "DEBIAN_ETCH_WORKAROUND")) {
+				pc->debian_etch_workaround = 1;
+			} else if (!strcmp(buf, "ARCH_DISKDEVICE")) {
+				pc->arch_diskdevice = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "HTTP_PROXY")) {
+				pc->http_proxy = strdup_strip_quote(delim);
+			} else if (!strcmp(buf, "ALLOWUNTRUSTED")) {
+				pc->allow_untrusted = !strcmp(delim, "yes");
+			} else if (!strcmp(buf, "BUILDRESULTUID")) {
+				pc->buildresultuid = atoi(delim);
+			} else if (!strcmp(buf, "BUILDRESULTGID")) {
+				pc->buildresultgid = atoi(delim);
+			}
 		}
-	    }
-	  else if (!strcmp(buf, "NO_COWDANCER_UPDATE"))
-	    {
-	      pc->no_cowdancer_update=1;
-	    }
-	  else if (!strcmp(buf, "DEBIAN_ETCH_WORKAROUND"))
-	    {
-	      pc->debian_etch_workaround=1;
-	    }
-	  else if (!strcmp(buf, "ARCH_DISKDEVICE"))
-	    {
-	      pc->arch_diskdevice=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "HTTP_PROXY"))
-	    {
-	      pc->http_proxy=strdup_strip_quote(delim);
-	    }
-	  else if (!strcmp(buf, "ALLOWUNTRUSTED"))
-	    {
-	      pc->allow_untrusted=!strcmp(delim, "yes");
-	    }
-	  else if (!strcmp(buf, "BUILDRESULTUID"))
-	    {
-	      pc->buildresultuid=atoi(delim);
-	    }
-	  else if (!strcmp(buf, "BUILDRESULTGID"))
-	    {
-	      pc->buildresultgid=atoi(delim);
-	    }
 	}
-    }
-
-  result = WEXITSTATUS( pclose(f) );
-  if (buf) {
-    // Don't warn of missing config files
-    if (result > 1)
-      log_printf(log_error, "(exit %i) -> %s", result, buf);
-    free(buf);
-  }
-  if (s) free(s);
-  return result;
+
+	result = WEXITSTATUS(pclose(f));
+	if (buf) {
+		// Don't warn of missing config files
+		if (result > 1)
+			log_printf(log_error, "(exit %i) -> %s", result, buf);
+		free(buf);
+	}
+	if (s)
+		free(s);
+	return result;
 }
 
+int cpbuilder_dumpconfig(pbuilderconfig *pc) {
+	/* dump configuration */
+	int i;
 
-int cpbuilder_dumpconfig(pbuilderconfig* pc)
-{
-  /* dump configuration */
-  int i;
-
-  log_printf(log_info, "start dump config");
-#define DUMPINT(S) printf("  "#S": %i\n", pc->S);
-#define DUMPSTR(S) printf("  "#S": %s\n", pc->S);
-#define DUMPSTRARRAY(S) \
-  for (i = 0; pc->S[i]; ++i) \
-    { \
-      printf("  "#S"[%i]: %s\n", i, pc->S[i]); \
-    }
-
-#define DUMPLOGLEVEL(S) printf("  log_level: "S"\n");
-#define CASELOGLEVEL(L, S) case L: DUMPLOGLEVEL(S); break;
-  switch (log_get_filter_level()) {
-    CASELOGLEVEL(log_debug, "D");
-    CASELOGLEVEL(log_info,  "I");
-    CASELOGLEVEL(log_warn,  "W");
-    CASELOGLEVEL(log_error, "E");
-    default:
-      DUMPLOGLEVEL("?");
-      break;
-  }
-
-#define DUMPUSECOLORS(S) printf("  use_colors: "S"\n")
-#define DUMPUSECOLORSAUTO(S) DUMPUSECOLORS("auto ("S")")
-#define CASEUSECOLORS(S) case log_use_colors_##S: DUMPUSECOLORS(#S); break;
-#define CASEUSECOLORSAUTO(S) case log_use_colors_##S: DUMPUSECOLORSAUTO(#S); break;
-  switch (log_get_use_colors_unresolved()) {
-    case log_use_colors_auto:
-      switch (log_get_use_colors()) {
-	CASEUSECOLORSAUTO(no);
-	CASEUSECOLORSAUTO(yes);
-	default:
-	  DUMPUSECOLORSAUTO("?");
-	  break;
-      }
-      break;
-    CASEUSECOLORS(no);
-    CASEUSECOLORS(yes);
-    default:
-      DUMPUSECOLORS("?");
-      break;
-  }
-
-  DUMPINT(mountproc);
-  DUMPINT(mountdev);
-  DUMPINT(mountdevpts);
-  DUMPINT(save_after_login);
-  DUMPINT(debug);
-
-  DUMPSTR(buildplace);
-  DUMPSTR(buildresult);
-  DUMPSTR(basepath);
-  DUMPSTR(mirror);
-  DUMPSTR(distribution);
-  DUMPSTR(components);
-  DUMPSTR(extrapackages);
-  DUMPSTR(othermirror);
-  DUMPSTR(hookdir);
-  DUMPSTR(debbuildopts);
-  DUMPINT(binary_arch);
-  DUMPINT(binary_indep);
-  DUMPSTR(http_proxy);
-  DUMPINT(allow_untrusted);
-
-  DUMPSTRARRAY(inputfile);
-  DUMPSTRARRAY(outputfile);
-
-  DUMPINT(buildresultuid);
-  DUMPINT(buildresultgid);
-
-  DUMPINT(no_cowdancer_update);
-  DUMPINT(debian_etch_workaround);
-
-  DUMPSTR(kernel_image);
-  DUMPSTR(initrd);
-  DUMPSTR(smp);
-  DUMPINT(memory_megs);
-  DUMPSTR(arch);
-  DUMPSTR(arch_diskdevice);
-
-  log_printf(log_info, "end dump config");
-  return 0;
-}
+	log_printf(log_info, "start dump config");
+#define DUMPINT(S) printf("  " #S ": %i\n", pc->S);
+#define DUMPSTR(S) printf("  " #S ": %s\n", pc->S);
+#define DUMPSTRARRAY(S)                                                        \
+	for (i = 0; pc->S[i]; ++i) {                                               \
+		printf("  " #S "[%i]: %s\n", i, pc->S[i]);                             \
+	}
 
-int parse_parameter(int ac, char** av,
-		    const char* keyword)
-{
-  int c;			/* option */
-  int i;
-  int index_point;
-  int config_ok = -1, load_ok;
-  char * cmdstr=NULL;
-  static pbuilderconfig pc;
-
-  static const struct pbuilder_operation_arg operations[] =
-  {
-    {"--create",     pbuilder_create},
-    {"create",       pbuilder_create},
-
-    {"--update",     pbuilder_update},
-    {"update",       pbuilder_update},
-    {"up",           pbuilder_update},
-    {"u",            pbuilder_update},
-
-    {"--build",      pbuilder_build},
-    {"build",        pbuilder_build},
-    {"b",            pbuilder_build},
-
-    {"--login",      pbuilder_login},
-    {"login",        pbuilder_login},
-    {"l",            pbuilder_login},
-
-    {"--execute",    pbuilder_execute},
-    {"execute",      pbuilder_execute},
-    {"e",            pbuilder_execute},
-
-    {"--dumpconfig", pbuilder_dumpconfig},
-    {"dumpconfig",   pbuilder_dumpconfig},
-
-    {"--help",       pbuilder_help},
-    {"help",         pbuilder_help},
-    {"h",            pbuilder_help},
-    {"--version",    pbuilder_help},
-    {"version",      pbuilder_help},
-    {"-v",           pbuilder_help},
-    {"v",            pbuilder_help},
-
-    {NULL,           pbuilder_do_nothing}
-  };
-
-  static struct option long_options[]=
-  {
-    {"basepath", required_argument, 0, 'b'},
-    {"buildplace", required_argument, 0, 'B'},
-    {"mountproc", no_argument, &(pc.mountproc), 1},
-    {"mountdev", no_argument, &(pc.mountdev), 1},
-    {"mountdevpts", no_argument, &(pc.mountdevpts), 1},
-    {"nomountproc", no_argument, &(pc.mountproc), 0},
-    {"nomountdev", no_argument, &(pc.mountdev), 0},
-    {"nomountdevpts", no_argument, &(pc.mountdevpts), 0},
-    {"save-after-login", no_argument, &(pc.save_after_login), 1},
-    {"save-after-exec", no_argument, &(pc.save_after_login), 1},
-    {"help", no_argument, (int*)&(pc.operation), pbuilder_help},
-    {"version", no_argument, 0, 'v'},
-    {"debug", no_argument, 0, 0},
-    {"loglevel", required_argument, 0, 0},
-    {"configfile", required_argument, 0, 'c'},
-    {"mirror", required_argument, 0, 0},
-    {"buildresult", required_argument, 0, 0},
-    {"distribution", required_argument, 0, 0},
-    {"components", required_argument, 0, 0},
-    {"extrapackages", required_argument, 0, 0},
-    {"othermirror", required_argument, 0, 0},
-    {"smp", required_argument, 0, 0},
-    {"hookdir", required_argument, 0, 0},
-    {"debbuildopts", required_argument, 0, 0},
-    {"binary-arch", no_argument, 0, 0},
-    {"binary-indep", no_argument, 0, 0},
-    {"inputfile", required_argument, 0, 0},
-    {"outputfile", required_argument, 0, 0},
-    {"architecture", required_argument, 0, 0},
-    {"http-proxy", required_argument, 0, 0},
-    {"allow-untrusted", no_argument, 0, 0},
-    {"debootstrapopts", required_argument, 0, 0},
-    {"debootstrap", required_argument, 0, 0},
-
-    /* cowbuilder specific options */
-    {"no-cowdancer-update", no_argument, 0, 0},
-    {"debian-etch-workaround", no_argument, 0, 0},
-
-    /* qemubuilder specific options */
-    {"arch-diskdevice", no_argument, 0, 0},
-
-    /* verbatim options with argument, synced as of pbuilder 0.228 */
-    {"use-network", required_argument, 0, 'M'},
-    {"host-arch", required_argument, 0, 'M'},
-    {"aptcache", required_argument, 0, 'M'},
-    {"debemail", required_argument, 0, 'M'},
-    {"profiles", required_argument, 0, 'M'},
-    {"logfile", required_argument, 0, 'M'},
-    {"aptconfdir", required_argument, 0, 'M'},
-    {"timeout", required_argument, 0, 'M'},
-    {"bin-nmu", required_argument, 0, 'M'},
-    {"bin-nmu-maintainer", required_argument, 0, 'M'},
-    {"bin-nmu-version", required_argument, 0, 'M'},
-    {"bin-nmu-timestamp", required_argument, 0, 'M'},
-    {"keyring", required_argument, 0, 'M'},
-    {"bindmounts", required_argument, 0, 'M'},
-
-    /* verbatim options without argument, synced as of pbuilder 0.228 */
-    {"no-auto-cross", no_argument, 0, 'm'},
-    {"autocleanaptcache", no_argument, 0, 'm'},
-    {"removepackages", no_argument, 0, 'm'},
-    {"pkgname-logfile", no_argument, 0, 'm'},
-    {"override-config", no_argument, 0, 'm'},
-    {"source-only-changes", no_argument, 0, 'm'},
-    {"preserve-buildplace", no_argument, 0, 'm'},
-    {"debdelta", no_argument, 0, 'm'},
-    {"twice", no_argument, 0, 'm'},
-
-    {0,0,0,0}
-  };
-
-  /* define pc to be clear. */
-  memset (&pc, 0, sizeof(pbuilderconfig));
-  /* default command-line component */
-  pbuildercommandline[0]="pbuilder";
-  debootstrap_command_line[0]=strdup("debootstrap");
-
-  /**
-   * Try to load all standard config files.
-   * Skip non existing, but exit on broken ones.
-   * config_ok is 0, if any load was successfull
-   **/
-  load_ok = load_config_file("/usr/share/pbuilder/pbuilderrc", &pc);
-  if( load_ok > 1 ) exit( 2 );
-  if( config_ok != 0 ) config_ok = load_ok;
-
-  load_ok = load_config_file("/etc/pbuilderrc", &pc);
-  if( load_ok > 1 ) exit( 3 );
-  if( config_ok != 0 ) config_ok = load_ok;
-
-  load_ok = load_config_file("~/.pbuilderrc", &pc);
-  if( load_ok > 1 ) exit( 4 );
-  if( config_ok != 0 ) config_ok = load_ok;
-
-  if (ac >= 2)
-    {
-      for (i = 0; operations[i].arg; ++i)
-	{
-	  if (!strcmp(av[1], operations[i].arg))
-	    {
-	      pc.operation = operations[i].operation;
-	      break;
-	    }
+#define DUMPLOGLEVEL(S) printf("  log_level: " S "\n");
+#define CASELOGLEVEL(L, S)                                                     \
+	case L:                                                                    \
+		DUMPLOGLEVEL(S);                                                       \
+		break;
+	switch (log_get_filter_level()) {
+		CASELOGLEVEL(log_debug, "D");
+		CASELOGLEVEL(log_info, "I");
+		CASELOGLEVEL(log_warn, "W");
+		CASELOGLEVEL(log_error, "E");
+		default:
+			DUMPLOGLEVEL("?");
+			break;
 	}
 
-      if (pc.operation == pbuilder_do_nothing)
-	{
-	  log_printf(log_error, "Unknown operation: %s", av[1]);
-	  return 1;
+#define DUMPUSECOLORS(S) printf("  use_colors: " S "\n")
+#define DUMPUSECOLORSAUTO(S) DUMPUSECOLORS("auto (" S ")")
+#define CASEUSECOLORS(S)                                                       \
+	case log_use_colors_##S:                                                   \
+		DUMPUSECOLORS(#S);                                                     \
+		break;
+#define CASEUSECOLORSAUTO(S)                                                   \
+	case log_use_colors_##S:                                                   \
+		DUMPUSECOLORSAUTO(#S);                                                 \
+		break;
+	switch (log_get_use_colors_unresolved()) {
+		case log_use_colors_auto:
+			switch (log_get_use_colors()) {
+				CASEUSECOLORSAUTO(no);
+				CASEUSECOLORSAUTO(yes);
+				default:
+					DUMPUSECOLORSAUTO("?");
+					break;
+			}
+			break;
+			CASEUSECOLORS(no);
+			CASEUSECOLORS(yes);
+		default:
+			DUMPUSECOLORS("?");
+			break;
 	}
-    }
-  else
-    {
-      log_printf(log_error, "No operation specified");
-      return 1;
-    }
-
-  --ac;
-  av[1] = av[0];
-  ++av;
-
-#define PASS_TO_PBUILDER_WITH_PARAM PBUILDER_ADD_PARAM(cmdstr); \
-	      PBUILDER_ADD_PARAM(strdup(optarg));
-
-  /* load config files here. */
-  while((c = getopt_long (ac, av, "b:B:c:hv", long_options, &index_point)) != -1)
-    {
-      switch (c)
-	{
-	case 'b':		/* basepath */
-	  if (pc.operation == pbuilder_create)
-	    {
-	      if (mkdir(optarg, 0777)<0)
-		{
-		  log_printf(log_error,
-			     "Could not create basepath '%s': %s",
-			     optarg,
-			     strerror(errno));
-		  return 1;
-		}
-	    }
-	  else if (!pc.operation)
-	    {
-	      log_printf(log_error, "need to specify operation before --basepath option");
-	      return 1;
-	    }
-	  if (!(pc.basepath = canonicalize_file_name(optarg)))
-	    {
-	      log_printf(log_error, "cannot canonicalize filename %s, does not exist", optarg);
-	      return 1;
-	    }
-	  break;
-	case 'B':		/* buildplace */
-	  pc.buildplace = strdup(optarg);
-	  break;
-	case 'c':		/* --config */
-	  load_ok = load_config_file(optarg, &pc);
-	  if( load_ok > 1 ) exit( 5 );
-	  if( config_ok != 0 ) config_ok = load_ok;
-
-	  if (0>asprintf(&cmdstr, "--%s", long_options[index_point].name))
-	    {
-	      /* error */
-	      log_printf(log_error, "out of memory constructing command-line options");
-	      exit (1);
-	    }
-	  PASS_TO_PBUILDER_WITH_PARAM
-	  break;
-	case 'M':		/* pass through to pbuilder: duplicate with param */
-	  if (0>asprintf(&cmdstr, "--%s", long_options[index_point].name))
-	    {
-	      /* error */
-	      log_printf(log_error, "out of memory constructing command-line options");
-	      exit (1);
-	    }
-	  PASS_TO_PBUILDER_WITH_PARAM
-	  break;
-	case 'm':		/* pass through to pbuilder: duplicate without param */
-	  if (0>asprintf(&cmdstr, "--%s", long_options[index_point].name))
-	    {
-	      /* error */
-	      log_printf(log_error, "out of memory constructing command-line options");
-	      exit (1);
-	    }
-	  PBUILDER_ADD_PARAM(cmdstr);
-	  break;
-	case 0:
-	  /* other cases with long option with flags, this is expected
-	     behavior, so ignore it, for most of the time.
-	  */
-
-	  /* handle specific options which also give 0. */
-
-	  /* first, generate 'cmdstr' which is useful anyway */
-	  if (0>asprintf(&cmdstr, "--%s", long_options[index_point].name))
-	    {
-	      /* error */
-	      log_printf(log_error, "out of memory constructing command-line options");
-	      exit (1);
-	    }
-
-	  if (!strcmp(long_options[index_point].name,"loglevel"))
-	    {
-	      parse_set_log_level(optarg);
-	      PASS_TO_PBUILDER_WITH_PARAM
-	    }
-	  else if (!strcmp(long_options[index_point].name,"mirror"))
-	    {
-	      pc.mirror=strdup(optarg);
-	    }
-	  else if (!strcmp(long_options[index_point].name,"buildresult"))
-	    {
-	      pc.buildresult=strdup(optarg);
-	    }
-	  else if (!strcmp(long_options[index_point].name,"distribution"))
-	    {
-	      pc.distribution=strdup(optarg);
-	    }
-	  else if (!strcmp(long_options[index_point].name,"no-cowdancer-update"))
-	    {
-	      pc.no_cowdancer_update=1;
-	    }
-	  else if (!strcmp(long_options[index_point].name,"debian-etch-workaround"))
-	    {
-	      pc.debian_etch_workaround=1;
-	    }
-	  else if (!strcmp(long_options[index_point].name,"architecture"))
-	    {
-	      pc.arch=strdup(optarg);
-	    }
-	  else if (!strcmp(long_options[index_point].name,"arch-diskdevice"))
-	    {
-	      pc.arch_diskdevice=strdup(optarg);
-	    }
-	  else if (!strcmp(long_options[index_point].name,"debug"))
-	    {
-	      pc.debug=1;
-	      PBUILDER_ADD_PARAM(cmdstr);
-	    }
-	  else if (!strcmp(long_options[index_point].name,"inputfile"))
-	    {
-	      pc.inputfile[size_of_ntarray(pc.inputfile)]=strdup(optarg);
-	      if (size_of_ntarray(pc.inputfile) >= MAX_CUSTOM_FILES)
-		{
-		  log_printf(log_error, "too many inputfile options");
-		  exit (1);
+
+	DUMPINT(mountproc);
+	DUMPINT(mountdev);
+	DUMPINT(mountdevpts);
+	DUMPINT(save_after_login);
+	DUMPINT(debug);
+
+	DUMPSTR(buildplace);
+	DUMPSTR(buildresult);
+	DUMPSTR(basepath);
+	DUMPSTR(mirror);
+	DUMPSTR(distribution);
+	DUMPSTR(components);
+	DUMPSTR(extrapackages);
+	DUMPSTR(othermirror);
+	DUMPSTR(hookdir);
+	DUMPSTR(debbuildopts);
+	DUMPINT(binary_arch);
+	DUMPINT(binary_indep);
+	DUMPSTR(http_proxy);
+	DUMPINT(allow_untrusted);
+
+	DUMPSTRARRAY(inputfile);
+	DUMPSTRARRAY(outputfile);
+
+	DUMPINT(buildresultuid);
+	DUMPINT(buildresultgid);
+
+	DUMPINT(no_cowdancer_update);
+	DUMPINT(debian_etch_workaround);
+
+	DUMPSTR(kernel_image);
+	DUMPSTR(initrd);
+	DUMPSTR(smp);
+	DUMPINT(memory_megs);
+	DUMPSTR(arch);
+	DUMPSTR(arch_diskdevice);
+
+	log_printf(log_info, "end dump config");
+	return 0;
+}
+
+int parse_parameter(int ac, char **av, const char *keyword) {
+	int c; /* option */
+	int i;
+	int index_point;
+	int config_ok = -1, load_ok;
+	char *cmdstr = NULL;
+	static pbuilderconfig pc;
+
+	static const struct pbuilder_operation_arg operations[] = {
+		{"--create", pbuilder_create},
+		{"create", pbuilder_create},
+
+		{"--update", pbuilder_update},
+		{"update", pbuilder_update},
+		{"up", pbuilder_update},
+		{"u", pbuilder_update},
+
+		{"--build", pbuilder_build},
+		{"build", pbuilder_build},
+		{"b", pbuilder_build},
+
+		{"--login", pbuilder_login},
+		{"login", pbuilder_login},
+		{"l", pbuilder_login},
+
+		{"--execute", pbuilder_execute},
+		{"execute", pbuilder_execute},
+		{"e", pbuilder_execute},
+
+		{"--dumpconfig", pbuilder_dumpconfig},
+		{"dumpconfig", pbuilder_dumpconfig},
+
+		{"--help", pbuilder_help},
+		{"help", pbuilder_help},
+		{"h", pbuilder_help},
+		{"--version", pbuilder_help},
+		{"version", pbuilder_help},
+		{"-v", pbuilder_help},
+		{"v", pbuilder_help},
+
+		{NULL, pbuilder_do_nothing}};
+
+	static struct option long_options[] = {
+		{"basepath", required_argument, 0, 'b'},
+		{"buildplace", required_argument, 0, 'B'},
+		{"mountproc", no_argument, &(pc.mountproc), 1},
+		{"mountdev", no_argument, &(pc.mountdev), 1},
+		{"mountdevpts", no_argument, &(pc.mountdevpts), 1},
+		{"nomountproc", no_argument, &(pc.mountproc), 0},
+		{"nomountdev", no_argument, &(pc.mountdev), 0},
+		{"nomountdevpts", no_argument, &(pc.mountdevpts), 0},
+		{"save-after-login", no_argument, &(pc.save_after_login), 1},
+		{"save-after-exec", no_argument, &(pc.save_after_login), 1},
+		{"help", no_argument, (int *)&(pc.operation), pbuilder_help},
+		{"version", no_argument, 0, 'v'},
+		{"debug", no_argument, 0, 0},
+		{"loglevel", required_argument, 0, 0},
+		{"configfile", required_argument, 0, 'c'},
+		{"mirror", required_argument, 0, 0},
+		{"buildresult", required_argument, 0, 0},
+		{"distribution", required_argument, 0, 0},
+		{"components", required_argument, 0, 0},
+		{"extrapackages", required_argument, 0, 0},
+		{"othermirror", required_argument, 0, 0},
+		{"smp", required_argument, 0, 0},
+		{"hookdir", required_argument, 0, 0},
+		{"debbuildopts", required_argument, 0, 0},
+		{"binary-arch", no_argument, 0, 0},
+		{"binary-indep", no_argument, 0, 0},
+		{"inputfile", required_argument, 0, 0},
+		{"outputfile", required_argument, 0, 0},
+		{"architecture", required_argument, 0, 0},
+		{"http-proxy", required_argument, 0, 0},
+		{"allow-untrusted", no_argument, 0, 0},
+		{"debootstrapopts", required_argument, 0, 0},
+		{"debootstrap", required_argument, 0, 0},
+
+		/* cowbuilder specific options */
+		{"no-cowdancer-update", no_argument, 0, 0},
+		{"debian-etch-workaround", no_argument, 0, 0},
+
+		/* qemubuilder specific options */
+		{"arch-diskdevice", no_argument, 0, 0},
+
+		/* verbatim options with argument, synced as of pbuilder 0.228 */
+		{"use-network", required_argument, 0, 'M'},
+		{"host-arch", required_argument, 0, 'M'},
+		{"aptcache", required_argument, 0, 'M'},
+		{"debemail", required_argument, 0, 'M'},
+		{"profiles", required_argument, 0, 'M'},
+		{"logfile", required_argument, 0, 'M'},
+		{"aptconfdir", required_argument, 0, 'M'},
+		{"timeout", required_argument, 0, 'M'},
+		{"bin-nmu", required_argument, 0, 'M'},
+		{"bin-nmu-maintainer", required_argument, 0, 'M'},
+		{"bin-nmu-version", required_argument, 0, 'M'},
+		{"bin-nmu-timestamp", required_argument, 0, 'M'},
+		{"keyring", required_argument, 0, 'M'},
+		{"bindmounts", required_argument, 0, 'M'},
+
+		/* verbatim options without argument, synced as of pbuilder 0.228 */
+		{"no-auto-cross", no_argument, 0, 'm'},
+		{"autocleanaptcache", no_argument, 0, 'm'},
+		{"removepackages", no_argument, 0, 'm'},
+		{"pkgname-logfile", no_argument, 0, 'm'},
+		{"override-config", no_argument, 0, 'm'},
+		{"source-only-changes", no_argument, 0, 'm'},
+		{"preserve-buildplace", no_argument, 0, 'm'},
+		{"debdelta", no_argument, 0, 'm'},
+		{"twice", no_argument, 0, 'm'},
+
+		{0, 0, 0, 0}};
+
+	/* define pc to be clear. */
+	memset(&pc, 0, sizeof(pbuilderconfig));
+	/* default command-line component */
+	pbuildercommandline[0] = "pbuilder";
+	debootstrap_command_line[0] = strdup("debootstrap");
+
+	/**
+	 * Try to load all standard config files.
+	 * Skip non existing, but exit on broken ones.
+	 * config_ok is 0, if any load was successfull
+	 **/
+	load_ok = load_config_file("/usr/share/pbuilder/pbuilderrc", &pc);
+	if (load_ok > 1)
+		exit(2);
+	if (config_ok != 0)
+		config_ok = load_ok;
+
+	load_ok = load_config_file("/etc/pbuilderrc", &pc);
+	if (load_ok > 1)
+		exit(3);
+	if (config_ok != 0)
+		config_ok = load_ok;
+
+	load_ok = load_config_file("~/.pbuilderrc", &pc);
+	if (load_ok > 1)
+		exit(4);
+	if (config_ok != 0)
+		config_ok = load_ok;
+
+	if (ac >= 2) {
+		for (i = 0; operations[i].arg; ++i) {
+			if (!strcmp(av[1], operations[i].arg)) {
+				pc.operation = operations[i].operation;
+				break;
+			}
 		}
-	      PASS_TO_PBUILDER_WITH_PARAM
-	    }
-	  else if (!strcmp(long_options[index_point].name,"outputfile"))
-	    {
-	      pc.inputfile[size_of_ntarray(pc.outputfile)]=strdup(optarg);
-	      if (size_of_ntarray(pc.outputfile) >= MAX_CUSTOM_FILES)
-		{
-		  log_printf(log_error, "too many outputfile options");
-		  exit (1);
+
+		if (pc.operation == pbuilder_do_nothing) {
+			log_printf(log_error, "Unknown operation: %s", av[1]);
+			return 1;
 		}
-	    }
-	  else if (!strcmp(long_options[index_point].name,"components"))
-	    {
-	      /* this is for qemubuilder */
-	      pc.components=strdup(optarg);
-
-	      /* pass it for cowbuilder */
-	      PASS_TO_PBUILDER_WITH_PARAM
-	    }
-	  else if (!strcmp(long_options[index_point].name,"extrapackages"))
-	    {
-	      /* this is for qemubuilder and cowbuilder (adds cowdancer) */
-	      pc.extrapackages=strdup(optarg);
-	    }
-	  else if (!strcmp(long_options[index_point].name,"othermirror"))
-	    {
-	      /* this is for qemubuilder */
-	      pc.othermirror=strdup(optarg);
-
-	      /* pass it for cowbuilder */
-	      PASS_TO_PBUILDER_WITH_PARAM
-	    }
-	  else if (!strcmp(long_options[index_point].name,"smp"))
-	    {
-	      /* this is for qemubuilder */
-	      pc.smp=strdup(optarg);
-
-	      /* pass it for cowbuilder */
-	      PASS_TO_PBUILDER_WITH_PARAM
-	    }
-	  else if (!strcmp(long_options[index_point].name,"http-proxy"))
-	    {
-	      /* this is for qemubuilder */
-	      pc.http_proxy=strdup(optarg);
-
-	      /* pass it for cowbuilder */
-	      PASS_TO_PBUILDER_WITH_PARAM
-	    }
-	  else if (!strcmp(long_options[index_point].name,"hookdir"))
-	    {
-	      /* this is for qemubuilder */
-	      pc.hookdir=strdup(optarg);
-
-	      /* pass it for cowbuilder */
-	      PASS_TO_PBUILDER_WITH_PARAM
-	    }
-	  else if (!strcmp(long_options[index_point].name,"debbuildopts"))
-	    {
-	      /* this is for qemubuilder */
-	      pc.debbuildopts=strdup(optarg);
-
-	      /* pass it for cowbuilder */
-	      PASS_TO_PBUILDER_WITH_PARAM
-	    }
-	  else if (!strcmp(long_options[index_point].name,"binary-arch"))
-	    {
-	      pc.binary_arch=1;
-	      pc.binary_indep=0;
-	      PBUILDER_ADD_PARAM(cmdstr);
-	    }
-	  else if (!strcmp(long_options[index_point].name,"binary-indep"))
-	    {
-	      pc.binary_arch=0;
-	      pc.binary_indep=1;
-	      PBUILDER_ADD_PARAM(cmdstr);
-	    }
-	  else if (!strcmp(long_options[index_point].name,"allow-untrusted"))
-	    {
-	      pc.allow_untrusted=1;
-	      PBUILDER_ADD_PARAM(cmdstr);
-	    }
-	  else if (!strcmp(long_options[index_point].name,"debootstrapopts"))
-	    {
-	      /* this is for qemubuilder */
-	      DEBOOTSTRAP_ADD_PARAM(strdup(optarg));
-
-	      /* pass it for cowbuilder */
-	      PASS_TO_PBUILDER_WITH_PARAM
-	    }
-	  else if (!strcmp(long_options[index_point].name,"debootstrap"))
-	    {
-	      /* this is for qemubuilder */
-	      free(debootstrap_command_line[0]);
-	      debootstrap_command_line[0]=strdup(optarg);
-
-	      /* pass it for cowbuilder */
-	      PASS_TO_PBUILDER_WITH_PARAM
-	    }
-	  break;
-	case 'h':		/* -h */
-	case 'v':		/* -v --version */
-	  pc.operation=pbuilder_help;
-	  break;
-	default:
-	  log_printf(log_error, "Unhandled option");
-	  /* Error case. */
-	  return 1;
+	} else {
+		log_printf(log_error, "No operation specified");
+		return 1;
 	}
-    }
-
-  if( 0 != config_ok ) {
-    log_printf(log_error, "Couldn't load any valid config file.");
-    exit( 6 );
-  }
-
-  /* set default values */
-  if (!pc.basepath)
-    asprintf(&(pc.basepath), "/var/cache/pbuilder/base.%s", keyword);
-  if (!pc.buildplace)
-    {
-      log_printf(log_error, "BUILDPLACE is not set");
-      return 1;
-    }
-  else
-    {
-      char* buildplace_ = pc.buildplace;
-      mkdir(buildplace_,0777); /* create if it does not exist */
-      int len = strlen(buildplace_);
-      if (len > 0 && buildplace_[len-1] == '/')
-	{
-	  asprintf(&(pc.buildplace), "%s%s.%i",
-		   buildplace_, keyword, (int)getpid());
+
+	--ac;
+	av[1] = av[0];
+	++av;
+
+#define PASS_TO_PBUILDER_WITH_PARAM                                            \
+	PBUILDER_ADD_PARAM(cmdstr);                                                \
+	PBUILDER_ADD_PARAM(strdup(optarg));
+
+	/* load config files here. */
+	while ((c = getopt_long(ac, av, "b:B:c:hv", long_options, &index_point)) !=
+		   -1) {
+		switch (c) {
+			case 'b': /* basepath */
+				if (pc.operation == pbuilder_create) {
+					if (mkdir(optarg, 0777) < 0) {
+						log_printf(log_error,
+								   "Could not create basepath '%s': %s",
+								   optarg,
+								   strerror(errno));
+						return 1;
+					}
+				} else if (!pc.operation) {
+					log_printf(
+						log_error,
+						"need to specify operation before --basepath option");
+					return 1;
+				}
+				if (!(pc.basepath = canonicalize_file_name(optarg))) {
+					log_printf(
+						log_error,
+						"cannot canonicalize filename %s, does not exist",
+						optarg);
+					return 1;
+				}
+				break;
+			case 'B': /* buildplace */
+				pc.buildplace = strdup(optarg);
+				break;
+			case 'c': /* --config */
+				load_ok = load_config_file(optarg, &pc);
+				if (load_ok > 1)
+					exit(5);
+				if (config_ok != 0)
+					config_ok = load_ok;
+
+				if (0 >
+					asprintf(&cmdstr, "--%s", long_options[index_point].name)) {
+					/* error */
+					log_printf(
+						log_error,
+						"out of memory constructing command-line options");
+					exit(1);
+				}
+				PASS_TO_PBUILDER_WITH_PARAM
+				break;
+			case 'M': /* pass through to pbuilder: duplicate with param */
+				if (0 >
+					asprintf(&cmdstr, "--%s", long_options[index_point].name)) {
+					/* error */
+					log_printf(
+						log_error,
+						"out of memory constructing command-line options");
+					exit(1);
+				}
+				PASS_TO_PBUILDER_WITH_PARAM
+				break;
+			case 'm': /* pass through to pbuilder: duplicate without param */
+				if (0 >
+					asprintf(&cmdstr, "--%s", long_options[index_point].name)) {
+					/* error */
+					log_printf(
+						log_error,
+						"out of memory constructing command-line options");
+					exit(1);
+				}
+				PBUILDER_ADD_PARAM(cmdstr);
+				break;
+			case 0:
+				/* other cases with long option with flags, this is expected
+				 * behavior, so ignore it, for most of the time.
+				 */
+
+				/* handle specific options which also give 0. */
+
+				/* first, generate 'cmdstr' which is useful anyway */
+				if (0 >
+					asprintf(&cmdstr, "--%s", long_options[index_point].name)) {
+					/* error */
+					log_printf(
+						log_error,
+						"out of memory constructing command-line options");
+					exit(1);
+				}
+
+				if (!strcmp(long_options[index_point].name, "loglevel")) {
+					parse_set_log_level(optarg);
+					PASS_TO_PBUILDER_WITH_PARAM
+				} else if (!strcmp(long_options[index_point].name, "mirror")) {
+					pc.mirror = strdup(optarg);
+				} else if (!strcmp(long_options[index_point].name,
+								   "buildresult")) {
+					pc.buildresult = strdup(optarg);
+				} else if (!strcmp(long_options[index_point].name,
+								   "distribution")) {
+					pc.distribution = strdup(optarg);
+				} else if (!strcmp(long_options[index_point].name,
+								   "no-cowdancer-update")) {
+					pc.no_cowdancer_update = 1;
+				} else if (!strcmp(long_options[index_point].name,
+								   "debian-etch-workaround")) {
+					pc.debian_etch_workaround = 1;
+				} else if (!strcmp(long_options[index_point].name,
+								   "architecture")) {
+					pc.arch = strdup(optarg);
+				} else if (!strcmp(long_options[index_point].name,
+								   "arch-diskdevice")) {
+					pc.arch_diskdevice = strdup(optarg);
+				} else if (!strcmp(long_options[index_point].name, "debug")) {
+					pc.debug = 1;
+					PBUILDER_ADD_PARAM(cmdstr);
+				} else if (!strcmp(long_options[index_point].name,
+								   "inputfile")) {
+					pc.inputfile[size_of_ntarray(pc.inputfile)] =
+						strdup(optarg);
+					if (size_of_ntarray(pc.inputfile) >= MAX_CUSTOM_FILES) {
+						log_printf(log_error, "too many inputfile options");
+						exit(1);
+					}
+					PASS_TO_PBUILDER_WITH_PARAM
+				} else if (!strcmp(long_options[index_point].name,
+								   "outputfile")) {
+					pc.inputfile[size_of_ntarray(pc.outputfile)] =
+						strdup(optarg);
+					if (size_of_ntarray(pc.outputfile) >= MAX_CUSTOM_FILES) {
+						log_printf(log_error, "too many outputfile options");
+						exit(1);
+					}
+				} else if (!strcmp(long_options[index_point].name,
+								   "components")) {
+					/* this is for qemubuilder */
+					pc.components = strdup(optarg);
+
+					/* pass it for cowbuilder */
+					PASS_TO_PBUILDER_WITH_PARAM
+				} else if (!strcmp(long_options[index_point].name,
+								   "extrapackages")) {
+					/* this is for qemubuilder and cowbuilder (adds cowdancer) */
+					pc.extrapackages = strdup(optarg);
+				} else if (!strcmp(long_options[index_point].name,
+								   "othermirror")) {
+					/* this is for qemubuilder */
+					pc.othermirror = strdup(optarg);
+
+					/* pass it for cowbuilder */
+					PASS_TO_PBUILDER_WITH_PARAM
+				} else if (!strcmp(long_options[index_point].name, "smp")) {
+					/* this is for qemubuilder */
+					pc.smp = strdup(optarg);
+
+					/* pass it for cowbuilder */
+					PASS_TO_PBUILDER_WITH_PARAM
+				} else if (!strcmp(long_options[index_point].name,
+								   "http-proxy")) {
+					/* this is for qemubuilder */
+					pc.http_proxy = strdup(optarg);
+
+					/* pass it for cowbuilder */
+					PASS_TO_PBUILDER_WITH_PARAM
+				} else if (!strcmp(long_options[index_point].name, "hookdir")) {
+					/* this is for qemubuilder */
+					pc.hookdir = strdup(optarg);
+
+					/* pass it for cowbuilder */
+					PASS_TO_PBUILDER_WITH_PARAM
+				} else if (!strcmp(long_options[index_point].name,
+								   "debbuildopts")) {
+					/* this is for qemubuilder */
+					pc.debbuildopts = strdup(optarg);
+
+					/* pass it for cowbuilder */
+					PASS_TO_PBUILDER_WITH_PARAM
+				} else if (!strcmp(long_options[index_point].name,
+								   "binary-arch")) {
+					pc.binary_arch = 1;
+					pc.binary_indep = 0;
+					PBUILDER_ADD_PARAM(cmdstr);
+				} else if (!strcmp(long_options[index_point].name,
+								   "binary-indep")) {
+					pc.binary_arch = 0;
+					pc.binary_indep = 1;
+					PBUILDER_ADD_PARAM(cmdstr);
+				} else if (!strcmp(long_options[index_point].name,
+								   "allow-untrusted")) {
+					pc.allow_untrusted = 1;
+					PBUILDER_ADD_PARAM(cmdstr);
+				} else if (!strcmp(long_options[index_point].name,
+								   "debootstrapopts")) {
+					/* this is for qemubuilder */
+					DEBOOTSTRAP_ADD_PARAM(strdup(optarg));
+
+					/* pass it for cowbuilder */
+					PASS_TO_PBUILDER_WITH_PARAM
+				} else if (!strcmp(long_options[index_point].name,
+								   "debootstrap")) {
+					/* this is for qemubuilder */
+					free(debootstrap_command_line[0]);
+					debootstrap_command_line[0] = strdup(optarg);
+
+					/* pass it for cowbuilder */
+					PASS_TO_PBUILDER_WITH_PARAM
+				}
+				break;
+			case 'h': /* -h */
+			case 'v': /* -v --version */
+				pc.operation = pbuilder_help;
+				break;
+			default:
+				log_printf(log_error, "Unhandled option");
+				/* Error case. */
+				return 1;
+		}
 	}
-      else
-	{
-	  asprintf(&(pc.buildplace), "%s/%s.%i",
-		   buildplace_, keyword, (int)getpid());
+
+	if (0 != config_ok) {
+		log_printf(log_error, "Couldn't load any valid config file.");
+		exit(6);
 	}
-      free(buildplace_);
-    }
-
-  if (!pc.distribution)
-    pc.distribution=strdup("sid");
-
-  if (!pc.buildresultuid)
-    {
-      const char* sudo_uid = getenv("SUDO_UID");
-      if (sudo_uid)
-	pc.buildresultuid = atoi(sudo_uid);
-    }
-
-  if (!pc.buildresultgid)
-    {
-      const char* sudo_gid = getenv("SUDO_GID");
-      if (sudo_gid)
-	pc.buildresultgid = atoi(sudo_gid);
-    }
-
-  if (!pc.memory_megs)
-    pc.memory_megs=256;
-
-  switch(pc.operation)
-    {
-    case pbuilder_build:
-      if (cpbuilder_check_config(&pc))
-	return 1;
-
-      if (!av[optind])
-	{
-	  /* parameter missing */
-	  log_printf(log_error, "parameter missing for build operation");
-	  return 1;
+
+	/* set default values */
+	if (!pc.basepath)
+		asprintf(&(pc.basepath), "/var/cache/pbuilder/base.%s", keyword);
+	if (!pc.buildplace) {
+		log_printf(log_error, "BUILDPLACE is not set");
+		return 1;
+	} else {
+		char *buildplace_ = pc.buildplace;
+		mkdir(buildplace_, 0777); /* create if it does not exist */
+		int len = strlen(buildplace_);
+		if (len > 0 && buildplace_[len - 1] == '/') {
+			asprintf(&(pc.buildplace),
+					 "%s%s.%i",
+					 buildplace_,
+					 keyword,
+					 (int)getpid());
+		} else {
+			asprintf(&(pc.buildplace),
+					 "%s/%s.%i",
+					 buildplace_,
+					 keyword,
+					 (int)getpid());
+		}
+		free(buildplace_);
 	}
 
-      return cpbuilder_build(&pc, av[optind]);
+	if (!pc.distribution)
+		pc.distribution = strdup("sid");
 
-    case pbuilder_create:
-      if (cpbuilder_check_config(&pc))
-	return 1;
+	if (!pc.buildresultuid) {
+		const char *sudo_uid = getenv("SUDO_UID");
+		if (sudo_uid)
+			pc.buildresultuid = atoi(sudo_uid);
+	}
 
-      if (av[optind])
-	{
-	  /* extra parameter */
-	  log_printf(log_error, "too many parameters for create");
-	  return 1;
+	if (!pc.buildresultgid) {
+		const char *sudo_gid = getenv("SUDO_GID");
+		if (sudo_gid)
+			pc.buildresultgid = atoi(sudo_gid);
 	}
 
-      return cpbuilder_create(&pc);
+	if (!pc.memory_megs)
+		pc.memory_megs = 256;
 
-    case pbuilder_update:
-      if (cpbuilder_check_config(&pc))
-	return 1;
+	switch (pc.operation) {
+		case pbuilder_build:
+			if (cpbuilder_check_config(&pc))
+				return 1;
 
-      if (av[optind])
-	{
-	  /* extra parameter */
-	  log_printf(log_error, "too many parameters for update");
-	  return 1;
-	}
+			if (!av[optind]) {
+				/* parameter missing */
+				log_printf(log_error, "parameter missing for build operation");
+				return 1;
+			}
 
-      return cpbuilder_update(&pc);
+			return cpbuilder_build(&pc, av[optind]);
 
-    case pbuilder_login:
-      if (cpbuilder_check_config(&pc))
-	return 1;
+		case pbuilder_create:
+			if (cpbuilder_check_config(&pc))
+				return 1;
 
-      return cpbuilder_login(&pc);
+			if (av[optind]) {
+				/* extra parameter */
+				log_printf(log_error, "too many parameters for create");
+				return 1;
+			}
 
-    case pbuilder_execute:
-      if (cpbuilder_check_config(&pc))
-	return 1;
+			return cpbuilder_create(&pc);
 
-      if (!av[optind])
-	{
-	  /* parameter missing */
-	  log_printf(log_error, "parameter missing for execute operation");
-	  return 1;
-	}
+		case pbuilder_update:
+			if (cpbuilder_check_config(&pc))
+				return 1;
+
+			if (av[optind]) {
+				/* extra parameter */
+				log_printf(log_error, "too many parameters for update");
+				return 1;
+			}
+
+			return cpbuilder_update(&pc);
+
+		case pbuilder_login:
+			if (cpbuilder_check_config(&pc))
+				return 1;
 
-      return cpbuilder_execute(&pc, &av[optind]);
+			return cpbuilder_login(&pc);
 
-    case pbuilder_help:
-      return cpbuilder_help();
+		case pbuilder_execute:
+			if (cpbuilder_check_config(&pc))
+				return 1;
 
-    case pbuilder_dumpconfig:
-      return cpbuilder_dumpconfig(&pc);
+			if (!av[optind]) {
+				/* parameter missing */
+				log_printf(log_error,
+						   "parameter missing for execute operation");
+				return 1;
+			}
 
-    default:
-      log_printf(log_error, "Internal error: Unknown operation (%d)", pc.operation);
-      return 1;
-    }
+			return cpbuilder_execute(&pc, &av[optind]);
+
+		case pbuilder_help:
+			return cpbuilder_help();
+
+		case pbuilder_dumpconfig:
+			return cpbuilder_dumpconfig(&pc);
+
+		default:
+			log_printf(log_error,
+					   "Internal error: Unknown operation (%d)",
+					   pc.operation);
+			return 1;
+	}
 
-  return 0;
+	return 0;
 }
diff --git a/parameter.h b/parameter.h
index 7260e35..0874f2d 100644
--- a/parameter.h
+++ b/parameter.h
@@ -24,78 +24,76 @@
 
 #define MAX_CUSTOM_FILES 32
 
-enum pbuilder_operation
-{
-  pbuilder_do_nothing=0,
-  pbuilder_help,
-  pbuilder_build,
-  pbuilder_create,
-  pbuilder_update,
-  pbuilder_execute,
-  pbuilder_login,
-  pbuilder_dumpconfig
+enum pbuilder_operation {
+	pbuilder_do_nothing = 0,
+	pbuilder_help,
+	pbuilder_build,
+	pbuilder_create,
+	pbuilder_update,
+	pbuilder_execute,
+	pbuilder_login,
+	pbuilder_dumpconfig
 };
 
-typedef struct pbuilderconfig
-{
-  /* if you edit here, please add to parameter.c: dumpconfig */
-  log_level log_level;
-  int use_colors;
-  int mountproc;
-  int mountdev;
-  int mountdevpts;
-  int save_after_login;
-  int debug;
-  char* buildplace;		/* /var/cache/pbuilder/build/XXX.$$ */
-  char* buildresult;		/* /var/cache/pbuilder/result/ */
-  char* basepath;		/* /var/cache/pbuilder/cow */
-  char* mirror;
-  char* distribution;
-  char* components;
-  char* extrapackages;
-  char* othermirror;
-  char* hookdir;
-  char* debbuildopts;
-  int binary_arch;
-  int binary_indep;
-  char* http_proxy;
-  int allow_untrusted;
-
-  /* files to be copied into the chroot,
-     and copied out of the chroot */
-  char* inputfile[MAX_CUSTOM_FILES+1];
-  char* outputfile[MAX_CUSTOM_FILES+1];
-
-  int buildresultuid;
-  int buildresultgid;
-
-  /* cow-specific options */
-  int no_cowdancer_update;		/* --no-cowdancer-update */
-  int debian_etch_workaround;		/* --debian-etch-workaround */
-
-  /* more qemu-isque options */
-  char* kernel_image;
-  char* initrd;
-  char* smp;
-  int memory_megs;		/* megabytes of memory */
-  char* arch;
-  char* arch_diskdevice;
-
-  enum pbuilder_operation operation;
+typedef struct pbuilderconfig {
+	/* if you edit here, please add to parameter.c: dumpconfig */
+	log_level log_level;
+	int use_colors;
+	int mountproc;
+	int mountdev;
+	int mountdevpts;
+	int save_after_login;
+	int debug;
+	char *buildplace;  /* /var/cache/pbuilder/build/XXX.$$ */
+	char *buildresult; /* /var/cache/pbuilder/result/ */
+	char *basepath;	/* /var/cache/pbuilder/cow */
+	char *mirror;
+	char *distribution;
+	char *components;
+	char *extrapackages;
+	char *othermirror;
+	char *hookdir;
+	char *debbuildopts;
+	int binary_arch;
+	int binary_indep;
+	char *http_proxy;
+	int allow_untrusted;
+
+	/* files to be copied into the chroot,
+	 * and copied out of the chroot */
+	char *inputfile[MAX_CUSTOM_FILES + 1];
+	char *outputfile[MAX_CUSTOM_FILES + 1];
+
+	int buildresultuid;
+	int buildresultgid;
+
+	/* cow-specific options */
+	int no_cowdancer_update;	/* --no-cowdancer-update */
+	int debian_etch_workaround; /* --debian-etch-workaround */
+
+	/* more qemu-isque options */
+	char *kernel_image;
+	char *initrd;
+	char *smp;
+	int memory_megs; /* megabytes of memory */
+	char *arch;
+	char *arch_diskdevice;
+
+	enum pbuilder_operation operation;
 } pbuilderconfig;
 
-int load_config_file(const char* config, pbuilderconfig* pc);
-int size_of_ntarray(char ** buf);
-int forkexeclp (const char *path, const char *arg0, ...);
-int forkexecvp (char *const argv[]);
-int parse_parameter(int ac, char** av, const char* keyword);
-int cpbuilder_check_config(const struct pbuilderconfig* pc);
-int cpbuilder_build(const struct pbuilderconfig* pc, const char* dscfile);
-int cpbuilder_login(const struct pbuilderconfig* pc);
-int cpbuilder_execute(const struct pbuilderconfig* pc, char** av);
-int cpbuilder_update(const struct pbuilderconfig* pc);
+int load_config_file(const char *config, pbuilderconfig *pc);
+int size_of_ntarray(char **buf);
+int forkexeclp(const char *path, const char *arg0, ...);
+int forkexecvp(char *const argv[]);
+int parse_parameter(int ac, char **av, const char *keyword);
+int cpbuilder_check_config(const struct pbuilderconfig *pc);
+int cpbuilder_build(const struct pbuilderconfig *pc, const char *dscfile);
+int cpbuilder_login(const struct pbuilderconfig *pc);
+int cpbuilder_execute(const struct pbuilderconfig *pc, char **av);
+int cpbuilder_update(const struct pbuilderconfig *pc);
 int cpbuilder_help(void);
-int cpbuilder_create(const struct pbuilderconfig* pc);
+int cpbuilder_create(const struct pbuilderconfig *pc);
 
 /*
 
@@ -110,12 +108,15 @@ PBUILDER_ADD_PARAM(NULL);
 
  */
 #define MAXPBUILDERCOMMANDLINE 256
-#define PBUILDER_ADD_PARAM(a) \
- if(offset<(MAXPBUILDERCOMMANDLINE-1)) \
- {pbuildercommandline[offset++]=a;} \
- else \
- {pbuildercommandline[offset]=NULL; log_printf(log_error, "pbuilder-command-line: Max command-line exceeded\n");}
-extern char* pbuildercommandline[MAXPBUILDERCOMMANDLINE];
+#define PBUILDER_ADD_PARAM(a)                                                  \
+	if (offset < (MAXPBUILDERCOMMANDLINE - 1)) {                               \
+		pbuildercommandline[offset++] = a;                                     \
+	} else {                                                                   \
+		pbuildercommandline[offset] = NULL;                                    \
+		log_printf(log_error,                                                  \
+				   "pbuilder-command-line: Max command-line exceeded\n");      \
+	}
+extern char *pbuildercommandline[MAXPBUILDERCOMMANDLINE];
 extern int offset;
 
 /*
@@ -134,21 +135,19 @@ DEBOOTSTRAP_ADD_PARAM(NULL);
 
  */
 #define MAX_DEBOOTSTRAP_COMMAND_LINE 256
-#define DEBOOTSTRAP_ADD_PARAM(a) \
-  do \
-    { \
-      if (debootstrap_param_offset < MAX_DEBOOTSTRAP_COMMAND_LINE-1) \
-	debootstrap_command_line[debootstrap_param_offset++] = a; \
-      else \
-	{ \
-	  debootstrap_command_line[debootstrap_param_offset] = NULL; \
-	  log_printf(log_error, "debootstrap_command_line: Max command-line length exceeded"); \
-	} \
-    } \
-  while (0)
-
-extern char* debootstrap_command_line[MAX_DEBOOTSTRAP_COMMAND_LINE];
+#define DEBOOTSTRAP_ADD_PARAM(a)                                               \
+	do {                                                                       \
+		if (debootstrap_param_offset < MAX_DEBOOTSTRAP_COMMAND_LINE - 1)       \
+			debootstrap_command_line[debootstrap_param_offset++] = a;          \
+		else {                                                                 \
+			debootstrap_command_line[debootstrap_param_offset] = NULL;         \
+			log_printf(                                                        \
+				log_error,                                                     \
+				"debootstrap_command_line: Max command-line length exceeded"); \
+		}                                                                      \
+	} while (0)
+
+extern char *debootstrap_command_line[MAX_DEBOOTSTRAP_COMMAND_LINE];
 extern int debootstrap_param_offset;
 
 #endif
-
diff --git a/qemuarch.c b/qemuarch.c
index 3f90b8e..298de8c 100644
--- a/qemuarch.c
+++ b/qemuarch.c
@@ -42,165 +42,149 @@
 /**
  * arch-specific routine; disk device name to use
  */
-const char* qemu_arch_diskdevice(const struct pbuilderconfig* pc)
-{
-  if (pc->arch_diskdevice)
-    return pc->arch_diskdevice;
-  return "sd";
+const char *qemu_arch_diskdevice(const struct pbuilderconfig *pc) {
+	if (pc->arch_diskdevice)
+		return pc->arch_diskdevice;
+	return "sd";
 }
 
 /**
  * arch-specific routine; the console device to make
  */
- const int qemu_create_arch_serialdevice(const char* basedir, const char* arch)
-{
-  dev_t consoledev;
-
-  if (!strcmp(arch, "arm") ||
-      !strcmp(arch, "armel") ||
-      !strcmp(arch, "armhf") ||
-      !strcmp(arch, "arm64"))
-    consoledev = makedev(204, 64);
-  else
-    consoledev = makedev(4, 64);
-
-  return mknod_inside_chroot(basedir, "dev/console", S_IFCHR | 0660, consoledev);
+const int qemu_create_arch_serialdevice(const char *basedir, const char *arch) {
+	dev_t consoledev;
+
+	if (!strcmp(arch, "arm") || !strcmp(arch, "armel") ||
+		!strcmp(arch, "armhf") || !strcmp(arch, "arm64"))
+		consoledev = makedev(204, 64);
+	else
+		consoledev = makedev(4, 64);
+
+	return mknod_inside_chroot(
+		basedir, "dev/console", S_IFCHR | 0660, consoledev);
 }
 
 /**
  * arch-specific routine; make device files inside chroot
  */
-const int qemu_create_arch_devices(const char* basedir, const char* arch)
-{
-  int ret=0;
-  char* s=0;
-
-  asprintf(&s, "%s/%s", basedir, "dev");
-  if (-1==mkdir(s, 0777))
-    {
-      log_perror("mkdir chroot-/dev");
-      ret=1;
-    }
-  free(s);
-
-  ret+=qemu_create_arch_serialdevice(basedir, arch);
-  ret+=mknod_inside_chroot(basedir, "dev/ttyS0", S_IFCHR | 0660, makedev(4, 64));
-  ret+=mknod_inside_chroot(basedir, "dev/ttyAMA0", S_IFCHR | 0660, makedev(204, 64));
-  ret+=mknod_inside_chroot(basedir, "dev/sda", S_IFBLK | 0660, makedev(8, 0));
-  ret+=mknod_inside_chroot(basedir, "dev/sdb", S_IFBLK | 0660, makedev(8, 16));
-  ret+=mknod_inside_chroot(basedir, "dev/hda", S_IFBLK | 0660, makedev(3, 0));
-  ret+=mknod_inside_chroot(basedir, "dev/hdb", S_IFBLK | 0660, makedev(3, 64));
-  return ret;
+const int qemu_create_arch_devices(const char *basedir, const char *arch) {
+	int ret = 0;
+	char *s = 0;
+
+	asprintf(&s, "%s/%s", basedir, "dev");
+	if (-1 == mkdir(s, 0777)) {
+		log_perror("mkdir chroot-/dev");
+		ret = 1;
+	}
+	free(s);
+
+	ret += qemu_create_arch_serialdevice(basedir, arch);
+	ret += mknod_inside_chroot(
+		basedir, "dev/ttyS0", S_IFCHR | 0660, makedev(4, 64));
+	ret += mknod_inside_chroot(
+		basedir, "dev/ttyAMA0", S_IFCHR | 0660, makedev(204, 64));
+	ret +=
+		mknod_inside_chroot(basedir, "dev/sda", S_IFBLK | 0660, makedev(8, 0));
+	ret +=
+		mknod_inside_chroot(basedir, "dev/sdb", S_IFBLK | 0660, makedev(8, 16));
+	ret +=
+		mknod_inside_chroot(basedir, "dev/hda", S_IFBLK | 0660, makedev(3, 0));
+	ret +=
+		mknod_inside_chroot(basedir, "dev/hdb", S_IFBLK | 0660, makedev(3, 64));
+	return ret;
 }
 
 /**
  * get output of dpkg --print-architecture
  * returns a malloc'd string, you need to free it.
  */
-char* get_host_dpkg_arch()
-{
-  FILE*f=popen("dpkg --print-architecture", "r");
-  char*host_arch;
-  fscanf(f, "%ms", &host_arch);
-  pclose(f);
-  return host_arch;
+char *get_host_dpkg_arch() {
+	FILE *f = popen("dpkg --print-architecture", "r");
+	char *host_arch;
+	fscanf(f, "%ms", &host_arch);
+	pclose(f);
+	return host_arch;
 }
 
 /**
  * arch-specific routine; qemu command to use.
  */
-const char* qemu_arch_qemu(const char* arch)
-{
-  if (!strcmp(arch, "arm") ||
-      !strcmp(arch, "armel") ||
-      !strcmp(arch, "armhf"))
-    return "qemu-system-arm";
-  else if (!strcmp(arch, "arm64"))
-    return "qemu-system-aarch64";
-  else if (!strcmp(arch, "mips"))
-    return "qemu-system-mips";
-  else if (!strcmp(arch, "mipsel"))
-    return "qemu-system-mipsel";
-  else if (!strcmp(arch, "i386")
-        || !strcmp(arch, "hurd-i386")
-        || !strcmp(arch, "amd64")
-        || !strcmp(arch, "x32"))
-    {
-      /* we're leaking this memory, but don't care too much */
-      char* host_arch = get_host_dpkg_arch();
-
-      /* special-case
-	 use kvm if possible
-	 use qemu if i386/i386
-	 use qemu-system-x86_64 otherwise
-       */
-      int kvm_exists = !system("which kvm");
-
-      if(!strcmp(host_arch,arch) && kvm_exists)
-	return "kvm";
-      else if(!strcmp(host_arch, "amd64") && kvm_exists)
-        return "kvm";
-      else if((!strcmp(host_arch, "i386")) && (!strcmp(arch, "i386")))
-	return "qemu";
-      else
-	return "qemu-system-x86_64";
-    }
-  else if (!strcmp(arch, "hurd-i386"))
-    return "qemu";
-  else if (!strcmp(arch, "powerpc"))
-    return "qemu-system-ppc";
-  else if (!strcmp(arch, "sparc"))
-    return "qemu-system-sparc";
-  else
-    return NULL;
+const char *qemu_arch_qemu(const char *arch) {
+	if (!strcmp(arch, "arm") || !strcmp(arch, "armel") ||
+		!strcmp(arch, "armhf"))
+		return "qemu-system-arm";
+	else if (!strcmp(arch, "arm64"))
+		return "qemu-system-aarch64";
+	else if (!strcmp(arch, "mips"))
+		return "qemu-system-mips";
+	else if (!strcmp(arch, "mipsel"))
+		return "qemu-system-mipsel";
+	else if (!strcmp(arch, "i386") || !strcmp(arch, "hurd-i386") ||
+			 !strcmp(arch, "amd64") || !strcmp(arch, "x32")) {
+		/* we're leaking this memory, but don't care too much */
+		char *host_arch = get_host_dpkg_arch();
+
+		/* special-case
+		 * use kvm if possible
+		 * use qemu if i386/i386
+		 * use qemu-system-x86_64 otherwise
+		 */
+		int kvm_exists = !system("which kvm");
+
+		if (!strcmp(host_arch, arch) && kvm_exists)
+			return "kvm";
+		else if (!strcmp(host_arch, "amd64") && kvm_exists)
+			return "kvm";
+		else if ((!strcmp(host_arch, "i386")) && (!strcmp(arch, "i386")))
+			return "qemu";
+		else
+			return "qemu-system-x86_64";
+	} else if (!strcmp(arch, "hurd-i386"))
+		return "qemu";
+	else if (!strcmp(arch, "powerpc"))
+		return "qemu-system-ppc";
+	else if (!strcmp(arch, "sparc"))
+		return "qemu-system-sparc";
+	else
+		return NULL;
 }
 
 /**
  * arch-specific routine; the machine spec for this arch
  */
-const char* qemu_arch_qemumachine(const char* arch)
-{
-  if (!strcmp(arch, "arm") ||
-      !strcmp(arch, "armel"))
-    return "versatilepb";
-  else if (!strcmp(arch, "armhf") ||
-	   !strcmp(arch, "arm64"))
-    return "virt";
-  else if (!strcmp(arch, "i386") ||
-	   !strcmp(arch, "hurd-i386") ||
-	   !strcmp(arch, "amd64") ||
-	   !strcmp(arch, "x32"))
-    return "pc";
-  else if (!strcmp(arch, "mips")||
-	   !strcmp(arch, "mipsel"))
-    return "malta";
-  else if (!strcmp(arch, "powerpc"))
-    return "prep";
-  else if (!strcmp(arch, "sparc"))
-    return "SS-5";
-  return NULL;
+const char *qemu_arch_qemumachine(const char *arch) {
+	if (!strcmp(arch, "arm") || !strcmp(arch, "armel"))
+		return "versatilepb";
+	else if (!strcmp(arch, "armhf") || !strcmp(arch, "arm64"))
+		return "virt";
+	else if (!strcmp(arch, "i386") || !strcmp(arch, "hurd-i386") ||
+			 !strcmp(arch, "amd64") || !strcmp(arch, "x32"))
+		return "pc";
+	else if (!strcmp(arch, "mips") || !strcmp(arch, "mipsel"))
+		return "malta";
+	else if (!strcmp(arch, "powerpc"))
+		return "prep";
+	else if (!strcmp(arch, "sparc"))
+		return "SS-5";
+	return NULL;
 }
 
 /**
  * arch-specific routine; the serial device
  */
-const char* qemu_arch_tty(const char* arch)
-{
-
-  if (!strcmp(arch, "arm")||
-      !strcmp(arch, "armel") ||
-      !strcmp(arch, "armhf") ||
-      !strcmp(arch, "arm64"))
-    return "ttyAMA0,115200n8";
-  return "ttyS0,115200n8";
+const char *qemu_arch_tty(const char *arch) {
+
+	if (!strcmp(arch, "arm") || !strcmp(arch, "armel") ||
+		!strcmp(arch, "armhf") || !strcmp(arch, "arm64"))
+		return "ttyAMA0,115200n8";
+	return "ttyS0,115200n8";
 }
 
 /**
  * arch-specific routine; the CPU
  */
-const char* qemu_arch_cpu(const char* arch)
-{
-  if (!strcmp(arch, "arm64"))
-    return "cortex-a57";
-  return NULL;
+const char *qemu_arch_cpu(const char *arch) {
+	if (!strcmp(arch, "arm64"))
+		return "cortex-a57";
+	return NULL;
 }
diff --git a/qemuarch.h b/qemuarch.h
index f8eee9c..5440f1c 100644
--- a/qemuarch.h
+++ b/qemuarch.h
@@ -7,13 +7,13 @@
 
 #include "parameter.h"
 
-const char* qemu_arch_diskdevice(const struct pbuilderconfig* pc);
-const int qemu_create_arch_serialdevice(const char* basedir, const char* arch);
-const int qemu_create_arch_devices(const char* basedir, const char* arch);
-char* get_host_dpkg_arch();
-const char* qemu_arch_qemu(const char* arch);
-const char* qemu_arch_qemumachine(const char* arch);
-const char* qemu_arch_tty(const char* arch);
-const char* qemu_arch_cpu(const char* arch);
+const char *qemu_arch_diskdevice(const struct pbuilderconfig *pc);
+const int qemu_create_arch_serialdevice(const char *basedir, const char *arch);
+const int qemu_create_arch_devices(const char *basedir, const char *arch);
+char *get_host_dpkg_arch();
+const char *qemu_arch_qemu(const char *arch);
+const char *qemu_arch_qemumachine(const char *arch);
+const char *qemu_arch_tty(const char *arch);
+const char *qemu_arch_cpu(const char *arch);
 
 #endif
diff --git a/qemubuilder.c b/qemubuilder.c
old mode 100755
new mode 100644
index 952f73e..96ad028
--- a/qemubuilder.c
+++ b/qemubuilder.c
@@ -85,7 +85,7 @@ indent"fi\n"
  * END OF WORK EXIT CODE=0
  * END OF WORK EXIT CODE=16
  */
-const char* qemu_keyword="END OF WORK EXIT CODE=";
+const char *qemu_keyword = "END OF WORK EXIT CODE=";
 
 /** create a sparse ext3 block device suitable for
     loop-mount.
@@ -94,55 +94,52 @@ const char* qemu_keyword="END OF WORK EXIT CODE=";
 
    @returns -1 on error, 0 on success
  */
-static int create_ext3_block_device(const char* filename,
-				    unsigned long int gigabyte)
-{
-  int ret = 0;
-  char *s = NULL;
-  char *s2 = NULL;
-
-  /* create 10GB sparse data */
-  if (create_sparse_file(filename,
-			 gigabyte*1UL<<30UL))
-    {
-      ret=-1;
-      goto out;
-    }
-
-  if ((ret=forkexeclp("mke2fs", "mke2fs",
-		      "-q",
-		      "-F",
-		      "-j",
-		      "-m1",
-		      "-O", "sparse_super",
-		      filename, NULL)))
-    {
-      ret=-1;
-      goto out;
-    }
-
-  if ((ret=forkexeclp("tune2fs", "tune2fs",
-		      "-c", "0",
-		      "-i", "0",
-		      filename, NULL)))
-    {
-      ret=-1;
-    }
-
- out:
-  if(s) free(s);
-  if(s2) free(s2);
-  return ret;
+static int create_ext3_block_device(const char *filename,
+									unsigned long int gigabyte) {
+	int ret = 0;
+	char *s = NULL;
+	char *s2 = NULL;
+
+	/* create 10GB sparse data */
+	if (create_sparse_file(filename, gigabyte * 1UL << 30UL)) {
+		ret = -1;
+		goto out;
+	}
+
+	if ((ret = forkexeclp("mke2fs",
+						  "mke2fs",
+						  "-q",
+						  "-F",
+						  "-j",
+						  "-m1",
+						  "-O",
+						  "sparse_super",
+						  filename,
+						  NULL))) {
+		ret = -1;
+		goto out;
+	}
+
+	if ((ret = forkexeclp(
+			 "tune2fs", "tune2fs", "-c", "0", "-i", "0", filename, NULL))) {
+		ret = -1;
+	}
+
+out:
+	if (s)
+		free(s);
+	if (s2)
+		free(s2);
+	return ret;
 }
 
 /** loopback mount file system.
     @returns 0 on success
 */
-static int loop_mount(const char* device, const char* mountpoint)
-{
-  int ret=forkexeclp("mount", "mount", "-o", "loop",
-	     device, mountpoint, NULL);
-  return ret;
+static int loop_mount(const char *device, const char *mountpoint) {
+	int ret =
+		forkexeclp("mount", "mount", "-o", "loop", device, mountpoint, NULL);
+	return ret;
 }
 
 /**
@@ -150,279 +147,238 @@ static int loop_mount(const char* device, const char* mountpoint)
 
    @returns 0 on success
  */
-static int loop_umount(const char* device)
-{
-  int ret=forkexeclp("umount", "umount", device,
-		     NULL);
-  return ret;
+static int loop_umount(const char *device) {
+	int ret = forkexeclp("umount", "umount", device, NULL);
+	return ret;
 }
 
 /** create a script file.
 
 @returns NULL on failure, FILE* on success
 */
-static FILE* create_script(const char* mountpoint, const char* relative_path)
-{
-  char *s = NULL;
-  FILE *f = NULL;
-  FILE *ret = NULL;
-
-  asprintf(&s, "%s/%s", mountpoint, relative_path);
-
-  /* Create directories in relative_path */
-  for (char *c = s + strlen(mountpoint) + 1; *c; ++c)
-    {
-      if (*c != '/') continue;
-      *c = 0;
-      if (mkdir(s, 0777) && errno != EEXIST)
-	{
-	  log_printf(log_error,
-              "Could not create directory '%s': %s",
-              s,
-              strerror(errno));
-	  goto fail;
+static FILE *create_script(const char *mountpoint, const char *relative_path) {
+	char *s = NULL;
+	FILE *f = NULL;
+	FILE *ret = NULL;
+
+	asprintf(&s, "%s/%s", mountpoint, relative_path);
+
+	/* Create directories in relative_path */
+	for (char *c = s + strlen(mountpoint) + 1; *c; ++c) {
+		if (*c != '/')
+			continue;
+		*c = 0;
+		if (mkdir(s, 0777) && errno != EEXIST) {
+			log_printf(log_error,
+					   "Could not create directory '%s': %s",
+					   s,
+					   strerror(errno));
+			goto fail;
+		}
+		*c = '/';
 	}
-      *c = '/';
-    }
-
-  if(!(f=fopen(s, "w")))
-    goto fail;
-  if(chmod(s, 0700))
-    {
-      fclose(f);
-      goto fail;
-    }
-  ret=f;
- fail:
-  free(s);
-  return ret;
+
+	if (!(f = fopen(s, "w")))
+		goto fail;
+	if (chmod(s, 0700)) {
+		fclose(f);
+		goto fail;
+	}
+	ret = f;
+fail:
+	free(s);
+	return ret;
 }
 
 static struct termios saved_termios;
 
-static void save_termios(void)
-{
-  if (isatty(1))
-    {
-      tcgetattr(1, &saved_termios);
-    }
+static void save_termios(void) {
+	if (isatty(1)) {
+		tcgetattr(1, &saved_termios);
+	}
 }
 
-static void restore_termios(void)
-{
-  if (isatty(1))
-    {
-      tcsetattr(1, TCSANOW, &saved_termios);
-    }
+static void restore_termios(void) {
+	if (isatty(1)) {
+		tcsetattr(1, TCSANOW, &saved_termios);
+	}
 }
 
-static int copy_file_contents_to_temp(const char* orig,
-				      const char* tempdir,
-				      const char* tempname)
-{
-  char* temppath;
-  int ret;
-  if (tempname == NULL)
-    tempname = basename(orig);
-
-  asprintf(&temppath, "%s/%s", tempdir, tempname);
-  ret=copy_file(orig, temppath);
-  if (ret == -1)
-    {
-      log_printf(log_error, "Copy file error in %s to %s",
-          orig, temppath);
-      goto out;
-    }
+static int copy_file_contents_to_temp(const char *orig,
+									  const char *tempdir,
+									  const char *tempname) {
+	char *temppath;
+	int ret;
+	if (tempname == NULL)
+		tempname = basename(orig);
+
+	asprintf(&temppath, "%s/%s", tempdir, tempname);
+	ret = copy_file(orig, temppath);
+	if (ret == -1) {
+		log_printf(log_error, "Copy file error in %s to %s", orig, temppath);
+		goto out;
+	}
 out:
-  free(temppath);
-  return ret;
+	free(temppath);
+	return ret;
 }
 
 static int copy_file_contents_in_temp(FILE *f,
-				      const char* tempname,
-				      const char* targetdir,
-				      const char* targetname)
-{
-  int trailing_slash = targetdir[strlen(targetdir)-1] == '/';
-  const char* sep = trailing_slash ? "" : "/";
-  fprintf(f,
-	  "log.i \"copying %s%s%s from temporary location\"\n"
-	  "mkdir -p %s\n"
-	  "cp $BUILDDIR/%s %s%s%s || log.e \"Copy failed\"\n",
-	  targetdir, sep, targetname,
-	  targetdir,
-	  tempname,
-	  targetdir, sep, targetname);
-  return 0;
+									  const char *tempname,
+									  const char *targetdir,
+									  const char *targetname) {
+	int trailing_slash = targetdir[strlen(targetdir) - 1] == '/';
+	const char *sep = trailing_slash ? "" : "/";
+	fprintf(f,
+			"log.i \"copying %s%s%s from temporary location\"\n"
+			"mkdir -p %s\n"
+			"cp $BUILDDIR/%s %s%s%s || log.e \"Copy failed\"\n",
+			targetdir,
+			sep,
+			targetname,
+			targetdir,
+			tempname,
+			targetdir,
+			sep,
+			targetname);
+	return 0;
 }
 
-static int copy_file_contents_through_temp(FILE* f,
-					   const char* orig,
-					   const char* tempdir,
-					   const char* targetdir)
-{
-  int ret;
-  char* file_basename = basename(orig);
-  char* tempname = NULL;
-  char* tempinput = NULL;
-  if (0>asprintf(&tempinput,
-		 "%s/input",
-		 tempdir))
-    {
-      log_printf(log_error,
-	  "failed to allocate string for '%s/input': %s",
-	  tempdir,
-	  strerror(errno));
-      ret = 1;
-      goto out;
-    }
-
-  if (0>asprintf(&tempname,
-		 "input/%s",
-		 file_basename))
-    {
-      log_printf(log_error,
-	  "failed to allocate string for 'input/%s': %s",
-	  file_basename,
-	  strerror(errno));
-      ret = 1;
-      goto out;
-    }
-
-  if (mkdir(tempinput, 0777) && errno != EEXIST)
-    {
-      log_printf(log_error,
-	  "failed to create directory '%s': %s",
-	  tempinput,
-	  strerror(errno));
-      ret = 1;
-      goto out;
-    }
-
-  ret = copy_file_contents_to_temp(orig, tempinput, file_basename);
-  if (ret != 0)
-    goto out;
-
-  ret = copy_file_contents_in_temp(f, tempname, targetdir, file_basename);
+static int copy_file_contents_through_temp(FILE *f,
+										   const char *orig,
+										   const char *tempdir,
+										   const char *targetdir) {
+	int ret;
+	char *file_basename = basename(orig);
+	char *tempname = NULL;
+	char *tempinput = NULL;
+	if (0 > asprintf(&tempinput, "%s/input", tempdir)) {
+		log_printf(log_error,
+				   "failed to allocate string for '%s/input': %s",
+				   tempdir,
+				   strerror(errno));
+		ret = 1;
+		goto out;
+	}
+
+	if (0 > asprintf(&tempname, "input/%s", file_basename)) {
+		log_printf(log_error,
+				   "failed to allocate string for 'input/%s': %s",
+				   file_basename,
+				   strerror(errno));
+		ret = 1;
+		goto out;
+	}
+
+	if (mkdir(tempinput, 0777) && errno != EEXIST) {
+		log_printf(log_error,
+				   "failed to create directory '%s': %s",
+				   tempinput,
+				   strerror(errno));
+		ret = 1;
+		goto out;
+	}
+
+	ret = copy_file_contents_to_temp(orig, tempinput, file_basename);
+	if (ret != 0)
+		goto out;
+
+	ret = copy_file_contents_in_temp(f, tempname, targetdir, file_basename);
 
 out:
-  if (tempname != NULL)
-    {
-      free(tempname);
-    }
-  if (tempinput != NULL)
-    {
-      free(tempinput);
-    }
-  return ret;
+	if (tempname != NULL) {
+		free(tempname);
+	}
+	if (tempinput != NULL) {
+		free(tempinput);
+	}
+	return ret;
 }
 
-static int copy_hookdir(const char *hookdir, const char *tmp)
-{
-  int ret = 0;
-  struct dirent *dirp;
-  DIR *hd = opendir(hookdir);
-  char *hookstmp = NULL;
-  if (0>asprintf(&hookstmp,
-		 "%s/hooks",
-		 tmp))
-    {
-      log_printf(log_error,
-	  "Error allocating string for '%s/hooks': %s",
-	  tmp,
-	  strerror(errno));
-      ret = 1;
-      goto out;
-    }
-
-  if (mkdir(hookstmp,0777))
-    {
-      log_printf(log_error,
-	  "Error creating directory for hooks: %s",
-	  strerror(errno));
-      ret = 1;
-      goto out;
-    }
-
-  if (hd == NULL)
-    {
-      log_printf(log_error,
-	  "Error copying hooks from '%s': %s",
-	  hookdir,
-	  strerror(errno));
-      ret = 1;
-      goto out;
-    }
-
-  while ((dirp = readdir(hd)) != NULL)
-    {
-      struct stat st;
-      char *src = NULL;
-
-      if (0>asprintf(&src,
-		     "%s/%s",
-		     hookdir,
-		     dirp->d_name))
-	{
-	  log_printf(log_error,
-	      "Error allocating string for '%s/%s': %s",
-	      hookdir,
-	      dirp->d_name,
-	      strerror(errno));
-	  ret = 1;
-	  goto out;
+static int copy_hookdir(const char *hookdir, const char *tmp) {
+	int ret = 0;
+	struct dirent *dirp;
+	DIR *hd = opendir(hookdir);
+	char *hookstmp = NULL;
+	if (0 > asprintf(&hookstmp, "%s/hooks", tmp)) {
+		log_printf(log_error,
+				   "Error allocating string for '%s/hooks': %s",
+				   tmp,
+				   strerror(errno));
+		ret = 1;
+		goto out;
 	}
-      if (0>stat(src, &st))
-	{
-	  log_printf(log_error,
-	      "Error calling stat '%s': %s",
-	      src,
-	      strerror(errno));
-	  free(src);
-	  ret = 1;
-	  goto out;
+
+	if (mkdir(hookstmp, 0777)) {
+		log_printf(log_error,
+				   "Error creating directory for hooks: %s",
+				   strerror(errno));
+		ret = 1;
+		goto out;
 	}
 
-      if ((st.st_mode & S_IFMT) != S_IFREG)
-	{
-	  // Not a regular file
-	  free(src);
-	  continue;
+	if (hd == NULL) {
+		log_printf(log_error,
+				   "Error copying hooks from '%s': %s",
+				   hookdir,
+				   strerror(errno));
+		ret = 1;
+		goto out;
 	}
 
-      copy_file_contents_to_temp(src, hookstmp, basename(src));
-      free(src);
-    }
+	while ((dirp = readdir(hd)) != NULL) {
+		struct stat st;
+		char *src = NULL;
+
+		if (0 > asprintf(&src, "%s/%s", hookdir, dirp->d_name)) {
+			log_printf(log_error,
+					   "Error allocating string for '%s/%s': %s",
+					   hookdir,
+					   dirp->d_name,
+					   strerror(errno));
+			ret = 1;
+			goto out;
+		}
+		if (0 > stat(src, &st)) {
+			log_printf(
+				log_error, "Error calling stat '%s': %s", src, strerror(errno));
+			free(src);
+			ret = 1;
+			goto out;
+		}
+
+		if ((st.st_mode & S_IFMT) != S_IFREG) {
+			// Not a regular file
+			free(src);
+			continue;
+		}
+
+		copy_file_contents_to_temp(src, hookstmp, basename(src));
+		free(src);
+	}
 
 out:
-  if (hookstmp != NULL)
-    {
-      free(hookstmp);
-    }
-  if (hd != NULL)
-    {
-      closedir(hd);
-    }
-  return ret;
+	if (hookstmp != NULL) {
+		free(hookstmp);
+	}
+	if (hd != NULL) {
+		closedir(hd);
+	}
+	return ret;
 }
 
-static const char* format_for_image(const char* file)
-{
-  static const char *formats[][2] = {
-    { ".cowdev", "qcow2" },
-    { ".dev", "raw" },
-    { ".qemu", "raw" }
-  };
-  int file_len = strlen(file);
-  for (int i = 0; i < sizeof(formats) / sizeof(formats[0]); ++i)
-    {
-      const char *ext = formats[i][0];
-      const char *format = formats[i][1];
-      int ext_len = strlen(ext);
-      if (file_len >= ext_len && !strcmp(ext, file + file_len - ext_len))
-	return format;
-    }
-  return NULL;
+static const char *format_for_image(const char *file) {
+	static const char *formats[][2] = {
+		{".cowdev", "qcow2"}, {".dev", "raw"}, {".qemu", "raw"}};
+	int file_len = strlen(file);
+	for (int i = 0; i < sizeof(formats) / sizeof(formats[0]); ++i) {
+		const char *ext = formats[i][0];
+		const char *format = formats[i][1];
+		int ext_len = strlen(ext);
+		if (file_len >= ext_len && !strcmp(ext, file + file_len - ext_len))
+			return format;
+	}
+	return NULL;
 }
 
 /**
@@ -433,415 +389,404 @@ static const char* format_for_image(const char* file)
    -1: error
    0..X: return code from inside qemu
 */
-static int fork_qemu(const char* hda, const char* hdb, const struct pbuilderconfig* pc)
-{
-  pid_t child;
-  int sp[2];
-  fd_set readfds;
-  int exit_code=-1;
-  const int buffer_size=4096;
-  char* buf=malloc(buffer_size);
-  size_t count;
-
-  if (-1==socketpair(AF_UNIX, SOCK_STREAM,0,sp))
-    {
-      /* error handle? */
-      return -1;
-    }
-
-  save_termios();
-
-  fflush(NULL);
-  if ((child=fork()))
-    {
-      /* this is parent process */
-
-      close(sp[1]);
-      close(0);
-
-      FD_ZERO(&readfds);
-      while (1)
-	{
-	  int status;
-	  if (0<waitpid(child, &status, WNOHANG))
-	    {
-	      /* child has exited */
-	      log_printf(log_error, "qemu exited unexpectedly: %d", status);
-	      break;
-	    }
-	  FD_SET(sp[0],&readfds);
-	  if (-1!=(select(sp[0]+1,&readfds, NULL, NULL, NULL)))
-	    {
-	      if (FD_ISSET(sp[0],&readfds))
-		{
-		  void* matchptr;
-
-		  /* data available from qemu */
-
-		  /* sleep a bit to let it buffer-up a bit more. */
-		  usleep(100000);
-
-		  count=read(sp[0],buf,buffer_size);
-
-		  /* this won't work sometimes, but this is a good best-effort thing. */
-		  if ((matchptr=memmem(buf, count,
-				       qemu_keyword, strlen(qemu_keyword)))!=0)
-		    {
-		      exit_code = atoi(matchptr + strlen(qemu_keyword));
-		      log_printf(log_info, "received termination message from inside qemu with exit-code %i, killing child process (qemu:%i)",
-			  exit_code,
-			  child);
-
-		      assert(child != 0);assert(child > 0);
-
-		      if (!kill(child, SIGTERM))
-			log_printf(log_info, "successfully killed qemu");
-		      else
-			log_perror("failed to kill qemu?");
-		      if (-1==waitpid(child, &status, 0))
-			{
-			  log_perror("qemubuilder: waitpid");
-			}
-		      break;
-		    }
-		  write(1,buf,count);
-		}
-	    }
-	  else
-	    {
-	      log_perror("select");
-	      break;
-	    }
+static int
+fork_qemu(const char *hda, const char *hdb, const struct pbuilderconfig *pc) {
+	pid_t child;
+	int sp[2];
+	fd_set readfds;
+	int exit_code = -1;
+	const int buffer_size = 4096;
+	char *buf = malloc(buffer_size);
+	size_t count;
+
+	if (-1 == socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) {
+		/* error handle? */
+		return -1;
 	}
-    }
-  else if(child == 0)
-    {
-      /* this is the child process */
-      const char* qemu = qemu_arch_qemu(pc->arch);
-      const char* machine = qemu_arch_qemumachine(pc->arch);
-      const char* cpu = qemu_arch_cpu(pc->arch);
-      const char* hda_format = format_for_image(hda);
-      const char* hdb_format = format_for_image(hdb);
-      char* hda_command;
-      char* hdb_command;
-      int virt;
-      char* append_command;
-      const char* kernel_image = pc->kernel_image;
-      const char* initrd = pc->initrd;
-      char* mem;
-      int argc = 0;
-      const int MAX_ARGS = 40;
-      char *argv[MAX_ARGS];
-      int i;
-      int is_tty = isatty(1);
-      const char *term;
-      if (is_tty)
-	{
-	  term = getenv("TERM");
-	  if (!term)
-	    term = ""; /* Use whatever getty defaults to */
-	}
-      else
-	term = "dumb";
 
-      if (qemu == NULL || machine == NULL) {
-	log_printf(log_error, "Your architecture %s does not seem to be supported", pc->arch);
-	exit(1);
-      }
+	save_termios();
 
-      if (kernel_image == NULL || !strcmp(kernel_image, "")) {
-	log_printf(log_error, "No KERNEL_IMAGE defined in pbuilderrc");
-	exit(1);
-      }
+	fflush(NULL);
+	if ((child = fork())) {
+		/* this is parent process */
 
-      asprintf(&mem, "%i", pc->memory_megs);
+		close(sp[1]);
+		close(0);
 
-      if (hda_format == NULL)
-	{
-	  log_printf(log_error, "Unknown format for disk image %s", hda);
-	  exit(1);
-	}
+		FD_ZERO(&readfds);
+		while (1) {
+			int status;
+			if (0 < waitpid(child, &status, WNOHANG)) {
+				/* child has exited */
+				log_printf(log_error, "qemu exited unexpectedly: %d", status);
+				break;
+			}
+			FD_SET(sp[0], &readfds);
+			if (-1 != (select(sp[0] + 1, &readfds, NULL, NULL, NULL))) {
+				if (FD_ISSET(sp[0], &readfds)) {
+					void *matchptr;
+
+					/* data available from qemu */
+
+					/* sleep a bit to let it buffer-up a bit more. */
+					usleep(100000);
+
+					count = read(sp[0], buf, buffer_size);
+
+					/* this won't work sometimes, but this is a good best-effort thing. */
+					if ((matchptr = memmem(
+							 buf, count, qemu_keyword, strlen(qemu_keyword))) !=
+						0) {
+						exit_code = atoi(matchptr + strlen(qemu_keyword));
+						log_printf(
+							log_info,
+							"received termination message from inside qemu with exit-code %i, killing child process (qemu:%i)",
+							exit_code,
+							child);
+
+						assert(child != 0);
+						assert(child > 0);
+
+						if (!kill(child, SIGTERM))
+							log_printf(log_info, "successfully killed qemu");
+						else
+							log_perror("failed to kill qemu?");
+						if (-1 == waitpid(child, &status, 0)) {
+							log_perror("qemubuilder: waitpid");
+						}
+						break;
+					}
+					write(1, buf, count);
+				}
+			} else {
+				log_perror("select");
+				break;
+			}
+		}
+	} else if (child == 0) {
+		/* this is the child process */
+		const char *qemu = qemu_arch_qemu(pc->arch);
+		const char *machine = qemu_arch_qemumachine(pc->arch);
+		const char *cpu = qemu_arch_cpu(pc->arch);
+		const char *hda_format = format_for_image(hda);
+		const char *hdb_format = format_for_image(hdb);
+		char *hda_command;
+		char *hdb_command;
+		int virt;
+		char *append_command;
+		const char *kernel_image = pc->kernel_image;
+		const char *initrd = pc->initrd;
+		char *mem;
+		int argc = 0;
+		const int MAX_ARGS = 40;
+		char *argv[MAX_ARGS];
+		int i;
+		int is_tty = isatty(1);
+		const char *term;
+		if (is_tty) {
+			term = getenv("TERM");
+			if (!term)
+				term = ""; /* Use whatever getty defaults to */
+		} else
+			term = "dumb";
+
+		if (qemu == NULL || machine == NULL) {
+			log_printf(log_error,
+					   "Your architecture %s does not seem to be supported",
+					   pc->arch);
+			exit(1);
+		}
 
-      if (hdb_format == NULL)
-	{
-	  log_printf(log_error, "Unknown format for disk image %s", hdb);
-	  exit(1);
-	}
+		if (kernel_image == NULL || !strcmp(kernel_image, "")) {
+			log_printf(log_error, "No KERNEL_IMAGE defined in pbuilderrc");
+			exit(1);
+		}
 
-      virt = !strcmp(machine, "virt");
-
-      asprintf(&hda_command,
-	       "%sfile=%s,format=%s,index=0,media=disk,cache=writeback,id=hd0",
-	       virt ? "if=none," : "",
-	       hda, hda_format);
-
-      asprintf(&hdb_command,
-	       "%sfile=%s,format=%s,index=1,media=disk,cache=writeback,id=hd1",
-	       virt ? "if=none," : "",
-	       hdb, hdb_format);
-
-      /* panic < 0 means reboot immediately on panic; this will actually
-         halt as -no-reboot is given to qemu */
-      asprintf(&append_command,
-	       "root=/dev/%sa quiet init=/usr/bin/setsid console=%s panic=-1 -- -c -w /sbin/getty -n -l /pbuilder-run -8 -L - %s",
-	       qemu_arch_diskdevice(pc),
-	       qemu_arch_tty(pc->arch),
-	       term);
-
-      dup2(sp[1],1);
-      dup2(sp[1],2);
-      close(sp[0]);
-
-      argv[argc++]=strdupa(qemu);
-      argv[argc++]="-nodefaults";
-      argv[argc++]="-nographic";
-      argv[argc++]="-no-reboot"; /* halt instead of rebooting */
-      argv[argc++]="-M";
-      argv[argc++]=strdupa(machine);
-      if (cpu)
-	{
-	  argv[argc++]="-cpu";
-	  argv[argc++]=strdupa(cpu);
-	}
-      argv[argc++]="-m";
-      argv[argc++]=mem;
-      if (pc->smp) {
-	argv[argc++]="-smp";
-	argv[argc++]=strdupa(pc->smp);
-      }
-      argv[argc++]="-kernel";
-      argv[argc++]=strdupa(kernel_image);
-      if (initrd && strcmp(initrd, ""))
-	{
-	  argv[argc++]="-initrd";
-	  argv[argc++]=strdupa(initrd);
-	}
-      argv[argc++]="-drive";
-      argv[argc++]=hda_command;
-      argv[argc++]="-drive";
-      argv[argc++]=hdb_command;
-      argv[argc++]="-append";
-      argv[argc++]=append_command;
-      argv[argc++]="-serial";
-      if (is_tty)
-	{
-	  argv[argc++]="mon:stdio";
-	}
-      else
-	{
-	  argv[argc++]="stdio";
-	}
-      argv[argc++]="-net";
-      argv[argc++]="user";
-      if (virt)
-	{
-	  argv[argc++]="-device";
-	  argv[argc++]="virtio-scsi-device,id=scsi";
-	  argv[argc++]="-device";
-	  argv[argc++]="virtio-net-device,netdev=net0";
-	  argv[argc++]="-device";
-	  argv[argc++]="scsi-hd,drive=hd0";
-	  argv[argc++]="-device";
-	  argv[argc++]="scsi-hd,drive=hd1";
-	  argv[argc++]="-netdev";
-	  argv[argc++]="user,id=net0";
-	}
-      else
-	{
-	  argv[argc++]="-net";
-	  argv[argc++]="nic";
-	}
-      argv[argc]=NULL;
-      assert(argc < MAX_ARGS);
+		asprintf(&mem, "%i", pc->memory_megs);
 
-      log_begin(log_info);
-      log_middle(log_info, "forking qemu:");
-      for (i=0; i<argc; ++i)
-	{
-	  if (strchr(argv[i], ' '))
-	    log_middle(log_info, " '%s'", argv[i]);
-	  else
-	    log_middle(log_info, " %s", argv[i]);
+		if (hda_format == NULL) {
+			log_printf(log_error, "Unknown format for disk image %s", hda);
+			exit(1);
+		}
+
+		if (hdb_format == NULL) {
+			log_printf(log_error, "Unknown format for disk image %s", hdb);
+			exit(1);
+		}
+
+		virt = !strcmp(machine, "virt");
+
+		asprintf(
+			&hda_command,
+			"%sfile=%s,format=%s,index=0,media=disk,cache=writeback,id=hd0",
+			virt ? "if=none," : "",
+			hda,
+			hda_format);
+
+		asprintf(
+			&hdb_command,
+			"%sfile=%s,format=%s,index=1,media=disk,cache=writeback,id=hd1",
+			virt ? "if=none," : "",
+			hdb,
+			hdb_format);
+
+		/* panic < 0 means reboot immediately on panic; this will actually
+		 * halt as -no-reboot is given to qemu */
+		asprintf(
+			&append_command,
+			"root=/dev/%sa quiet init=/usr/bin/setsid console=%s panic=-1 -- -c -w /sbin/getty -n -l /pbuilder-run -8 -L - %s",
+			qemu_arch_diskdevice(pc),
+			qemu_arch_tty(pc->arch),
+			term);
+
+		dup2(sp[1], 1);
+		dup2(sp[1], 2);
+		close(sp[0]);
+
+		argv[argc++] = strdupa(qemu);
+		argv[argc++] = "-nodefaults";
+		argv[argc++] = "-nographic";
+		argv[argc++] = "-no-reboot"; /* halt instead of rebooting */
+		argv[argc++] = "-M";
+		argv[argc++] = strdupa(machine);
+		if (cpu) {
+			argv[argc++] = "-cpu";
+			argv[argc++] = strdupa(cpu);
+		}
+		argv[argc++] = "-m";
+		argv[argc++] = mem;
+		if (pc->smp) {
+			argv[argc++] = "-smp";
+			argv[argc++] = strdupa(pc->smp);
+		}
+		argv[argc++] = "-kernel";
+		argv[argc++] = strdupa(kernel_image);
+		if (initrd && strcmp(initrd, "")) {
+			argv[argc++] = "-initrd";
+			argv[argc++] = strdupa(initrd);
+		}
+		argv[argc++] = "-drive";
+		argv[argc++] = hda_command;
+		argv[argc++] = "-drive";
+		argv[argc++] = hdb_command;
+		argv[argc++] = "-append";
+		argv[argc++] = append_command;
+		argv[argc++] = "-serial";
+		if (is_tty) {
+			argv[argc++] = "mon:stdio";
+		} else {
+			argv[argc++] = "stdio";
+		}
+		argv[argc++] = "-net";
+		argv[argc++] = "user";
+		if (virt) {
+			argv[argc++] = "-device";
+			argv[argc++] = "virtio-scsi-device,id=scsi";
+			argv[argc++] = "-device";
+			argv[argc++] = "virtio-net-device,netdev=net0";
+			argv[argc++] = "-device";
+			argv[argc++] = "scsi-hd,drive=hd0";
+			argv[argc++] = "-device";
+			argv[argc++] = "scsi-hd,drive=hd1";
+			argv[argc++] = "-netdev";
+			argv[argc++] = "user,id=net0";
+		} else {
+			argv[argc++] = "-net";
+			argv[argc++] = "nic";
+		}
+		argv[argc] = NULL;
+		assert(argc < MAX_ARGS);
+
+		log_begin(log_info);
+		log_middle(log_info, "forking qemu:");
+		for (i = 0; i < argc; ++i) {
+			if (strchr(argv[i], ' '))
+				log_middle(log_info, " '%s'", argv[i]);
+			else
+				log_middle(log_info, " %s", argv[i]);
+		}
+		log_end(log_info);
+
+		execvp(argv[0], argv);
+		log_perror("fork_qemu");
+		exit(1);
+	} else {
+		log_perror("fork");
+		return -1;
 	}
-      log_end(log_info);
-
-      execvp(argv[0], argv);
-      log_perror("fork_qemu");
-      exit (1);
-    }
-  else
-    {
-      log_perror("fork");
-      return -1;
-    }
-
-  restore_termios();
-  return exit_code;
+
+	restore_termios();
+	return exit_code;
 }
 
-static int do_fsck(const char* devfile)
-{
-  /* force-running this fsck isn't a good idea; let it fail.
-     If it's mounted by someone else, I don't want to touch it,
-     and chroots can be re-created any time, right?
-   */
-  return forkexeclp("/sbin/fsck",
-		    "/sbin/fsck",
-		    devfile,
-		    NULL);
+static int do_fsck(const char *devfile) {
+	/* force-running this fsck isn't a good idea; let it fail.
+	 * If it's mounted by someone else, I don't want to touch it,
+	 * and chroots can be re-created any time, right?
+	 */
+	return forkexeclp("/sbin/fsck", "/sbin/fsck", devfile, NULL);
 }
 
 /*
    get the current time string which can be used in date command to
    set time inside the chroot.
  */
-static char* get_current_time_string(void)
-{
-  char* locsave, *timestring;
-  time_t currenttime;
-
-  /* save/set/restore locale settings to get current time in POSIX format */
-  locsave = setlocale(LC_TIME, NULL);
-  (void) setlocale(LC_TIME, "POSIX");
-  currenttime=time(NULL);
-  timestring=asctime(gmtime(&currenttime));
-  (void) setlocale(LC_TIME, locsave);
-  return timestring;
+static char *get_current_time_string(void) {
+	char *locsave, *timestring;
+	time_t currenttime;
+
+	/* save/set/restore locale settings to get current time in POSIX format */
+	locsave = setlocale(LC_TIME, NULL);
+	(void)setlocale(LC_TIME, "POSIX");
+	currenttime = time(NULL);
+	timestring = asctime(gmtime(&currenttime));
+	(void)setlocale(LC_TIME, locsave);
+	return timestring;
 }
 
-static void write_first_stage(FILE *f, const struct pbuilderconfig* pc)
-{
-  fprintf(f,
-	  "#!/bin/bash\n"
-	  "echo \n"
-	  /* Can't use log.i or check LOGLEVEL in first stage, since this lives
-	   * inside the base chroot, and has not yet mounted the input disk. */
-	  "echo 'I: qemu-pbuilder first-stage' \n"
-	  "export PBUILDER_INIT_VERSION="XSTR(PBUILDER_INIT_VERSION)"\n"
-	  "export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'\n"
-	  "stty sane\n"
-	  "[ -d /proc/1 ] || mount -n /proc /proc -t proc\n"
-	  "ln -s /dev/shm /run/shm\n"
-	  "mkdir /run/lock\n"
-	  "chmod 01777 /run/lock\n"
-	  "mount -t tmpfs tmpfs /run/lock\n"
-	  "mount -n -o rw,remount / || mount -v -n -o rw,remount /dev/root \n"
-	  "find /tmp -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf --\n"
-	  "mount -t tmpfs tmpfs /tmp\n"
-	  "mkdir /dev/shm\n"
-	  "chmod 01777 /dev/shm\n"
-	  "mount -t tmpfs tmpfs /dev/shm\n"
-	  "ln -fs /proc/mounts /etc/mtab\n"
-	  "export BUILDDIR="BUILDDIR"\n"
-	  "mkdir -p $BUILDDIR\n"
-	  "ln -s $BUILDDIR /tmp/buildd\n"
-	  "mount -n -t ext3 /dev/%sb $BUILDDIR \n"
-	  "$BUILDDIR/input/pbuilder-run \n",
-	  qemu_arch_diskdevice(pc)
-	  );
+static void write_first_stage(FILE *f, const struct pbuilderconfig *pc) {
+	fprintf(
+		f,
+		"#!/bin/bash\n"
+		"echo \n"
+		/* Can't use log.i or check LOGLEVEL in first stage, since this lives
+		 * inside the base chroot, and has not yet mounted the input disk. */
+		"echo 'I: qemu-pbuilder first-stage' \n"
+		"export PBUILDER_INIT_VERSION=" XSTR(PBUILDER_INIT_VERSION) "\n"
+		"export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'\n"
+		"stty sane\n"
+		"[ -d /proc/1 ] || mount -n /proc /proc -t proc\n"
+		"ln -s /dev/shm /run/shm\n"
+		"mkdir /run/lock\n"
+		"chmod 01777 /run/lock\n"
+		"mount -t tmpfs tmpfs /run/lock\n"
+		"mount -n -o rw,remount / || mount -v -n -o rw,remount /dev/root \n"
+		"find /tmp -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf --\n"
+		"mount -t tmpfs tmpfs /tmp\n"
+		"mkdir /dev/shm\n"
+		"chmod 01777 /dev/shm\n"
+		"mount -t tmpfs tmpfs /dev/shm\n"
+		"ln -fs /proc/mounts /etc/mtab\n"
+		"export BUILDDIR=" BUILDDIR "\n"
+		"mkdir -p $BUILDDIR\n"
+		"ln -s $BUILDDIR /tmp/buildd\n"
+		"mount -n -t ext3 /dev/%sb $BUILDDIR \n"
+		"$BUILDDIR/input/pbuilder-run \n",
+		qemu_arch_diskdevice(pc));
 }
 
 static void write_second_stage_header(FILE *f, int debug_shell) {
-  const char *log_level;
-
-  switch (log_get_filter_level()) {
-    case log_debug: log_level = "D"; break;
-    case log_info:  log_level = "I"; break;
-    case log_warn:  log_level = "W"; break;
-    case log_error: log_level = "E"; break;
-    default:        log_level = "I"; break;
-  }
-
-  fprintf(f,
-	  "#!/bin/bash\n"
-
-	  /* Will not be auto (using log_get_use_colors, not _unresolved) */
-	  "export USECOLORS=\"%s\"\n"
-	  "export LOGLEVEL=\"%s\"\n"
-
-	  /* Bash logging taken from pbuilder */
-	  "# Log a message\n"
-	  "# message is of a format\n"
-	  "#  E: error message\n"
-	  "#  W: warning message\n"
-	  "#  I: informational message\n"
-	  "_log() {\n"
-	  "    set -u\n"
-	  "    local color=\"$1\" ; shift\n"
-	  "    local red='\\033[0;31m'\n"
-	  "    local yellow='\\033[1;33m'\n"
-	  "    local blue='\\033[0;34m'\n"
-	  "    local reset='\\033[0m'\n"
-	  "    case \"$USECOLORS\" in\n"
-	  "        yes)\n"
-	  "            printf \"${!color}${*}${reset}\\n\"\n"
-	  "            ;;\n"
-	  "        no)\n"
-	  "            printf \"${*}\\n\"\n"
-	  "            ;;\n"
-	  "        *)\n"
-	  "            printf \"malformed value of USECOLORS: [%%s]\\n\" \"$USECOLORS\" >&2\n"
-	  "            exit 1\n"
-	  "            ;;\n"
-	  "    esac\n"
-	  "    set +u\n"
-	  "}\n"
-	  "function log() {\n"
-	  "    case \"$*\" in\n"
-	  "        \"E: \"*)\n"
-	  "            _log 'red' \"$*\" >&2\n"
-	  "            ;;\n"
-	  "        \"W: \"*)\n"
-	  "            _log 'yellow' \"$*\" >&2\n"
-	  "            ;;\n"
-	  "        \"I: \"*)\n"
-	  "            _log 'reset' \"$*\"\n"
-	  "            ;;\n"
-	  "        \"D: \"*)\n"
-	  "            _log 'blue' \"$*\"\n"
-	  "            ;;\n"
-	  "        *)\n"
-	  "            echo \"malformed log message: $*\" >&2\n"
-	  "            exit 1\n"
-	  "            ;;\n"
-	  "    esac\n"
-	  "}\n"
-
-	  "log.e() {\n"
-	  "    case \"$LOGLEVEL\" in\n"
-	  "        D|I|W|E) log \"E: $*\" ;;\n"
-	  "    esac\n"
-	  "}\n"
-	  "log.w() {\n"
-	  "    case \"$LOGLEVEL\" in\n"
-	  "        D|I|W) log \"W: $*\" ;;\n"
-	  "    esac\n"
-	  "}\n"
-	  "log.i() {\n"
-	  "    case \"$LOGLEVEL\" in\n"
-	  "        D|I) log \"I: $*\" ;;\n"
-	  "    esac\n"
-	  "}\n"
-	  "log.d() {\n"
-	  "    case \"$LOGLEVEL\" in\n"
-	  "        D) log \"D: $*\" ;;\n"
-	  "    esac\n"
-	  "}\n"
-
-	  /* define function to terminate qemu */
-	  "function exit_from_qemu() {\n"
-	  "    %s\n"
-	  "    sync\n"
-	  "    sync\n"
-	  "    sleep 1s\n"		/* sleep before sending dying message */
-	  "    log.i 'qemu-pbuilder %s'\"$1\"\n"
-	  "    sleep 1s\n"
-	  "    halt -f -p\n"		/* just halt myself if possible */
-	  "}\n",
-	  log_get_use_colors() == log_use_colors_yes ? "yes" : "no",
-	  log_level,
-	  debug_shell?"log.i \"Debug shell\"; /bin/bash":"",
-	  qemu_keyword);
+	const char *log_level;
+
+	switch (log_get_filter_level()) {
+		case log_debug:
+			log_level = "D";
+			break;
+		case log_info:
+			log_level = "I";
+			break;
+		case log_warn:
+			log_level = "W";
+			break;
+		case log_error:
+			log_level = "E";
+			break;
+		default:
+			log_level = "I";
+			break;
+	}
+
+	fprintf(
+		f,
+		"#!/bin/bash\n"
+
+		/* Will not be auto (using log_get_use_colors, not _unresolved) */
+		"export USECOLORS=\"%s\"\n"
+		"export LOGLEVEL=\"%s\"\n"
+
+		/* Bash logging taken from pbuilder */
+		"# Log a message\n"
+		"# message is of a format\n"
+		"#  E: error message\n"
+		"#  W: warning message\n"
+		"#  I: informational message\n"
+		"_log() {\n"
+		"    set -u\n"
+		"    local color=\"$1\" ; shift\n"
+		"    local red='\\033[0;31m'\n"
+		"    local yellow='\\033[1;33m'\n"
+		"    local blue='\\033[0;34m'\n"
+		"    local reset='\\033[0m'\n"
+		"    case \"$USECOLORS\" in\n"
+		"        yes)\n"
+		"            printf \"${!color}${*}${reset}\\n\"\n"
+		"            ;;\n"
+		"        no)\n"
+		"            printf \"${*}\\n\"\n"
+		"            ;;\n"
+		"        *)\n"
+		"            printf \"malformed value of USECOLORS: [%%s]\\n\" \"$USECOLORS\" >&2\n"
+		"            exit 1\n"
+		"            ;;\n"
+		"    esac\n"
+		"    set +u\n"
+		"}\n"
+		"function log() {\n"
+		"    case \"$*\" in\n"
+		"        \"E: \"*)\n"
+		"            _log 'red' \"$*\" >&2\n"
+		"            ;;\n"
+		"        \"W: \"*)\n"
+		"            _log 'yellow' \"$*\" >&2\n"
+		"            ;;\n"
+		"        \"I: \"*)\n"
+		"            _log 'reset' \"$*\"\n"
+		"            ;;\n"
+		"        \"D: \"*)\n"
+		"            _log 'blue' \"$*\"\n"
+		"            ;;\n"
+		"        *)\n"
+		"            echo \"malformed log message: $*\" >&2\n"
+		"            exit 1\n"
+		"            ;;\n"
+		"    esac\n"
+		"}\n"
+
+		"log.e() {\n"
+		"    case \"$LOGLEVEL\" in\n"
+		"        D|I|W|E) log \"E: $*\" ;;\n"
+		"    esac\n"
+		"}\n"
+		"log.w() {\n"
+		"    case \"$LOGLEVEL\" in\n"
+		"        D|I|W) log \"W: $*\" ;;\n"
+		"    esac\n"
+		"}\n"
+		"log.i() {\n"
+		"    case \"$LOGLEVEL\" in\n"
+		"        D|I) log \"I: $*\" ;;\n"
+		"    esac\n"
+		"}\n"
+		"log.d() {\n"
+		"    case \"$LOGLEVEL\" in\n"
+		"        D) log \"D: $*\" ;;\n"
+		"    esac\n"
+		"}\n"
+
+		/* define function to terminate qemu */
+		"function exit_from_qemu() {\n"
+		"    %s\n"
+		"    sync\n"
+		"    sync\n"
+		"    sleep 1s\n" /* sleep before sending dying message */
+		"    log.i 'qemu-pbuilder %s'\"$1\"\n"
+		"    sleep 1s\n"
+		"    halt -f -p\n" /* just halt myself if possible */
+		"}\n",
+		log_get_use_colors() == log_use_colors_yes ? "yes" : "no",
+		log_level,
+		debug_shell ? "log.i \"Debug shell\"; /bin/bash" : "",
+		qemu_keyword);
 }
 
 /**
@@ -849,242 +794,226 @@ static void write_second_stage_header(FILE *f, int debug_shell) {
  *
  * hostcommand1 is used from build and login and exeute
  */
-static int run_second_stage_script
-(
- /** save the result of this command*/
- int save_result,
- /** the command-line to invoke within QEMU */
- const char* commandline,
- const struct pbuilderconfig* pc,
- /** the commands to invoke in the host OS */
- const char* hostcommand1,
- /** the commands to invoke in the guest OS */
- const char* hostcommand2)
-{
-  char* script=NULL;
-  char* workblockdevicepath=NULL;
-  char* cowdevpath=NULL;
-  char* timestring;
-  int ret=1;
-  FILE* f;
-  int i;
-
-  if (mkdir(pc->buildplace,0777))
-    {
-      /* could not create the buildplace here. */
-      log_perror("mkdir");
-      goto out;
-    }
-
-  do_fsck(pc->basepath);
-
-  timestring=get_current_time_string();
-
-  asprintf(&workblockdevicepath, "%s.dev", pc->buildplace);
-  ret=create_ext3_block_device(workblockdevicepath, 1);
-  loop_mount(workblockdevicepath, pc->buildplace);
-
-  f = create_script(pc->buildplace, "input/pbuilder-run");
-  write_second_stage_header(f, 0);
-  fprintf(f,
-	  /* main code */
-	  "echo \n"
-	  "log.i 'qemu-pbuilder second-stage' \n"
-	  // Remove compatibility symlink
-	  "rm \"$BUILDDIR\"/pbuilder-run\n"
-	  //"mount -n /proc /proc -t proc\n" // this is done in first stage.
-	  "if [ \"${PBUILDER_INIT_VERSION:-0}\" -lt "XSTR(PBUILDER_INIT_VERSION)" ]; then\n"
-	  "    log.e \"qemubuilder init script is out of date (${PBUILDER_INIT_VERSION:-0} < "XSTR(PBUILDER_INIT_VERSION)")\"\n"
-	  "    log.e \"Please run qemubuilder --update\"\n"
-	  "    exit_from_qemu 1\n"
-	  "elif [ \"${PBUILDER_INIT_VERSION:-0}\" -gt "XSTR(PBUILDER_INIT_VERSION)" ]; then\n"
-	  "    log.e \"qemubuilder init script is newer than expected (${PBUILDER_INIT_VERSION:-0} < "XSTR(PBUILDER_INIT_VERSION)")\"\n"
-	  "    exit_from_qemu 1\n"
-	  "fi\n"
-	  "log.i 'setting time to %s' \n"
-	  "date --set=\"%s\"\n"
-	  "log.i 'configuring network' \n"
-	  "ifconfig -a\n"
-	  "export IFNAME=`/sbin/ifconfig -a | grep eth | head -n1 | awk '{print $1}'`\n"
-	  "dhclient $IFNAME\n"
-	  "mkdir -p \""BUILDDIR"\"\n"
-	  "%s\n"
-	  "$BUILDDIR/input/run-copyfiles\n"
-	  "hostname pbuilder-$(cat /etc/hostname)\n"
-	  "%s\n"
-	  //TODO: I can mount /var/cache/apt/archives from some scratch space to not need this:
-	  "apt-get clean || true\n"
-	  "exit_from_qemu 0\n",
-	  timestring,
-	  timestring,
-	  pc->hookdir && pc->hookdir[0] ? "mkdir -p \""CHROOT_HOOKDIR"\"" : "",
-	  commandline);
-  fclose(f);
-  {
-    char *compat_symlink;
-    if (0>asprintf(&compat_symlink,
-		   "%s/pbuilder-run",
-		   pc->buildplace))
-      {
-	log_printf(log_error,
-	    "Failed to allocate string for compatibility symlink path '%s/pbuilder-run': %s",
-	    pc->buildplace,
-	    strerror(errno));
-	goto out;
-      }
-    if (symlink("input/pbuilder-run", compat_symlink))
-      {
-	log_printf(log_error,
-	    "Failed to create compatibility symlink: %s",
-	    strerror(errno));
-	free(compat_symlink);
-	goto out;
-      }
-    free(compat_symlink);
-  }
-
-  /* copy files script */
-  f = create_script(pc->buildplace, "input/run-copyfiles");
-  copy_file_contents_through_temp(f, "/etc/hosts", pc->buildplace, "/etc");
-  copy_file_contents_through_temp(f, "/etc/hostname", pc->buildplace, "/etc");
-  /* copy inputfile */
-  for (i=0; pc->inputfile[i]; ++i)
-    {
-      copy_file_contents_to_temp(pc->inputfile[i], pc->buildplace, NULL);
-    }
-  if (pc->hookdir != NULL && pc->hookdir[0])
-    {
-      copy_hookdir(pc->hookdir, pc->buildplace);
-    }
-  fclose(f);
-
-  /* do I not need to copy /etc/pbuilderrc, and ~/.pbuilderrc to inside chroot? */
-  /* TODO: recover aptcache */
-
-  if(hostcommand1)
-    {
-      log_printf(log_info, "running host command: %s", hostcommand1);
-      system(hostcommand1);
-    }
-
-  loop_umount(pc->buildplace);
-
-  asprintf(&cowdevpath, "%s.cowdev", pc->buildplace);
-  ret=forkexeclp("qemu-img", "qemu-img",
-		 "create",
-		 "-f",
-		 "qcow2",
-		 "-b",
-		 pc->basepath,
-		 cowdevpath,
-		 NULL);
-
-  fork_qemu(cowdevpath, workblockdevicepath, pc);
-  /* this will always return 0. */
-
-  /* commit the change here */
-  if (save_result)
-    {
-      log_printf(log_info, "committing changes to qemu image");
-      ret=forkexeclp("qemu-img", "qemu-img",
-		     "commit",
-		     cowdevpath,
-		     NULL);
-    }
-
-  /* after-run */
-  loop_mount(workblockdevicepath, pc->buildplace);
-  log_printf(log_info, "running post-run process");
-  if(hostcommand2)
-    {
-      log_printf(log_info, "running host command: %s", hostcommand2);
-      system(hostcommand2);
-    }
-  loop_umount(pc->buildplace);
-  rmdir(pc->buildplace);
-  log_printf(log_info, "clean up COW device files");
-  unlink(workblockdevicepath);
-  unlink(cowdevpath);
-  ret=0;
-
- out:
-  if(workblockdevicepath) free(workblockdevicepath);
-  if(cowdevpath) free(cowdevpath);
-  if(script) free(script);
-  return ret;
+static int run_second_stage_script(
+	/** save the result of this command*/
+	int save_result,
+	/** the command-line to invoke within QEMU */
+	const char *commandline,
+	const struct pbuilderconfig *pc,
+	/** the commands to invoke in the host OS */
+	const char *hostcommand1,
+	/** the commands to invoke in the guest OS */
+	const char *hostcommand2) {
+	char *script = NULL;
+	char *workblockdevicepath = NULL;
+	char *cowdevpath = NULL;
+	char *timestring;
+	int ret = 1;
+	FILE *f;
+	int i;
+
+	if (mkdir(pc->buildplace, 0777)) {
+		/* could not create the buildplace here. */
+		log_perror("mkdir");
+		goto out;
+	}
+
+	do_fsck(pc->basepath);
+
+	timestring = get_current_time_string();
+
+	asprintf(&workblockdevicepath, "%s.dev", pc->buildplace);
+	ret = create_ext3_block_device(workblockdevicepath, 1);
+	loop_mount(workblockdevicepath, pc->buildplace);
+
+	f = create_script(pc->buildplace, "input/pbuilder-run");
+	write_second_stage_header(f, 0);
+	fprintf(
+		f,
+		/* main code */
+		"echo \n"
+		"log.i 'qemu-pbuilder second-stage' \n"
+		// Remove compatibility symlink
+		"rm \"$BUILDDIR\"/pbuilder-run\n"
+		//"mount -n /proc /proc -t proc\n" // this is done in first stage.
+		"if [ \"${PBUILDER_INIT_VERSION:-0}\" -lt " XSTR(PBUILDER_INIT_VERSION) " ]; then\n"
+		"    log.e \"qemubuilder init script is out of date (${PBUILDER_INIT_VERSION:-0} < " XSTR(PBUILDER_INIT_VERSION) ")\"\n"
+		"    log.e \"Please run qemubuilder --update\"\n"
+		"    exit_from_qemu 1\n"
+		"elif [ \"${PBUILDER_INIT_VERSION:-0}\" -gt " XSTR(PBUILDER_INIT_VERSION) " ]; then\n"
+		"    log.e \"qemubuilder init script is newer than expected (${PBUILDER_INIT_VERSION:-0} < " XSTR(PBUILDER_INIT_VERSION) ")\"\n"
+		"    exit_from_qemu 1\n"
+		"fi\n"
+		"log.i 'setting time to %s' \n"
+		"date --set=\"%s\"\n"
+		"log.i 'configuring network' \n"
+		"ifconfig -a\n"
+		"export IFNAME=`/sbin/ifconfig -a | grep eth | head -n1 | awk '{print $1}'`\n"
+		"dhclient $IFNAME\n"
+		"mkdir -p \"" BUILDDIR
+		"\"\n"
+		"%s\n"
+		"$BUILDDIR/input/run-copyfiles\n"
+		"hostname pbuilder-$(cat /etc/hostname)\n"
+		"%s\n"
+		//TODO: I can mount /var/cache/apt/archives from some scratch space to not need this:
+		"apt-get clean || true\n"
+		"exit_from_qemu 0\n",
+		timestring,
+		timestring,
+		pc->hookdir && pc->hookdir[0] ? "mkdir -p \"" CHROOT_HOOKDIR "\"" : "",
+		commandline);
+	fclose(f);
+	{
+		char *compat_symlink;
+		if (0 > asprintf(&compat_symlink, "%s/pbuilder-run", pc->buildplace)) {
+			log_printf(
+				log_error,
+				"Failed to allocate string for compatibility symlink path '%s/pbuilder-run': %s",
+				pc->buildplace,
+				strerror(errno));
+			goto out;
+		}
+		if (symlink("input/pbuilder-run", compat_symlink)) {
+			log_printf(log_error,
+					   "Failed to create compatibility symlink: %s",
+					   strerror(errno));
+			free(compat_symlink);
+			goto out;
+		}
+		free(compat_symlink);
+	}
+
+	/* copy files script */
+	f = create_script(pc->buildplace, "input/run-copyfiles");
+	copy_file_contents_through_temp(f, "/etc/hosts", pc->buildplace, "/etc");
+	copy_file_contents_through_temp(f, "/etc/hostname", pc->buildplace, "/etc");
+	/* copy inputfile */
+	for (i = 0; pc->inputfile[i]; ++i) {
+		copy_file_contents_to_temp(pc->inputfile[i], pc->buildplace, NULL);
+	}
+	if (pc->hookdir != NULL && pc->hookdir[0]) {
+		copy_hookdir(pc->hookdir, pc->buildplace);
+	}
+	fclose(f);
+
+	/* do I not need to copy /etc/pbuilderrc, and ~/.pbuilderrc to inside chroot? */
+	/* TODO: recover aptcache */
+
+	if (hostcommand1) {
+		log_printf(log_info, "running host command: %s", hostcommand1);
+		system(hostcommand1);
+	}
+
+	loop_umount(pc->buildplace);
+
+	asprintf(&cowdevpath, "%s.cowdev", pc->buildplace);
+	ret = forkexeclp("qemu-img",
+					 "qemu-img",
+					 "create",
+					 "-f",
+					 "qcow2",
+					 "-b",
+					 pc->basepath,
+					 cowdevpath,
+					 NULL);
+
+	fork_qemu(cowdevpath, workblockdevicepath, pc);
+	/* this will always return 0. */
+
+	/* commit the change here */
+	if (save_result) {
+		log_printf(log_info, "committing changes to qemu image");
+		ret = forkexeclp("qemu-img", "qemu-img", "commit", cowdevpath, NULL);
+	}
+
+	/* after-run */
+	loop_mount(workblockdevicepath, pc->buildplace);
+	log_printf(log_info, "running post-run process");
+	if (hostcommand2) {
+		log_printf(log_info, "running host command: %s", hostcommand2);
+		system(hostcommand2);
+	}
+	loop_umount(pc->buildplace);
+	rmdir(pc->buildplace);
+	log_printf(log_info, "clean up COW device files");
+	unlink(workblockdevicepath);
+	unlink(cowdevpath);
+	ret = 0;
+
+out:
+	if (workblockdevicepath)
+		free(workblockdevicepath);
+	if (cowdevpath)
+		free(cowdevpath);
+	if (script)
+		free(script);
+	return ret;
 }
 
 /*
    @return shell command to copy the dsc file.
  */
-static char* copy_dscfile(const char* dscfile_, const char* destdir)
-{
-  int ret=1;
-  size_t bufsiz=0;
-  char* buf=NULL;
-  char* filename=NULL;
-  char* origdir=NULL;
-  char* dscfile=canonicalize_file_name(dscfile_);
-  FILE* f=fopen(dscfile,"r");
-
-  char* memstr=0;
-  size_t len=0;
-  FILE* fmem=open_memstream(&memstr, &len);
-  int filelist=0;
-
-  origdir=strdup(dscfile);
-
-  assert(strrchr(origdir,'/') != 0);
-  (*(strrchr(origdir,'/')))=0;
-
-  fprintf(fmem, "cp %s %s/\n",
-	  dscfile, destdir);
-
-  while (getline(&buf,&bufsiz,f)>0)
-    {
-      if (strrchr(buf,'\n'))
-	{
-	  *(strrchr(buf,'\n'))=0;
-	}
-      if(filelist)
-	{
-	  if(sscanf(buf, " %*s %*s %ms", &filename)!=1)
-	    filelist=0;
-	  else
-	    {
-	      fprintf(fmem, "cp %s/%s %s/\n",
-		      origdir, filename, destdir);
-	      assert(filename);
-	      free(filename);
-	    }
-	}
-      if (!(buf[0]==' ')&&
-	  !strncmp(buf,"Files:",6))
-	{
-	  filelist=1;
+static char *copy_dscfile(const char *dscfile_, const char *destdir) {
+	int ret = 1;
+	size_t bufsiz = 0;
+	char *buf = NULL;
+	char *filename = NULL;
+	char *origdir = NULL;
+	char *dscfile = canonicalize_file_name(dscfile_);
+	FILE *f = fopen(dscfile, "r");
+
+	char *memstr = 0;
+	size_t len = 0;
+	FILE *fmem = open_memstream(&memstr, &len);
+	int filelist = 0;
+
+	origdir = strdup(dscfile);
+
+	assert(strrchr(origdir, '/') != 0);
+	(*(strrchr(origdir, '/'))) = 0;
+
+	fprintf(fmem, "cp %s %s/\n", dscfile, destdir);
+
+	while (getline(&buf, &bufsiz, f) > 0) {
+		if (strrchr(buf, '\n')) {
+			*(strrchr(buf, '\n')) = 0;
+		}
+		if (filelist) {
+			if (sscanf(buf, " %*s %*s %ms", &filename) != 1)
+				filelist = 0;
+			else {
+				fprintf(fmem, "cp %s/%s %s/\n", origdir, filename, destdir);
+				assert(filename);
+				free(filename);
+			}
+		}
+		if (!(buf[0] == ' ') && !strncmp(buf, "Files:", 6)) {
+			filelist = 1;
+		}
 	}
-    }
-
-  ret=0;
-  assert(fmem);
-  assert(f);
-  fclose(fmem);
-  fclose(f);
-
-  if(buf) free(buf);
-  if(origdir) free(origdir);
-  if(dscfile) free(dscfile);
-  return ret?NULL:memstr;
+
+	ret = 0;
+	assert(fmem);
+	assert(f);
+	fclose(fmem);
+	fclose(f);
+
+	if (buf)
+		free(buf);
+	if (origdir)
+		free(origdir);
+	if (dscfile)
+		free(dscfile);
+	return ret ? NULL : memstr;
 }
 
-int cpbuilder_check_config(const struct pbuilderconfig* pc)
-{
-  if (!pc->arch || !*pc->arch)
-    {
-      log_printf(log_error, "No architecture specified");
-      return 1;
-    }
-  return 0;
+int cpbuilder_check_config(const struct pbuilderconfig *pc) {
+	if (!pc->arch || !*pc->arch) {
+		log_printf(log_error, "No architecture specified");
+		return 1;
+	}
+	return 0;
 }
 
 /*
@@ -1092,291 +1021,292 @@ int cpbuilder_check_config(const struct pbuilderconfig* pc)
 
    variable ret holds the state.
  */
-int cpbuilder_create(const struct pbuilderconfig* pc)
-{
-  int ret=0;
-  char* s=NULL;  		/* generic asprintf buffer */
-  char* workblockdevicepath=NULL;
-  FILE* f;			/* generic FILE pointer which is reused. */
-  char* t;
-  char* timestring;
-
-  /* remove existing file; it can be old qemu image, or a directory if
-     it didn't exist before. */
-  unlink(pc->basepath);
-  rmdir(pc->basepath);
-
-  /* 3GB should be enough to install any Debian system; hopefully */
-  ret=create_ext3_block_device(pc->basepath, 3);
-
-  if (ret)
-    {
-      goto out;
-    }
-
-  if (mkdir(pc->buildplace,0777))
-    {
-      /* could not create the buildplace here. */
-      ret=1;
-      log_perror("mkdir");
-      goto out;
-    }
-
-  ret=loop_mount(pc->basepath, pc->buildplace);
-  if (ret)
-    {
-      goto out;
-    }
-
-  debootstrap_command_line[1] = "--arch";
-  debootstrap_command_line[2] = pc->arch;
-  debootstrap_command_line[3] = "--foreign";
-  DEBOOTSTRAP_ADD_PARAM(pc->distribution);
-  DEBOOTSTRAP_ADD_PARAM(pc->buildplace);
-  DEBOOTSTRAP_ADD_PARAM(pc->mirror);
-  DEBOOTSTRAP_ADD_PARAM(NULL);
-
-  log_printf(log_info, "Invoking debootstrap");
-  ret=forkexecvp(debootstrap_command_line);
-  if (ret)
-    {
-      log_printf(log_error, "debootstrap failed with %i", ret);
-      goto umount_out;
-    }
-
-  /* arch-dependent code here.
-     create required device files.
-
-     ttyAMA0 is probably ARM-specific
-     others are probably linux-portable as documented in linux/Documentation/devices.txt
-     other OSes will require different, but hey, they probably don't even boot from ext3,
-     we'll need think of other ways to work with them.
-
-   */
-  log_printf(log_info, "Doing architecture-specific /dev population");
-
-  qemu_create_arch_devices(pc->buildplace, pc->arch);
-
-  f = create_script(pc->buildplace, "pbuilder-run");
-  write_first_stage(f, pc);
-  fclose(f);
-  f = NULL;
-
-  if (pc->http_proxy != NULL)
-    {
-	f = create_script(pc->buildplace, "etc/apt/apt.conf.d/20pbuilder-proxy");
-	fprintf(f,
-		"Acquire\n"
-		"{\n"
-			"http \n"
-			"{\n"
+int cpbuilder_create(const struct pbuilderconfig *pc) {
+	int ret = 0;
+	char *s = NULL; /* generic asprintf buffer */
+	char *workblockdevicepath = NULL;
+	FILE *f; /* generic FILE pointer which is reused. */
+	char *t;
+	char *timestring;
+
+	/* remove existing file; it can be old qemu image, or a directory if
+	 * it didn't exist before. */
+	unlink(pc->basepath);
+	rmdir(pc->basepath);
+
+	/* 3GB should be enough to install any Debian system; hopefully */
+	ret = create_ext3_block_device(pc->basepath, 3);
+
+	if (ret) {
+		goto out;
+	}
+
+	if (mkdir(pc->buildplace, 0777)) {
+		/* could not create the buildplace here. */
+		ret = 1;
+		log_perror("mkdir");
+		goto out;
+	}
+
+	ret = loop_mount(pc->basepath, pc->buildplace);
+	if (ret) {
+		goto out;
+	}
+
+	debootstrap_command_line[1] = "--arch";
+	debootstrap_command_line[2] = pc->arch;
+	debootstrap_command_line[3] = "--foreign";
+	DEBOOTSTRAP_ADD_PARAM(pc->distribution);
+	DEBOOTSTRAP_ADD_PARAM(pc->buildplace);
+	DEBOOTSTRAP_ADD_PARAM(pc->mirror);
+	DEBOOTSTRAP_ADD_PARAM(NULL);
+
+	log_printf(log_info, "Invoking debootstrap");
+	ret = forkexecvp(debootstrap_command_line);
+	if (ret) {
+		log_printf(log_error, "debootstrap failed with %i", ret);
+		goto umount_out;
+	}
+
+	/* arch-dependent code here.
+	 * create required device files.
+
+	 * ttyAMA0 is probably ARM-specific
+	 * others are probably linux-portable as documented in
+	 * linux/Documentation/devices.txt
+	 * other OSes will require different, but hey, they probably don't
+	 * even boot from ext3,
+	 * we'll need think of other ways to work with them.
+	 */
+	log_printf(log_info, "Doing architecture-specific /dev population");
+
+	qemu_create_arch_devices(pc->buildplace, pc->arch);
+
+	f = create_script(pc->buildplace, "pbuilder-run");
+	write_first_stage(f, pc);
+	fclose(f);
+	f = NULL;
+
+	if (pc->http_proxy != NULL) {
+		f = create_script(pc->buildplace,
+						  "etc/apt/apt.conf.d/20pbuilder-proxy");
+		fprintf(f,
+				"Acquire\n"
+				"{\n"
+				"http \n"
+				"{\n"
 				"Proxy \"%s\";\n"
-			"};\n"
-		"};\n",
-		pc->http_proxy
-	);
+				"};\n"
+				"};\n",
+				pc->http_proxy);
+		fclose(f);
+		f = NULL;
+	}
+
+	free(s);
+	s = 0;
+
+	ret = loop_umount(pc->buildplace);
+
+	/* create the temporary device for command-execution */
+	asprintf(&workblockdevicepath, "%s.dev", pc->buildplace);
+	ret = create_ext3_block_device(workblockdevicepath, 1);
+
+	loop_mount(workblockdevicepath, pc->buildplace);
+
+	timestring = get_current_time_string();
+
+	f = create_script(pc->buildplace, "input/pbuilder-run");
+	write_second_stage_header(f, pc->debug);
+	fprintf(
+		f,
+		/* start of main code */
+		"export RET=0\n"
+		"echo \n"
+		"log.i 'qemu-pbuilder second-stage' \n"
+		"log.i 'setting time to %s' \n"
+		"date --set=\"%s\"\n"
+		"log.i 'Running debootstrap second-stage script' \n"
+		"touch /etc/udev/disabled\n" // work-around for #520742
+		"/debootstrap/debootstrap --second-stage || ( "
+		"  log.i \"dumping debootstrap log\"\n"
+		"  cat /debootstrap/debootstrap.log\n"
+		"  exit_from_qemu\n"
+		"\n )\n"
+		"rm /etc/udev/disabled\n" // work-around for #520742
+		"echo deb %s %s %s > /etc/apt/sources.list \n"
+		"echo 'APT::Install-Recommends \"false\"; ' > /etc/apt/apt.conf.d/15pbuilder\n"
+		"mount -n proc /proc -t proc\n"
+		"mount -n sysfs /sys -t sysfs\n"
+		"mkdir /dev/pts\n"
+		"mount -n devpts /dev/pts -t devpts\n"
+		"dhclient eth0\n"
+		"%s\n"
+		"$BUILDDIR/input/run-copyfiles\n"
+		"hostname pbuilder-$(cat /etc/hostname)\n"
+		//TODO: installaptlines
+		"echo '%s' > /etc/apt/sources.list.d/other.list\n"
+		EXECUTE_HOOKS("G")
+		"apt-get update || exit_from_qemu 1\n"
+		//TODO: "dpkg --purge $REMOVEPACKAGES\n"
+		//recover aptcache
+		"apt-get -y %s -o DPkg::Options::=--force-confnew dist-upgrade || exit_from_qemu 1\n"
+		"apt-get install %s -y build-essential dpkg-dev apt aptitude pbuilder %s || exit_from_qemu 1\n"
+		//TODO: save aptcache
+		//optionally autoclean aptcache
+		EXECUTE_HOOKS("E")
+		//TODO: I can mount /var/cache/apt/archives from some scratch space to not need this:
+		"apt-get clean || true\n"
+		"exit_from_qemu $RET\n"
+		"bash\n",
+		timestring,
+		timestring,
+		t = sanitize_mirror(pc->mirror),
+		pc->distribution,
+		pc->components,
+		pc->hookdir && pc->hookdir[0] ? "mkdir -p \"" CHROOT_HOOKDIR "\"" : "",
+		pc->othermirror ? pc->othermirror : "",
+		pc->allow_untrusted ? "--force-yes" : "",
+		pc->allow_untrusted ? "--force-yes" : "",
+		pc->extrapackages ? pc->extrapackages : "");
 	fclose(f);
 	f = NULL;
-    }
-
-  free(s); s=0;
-
-  ret=loop_umount(pc->buildplace);
-
-  /* create the temporary device for command-execution */
-  asprintf(&workblockdevicepath, "%s.dev", pc->buildplace);
-  ret=create_ext3_block_device(workblockdevicepath, 1);
-
-  loop_mount(workblockdevicepath, pc->buildplace);
-
-  timestring=get_current_time_string();
-
-  f = create_script(pc->buildplace, "input/pbuilder-run");
-  write_second_stage_header(f, pc->debug);
-  fprintf(f,
-	  /* start of main code */
-	  "export RET=0\n"
-	  "echo \n"
-	  "log.i 'qemu-pbuilder second-stage' \n"
-	  "log.i 'setting time to %s' \n"
-	  "date --set=\"%s\"\n"
-	  "log.i 'Running debootstrap second-stage script' \n"
-	  "touch /etc/udev/disabled\n" // work-around for #520742
-	  "/debootstrap/debootstrap --second-stage || ( "
-	  "  log.i \"dumping debootstrap log\"\n"
-	  "  cat /debootstrap/debootstrap.log\n"
-	  "  exit_from_qemu\n"
-	  "\n )\n"
-	  "rm /etc/udev/disabled\n" // work-around for #520742
-	  "echo deb %s %s %s > /etc/apt/sources.list \n"
-	  "echo 'APT::Install-Recommends \"false\"; ' > /etc/apt/apt.conf.d/15pbuilder\n"
-	  "mount -n proc /proc -t proc\n"
-	  "mount -n sysfs /sys -t sysfs\n"
-	  "mkdir /dev/pts\n"
-	  "mount -n devpts /dev/pts -t devpts\n"
-	  "dhclient eth0\n"
-	  "%s\n"
-	  "$BUILDDIR/input/run-copyfiles\n"
-	  "hostname pbuilder-$(cat /etc/hostname)\n"
-	  //TODO: installaptlines
-	  "echo '%s' > /etc/apt/sources.list.d/other.list\n"
-	  EXECUTE_HOOKS("G")
-	  "apt-get update || exit_from_qemu 1\n"
-	  //TODO: "dpkg --purge $REMOVEPACKAGES\n"
-	  //recover aptcache
-	  "apt-get -y %s -o DPkg::Options::=--force-confnew dist-upgrade || exit_from_qemu 1\n"
-	  "apt-get install %s -y build-essential dpkg-dev apt aptitude pbuilder %s || exit_from_qemu 1\n"
-	  //TODO: save aptcache
-	  //optionally autoclean aptcache
-	  EXECUTE_HOOKS("E")
-	  //TODO: I can mount /var/cache/apt/archives from some scratch space to not need this:
-	  "apt-get clean || true\n"
-	  "exit_from_qemu $RET\n"
-	  "bash\n",
-	  timestring,
-	  timestring,
-	  t=sanitize_mirror(pc->mirror), pc->distribution, pc->components,
-	  pc->hookdir && pc->hookdir[0] ? "mkdir -p \""CHROOT_HOOKDIR"\"" : "",
-	  pc->othermirror?pc->othermirror:"",
-	  pc->allow_untrusted?"--force-yes":"",
-	  pc->allow_untrusted?"--force-yes":"",
-	  pc->extrapackages?pc->extrapackages:"");
-  fclose(f);
-  f = NULL;
-
-  free(t);
-
-  /* TODO: can I do 'date --set' from output of 'LC_ALL=C date' */
-
-  /* copy files script */
-  f = create_script(pc->buildplace, "input/run-copyfiles");
-  copy_file_contents_through_temp(f, "/etc/hosts", pc->buildplace, "/etc");
-  copy_file_contents_through_temp(f, "/etc/hostname", pc->buildplace, "/etc");
-  if (pc->hookdir != NULL && pc->hookdir[0])
-    {
-      copy_hookdir(pc->hookdir, pc->buildplace);
-    }
-  fclose(f);
-
-  /* do I not need to copy /etc/pbuilderrc, and ~/.pbuilderrc to inside chroot? */
-  /* TODO: recover aptcache */
-
-  loop_umount(pc->buildplace);
-  rmdir(pc->buildplace);
-
-  // this will have wrong time. how to workaround?
-  ret=fork_qemu(pc->basepath, workblockdevicepath, pc);
-
-  unlink(workblockdevicepath);
-
- out:
-  if(workblockdevicepath)
-    {
-      free(workblockdevicepath);
-    }
-  if(s) free(s);
-  return ret;
-
- umount_out:
-  loop_umount(pc->buildplace);
-  if(s) free(s);
-  return ret;
+
+	free(t);
+
+	/* TODO: can I do 'date --set' from output of 'LC_ALL=C date' */
+
+	/* copy files script */
+	f = create_script(pc->buildplace, "input/run-copyfiles");
+	copy_file_contents_through_temp(f, "/etc/hosts", pc->buildplace, "/etc");
+	copy_file_contents_through_temp(f, "/etc/hostname", pc->buildplace, "/etc");
+	if (pc->hookdir != NULL && pc->hookdir[0]) {
+		copy_hookdir(pc->hookdir, pc->buildplace);
+	}
+	fclose(f);
+
+	/* do I not need to copy /etc/pbuilderrc, and ~/.pbuilderrc to inside chroot? */
+	/* TODO: recover aptcache */
+
+	loop_umount(pc->buildplace);
+	rmdir(pc->buildplace);
+
+	// this will have wrong time. how to workaround?
+	ret = fork_qemu(pc->basepath, workblockdevicepath, pc);
+
+	unlink(workblockdevicepath);
+
+out:
+	if (workblockdevicepath) {
+		free(workblockdevicepath);
+	}
+	if (s)
+		free(s);
+	return ret;
+
+umount_out:
+	loop_umount(pc->buildplace);
+	if (s)
+		free(s);
+	return ret;
 }
 
 /*
  * @return: return code of pbuilder, or <0 on failure
  */
-int cpbuilder_build(const struct pbuilderconfig* pc, const char* dscfile)
-{
-  int ret;
-  char* hoststr=NULL;
-  char* hoststr2=NULL;
-  char* commandline=NULL;
-  const char* buildopt=NULL;
-  const char* debbuildopts=NULL;
-  char* debbuildopts_work=NULL;
-
-  if (pc->binary_arch) {
-    asprintf(&debbuildopts_work, "%s -B", pc->debbuildopts);
-    debbuildopts=debbuildopts_work;
-    buildopt="--binary-arch";
-  } else if (pc->binary_indep) {
-    asprintf(&debbuildopts_work, "%s -A", pc->debbuildopts);
-    debbuildopts=debbuildopts_work;
-    buildopt="--binary-indep";
-  } else {
-    debbuildopts=pc->debbuildopts;
-    buildopt="--binary-all";
-  }
-
-  hoststr=copy_dscfile(dscfile, pc->buildplace);
-
-  asprintf(&commandline,
-	   EXECUTE_HOOKS("D")
-	   "ALLOWUNTRUSTED=%s /usr/lib/pbuilder/pbuilder-satisfydepends --control $BUILDDIR/*.dsc --internal-chrootexec 'chroot . ' %s \n"
-	   "apt-get install %s -y %s\n"
-	   "cd $BUILDDIR; /usr/bin/dpkg-source -x $(basename %s) \n"
-	   "log.i 'Building the package'\n"
-	   EXECUTE_HOOKS("A")
-	   "if ! (\n"
-	   "    cd $BUILDDIR/*-*/\n"
-	   "    dpkg-buildpackage -us -uc %s\n"
-	   "); then\n"
-	   EXECUTE_HOOKS_INDENT("    ", "C")
-	   "    exit_from_qemu 1\n"
-	   "fi\n"
-	   EXECUTE_HOOKS("B"),
-	   pc->allow_untrusted?"yes":"no",
-	   buildopt,
-	   pc->allow_untrusted?"--force-yes":"",
-	   pc->extrapackages?pc->extrapackages:"",
-	   dscfile,
-	   debbuildopts);
-
-  /* Obscure assumption!: assume _ is significant for package name and
-     no other file will have _. */
-
-  asprintf(&hoststr2,
-	   "BUILDPLACE='%s'\n"
-	   "BUILDRESULT='%s'\n"
-	   "BUILDRESULTUID=%d\n"
-	   "BUILDRESULTGID=%d\n"
-	   "if [ -d \"${BUILDRESULT}\" ]; then\n"
-	   "    chown \"${BUILDRESULTUID}:${BUILDRESULTGID}\" \"${BUILDPLACE}$BUILDDIR/\"*\n"
-	   "    chgrp \"${BUILDRESULTGID}\" \"${BUILDPLACE}$BUILDDIR/\"*\n"
-	   "    for FILE in \"${BUILDPLACE}$BUILDDIR\"/*; do\n"
-	   "        if [ -f \"${FILE}\" ]; then\n"
-	   "            cp -p \"${FILE}\" \"${BUILDRESULT}\" || true\n"
-	   "        fi\n"
-	   "    done\n"
-	   "else\n"
-	   "    log.e \"BUILDRESULT=[$BUILDRESULT] is not a directory.\"\n"
-	   "fi\n",
-	   pc->buildplace, pc->buildresult,
-	   pc->buildresultuid, pc->buildresultgid);
-
-  ret=run_second_stage_script
-    (0,
-     commandline, pc,
-     hoststr,
-     hoststr2);
-
-  if(debbuildopts_work) free(debbuildopts_work);
-  if(hoststr2) free(hoststr2);
-  if(hoststr) free(hoststr);
-  if(commandline) free(commandline);
-  return ret;
+int cpbuilder_build(const struct pbuilderconfig *pc, const char *dscfile) {
+	int ret;
+	char *hoststr = NULL;
+	char *hoststr2 = NULL;
+	char *commandline = NULL;
+	const char *buildopt = NULL;
+	const char *debbuildopts = NULL;
+	char *debbuildopts_work = NULL;
+
+	if (pc->binary_arch) {
+		asprintf(&debbuildopts_work, "%s -B", pc->debbuildopts);
+		debbuildopts = debbuildopts_work;
+		buildopt = "--binary-arch";
+	} else if (pc->binary_indep) {
+		asprintf(&debbuildopts_work, "%s -A", pc->debbuildopts);
+		debbuildopts = debbuildopts_work;
+		buildopt = "--binary-indep";
+	} else {
+		debbuildopts = pc->debbuildopts;
+		buildopt = "--binary-all";
+	}
+
+	hoststr = copy_dscfile(dscfile, pc->buildplace);
+
+	asprintf(
+		&commandline,
+		EXECUTE_HOOKS("D")
+		"ALLOWUNTRUSTED=%s /usr/lib/pbuilder/pbuilder-satisfydepends --control $BUILDDIR/*.dsc --internal-chrootexec 'chroot . ' %s \n"
+		"apt-get install %s -y %s\n"
+		"cd $BUILDDIR; /usr/bin/dpkg-source -x $(basename %s) \n"
+		"log.i 'Building the package'\n"
+		EXECUTE_HOOKS("A")
+		"if ! (\n"
+		"    cd $BUILDDIR/*-*/\n"
+		"    dpkg-buildpackage -us -uc %s\n"
+		"); then\n"
+		EXECUTE_HOOKS_INDENT("    ", "C")
+		"    exit_from_qemu 1\n"
+		"fi\n"
+		EXECUTE_HOOKS("B"),
+		pc->allow_untrusted ? "yes" : "no",
+		buildopt,
+		pc->allow_untrusted ? "--force-yes" : "",
+		pc->extrapackages ? pc->extrapackages : "",
+		dscfile,
+		debbuildopts);
+
+	/* Obscure assumption!: assume _ is significant for package name
+	 * and no other file will have _. */
+
+	asprintf(
+		&hoststr2,
+		"BUILDPLACE='%s'\n"
+		"BUILDRESULT='%s'\n"
+		"BUILDRESULTUID=%d\n"
+		"BUILDRESULTGID=%d\n"
+		"if [ -d \"${BUILDRESULT}\" ]; then\n"
+		"    chown \"${BUILDRESULTUID}:${BUILDRESULTGID}\" \"${BUILDPLACE}$BUILDDIR/\"*\n"
+		"    chgrp \"${BUILDRESULTGID}\" \"${BUILDPLACE}$BUILDDIR/\"*\n"
+		"    for FILE in \"${BUILDPLACE}$BUILDDIR\"/*; do\n"
+		"        if [ -f \"${FILE}\" ]; then\n"
+		"            cp -p \"${FILE}\" \"${BUILDRESULT}\" || true\n"
+		"        fi\n"
+		"    done\n"
+		"else\n"
+		"    log.e \"BUILDRESULT=[$BUILDRESULT] is not a directory.\"\n"
+		"fi\n",
+		pc->buildplace,
+		pc->buildresult,
+		pc->buildresultuid,
+		pc->buildresultgid);
+
+	ret = run_second_stage_script(0, commandline, pc, hoststr, hoststr2);
+
+	if (debbuildopts_work)
+		free(debbuildopts_work);
+	if (hoststr2)
+		free(hoststr2);
+	if (hoststr)
+		free(hoststr);
+	if (commandline)
+		free(commandline);
+	return ret;
 }
 
-int cpbuilder_login(const struct pbuilderconfig* pc)
-{
-  return run_second_stage_script(pc->save_after_login,
-				 EXECUTE_HOOKS("H")
-				 EXECUTE_HOOKS("F")
-				 "bash",
-				 pc,
-				 NULL,
-				 NULL);
+int cpbuilder_login(const struct pbuilderconfig *pc) {
+	return run_second_stage_script(pc->save_after_login,
+								   EXECUTE_HOOKS("H")
+								   EXECUTE_HOOKS("F")
+								   "bash",
+								   pc,
+								   NULL,
+								   NULL);
 }
 
 /*
@@ -1384,30 +1314,30 @@ int cpbuilder_login(const struct pbuilderconfig* pc)
 Mostly a copy of pbuilder login, executes a script.
 
  */
-int cpbuilder_execute(const struct pbuilderconfig* pc, char** av)
-{
-  char* hostcommand;
-  char* runcommandline;
-  int ret;
-
-  asprintf(&hostcommand,
-	   "[ -d %s/input ] || mkdir %s/input\n || log.e \"Failed to create directory '%s/input'\"\n"
-	   "cp %s %s/input/runscript\n",
-	   pc->buildplace, pc->buildplace, pc->buildplace,
-	   av[0], pc->buildplace);
-  /* TODO: add options too */
-  asprintf(&runcommandline,
-	   EXECUTE_HOOKS("H")
-	   EXECUTE_HOOKS("F")
-	   "sh $BUILDDIR/input/runscript");
-  ret=run_second_stage_script(pc->save_after_login,
-			      runcommandline,
-			      pc,
-			      hostcommand,
-			      NULL);
-  free(hostcommand);
-  free(runcommandline);
-  return ret;
+int cpbuilder_execute(const struct pbuilderconfig *pc, char **av) {
+	char *hostcommand;
+	char *runcommandline;
+	int ret;
+
+	asprintf(
+		&hostcommand,
+		"[ -d %s/input ] || mkdir %s/input\n || log.e \"Failed to create directory '%s/input'\"\n"
+		"cp %s %s/input/runscript\n",
+		pc->buildplace,
+		pc->buildplace,
+		pc->buildplace,
+		av[0],
+		pc->buildplace);
+	/* TODO: add options too */
+	asprintf(&runcommandline,
+			 EXECUTE_HOOKS("H")
+			 EXECUTE_HOOKS("F")
+			 "sh $BUILDDIR/input/runscript");
+	ret = run_second_stage_script(
+		pc->save_after_login, runcommandline, pc, hostcommand, NULL);
+	free(hostcommand);
+	free(runcommandline);
+	return ret;
 }
 
 /**
@@ -1415,103 +1345,91 @@ int cpbuilder_execute(const struct pbuilderconfig* pc, char** av)
 
    @return 0 on success, other values on failure.
  */
-int cpbuilder_update(const struct pbuilderconfig* pc)
-{
-  int ret;
-
-  ret = do_fsck(pc->basepath);
-  if (ret)
-    {
-      log_printf(log_error, "Failed fsck '%s'", pc->basepath);
-      return ret;
-    }
-
-  if (mkdir(pc->buildplace,0777))
-    {
-      log_printf(log_error,
-	  "Could not create directory '%s': %s",
-	  pc->buildplace,
-	  strerror(errno));
-      return 1;
-    }
-
-  ret = loop_mount(pc->basepath, pc->buildplace);
-  if (ret)
-    {
-      log_printf(log_error,
-	  "Could not mount '%s' on '%s'",
-	  pc->basepath,
-	  pc->buildplace);
-      return ret;
-    }
-  FILE *g = create_script(pc->buildplace, "pbuilder-run");
-  write_first_stage(g, pc);
-  fclose(g);
-  ret = loop_umount(pc->buildplace);
-  if (ret)
-    {
-      log_printf(log_error,
-	  "Could not unmount '%s'",
-	  pc->buildplace);
-      return ret;
-    }
-  ret = rmdir(pc->buildplace);
-  if (ret)
-    {
-      log_printf(log_error,
-	  "Could not rmdir '%s'",
-	  pc->buildplace);
-      return ret;
-    }
-
-  /* TODO: --override-config support, --othermirror support etc.
-     There is no way to change distribution in this code-path...
-   */
-  char *script;
-  if (0>asprintf(&script,
-		 EXECUTE_HOOKS("H")
-		 //TODO: installaptlines if required.
-		 //TODO: "dpkg --purge $REMOVEPACKAGES\n"
-		 //TODO: add error code handling.
-		 "apt-get update -o Acquire::PDiffs=false\n"
-		 "apt-get -y %s -o DPkg::Options::=--force-confnew dist-upgrade\n"
-		 "apt-get install %s -y build-essential dpkg-dev apt aptitude pbuilder %s\n"
-		 //TODO: optionally autoclean aptcache
-		 EXECUTE_HOOKS("E")
-		 , pc->allow_untrusted?"--force-yes":""
-		 , pc->allow_untrusted?"--force-yes":""
-		 , pc->extrapackages?pc->extrapackages:""))
-    {
-      log_printf(log_error, "qemubuilder: out of memory.");
-      return 1;
-    }
-
-  ret = run_second_stage_script(1, script, pc, NULL, NULL);
-  free(script);
-  return ret;
+int cpbuilder_update(const struct pbuilderconfig *pc) {
+	int ret;
+
+	ret = do_fsck(pc->basepath);
+	if (ret) {
+		log_printf(log_error, "Failed fsck '%s'", pc->basepath);
+		return ret;
+	}
+
+	if (mkdir(pc->buildplace, 0777)) {
+		log_printf(log_error,
+				   "Could not create directory '%s': %s",
+				   pc->buildplace,
+				   strerror(errno));
+		return 1;
+	}
+
+	ret = loop_mount(pc->basepath, pc->buildplace);
+	if (ret) {
+		log_printf(log_error,
+				   "Could not mount '%s' on '%s'",
+				   pc->basepath,
+				   pc->buildplace);
+		return ret;
+	}
+	FILE *g = create_script(pc->buildplace, "pbuilder-run");
+	write_first_stage(g, pc);
+	fclose(g);
+	ret = loop_umount(pc->buildplace);
+	if (ret) {
+		log_printf(log_error, "Could not unmount '%s'", pc->buildplace);
+		return ret;
+	}
+	ret = rmdir(pc->buildplace);
+	if (ret) {
+		log_printf(log_error, "Could not rmdir '%s'", pc->buildplace);
+		return ret;
+	}
+
+	/* TODO: --override-config support, --othermirror support etc.
+	 * There is no way to change distribution in this code-path...
+	 */
+	char *script;
+	if (0 >
+		asprintf(
+			&script,
+			EXECUTE_HOOKS("H")
+			//TODO: installaptlines if required.
+			//TODO: "dpkg --purge $REMOVEPACKAGES\n"
+			//TODO: add error code handling.
+			"apt-get update -o Acquire::PDiffs=false\n"
+			"apt-get -y %s -o DPkg::Options::=--force-confnew dist-upgrade\n"
+			"apt-get install %s -y build-essential dpkg-dev apt aptitude pbuilder %s\n"
+			//TODO: optionally autoclean aptcache
+			EXECUTE_HOOKS("E"),
+			pc->allow_untrusted ? "--force-yes" : "",
+			pc->allow_untrusted ? "--force-yes" : "",
+			pc->extrapackages ? pc->extrapackages : "")) {
+		log_printf(log_error, "qemubuilder: out of memory.");
+		return 1;
+	}
+
+	ret = run_second_stage_script(1, script, pc, NULL, NULL);
+	free(script);
+	return ret;
 }
 
-int cpbuilder_help(void)
-{
-  printf("qemubuilder [operation] [options]\n"
-	 "operation:\n"
-	 " --build\n"
-	 " --create\n"
-	 " --update\n"
-	 " --login\n"
-	 " --execute\n"
-	 " --help\n"
-	 " --dumpconfig\n"
-	 "options:\n"
-	 " --basepath:\n"
-	 " --buildplace:\n"
-	 " --distribution:\n"
-	 " ... and other pbuilder options \n"
-	 );
-  return 0;
+int cpbuilder_help(void) {
+	printf("qemubuilder [operation] [options]\n"
+		   "operation:\n"
+		   " --build\n"
+		   " --create\n"
+		   " --update\n"
+		   " --login\n"
+		   " --execute\n"
+		   " --help\n"
+		   " --dumpconfig\n"
+		   "options:\n"
+		   " --basepath:\n"
+		   " --buildplace:\n"
+		   " --distribution:\n"
+		   " ... and other pbuilder options \n");
+	return 0;
 }
 
-int app_main(int ac, char** av)
-{
-  return parse_parameter(ac, av, "qemu");
+int app_main(int ac, char **av) {
+	return parse_parameter(ac, av, "qemu");
 }
diff --git a/qemuipsanitize.c b/qemuipsanitize.c
index 951ef67..fb162d3 100644
--- a/qemuipsanitize.c
+++ b/qemuipsanitize.c
@@ -10,66 +10,59 @@
 #include "qemuipsanitize.h"
 #include "log.h"
 
-const char* sanitize_ipaddress(const char*addr)
-{
-  /* return 10.0.2.2 (qemu host OS address) if localhost */
-  const char* local_host="10.0.2.2";
-  struct hostent* h;
-  h=gethostbyname(addr);
-  if(h)
-    {
-      if (h->h_addr[0]==127)
-	{
-	  return local_host;
+const char *sanitize_ipaddress(const char *addr) {
+	/* return 10.0.2.2 (qemu host OS address) if localhost */
+	const char *local_host = "10.0.2.2";
+	struct hostent *h;
+	h = gethostbyname(addr);
+	if (h) {
+		if (h->h_addr[0] == 127) {
+			return local_host;
+		}
 	}
-    }
-  return addr;
+	return addr;
 }
 
-static char* r_offstr(regmatch_t m, const char*s)
-{
-  char*r;
-  if(m.rm_so!=-1)
-    {
-      r=strdup(s+m.rm_so);
-      r[m.rm_eo-m.rm_so]=0;
-      return r;
-    }
-  else return strdup("");
+static char *r_offstr(regmatch_t m, const char *s) {
+	char *r;
+	if (m.rm_so != -1) {
+		r = strdup(s + m.rm_so);
+		r[m.rm_eo - m.rm_so] = 0;
+		return r;
+	} else
+		return strdup("");
 }
 
-char* sanitize_mirror(const char*addr)
-{
-  /* parse IP address string */
-  regex_t r;
-  int e;
-  regmatch_t m[5];
-  char*buf=NULL;
-  char* a;
-  char* b;
-  char* c;
-  char* d;
-  
-  if((e=regcomp(&r, "^([^:]*://)([^:/]*)(:[0-9]+)?(.*)$", REG_EXTENDED)))
-    {
-      /* error */
-      log_printf(log_error, "failed compiling regexp: %i", e);
-      return strdup(addr);
-    }
-  if((e=regexec(&r, addr, 5, m, 0)))
-    {
-      log_printf(log_error, "failed regexp match: %i", e);
-      return strdup(addr);
-    }
-  asprintf(&buf,"%s%s%s%s",
-	   a=r_offstr(m[1],addr),
-	   sanitize_ipaddress(b=r_offstr(m[2],addr)),
-	   c=r_offstr(m[3],addr),
-	   d=r_offstr(m[4],addr));
-  free(a);
-  free(b);
-  free(c);
-  free(d);
-  regfree(&r);
-  return buf;
+char *sanitize_mirror(const char *addr) {
+	/* parse IP address string */
+	regex_t r;
+	int e;
+	regmatch_t m[5];
+	char *buf = NULL;
+	char *a;
+	char *b;
+	char *c;
+	char *d;
+
+	if ((e = regcomp(&r, "^([^:]*://)([^:/]*)(:[0-9]+)?(.*)$", REG_EXTENDED))) {
+		/* error */
+		log_printf(log_error, "failed compiling regexp: %i", e);
+		return strdup(addr);
+	}
+	if ((e = regexec(&r, addr, 5, m, 0))) {
+		log_printf(log_error, "failed regexp match: %i", e);
+		return strdup(addr);
+	}
+	asprintf(&buf,
+			 "%s%s%s%s",
+			 a = r_offstr(m[1], addr),
+			 sanitize_ipaddress(b = r_offstr(m[2], addr)),
+			 c = r_offstr(m[3], addr),
+			 d = r_offstr(m[4], addr));
+	free(a);
+	free(b);
+	free(c);
+	free(d);
+	regfree(&r);
+	return buf;
 }
diff --git a/qemuipsanitize.h b/qemuipsanitize.h
index ebe7999..c449469 100644
--- a/qemuipsanitize.h
+++ b/qemuipsanitize.h
@@ -5,11 +5,10 @@
 
  */
 
-
 #ifndef __QEMUIPSANITIZE_H__
 #define __QEMUIPSANITIZE_H__
 
-const char* sanitize_ipaddress(const char*addr);
-char* sanitize_mirror(const char*addr);
+const char *sanitize_ipaddress(const char *addr);
+char *sanitize_mirror(const char *addr);
 
 #endif
diff --git a/test_cowbuilder.c b/test_cowbuilder.c
old mode 100755
new mode 100644
index f9a03cf..c7f9434
--- a/test_cowbuilder.c
+++ b/test_cowbuilder.c
@@ -9,34 +9,31 @@
 #include "parameter.h"
 
 // utility to check hardlink count of file.
-static int check_hardlink_count(const char* filename)
-{
-  struct stat buf;
-  stat(filename, &buf);
-  return buf.st_nlink;
+static int check_hardlink_count(const char *filename) {
+	struct stat buf;
+	stat(filename, &buf);
+	return buf.st_nlink;
 }
 
-int break_cowlink(const char* s);
+int break_cowlink(const char *s);
 
-void test_break_cowlink()
-{
-  char *temp = strdupa("/tmp/cowtestXXXXXX");
-  char *temp2 = strdupa("/tmp/cowtestXXXXXX");
-  close(mkstemp(temp));
-  close(mkstemp(temp2));
-  assert(check_hardlink_count(temp2) == 1);
-  assert(check_hardlink_count(temp) == 1);
-  assert(-1!=unlink(temp2));
-  assert(-1!=link(temp, temp2));
-  assert(check_hardlink_count(temp2) == 2);
-  assert(check_hardlink_count(temp) == 2);
-  break_cowlink(temp2);
-  assert(check_hardlink_count(temp2) == 1);
-  assert(check_hardlink_count(temp) == 1);
+void test_break_cowlink() {
+	char *temp = strdupa("/tmp/cowtestXXXXXX");
+	char *temp2 = strdupa("/tmp/cowtestXXXXXX");
+	close(mkstemp(temp));
+	close(mkstemp(temp2));
+	assert(check_hardlink_count(temp2) == 1);
+	assert(check_hardlink_count(temp) == 1);
+	assert(-1 != unlink(temp2));
+	assert(-1 != link(temp, temp2));
+	assert(check_hardlink_count(temp2) == 2);
+	assert(check_hardlink_count(temp) == 2);
+	break_cowlink(temp2);
+	assert(check_hardlink_count(temp2) == 1);
+	assert(check_hardlink_count(temp) == 1);
 }
 
-int main()
-{
-  test_break_cowlink();
-  return 0;
+int main() {
+	test_break_cowlink();
+	return 0;
 }
diff --git a/test_cowbuilder_util.c b/test_cowbuilder_util.c
old mode 100755
new mode 100644
index 5c3ec9b..e96fec8
--- a/test_cowbuilder_util.c
+++ b/test_cowbuilder_util.c
@@ -4,60 +4,59 @@
 #include <assert.h>
 #include <string.h>
 
-
 void test_check_mountpoint() {
-  // something is usually mounted under /.
-  if(0 == check_mountpoint("/")) {
-    fprintf(stderr, 
-	    "*******************************************************************************\n"
-	    "* '/' is not mounted, something is wrong with the system or the code\n"
-	    "*******************************************************************************\n");
-  }
-  // something is usually mounted under /run, but can't be sure.
-  // assert(1 == check_mountpoint("/run")); // commented out so that I don't break automatic builds.
-  // usually nothing is mounted under /tmp/nothing-is-mounted
-  fprintf(stderr, "The following message is expected:\n");
-  assert(0 == check_mountpoint("/tmp/nothing-is-mounted"));
+	// something is usually mounted under /.
+	if (0 == check_mountpoint("/")) {
+		fprintf(
+			stderr,
+			"*******************************************************************************\n"
+			"* '/' is not mounted, something is wrong with the system or the code\n"
+			"*******************************************************************************\n");
+	}
+	// something is usually mounted under /run, but can't be sure.
+	// assert(1 == check_mountpoint("/run")); // commented out so that I don't break automatic builds.
+	// usually nothing is mounted under /tmp/nothing-is-mounted
+	fprintf(stderr, "The following message is expected:\n");
+	assert(0 == check_mountpoint("/tmp/nothing-is-mounted"));
 }
 
 /** 
  * make sure strings are equal, and return true when they are. Just to
  * save myself from strcmp being a macro.
  */
-int verify_string_equal(const char* a, const char* b) {
-  return !strcmp(a, b);
+int verify_string_equal(const char *a, const char *b) {
+	return !strcmp(a, b);
 }
 
 void test_canonicalize_doubleslash() {
-  char dest[256];
-  canonicalize_doubleslash("/no/double/slash", dest);
-  assert(verify_string_equal(dest, "/no/double/slash"));
-  canonicalize_doubleslash("/trailing/slash/", dest);
-  assert(verify_string_equal(dest, "/trailing/slash/"));
-  canonicalize_doubleslash("//starting/double/slash/", dest);
-  assert(verify_string_equal(dest, "/starting/double/slash/"));
-  canonicalize_doubleslash("/double//slash", dest);
-  assert(verify_string_equal(dest, "/double/slash"));
-  canonicalize_doubleslash("/more//double//slash", dest);
-  assert(verify_string_equal(dest, "/more/double/slash"));
-  canonicalize_doubleslash("no/starting/slash//", dest);
-  assert(verify_string_equal(dest, "no/starting/slash/"));
-  canonicalize_doubleslash("///", dest);
-  assert(verify_string_equal(dest, "/"));
-
-  const char* test_buffer_overrun = "/some/string";
-  strcpy(dest,                      "-some-string+g");
-  canonicalize_doubleslash(test_buffer_overrun, dest);
-  assert(verify_string_equal(dest, "/some/string"));
-  assert(strlen(dest) == 12);
-  assert(dest[12] == 0);
-  assert(dest[13] == 'g');
+	char dest[256];
+	canonicalize_doubleslash("/no/double/slash", dest);
+	assert(verify_string_equal(dest, "/no/double/slash"));
+	canonicalize_doubleslash("/trailing/slash/", dest);
+	assert(verify_string_equal(dest, "/trailing/slash/"));
+	canonicalize_doubleslash("//starting/double/slash/", dest);
+	assert(verify_string_equal(dest, "/starting/double/slash/"));
+	canonicalize_doubleslash("/double//slash", dest);
+	assert(verify_string_equal(dest, "/double/slash"));
+	canonicalize_doubleslash("/more//double//slash", dest);
+	assert(verify_string_equal(dest, "/more/double/slash"));
+	canonicalize_doubleslash("no/starting/slash//", dest);
+	assert(verify_string_equal(dest, "no/starting/slash/"));
+	canonicalize_doubleslash("///", dest);
+	assert(verify_string_equal(dest, "/"));
+
+	const char *test_buffer_overrun = "/some/string";
+	strcpy(dest, "-some-string+g");
+	canonicalize_doubleslash(test_buffer_overrun, dest);
+	assert(verify_string_equal(dest, "/some/string"));
+	assert(strlen(dest) == 12);
+	assert(dest[12] == 0);
+	assert(dest[13] == 'g');
 }
 
-int main()
-{
-  test_check_mountpoint();
-  test_canonicalize_doubleslash();
+int main() {
+	test_check_mountpoint();
+	test_canonicalize_doubleslash();
 
-  return 0;
+	return 0;
 }
diff --git a/test_file.c b/test_file.c
old mode 100755
new mode 100644
index e60971a..fe59dea
--- a/test_file.c
+++ b/test_file.c
@@ -8,63 +8,50 @@
 #include "file.h"
 #include "parameter.h"
 
-void test_copy_file()
-{
-  char* temp=strdupa("/tmp/testfileXXXXXX");
-  mkstemp(temp);
-  assert(copy_file("/proc/mounts", temp)==0);
-  assert(forkexeclp("diff", "diff", "-u", "/proc/mounts", 
-		    temp, NULL)==0);
-  assert(copy_file("/proc/mounts", "/dev/path/does/not/exist")==-1);
+void test_copy_file() {
+	char *temp = strdupa("/tmp/testfileXXXXXX");
+	mkstemp(temp);
+	assert(copy_file("/proc/mounts", temp) == 0);
+	assert(forkexeclp("diff", "diff", "-u", "/proc/mounts", temp, NULL) == 0);
+	assert(copy_file("/proc/mounts", "/dev/path/does/not/exist") == -1);
 }
 
-void test_create_sparse_file()
-{
-  char* temp=strdupa("/tmp/sparseXXXXXX");
-  mkstemp(temp);
-  assert(create_sparse_file(temp,18UL*1UL<<30UL)==0);
+void test_create_sparse_file() {
+	char *temp = strdupa("/tmp/sparseXXXXXX");
+	mkstemp(temp);
+	assert(create_sparse_file(temp, 18UL * 1UL << 30UL) == 0);
 }
 
-void test_fail_create_sparse_file()
-{
-  assert(create_sparse_file("/tmp/nonexisting/file/path/here",18UL*1UL<<30UL)==1);
+void test_fail_create_sparse_file() {
+	assert(create_sparse_file("/tmp/nonexisting/file/path/here",
+							  18UL * 1UL << 30UL) == 1);
 }
 
-int test_mknod_inside_chroot()
-{
-  /* if you are running this in normal user, or running through 
-     fakeroot, you would (probably) get this */
-  if (getuid()!=0 || 
-      (getenv("FAKEROOTKEY") && strcmp(getenv("FAKEROOTKEY"),"")))
-    {
-      assert(mknod_inside_chroot("/root", "/dev", 
-				 S_IFCHR, makedev(204, 64))
-	     == -1);
-    }
-  else 
-    {
-      /* if you are running this as root, 
-	 this would be the tested codepath.
-      */
-      struct stat s;
-      umask(S_IWOTH);
-      unlink("/root/dev5");
-      assert(mknod_inside_chroot("/root", 
-				 "/dev5", 
-				 S_IFCHR | 0660, 
-				 makedev(204, 64))
-	     == 0);
-      assert(stat("/root/dev5", &s)==0);
-      assert(S_ISCHR(s.st_mode));
-    }
-  return 0;
+int test_mknod_inside_chroot() {
+	/* if you are running this in normal user, or running through
+	 * fakeroot, you would (probably) get this */
+	if (getuid() != 0 ||
+		(getenv("FAKEROOTKEY") && strcmp(getenv("FAKEROOTKEY"), ""))) {
+		assert(mknod_inside_chroot(
+				   "/root", "/dev", S_IFCHR, makedev(204, 64)) == -1);
+	} else {
+		/* if you are running this as root,
+		 * this would be the tested codepath. */
+		struct stat s;
+		umask(S_IWOTH);
+		unlink("/root/dev5");
+		assert(mknod_inside_chroot(
+				   "/root", "/dev5", S_IFCHR | 0660, makedev(204, 64)) == 0);
+		assert(stat("/root/dev5", &s) == 0);
+		assert(S_ISCHR(s.st_mode));
+	}
+	return 0;
 }
 
-int main() 
-{
-  test_mknod_inside_chroot();
-  test_copy_file();
-  test_create_sparse_file();
-  test_fail_create_sparse_file();
-  return 0;
+int main() {
+	test_mknod_inside_chroot();
+	test_copy_file();
+	test_create_sparse_file();
+	test_fail_create_sparse_file();
+	return 0;
 }
diff --git a/test_forkexec.c b/test_forkexec.c
old mode 100755
new mode 100644
index 0aec386..2dd8050
--- a/test_forkexec.c
+++ b/test_forkexec.c
@@ -5,41 +5,18 @@
 #include <assert.h>
 #include "parameter.h"
 
-int main()
-{
-  char *const successvp[] = 
-    {
-      "/bin/true", 
-      NULL
-    };
-  char *const failurevp[] = 
-    {
-      "/bin/false", 
-      NULL
-    };
-  char *const echovp[] = 
-    {
-      "/bin/echo", 
-      "Hello world",
-      NULL
-    };
-  
-  assert(forkexecvp(successvp) == 0);
-  assert(forkexecvp(failurevp) == 1);
-  assert(forkexecvp(echovp) == 0);
+int main() {
+	char *const successvp[] = {"/bin/true", NULL};
+	char *const failurevp[] = {"/bin/false", NULL};
+	char *const echovp[] = {"/bin/echo", "Hello world", NULL};
 
-  assert(forkexeclp("/bin/true",
-		    "/bin/true",
-		    NULL) == 0);
-  assert(forkexeclp("/bin/false",
-		    "/bin/false",
-		    NULL) == 1);
-  assert(forkexeclp("/bin/echo",
-		    "/bin/echo",
-		    "Hello World",
-		    NULL) == 0);
-  
-  return 0;
-}
+	assert(forkexecvp(successvp) == 0);
+	assert(forkexecvp(failurevp) == 1);
+	assert(forkexecvp(echovp) == 0);
 
+	assert(forkexeclp("/bin/true", "/bin/true", NULL) == 0);
+	assert(forkexeclp("/bin/false", "/bin/false", NULL) == 1);
+	assert(forkexeclp("/bin/echo", "/bin/echo", "Hello World", NULL) == 0);
 
+	return 0;
+}
diff --git a/test_ilistcreate.c b/test_ilistcreate.c
old mode 100755
new mode 100644
index 3c86ac5..91cde5e
--- a/test_ilistcreate.c
+++ b/test_ilistcreate.c
@@ -8,10 +8,9 @@
 #include <assert.h>
 #include "ilist.h"
 
-const char* ilist_PRGNAME="testcode";
+const char *ilist_PRGNAME = "testcode";
 
-int main()
-{
-  ilist_outofmemory("hello world\n");
-  return 0;
+int main() {
+	ilist_outofmemory("hello world\n");
+	return 0;
 }
diff --git a/test_parameter.c b/test_parameter.c
old mode 100755
new mode 100644
index dded519..a8cb75b
--- a/test_parameter.c
+++ b/test_parameter.c
@@ -6,39 +6,32 @@
 /*
    Mock functions
  */
-int cpbuilder_check_config(const struct pbuilderconfig* pc)
-{
-  return 0;
+int cpbuilder_check_config(const struct pbuilderconfig *pc) {
+	return 0;
 }
 
-int cpbuilder_build(const struct pbuilderconfig* pc, const char* dscfile)
-{
-  return 0;
+int cpbuilder_build(const struct pbuilderconfig *pc, const char *dscfile) {
+	return 0;
 }
 
-int cpbuilder_login(const struct pbuilderconfig* pc)
-{
-  return 0;
+int cpbuilder_login(const struct pbuilderconfig *pc) {
+	return 0;
 }
 
-int cpbuilder_execute(const struct pbuilderconfig* pc, char** av)
-{
-  return 0;
+int cpbuilder_execute(const struct pbuilderconfig *pc, char **av) {
+	return 0;
 }
 
-int cpbuilder_update(const struct pbuilderconfig* pc)
-{
-  return 0;
+int cpbuilder_update(const struct pbuilderconfig *pc) {
+	return 0;
 }
 
-int cpbuilder_help(void)
-{
-  return 0;
+int cpbuilder_help(void) {
+	return 0;
 }
 
-int cpbuilder_create(const struct pbuilderconfig* pc)
-{
-  return 0;
+int cpbuilder_create(const struct pbuilderconfig *pc) {
+	return 0;
 }
 /*
    end of mock functions
@@ -47,29 +40,27 @@ int cpbuilder_create(const struct pbuilderconfig* pc)
 /*
   Test size of Null Terminated String Array handling.
  */
-void test_size_of_ntarray()
-{
-  char* test[32];
-  test[0]="test1";
-  test[1]="test2";
-  test[2]=NULL;
-  assert(size_of_ntarray(test)==2);
+void test_size_of_ntarray() {
+	char *test[32];
+	test[0] = "test1";
+	test[1] = "test2";
+	test[2] = NULL;
+	assert(size_of_ntarray(test) == 2);
 }
 
 /*
   Test handling of quoted parameter string.
  */
-void test_load_quoted_config()
-{
-  pbuilderconfig pc;
-  assert(0 == load_config_file("tests/102_test_cowbuilder_debbuildopts.config", &pc));
-  assert(!strcmp("-j2 -I", pc.debbuildopts));
-  assert(!strcmp("/boot/vmlinuz-x.y.z", pc.kernel_image));
+void test_load_quoted_config() {
+	pbuilderconfig pc;
+	assert(0 == load_config_file(
+					"tests/102_test_cowbuilder_debbuildopts.config", &pc));
+	assert(!strcmp("-j2 -I", pc.debbuildopts));
+	assert(!strcmp("/boot/vmlinuz-x.y.z", pc.kernel_image));
 }
 
-int main()
-{
-  test_size_of_ntarray();
-  test_load_quoted_config();
-  return 0;
+int main() {
+	test_size_of_ntarray();
+	test_load_quoted_config();
+	return 0;
 }
diff --git a/test_qemuarch.c b/test_qemuarch.c
old mode 100755
new mode 100644
index 3a4d846..b230852
--- a/test_qemuarch.c
+++ b/test_qemuarch.c
@@ -13,57 +13,49 @@
 #include <unistd.h>
 #include "qemuarch.h"
 
-int test_get_host_dpkg_arch()
-{
-  /* just try to test x86_64 case */
+int test_get_host_dpkg_arch() {
+/* just try to test x86_64 case */
 #if defined(__x86_64__)
-# if defined(__ILP32__)
-  assert (!strcmp(get_host_dpkg_arch(), "x32"));
-# else
-  assert (!strcmp(get_host_dpkg_arch(), "amd64"));
-# endif
+#if defined(__ILP32__)
+	assert(!strcmp(get_host_dpkg_arch(), "x32"));
+#else
+	assert(!strcmp(get_host_dpkg_arch(), "amd64"));
+#endif
 #elif defined(__i386__)
-  assert ((!strcmp(get_host_dpkg_arch(), "i386")) || (!strcmp(get_host_dpkg_arch(), "lpia")));
+	assert((!strcmp(get_host_dpkg_arch(), "i386")) ||
+		   (!strcmp(get_host_dpkg_arch(), "lpia")));
 #else
-  printf("W: no check for this architecture\n");
+	printf("W: no check for this architecture\n");
 #endif
-  return 0;
+	return 0;
 }
 
-int test_qemu_create_arch_devices()
-{
-  char* temp=strdupa("/tmp/dancerXXXXXX");
-  temp=mkdtemp(temp);
-  printf("%s\n", temp);
+int test_qemu_create_arch_devices() {
+	char *temp = strdupa("/tmp/dancerXXXXXX");
+	temp = mkdtemp(temp);
+	printf("%s\n", temp);
 
-  /* if you are running this in normal user, or running through
-     fakeroot, you would (probably) get this */
-  if (getuid()!=0 ||
-      (getenv("FAKEROOTKEY") && strcmp(getenv("FAKEROOTKEY"),"")))
-    {
-      assert(qemu_create_arch_devices(temp, "x86_64")
-	     < 0);
-    }
-  else
-    {
-      /* if you are running this as root,
-	 this would be the tested codepath.
-      */
-      struct stat s;
-      umask(S_IWOTH);
-      assert(qemu_create_arch_devices(temp, "x86_64")
-	     == 0);
-      chdir(temp);
-      assert(stat("./dev/ttyAMA0", &s)==0);
-      assert(S_ISCHR(s.st_mode));
-    }
-  return 0;
+	/* if you are running this in normal user, or running through
+	 * fakeroot, you would (probably) get this */
+	if (getuid() != 0 ||
+		(getenv("FAKEROOTKEY") && strcmp(getenv("FAKEROOTKEY"), ""))) {
+		assert(qemu_create_arch_devices(temp, "x86_64") < 0);
+	} else {
+		/* if you are running this as root,
+		 * this would be the tested codepath. */
+		struct stat s;
+		umask(S_IWOTH);
+		assert(qemu_create_arch_devices(temp, "x86_64") == 0);
+		chdir(temp);
+		assert(stat("./dev/ttyAMA0", &s) == 0);
+		assert(S_ISCHR(s.st_mode));
+	}
+	return 0;
 }
 
-int main()
-{
-  int val=0;
-  val+=test_get_host_dpkg_arch();
-  val+=test_qemu_create_arch_devices();
-  return val;
+int main() {
+	int val = 0;
+	val += test_get_host_dpkg_arch();
+	val += test_qemu_create_arch_devices();
+	return val;
 }
diff --git a/test_qemuipsanitize.c b/test_qemuipsanitize.c
old mode 100755
new mode 100644
index 878a3c4..6b1bf41
--- a/test_qemuipsanitize.c
+++ b/test_qemuipsanitize.c
@@ -8,42 +8,38 @@
 #include <string.h>
 #include "qemuipsanitize.h"
 
+#define assert_streq(a, b) assert_streq_f(__FILE__, __LINE__, a, b)
 
-#define assert_streq(a,b) \
-  assert_streq_f(__FILE__, __LINE__, a, b)
-
-void assert_streq_f(const char*f, int l, const char* a, const char* b)
-{
-  if(!strcmp(a,b))
-    return;
-  printf("%s:%i: [%s] and [%s] are different\n",
-	 f, l, a, b);
-  exit(1);
+void assert_streq_f(const char *f, int l, const char *a, const char *b) {
+	if (!strcmp(a, b))
+		return;
+	printf("%s:%i: [%s] and [%s] are different\n", f, l, a, b);
+	exit(1);
 }
 
-int test_sanitize_ipaddress()
-{
-  assert_streq("10.0.2.2", sanitize_ipaddress("localhost"));
-  assert_streq("10.0.2.2", sanitize_ipaddress("127.0.0.1"));
-  assert_streq("10.0.2.2", sanitize_ipaddress("127.0.0.2"));
-  assert_streq("www.netfort.gr.jp", sanitize_ipaddress("www.netfort.gr.jp"));
-  return 0;
+int test_sanitize_ipaddress() {
+	assert_streq("10.0.2.2", sanitize_ipaddress("localhost"));
+	assert_streq("10.0.2.2", sanitize_ipaddress("127.0.0.1"));
+	assert_streq("10.0.2.2", sanitize_ipaddress("127.0.0.2"));
+	assert_streq("www.netfort.gr.jp", sanitize_ipaddress("www.netfort.gr.jp"));
+	return 0;
 }
 
-int test_sanitize_mirror()
-{
-  assert_streq("http://www.netfort.gr.jp/debian", sanitize_mirror("http://www.netfort.gr.jp/debian"));
-  assert_streq("http://www.netfort.gr.jp:9999/debian", sanitize_mirror("http://www.netfort.gr.jp:9999/debian"));
-  assert_streq("http://10.0.2.2:9999/debian", sanitize_mirror("http://localhost:9999/debian"));
-  assert_streq("http://10.0.2.2/debian", sanitize_mirror("http://localhost/debian"));
-  return 0;
+int test_sanitize_mirror() {
+	assert_streq("http://www.netfort.gr.jp/debian",
+				 sanitize_mirror("http://www.netfort.gr.jp/debian"));
+	assert_streq("http://www.netfort.gr.jp:9999/debian",
+				 sanitize_mirror("http://www.netfort.gr.jp:9999/debian"));
+	assert_streq("http://10.0.2.2:9999/debian",
+				 sanitize_mirror("http://localhost:9999/debian"));
+	assert_streq("http://10.0.2.2/debian",
+				 sanitize_mirror("http://localhost/debian"));
+	return 0;
 }
 
-int main()
-{
-  int val=0;
-  val+=test_sanitize_ipaddress();
-  val+=test_sanitize_mirror();
-  return val;
+int main() {
+	int val = 0;
+	val += test_sanitize_ipaddress();
+	val += test_sanitize_mirror();
+	return val;
 }
-
diff --git a/tests/002_test_open.c b/tests/002_test_open.c
old mode 100755
new mode 100644
index d8c5289..ef765bc
--- a/tests/002_test_open.c
+++ b/tests/002_test_open.c
@@ -4,26 +4,24 @@ open test */
 #include <unistd.h>
 #include <fcntl.h>
 
-void writeandclose(int a)
-{
-  write(a,"abc",3);
-  close(a);
+void writeandclose(int a) {
+	write(a, "abc", 3);
+	close(a);
 }
 
-int main(int argc, char** argv)
-{
-  /* test that the three args work. */
-  int a,b,c;
-  
-  if(argc < 4)
-    exit(1);
-  
-  a=open(argv[1], O_RDONLY);
-  b=open(argv[2], O_WRONLY);
-  c=open(argv[3], O_RDWR);
-  writeandclose(a);
-  writeandclose(b);
-  writeandclose(c);
+int main(int argc, char **argv) {
+	/* test that the three args work. */
+	int a, b, c;
 
-  return 0;
+	if (argc < 4)
+		exit(1);
+
+	a = open(argv[1], O_RDONLY);
+	b = open(argv[2], O_WRONLY);
+	c = open(argv[3], O_RDWR);
+	writeandclose(a);
+	writeandclose(b);
+	writeandclose(c);
+
+	return 0;
 }
diff --git a/tests/003_test_fopen.c b/tests/003_test_fopen.c
old mode 100755
new mode 100644
index 08386a1..4e1ccde
--- a/tests/003_test_fopen.c
+++ b/tests/003_test_fopen.c
@@ -3,20 +3,18 @@ fopen test */
 #include <stdio.h>
 #include <stdlib.h>
 
-void writeandclose(FILE* a)
-{
-  fprintf(a,"abc\n");
-  fclose(a);
+void writeandclose(FILE *a) {
+	fprintf(a, "abc\n");
+	fclose(a);
 }
 
-int main(int argc, char** argv)
-{
-  /* test that the three args work. */
-  if(argc < 4)
-    exit(1);
-  
-  writeandclose(fopen(argv[1], "r"));
-  writeandclose(fopen(argv[2], "w"));
-  writeandclose(fopen(argv[3], "a"));
-  return 0;
+int main(int argc, char **argv) {
+	/* test that the three args work. */
+	if (argc < 4)
+		exit(1);
+
+	writeandclose(fopen(argv[1], "r"));
+	writeandclose(fopen(argv[2], "w"));
+	writeandclose(fopen(argv[3], "a"));
+	return 0;
 }
diff --git a/tests/004_test_fopen64.c b/tests/004_test_fopen64.c
old mode 100755
new mode 100644
index 08386a1..4e1ccde
--- a/tests/004_test_fopen64.c
+++ b/tests/004_test_fopen64.c
@@ -3,20 +3,18 @@ fopen test */
 #include <stdio.h>
 #include <stdlib.h>
 
-void writeandclose(FILE* a)
-{
-  fprintf(a,"abc\n");
-  fclose(a);
+void writeandclose(FILE *a) {
+	fprintf(a, "abc\n");
+	fclose(a);
 }
 
-int main(int argc, char** argv)
-{
-  /* test that the three args work. */
-  if(argc < 4)
-    exit(1);
-  
-  writeandclose(fopen(argv[1], "r"));
-  writeandclose(fopen(argv[2], "w"));
-  writeandclose(fopen(argv[3], "a"));
-  return 0;
+int main(int argc, char **argv) {
+	/* test that the three args work. */
+	if (argc < 4)
+		exit(1);
+
+	writeandclose(fopen(argv[1], "r"));
+	writeandclose(fopen(argv[2], "w"));
+	writeandclose(fopen(argv[3], "a"));
+	return 0;
 }
diff --git a/tests/012_test_chown.c b/tests/012_test_chown.c
old mode 100755
new mode 100644
index 3678d38..333733b
--- a/tests/012_test_chown.c
+++ b/tests/012_test_chown.c
@@ -13,28 +13,27 @@ Used by 012 and 020 tests.
 //#include <sys/stat.h>
 //#include <fcntl.h>
 
-int main(int argc, char** argv)
-{
-  uid_t uid=getuid();
-  gid_t gid;
-  //int fid;
-  
-  /* test that four args are given. */
-  if(argc < 5)
-    exit(1);
-  
-  gid=atoi(argv[1]);		/* get the gid to change to */
-  
-  printf("chown %s\n", argv[2]);
-  chown(argv[2], uid, gid);
-  
-  //fid=open(argv[3], O_RDONLY);
-  //if (fchown(fid, uid, gid)!=-1)	/* this func will fail */
-  //  return 1;
-  //close(fid);
-
-  printf("lchown %s\n", argv[4]);
-  lchown(argv[4], uid, gid);
-
-  return 0;
+int main(int argc, char **argv) {
+	uid_t uid = getuid();
+	gid_t gid;
+	//int fid;
+
+	/* test that four args are given. */
+	if (argc < 5)
+		exit(1);
+
+	gid = atoi(argv[1]); /* get the gid to change to */
+
+	printf("chown %s\n", argv[2]);
+	chown(argv[2], uid, gid);
+
+	//fid=open(argv[3], O_RDONLY);
+	//if (fchown(fid, uid, gid)!=-1)	/* this func will fail */
+	//  return 1;
+	//close(fid);
+
+	printf("lchown %s\n", argv[4]);
+	lchown(argv[4], uid, gid);
+
+	return 0;
 }
diff --git a/tests/013_test_chmod.c b/tests/013_test_chmod.c
old mode 100755
new mode 100644
index 04e5cdc..abb8500
--- a/tests/013_test_chmod.c
+++ b/tests/013_test_chmod.c
@@ -8,19 +8,18 @@
 #include <fcntl.h>
 
 /* chmod to 0400 */
-int main(int argc, char** argv)
-{
-  int fid;
-  
-  /* test that the two args work. */
-  if(argc < 3)
-    exit(1);
-  
-  chmod(argv[1], 0400);
-  fid=open(argv[2], O_RDONLY);
-  //if (fchmod(fid, 0400)!=-1)
-  //  return 1;			/* this func will fail */
-  close(fid);
-	
-  return 0;
+int main(int argc, char **argv) {
+	int fid;
+
+	/* test that the two args work. */
+	if (argc < 3)
+		exit(1);
+
+	chmod(argv[1], 0400);
+	fid = open(argv[2], O_RDONLY);
+	//if (fchmod(fid, 0400)!=-1)
+	//  return 1;			/* this func will fail */
+	close(fid);
+
+	return 0;
 }
diff --git a/tests/015_test_ilistdelete.c b/tests/015_test_ilistdelete.c
old mode 100755
new mode 100644
index 567c983..cf37c27
--- a/tests/015_test_ilistdelete.c
+++ b/tests/015_test_ilistdelete.c
@@ -25,57 +25,46 @@
 
    The first open should fail, since .ilist doesn't exist.
 */
-static void* openclosetest(void*p)
-{
-  int fd;
-  if (-1==(fd=open("a", O_WRONLY)))
-    {
-      perror("open");
-      return "success";
-    }
-  
-  if (5 !=write(fd, "test\n", 5))
-    {
-      perror("write");
-      return NULL;
-    }
-  if (-1==close(fd))
-    {
-      perror("close");
-      return NULL;
-    }
-  return NULL;
+static void *openclosetest(void *p) {
+	int fd;
+	if (-1 == (fd = open("a", O_WRONLY))) {
+		perror("open");
+		return "success";
+	}
+
+	if (5 != write(fd, "test\n", 5)) {
+		perror("write");
+		return NULL;
+	}
+	if (-1 == close(fd)) {
+		perror("close");
+		return NULL;
+	}
+	return NULL;
 }
 
-int main(int argc, char** argv)
-{
-  /* remove ilist file */
-  void *ret;
-  pthread_t pth;
-  
-  unlink(".ilist");
+int main(int argc, char **argv) {
+	/* remove ilist file */
+	void *ret;
+	pthread_t pth;
 
+	unlink(".ilist");
 
-  /* test behavior when threads exist */
+	/* test behavior when threads exist */
 
-  pthread_create(&pth, NULL, openclosetest, NULL);
-  if (!openclosetest(NULL))
-    {
-      fprintf(stderr, "parent thread failure\n");
-      return 1;
-    }
-  if (!pthread_join(pth, &ret))
-    {
-      if (!ret)
-	{
-	  fprintf(stderr, "child thread failure\n");
-	  return 1;
+	pthread_create(&pth, NULL, openclosetest, NULL);
+	if (!openclosetest(NULL)) {
+		fprintf(stderr, "parent thread failure\n");
+		return 1;
+	}
+	if (!pthread_join(pth, &ret)) {
+		if (!ret) {
+			fprintf(stderr, "child thread failure\n");
+			return 1;
+		}
+	} else {
+		perror("pthread_join");
+		return 1;
 	}
-    }
-  else
-    {
-      perror("pthread_join");
-      return 1;
-    }
-  return 0;
+	return 0;
 }
diff --git a/tests/016_memleakcheck.c b/tests/016_memleakcheck.c
old mode 100755
new mode 100644
index 0fc37e1..1c21f2f
--- a/tests/016_memleakcheck.c
+++ b/tests/016_memleakcheck.c
@@ -20,93 +20,79 @@
 #include <sys/mman.h>
 #include <mcheck.h>
 
-int dump_procmap()
-{
-  FILE*f;
-  int c;
-  char *p;
+int dump_procmap() {
+	FILE *f;
+	int c;
+	char *p;
 
-  if (0>asprintf(&p, "/proc/%jd/maps", (intmax_t) getpid()))
-    {
-      perror("asprintf");
-      return 1;
-    }
+	if (0 > asprintf(&p, "/proc/%jd/maps", (intmax_t)getpid())) {
+		perror("asprintf");
+		return 1;
+	}
 
-  if (!(f=fopen(p, "r")))
-    {
-      perror("fopen");
-      free(p);
-      return 1;
-    }
-  while (EOF!=(c=fgetc(f)))
-    fputc(c,stdout);
-  fclose(f);
-  free(p);
-  return 0;
+	if (!(f = fopen(p, "r"))) {
+		perror("fopen");
+		free(p);
+		return 1;
+	}
+	while (EOF != (c = fgetc(f)))
+		fputc(c, stdout);
+	fclose(f);
+	free(p);
+	return 0;
 }
 
-int main(int argc, char** argv)
-{
-  int fd;
-  int i;
-  FILE* f;
-
-  /* Initialize */
-  if (-1==(fd=open("1/a", O_WRONLY)))
-    {
-      perror("open");
-      return 1;
-    }
-
-  if (5 !=write(fd, "test\n", 5))
-    {
-      perror("write");
-      return 1;
-    }
-  if (-1==close(fd))
-    {
-      perror("close");
-      return 1;
-    }
-
-  /* try open/close 100 times. */
-  mtrace();
+int main(int argc, char **argv) {
+	int fd;
+	int i;
+	FILE *f;
 
-  for (i=0; i<100; ++i)
-    {
-      if (-1==(fd=open("1/b", O_WRONLY)))
-	{
-	  perror("open");
-	  return 1;
+	/* Initialize */
+	if (-1 == (fd = open("1/a", O_WRONLY))) {
+		perror("open");
+		return 1;
 	}
 
-      if (5 !=write(fd, "test\n", 5))
-	{
-	  perror("write");
-	  return 1;
+	if (5 != write(fd, "test\n", 5)) {
+		perror("write");
+		return 1;
 	}
-      if (-1==close(fd))
-	{
-	  perror("close");
-	  return 1;
+	if (-1 == close(fd)) {
+		perror("close");
+		return 1;
 	}
 
-      if (!(f=fopen("1/c", "w")))
-	{
-	  return 1;
-	}
+	/* try open/close 100 times. */
+	mtrace();
 
-      if (5 !=fwrite("test\n", 1, 5, f))
-	{
-	  return 1;
-	}
-      if (EOF==fclose(f))
-	{
-	  return 1;
+	for (i = 0; i < 100; ++i) {
+		if (-1 == (fd = open("1/b", O_WRONLY))) {
+			perror("open");
+			return 1;
+		}
+
+		if (5 != write(fd, "test\n", 5)) {
+			perror("write");
+			return 1;
+		}
+		if (-1 == close(fd)) {
+			perror("close");
+			return 1;
+		}
+
+		if (!(f = fopen("1/c", "w"))) {
+			return 1;
+		}
+
+		if (5 != fwrite("test\n", 1, 5, f)) {
+			return 1;
+		}
+		if (EOF == fclose(f)) {
+			return 1;
+		}
 	}
-    }
 
-  muntrace();
+	muntrace();
 
-  return dump_procmap();
+	return dump_procmap();
 }
diff --git a/tests/018_testcxx.cc b/tests/018_testcxx.cc
old mode 100755
new mode 100644
index 44e3e93..b4bd55f
--- a/tests/018_testcxx.cc
+++ b/tests/018_testcxx.cc
@@ -1,7 +1,6 @@
 #include <fstream>
 
-int main(int argc, char** argv)
-{
-  std::ofstream ofs("1/a");
-  ofs << "Hello world\n";
+int main(int argc, char **argv) {
+	std::ofstream ofs("1/a");
+	ofs << "Hello world\n";
 }
diff --git a/tests/021_test_open.c b/tests/021_test_open.c
old mode 100755
new mode 100644
index 43dc032..655b32d
--- a/tests/021_test_open.c
+++ b/tests/021_test_open.c
@@ -13,15 +13,14 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
-int main(int ac, const char** av)
-{
-  int fd1, fd2;
-  close(0);
-  fd1=open(av[1], O_RDONLY);
-  close(0);
-  fd2=open(av[1], O_RDONLY);
+int main(int ac, const char **av) {
+	int fd1, fd2;
+	close(0);
+	fd1 = open(av[1], O_RDONLY);
+	close(0);
+	fd2 = open(av[1], O_RDONLY);
+
+	printf("close(0)open(): try1: %i, try2: %i\n", fd1, fd2);
 
-  printf("close(0)open(): try1: %i, try2: %i\n", fd1, fd2);
-  
-  return fd1;
+	return fd1;
 }
diff --git a/tests/022_kernel_waitpid.c b/tests/022_kernel_waitpid.c
old mode 100755
new mode 100644
index 83a24cf..b15e6ed
--- a/tests/022_kernel_waitpid.c
+++ b/tests/022_kernel_waitpid.c
@@ -6,29 +6,26 @@
 #include <stdio.h>
 #include <unistd.h>
 
-int main()
-{
-  pid_t pid;
-  int status;
+int main() {
+	pid_t pid;
+	int status;
 
-  switch(pid=fork())
-    {
-    case 0:
-      execl("/bin/cp", "/bin/cp", "-a", "/tmp/test", "/tmp/test2", NULL);
-      exit (0);
+	switch (pid = fork()) {
+		case 0:
+			execl("/bin/cp", "/bin/cp", "-a", "/tmp/test", "/tmp/test2", NULL);
+			exit(0);
 
-    case -1:
-      perror("fork()");
-      exit (1);
+		case -1:
+			perror("fork()");
+			exit(1);
 
-    default:
-      sleep(1);
-      if(-1==waitpid(pid, &status, 0))
-	{
-	  perror("waitpid:cp");
-	  exit(2);
+		default:
+			sleep(1);
+			if (-1 == waitpid(pid, &status, 0)) {
+				perror("waitpid:cp");
+				exit(2);
+			}
+			printf("waitpid: %x\n", status);
+			exit(0);
 	}
-      printf("waitpid: %x\n", status);
-      exit (0);
-    }
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pbuilder/cowdancer.git



More information about the Pbuilder-maint mailing list