- (stevek) Reworked progname support.
 - (bal) Misplaced #include "includes.h" in bsd-setproctitle.c.  Patch by
   Shinichi Maruyama <marya@st.jip.co.jp>

I assume the progname patch was finished.  I believe stevek is on vacation,
but it passes compiling under Linux and NeXTStep.
diff --git a/ChangeLog b/ChangeLog
index a6f5f04..77bb2e5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,9 @@
 20001117
  - (bal) Changed from 'primes' to 'primes.out' for consistancy sake.  It
    has no affect the output.  Patch by Corinna Vinschen <vinschen@redhat.com>
+ - (stevek) Reworked progname support. 
+ - (bal) Misplaced #include "includes.h" in bsd-setproctitle.c.  Patch by
+   Shinichi Maruyama <marya@st.jip.co.jp>
 
 20001116
  - (bal) Added in MAXSYMLINK test in bsd-realpath.c.  Required for some SCO
diff --git a/bsd-misc.c b/bsd-misc.c
index 5981494..2a05965 100644
--- a/bsd-misc.c
+++ b/bsd-misc.c
@@ -26,6 +26,26 @@
 #include "xmalloc.h"
 #include "ssh.h"
 
+char *get_progname(char *argv0)
+{
+#ifdef HAVE___PROGNAME
+	extern char *__progname;
+
+	return __progname;
+#else
+	char *p;
+
+	if (argv0 == NULL)
+		return "unknown";	/* XXX */
+	p = strrchr(argv0, '/');
+	if (p == NULL)
+		p = argv0;
+	else
+		p++;
+	return p;
+#endif
+}
+
 #ifndef HAVE_SETLOGIN
 int setlogin(const char *name)
 {
diff --git a/bsd-misc.h b/bsd-misc.h
index ae2b3ff..d837656 100644
--- a/bsd-misc.h
+++ b/bsd-misc.h
@@ -27,11 +27,12 @@
 
 #include "config.h"
 
+char *get_progname(char *argv0);
+
 #ifndef HAVE_SETSID
 #define setsid() setpgrp(0, getpid())
 #endif /* !HAVE_SETSID */
 
-
 #ifndef HAVE_SETENV
 int setenv(const char *name, const char *value, int overwrite);
 #endif /* !HAVE_SETENV */
diff --git a/bsd-setproctitle.c b/bsd-setproctitle.c
index 51a642e..38eca9a 100644
--- a/bsd-setproctitle.c
+++ b/bsd-setproctitle.c
@@ -38,10 +38,10 @@
 static char rcsid[] = "$OpenBSD: setproctitle.c,v 1.7 1999/02/25 22:10:12 art Exp $";
 #endif /* LIBC_SCCS and not lint */
 
-#ifndef HAVE_SETPROCTITLE
-
 #include "includes.h"
 
+#ifndef HAVE_SETPROCTITLE
+
 #define SPT_NONE	0
 #define SPT_PSTAT	1
 
@@ -56,11 +56,7 @@
 
 #define	MAX_PROCTITLE	2048
 
-#ifdef HAVE___PROGNAME
 extern char *__progname;
-#else
-static const char *__progname = "sshd";
-#endif /* HAVE___PROGNAME */
 
 /*
  * Set Process Title (SPT) defines.  Modeled after sendmail's
diff --git a/log-server.c b/log-server.c
index 270a3c6..de3d5cf 100644
--- a/log-server.c
+++ b/log-server.c
@@ -43,12 +43,6 @@
 #include "xmalloc.h"
 #include "ssh.h"
 
-#ifdef HAVE___PROGNAME
-extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "sshd";
-#endif /* HAVE___PROGNAME */
-
 static LogLevel log_level = SYSLOG_LEVEL_INFO;
 static int log_on_stderr = 0;
 static int log_facility = LOG_AUTH;
@@ -129,6 +123,7 @@
 	char fmtbuf[MSGBUFSIZ];
 	char *txt = NULL;
 	int pri = LOG_INFO;
+	extern char *__progname;
 
 	if (level > log_level)
 		return;
diff --git a/scp.c b/scp.c
index 86276d6..9af5c54 100644
--- a/scp.c
+++ b/scp.c
@@ -84,6 +84,12 @@
 #define _PATH_CP "cp"
 #endif
 
+#ifdef HAVE___PROGNAME
+extern char *__progname;
+#else
+char *__progname;
+#endif
+
 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
 #define STALLTIME	5
 
@@ -249,6 +255,8 @@
 	extern char *optarg;
 	extern int optind;
 
+	__progname = get_progname(argv[0]);
+
 	args.list = NULL;
 	addargs("ssh");	 	/* overwritten with ssh_program */
 	addargs("-x");
diff --git a/session.c b/session.c
index b94c8e8..890e16d 100644
--- a/session.c
+++ b/session.c
@@ -57,7 +57,10 @@
 #endif /* WITH_IRIX_PROJECT */
 #ifdef WITH_IRIX_JOBS
 #include <sys/resource.h>
-#endif
+#endif 
+#ifdef WITH_IRIX_AUDIT
+#include <sat.h>
+#endif /* WITH_IRIX_AUDIT */
 
 #if defined(HAVE_USERSEC_H)
 #include <usersec.h>
@@ -131,12 +134,7 @@
 
 /* import */
 extern ServerOptions options;
-#ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "sshd";
-#endif /* HAVE___PROGNAME */
-
 extern int log_stderr;
 extern int debug_flag;
 extern unsigned int utmp_len;
@@ -1104,7 +1102,6 @@
 				      strerror(errno));
                         }
 #  endif /* WITH_IRIX_JOBS */
