--- chrootuid-1.3.orig/chrootuid.c
+++ chrootuid-1.3/chrootuid.c
@@ -50,15 +50,23 @@
 
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
 #include <pwd.h>
 #include <grp.h>
 #include <syslog.h>
+extern char *optarg;
+extern int optind, opterr, optopt;
+
 
 int     main(argc, argv)
 int     argc;
 char  **argv;
 {
     struct passwd *pwd;
+    int interactive = 1;
+    int optstart = 0;
 
     /*
      * Open a channel to the syslog daemon. Older versions of openlog()
@@ -77,45 +85,91 @@
      * No need to make inetd complain, too.
      */
 
-    if (argc < 4) {
-	syslog(LOG_ERR, "usage: %s path user command", argv[0]);
+    /* If we use -i, skip it over and increment optstart */
+    /* we cannot use the getopt library using:
+     * if (getopt(argc, argv, "i") != -1) {
+     * in order to preserve the arguments provided to the command
+     * This means that -i must be the *first* (and only) argument */
+    if ( argv[1] != NULL && strncmp(argv[1], "-i", 2) == 0 ) {
+	interactive = 0;
+        optstart++;
+    }
+
+    if (argc-optstart < 4) {
+	if (interactive) {
+		syslog(LOG_ERR, "usage: %s [-i] path user command", argv[0]);
+	} else {
+	       fprintf(stderr,"usage: %s [-i] path user command\n", argv[0]);
+	       return (1);
+	}
 	return (0);
     }
     /* Must step into the new subtree. */
 
-    if (chdir(argv[1])) {
-	syslog(LOG_ERR, "chdir(%s): %m", argv[1]);
-	return (0);
+    if (chdir(argv[1+optstart])) {
+	if (interactive) {
+		syslog(LOG_ERR, "chdir(%s): %m", argv[1+optstart]);
+		return (0);
+	} else {
+               fprintf(stderr, "chdir(%s): %s\n", argv[1+optstart], strerror(errno));
+	       return (1);
+	}
     }
     /* The user must be known in the *unrestricted* universe... */
 
-    if ((pwd = getpwnam(argv[2])) == 0) {
-	syslog(LOG_ERR, "%s: user unknown", argv[2]);
-	return (0);
+    if ((pwd = getpwnam(argv[2+optstart])) == 0) {
+	if (interactive) {
+		syslog(LOG_ERR, "%s: user unknown", argv[2+optstart]);
+		return (0);
+	} else {
+		fprintf(stderr, "%s: user unknown\n", argv[2+optstart]);
+		return (1);
+	}
     }
     /* initgroups() accesses the group file in the unrestricted universe... */
 
     if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) {
-	syslog(LOG_ERR, "initgroups: %m");
-	return (0);
+	if (interactive) {
+		syslog(LOG_ERR, "initgroups: %m");
+		return (0);
+	} else {
+		fprintf(stderr, "initgroups: %s\n", strerror(errno));
+		return (1);
+	}
     }
     endgrent();
 
     /* Do the chroot() before giving away root privileges. */
 
-    if (chroot(argv[1])) {
-	syslog(LOG_ERR, "chroot(%s): %m", argv[1]);
-	return (0);
+    if (chroot(argv[1+optstart])) {
+	if (interactive) {
+		syslog(LOG_ERR, "chroot(%s): %m", argv[1+optstart]);
+		return (0);
+	} else {
+		fprintf(stderr, "chroot(%s): %s\n", argv[1+optstart], strerror(errno));
+		return (1);
+	}
+
     }
     /* Switch group id then user id. */
 
     if (setgid(pwd->pw_gid)) {
-	syslog(LOG_ERR, "setgid(%d): %m", pwd->pw_gid);
-	return (0);
+	if (interactive) {
+		syslog(LOG_ERR, "setgid(%d): %m", pwd->pw_gid);
+		return (0);
+	} else {
+		fprintf(stderr, "setgid(%d): %s\n", pwd->pw_gid, strerror(errno));
+		return (1);
+	}
     }
     if (setuid(pwd->pw_uid)) {
-	syslog(LOG_ERR, "setuid(%d): %m", pwd->pw_uid);
-	return (0);
+	if (interactive) {
+		syslog(LOG_ERR, "setuid(%d): %m", pwd->pw_uid);
+		return (0);
+	} else {
+		fprintf(stderr, "setuid(%d): %s\n", pwd->pw_uid, strerror(errno));
+		return (1);
+	}
     }
     /* In case we still have the /etc/passwd file still open. */
 
@@ -123,7 +177,11 @@
 
     /* Run the command and hope for the best. */
 
-    (void) execv(argv[3], argv + 3);
-    syslog(LOG_ERR, "%s: %m", argv[3]);
-    return (0);
+    (void) execv(argv[3+optstart], argv + 3+optstart);
+    if (interactive) {
+	    syslog(LOG_ERR, "%s: %m", argv[3+optstart]);
+	    return (0);
+    }
+    fprintf(stderr, "%s: %s", argv[3+optstart], strerror(errno));
+    return (1);
 }
