traceroute: fix help text to not show -6 when traceroute6 is off
[oweals/busybox.git] / libbb / setup_environment.c
index a98b9a5bdbb8bff38b284cd35add82457c6695c9..78318ce62f24da4187f0a29688b5255669188036 100644 (file)
 
 #include "libbb.h"
 
-void setup_environment(const char *shell, int loginshell, int changeenv, const struct passwd *pw)
+void FAST_FUNC setup_environment(const char *shell, int clear_env, int change_env, const struct passwd *pw)
 {
-       if (loginshell) {
-               const char *term;
+       /* Change the current working directory to be the home directory
+        * of the user */
+       if (chdir(pw->pw_dir)) {
+               xchdir("/");
+               bb_error_msg("can't chdir to home directory '%s'", pw->pw_dir);
+       }
 
-               /* Change the current working directory to be the home directory
-                * of the user.  It is a fatal error for this process to be unable
-                * to change to that directory.  There is no "default" home
-                * directory.
-                * Some systems default to HOME=/
-                */
-               if (chdir(pw->pw_dir)) {
-                       xchdir("/");
-                       fputs("warning: cannot change to home directory\n", stderr);
-               }
+       if (clear_env) {
+               const char *term;
 
-               /* Leave TERM unchanged.  Set HOME, SHELL, USER, LOGNAME, PATH.
+               /* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH.
                   Unset all other environment variables.  */
                term = getenv("TERM");
                clearenv();
                if (term)
                        xsetenv("TERM", term);
-               xsetenv("HOME",    pw->pw_dir);
-               xsetenv("SHELL",   shell);
-               xsetenv("USER",    pw->pw_name);
-               xsetenv("LOGNAME", pw->pw_name);
-               xsetenv("PATH",   (pw->pw_uid ? bb_default_login_path : bb_default_root_login_path));
+               xsetenv("PATH", (pw->pw_uid ? bb_default_path : bb_default_root_path));
+               goto shortcut;
+               // No, gcc (4.2.1) is not clever enougn to do it itself.
+               //xsetenv("USER",    pw->pw_name);
+               //xsetenv("LOGNAME", pw->pw_name);
+               //xsetenv("HOME",    pw->pw_dir);
+               //xsetenv("SHELL",   shell);
        }
-       else if (changeenv) {
+       else if (change_env) {
                /* Set HOME, SHELL, and if not becoming a super-user,
                   USER and LOGNAME.  */
-               xsetenv("HOME",  pw->pw_dir);
-               xsetenv("SHELL", shell);
                if (pw->pw_uid) {
+ shortcut:
                        xsetenv("USER",    pw->pw_name);
                        xsetenv("LOGNAME", pw->pw_name);
                }
+               xsetenv("HOME",    pw->pw_dir);
+               xsetenv("SHELL",   shell);
        }
 }