-
 #  ifdef WITH_IRIX_ARRAY
 			/* initialize array session */
 			if (jid == 0) {
@@ -1123,6 +1120,14 @@
 			  fatal("Failed to initialize project %d for %s: %.100s",
 				(int)projid, pw->pw_name, strerror(errno));
 #  endif /* WITH_IRIX_PROJECT */
+#ifdef WITH_IRIX_AUDIT
+			if (sysconf(_SC_AUDIT)) {
+				debug("Setting sat id to %d", (int) pw->pw_uid);
+				if (satsetid(pw->pw_uid))
+					debug("error setting satid: %.100s", strerror(errno));
+			}
+#endif /* WITH_IRIX_AUDIT */
+
 			/* Permanently switch to the desired uid. */
 			permanently_set_uid(pw->pw_uid);
 # endif /* HAVE_LOGIN_CAP */
diff --git a/sftp-server.c b/sftp-server.c
index 538e5e2..fb6f013 100644
--- a/sftp-server.c
+++ b/sftp-server.c
@@ -90,6 +90,12 @@
 #define get_string(lenp)		buffer_get_string(&iqueue, lenp);
 #define TRACE				log
 
+#ifdef HAVE___PROGNAME
+extern char *__progname;
+#else
+char *__progname;
+#endif
+
 /* input and output queue */
 Buffer iqueue;
 Buffer oqueue;
@@ -1015,6 +1021,7 @@
 	int in, out, max;
 	ssize_t len, olen;
 
+	__progname = get_progname(av[0]);
 	handle_init();
 
 	in = dup(STDIN_FILENO);
diff --git a/ssh-add.c b/ssh-add.c
index f49d13f..857f3d7 100644
--- a/ssh-add.c
+++ b/ssh-add.c
@@ -50,9 +50,9 @@
 
 #ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "ssh-add";
-#endif /* HAVE___PROGNAME */
+#else
+char *__progname;
+#endif
 
 void
 delete_file(AuthenticationConnection *ac, const char *filename)
@@ -249,6 +249,7 @@
 	int i;
 	int deleting = 0;
 
+	__progname = get_progname(argv[0]);
 	init_rng();
 
         SSLeay_add_all_algorithms();
diff --git a/ssh-agent.c b/ssh-agent.c
index 9f61aec..f5f87cc 100644
--- a/ssh-agent.c
+++ b/ssh-agent.c
@@ -93,9 +93,9 @@
 
 #ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "ssh-agent";
-#endif /* HAVE___PROGNAME */
+#else
+char *__progname;
+#endif
 
 void
 idtab_init(void)
@@ -672,6 +672,7 @@
 	char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
 	extern int optind;
 	
+	__progname = get_progname(av[0]);
 	init_rng();
 	
 #ifdef __GNU_LIBRARY__
diff --git a/ssh-keygen.c b/ssh-keygen.c
index 76edc53..3653fc2 100644
--- a/ssh-keygen.c
+++ b/ssh-keygen.c
@@ -74,9 +74,9 @@
 /* argv0 */
 #ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "ssh-keygen";
-#endif /* HAVE___PROGNAME */
+#else
+char *__progname;
+#endif
 
 char hostname[MAXHOSTNAMELEN];
 
@@ -610,6 +610,7 @@
 	extern int optind;
 	extern char *optarg;
 
+	__progname = get_progname(av[0]);
 	init_rng();
 
 	SSLeay_add_all_algorithms();
diff --git a/ssh.c b/ssh.c
index ab32e3b..a1cedc7 100644
--- a/ssh.c
+++ b/ssh.c
@@ -62,9 +62,9 @@
 
 #ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "ssh";
-#endif /* HAVE___PROGNAME */
+#else
+char *__progname;
+#endif
 
 /* Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
    Default value is AF_UNSPEC means both IPv4 and IPv6. */
@@ -237,6 +237,7 @@
 	int dummy;
 	uid_t original_effective_uid;
 
+	__progname = get_progname(av[0]);
 	init_rng();
 
 	/*
diff --git a/sshconnect.c b/sshconnect.c
index 12ca69f..b33f209 100644
--- a/sshconnect.c
+++ b/sshconnect.c
@@ -35,11 +35,7 @@
 char *server_version_string = NULL;
 
 extern Options options;
-#ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "ssh";
-#endif /* HAVE___PROGNAME */
 
 /*
  * Connect to the given ssh server using a proxy command.
diff --git a/sshd.c b/sshd.c
index 56a39bd..4a5f662 100644
--- a/sshd.c
+++ b/sshd.c
@@ -78,6 +78,12 @@
 #define O_NOCTTY	0
 #endif
 
+#ifdef HAVE___PROGNAME
+extern char *__progname;
+#else
+char *__progname;
+#endif
+
 /* Server configuration options. */
 ServerOptions options;
 
@@ -562,6 +568,7 @@
 	int startup_p[2];
 	int startups = 0;
 
+	__progname = get_progname(av[0]);
 	init_rng();
 
 	/* Save argv[0]. */
diff --git a/uidswap.c b/uidswap.c
index 48026b9..76cbd6c 100644
--- a/uidswap.c
+++ b/uidswap.c
@@ -16,9 +16,6 @@
 
 #include "ssh.h"
 #include "uidswap.h"
-#ifdef WITH_IRIX_AUDIT
-#include <sat.h>
-#endif /* WITH_IRIX_AUDIT */
 
 /*
  * Note: all these functions must work in all of the following cases:
@@ -91,14 +88,6 @@
 void
 permanently_set_uid(uid_t uid)
 {
-#ifdef WITH_IRIX_AUDIT
-	if (sysconf(_SC_AUDIT)) {
-		debug("Setting sat id to %d", (int) uid);
-		if (satsetid(uid))
-			debug("error setting satid: %.100s", strerror(errno));
-	}
-#endif /* WITH_IRIX_AUDIT */
-
 	if (setuid(uid) < 0)
 		debug("setuid %u: %.100s", (u_int) uid, strerror(errno));
 }