rmyorston / busybox-w32

WIN32 native port of BusyBox.
https://frippery.org/busybox
Other
670 stars 124 forks source link

The directory specified by HOME in /etc/profile is not automatically entered during startup. #403

Closed smalltalkman closed 4 months ago

smalltalkman commented 4 months ago

The directory specified by HOME in /etc/profile is not automatically entered during startup.

C:/Users/lenovo # echo $BB_SYSTEMROOT
D:/gym/softwares/busybox
C:/Users/lenovo #
C:/Users/lenovo # cat $BB_SYSTEMROOT/etc/profile
export HOME=$BB_SYSTEMROOT/home/$(whoami)
mkdir -p $HOME
export PATH=$BB_SYSTEMROOT/bin:$PATH
C:/Users/lenovo #
C:/Users/lenovo # echo $HOME
D:/gym/softwares/busybox/home/root
C:/Users/lenovo #
C:/Users/lenovo # pwd
C:/Users/lenovo
C:/Users/lenovo #

It seems to be related to the code here: https://github.com/rmyorston/busybox-w32/blob/758a72f55360a1f3a5400aeca29bc2ea0647bad1/shell/ash.c#L16242

smalltalkman commented 4 months ago

Retrieved from https://github.com/rmyorston/busybox-w32/issues/402#issuecomment-2042068908

can I specify the HOME variable in /etc/profile?

No, that isn't possible. It would be quite easy to add such a feature, but I'm a bit uneasy about the idea. Allowing /etc/profile to change HOME isn't how things work in Unix.

Although this is not a good way, it is still feasible. After starting buxybox, the only command I need to execute is cd ~

C:/Users/lenovo # echo $BB_SYSTEMROOT
D:/gym/softwares/busybox
C:/Users/lenovo #
C:/Users/lenovo # cat $BB_SYSTEMROOT/etc/profile
export HOME=$BB_SYSTEMROOT/home/$(whoami)
mkdir -p $HOME
export PATH=$BB_SYSTEMROOT/bin:$PATH
C:/Users/lenovo #
C:/Users/lenovo # echo $HOME
D:/gym/softwares/busybox/home/root
C:/Users/lenovo #
C:/Users/lenovo # pwd
C:/Users/lenovo
C:/Users/lenovo # cd ~
~ #
~ # pwd
D:/gym/softwares/busybox/home/root
~ #

Maybe moving https://github.com/rmyorston/busybox-w32/blob/758a72f55360a1f3a5400aeca29bc2ea0647bad1/shell/ash.c#L16237 to the back of read_profile("/etc/profile"); will work.(just a guess)

rmyorston commented 4 months ago

Maybe moving...

I'm sure it would. I'm just not convinced it's the right thing to do.

cochon-git commented 4 months ago

BB will search for .profile in %USERPROFILE% (or %WINDIR%\system32 for admin elevated) and invoke that. Why not use simple harness .profile(s) to search out (or create in your case) and cd to the HOME directory. Seems to cover the multitude of ways BB gets invoked for me.

#.profile in %USERPROFILE%
for _local_drive in /c /d /e "$HOMEDRIVE" C: D: E: F: G: W: X: Y: Z:; do
  if [ -d "$_local_drive/home/$USERNAME" ]; then
    export HOME="$_local_drive/home/$USERNAME"
    break
  fi
done

if [ -n "$HOME" ] && [ "$HOME" != "$(pwd)" ]; then
  cd "$HOME" || echo "Oops cannot cd to $HOME"
  [ -f "$HOME/.profile" ] && . "$HOME/.profile"
fi
#.profile in %WINDIR%\system32
[ -f "$USERPROFILE/.profile" ] && . "$USERPROFILE/.profile"       
smalltalkman commented 4 months ago

@cochon-git cd "$HOME" || echo "Oops cannot cd to $HOME" It solves my problem, but it causes busybox64 sh -l -d d:/gym not to work.

smalltalkman commented 4 months ago

@rmyorston The contradictory problem I am facing now is: cd $HOME in /etc/profile can enter the specified user directory, but busybox64 sh -l -d d:/gym cannot enter the d:/gym directory. On the other hand, if I get the latter to work, I must avoid using cd $HOME in /etc/profile, so that I cannot automatically enter the specified user directory.

If you have good suggestions it would be greatly appreciated.

rmyorston commented 4 months ago

The -d shell option is undocumented, non-portable and only intended for internal use.

Still, that hasn't stopped people using it.

In this case the problem is that -d is handled before /etc/profile. Since the intended use of -d doesn't involve a login shell it doesn't matter whether it's processed before or after /etc/profile. And, apart from some code churn, there's no cost to the change. (Unintended consequences excepted, of course.)

Try the latest prerelease, PRE-5323 or above.

smalltalkman commented 4 months ago

Thank you for considering my extreme situation. I'll test it later.

smalltalkman commented 4 months ago

Thank you for considering my extreme situation. I'll test it later.

It works perfectly, thank you very much!

The -d shell option is undocumented, non-portable and only intended for internal use.

Still, that hasn't stopped people using it.

'-d' is great! busybox sh -l -d ... is very helpful for Windows users to add the function of starting busybox on the folder right-click menu.