Add more compat code for non GNU environments
[oweals/busybox.git] / libbb / setup_environment.c
index 30d317cea0f3fee156e3176a2dfc3b4fb0ca9b73..78318ce62f24da4187f0a29688b5255669188036 100644 (file)
  * SUCH DAMAGE.
  */
 
-#include <stdio.h>
-#include <errno.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
-#include <syslog.h>
-#include <ctype.h>
 #include "libbb.h"
 
-
-
-#define DEFAULT_LOGIN_PATH      "/bin:/usr/bin"
-#define DEFAULT_ROOT_LOGIN_PATH "/usr/sbin:/bin:/usr/bin:/sbin"
-
-static void xsetenv ( const char *key, const char *value )
+void FAST_FUNC setup_environment(const char *shell, int clear_env, int change_env, const struct passwd *pw)
 {
-           if ( setenv ( key, value, 1 ))
-                               bb_error_msg_and_die ( "out of memory" );
-}
+       /* 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);
+       }
 
-void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw )
-{
-       if ( loginshell ) {
-               char *term;
-       
-               /* 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 )) {
-                       if ( chdir ( "/" )) {
-                               syslog ( LOG_WARNING, "unable to cd to %s' for user %s'\n", pw-> pw_dir, pw-> pw_name );
-                               bb_error_msg_and_die ( "cannot cd to home directory or /" );
-                       }
-                       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 ? DEFAULT_LOGIN_PATH : DEFAULT_ROOT_LOGIN_PATH ));
+               term = getenv("TERM");
+               clearenv();
+               if (term)
+                       xsetenv("TERM", term);
+               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 ) {
-                       xsetenv ( "USER",    pw-> pw_name );
-                       xsetenv ( "LOGNAME", pw-> pw_name );
+                  USER and LOGNAME.  */
+               if (pw->pw_uid) {
+ shortcut:
+                       xsetenv("USER",    pw->pw_name);
+                       xsetenv("LOGNAME", pw->pw_name);
                }
+               xsetenv("HOME",    pw->pw_dir);
+               xsetenv("SHELL",   shell);
        }
 }
-