Introduce the ability to control whether the shell run in pterm is a
[u/mdw/putty] / unix / pty.c
index 8edbe48..a3d2fc7 100644 (file)
 #endif
 #endif
 
+/*
+ * Set up a default for vaguely sane systems. The idea is that if
+ * OMIT_UTMP is not defined, then at least one of the symbols which
+ * enable particular forms of utmp processing should be, if only so
+ * that a link error can warn you that you should have defined
+ * OMIT_UTMP if you didn't want any. Currently HAVE_PUTUTLINE is
+ * the only such symbol.
+ */
+#ifndef OMIT_UTMP
+#if !defined HAVE_PUTUTLINE
+#define HAVE_PUTUTLINE
+#endif
+#endif
+
 int pty_master_fd;
+static int pty_stamped_utmp = 0;
 static int pty_child_pid;
 static sig_atomic_t pty_child_dead;
 #ifndef OMIT_UTMP
@@ -63,10 +78,14 @@ static void setup_utmp(char *ttyname)
     FILE *lastlog;
 #endif
     struct passwd *pw;
-    char *location = get_x_display();
+    char *location;
     FILE *wtmp;
 
+    if (!cfg.stamp_utmp)
+       return;
+
     pw = getpwuid(getuid());
+    location = get_x_display();
     memset(&utmp_entry, 0, sizeof(utmp_entry));
     utmp_entry.ut_type = USER_PROCESS;
     utmp_entry.ut_pid = getpid();
@@ -100,6 +119,8 @@ static void setup_utmp(char *ttyname)
     }
 #endif
 
+    pty_stamped_utmp = 1;
+
 #endif
 }
 
@@ -108,6 +129,9 @@ static void cleanup_utmp(void)
 #ifndef OMIT_UTMP
     FILE *wtmp;
 
+    if (!cfg.stamp_utmp || !pty_stamped_utmp)
+       return;
+
     utmp_entry.ut_type = DEAD_PROCESS;
     memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
     time(&utmp_entry.ut_time);
@@ -127,6 +151,7 @@ static void cleanup_utmp(void)
     endutent();
 #endif
 
+    pty_stamped_utmp = 0;             /* ensure we never double-cleanup */
 #endif
 }
 
@@ -315,8 +340,19 @@ static char *pty_init(char *host, int port, char **realhost, int nodelay)
        }
        if (pty_argv)
            execvp(pty_argv[0], pty_argv);
-       else
-           execl(getenv("SHELL"), getenv("SHELL"), NULL);
+       else {
+           char *shell = getenv("SHELL");
+           char *shellname;
+           if (cfg.login_shell) {
+               char *p = strrchr(shell, '/');
+               shellname = smalloc(2+strlen(shell));
+               p = p ? p+1 : shell;
+               sprintf(shellname, "-%s", p);
+           } else
+               shellname = shell;
+           execl(getenv("SHELL"), shellname, NULL);
+       }
+
        /*
         * If we're here, exec has gone badly foom.
         */