Outside of a shell, such as running some other process, what does the term current directory
mean, and is it possible to execute a binary without specifying the full path or the preceding './', assuming that the current directory
contains the executable?
-
"...is it possible to execute a binary without..." You should be more specific about what you want here. If you're using the shell to run commands, the shell has certain rules for how it interprets the command and decides what program to run. If you're writing a program in a programming language, then the language will provide certain ways to run programs. We would have to know what language you're using to priovide detailed help.– KensterMay 21, 2015 at 16:20
4 Answers
Yes, it is possible by setting up the search path appropriately (either containing your working directory explicitly or by containing "./"), but it is good practice to have the "./" in front of the program name. The reason is security: A malware could write an executable file with the name of a commonly used program (say, ls
) and the next call to ls
will execute the local copy instead of /bin/ls
. Therefore, standard PATH settings under UNIX do not contain "./".
-
Security wise it would be more enlightening an example with the script receiving a name like
sudo
orsu
. That way, any user having its path variable modified will be most probably giving away his password or root's (in casesu
is used and the user knows root password).– YoMismoMay 21, 2015 at 14:54
You should better define what is the "other process" and what method it uses to launch a program.
In any case, the current working directory is a property of each and every process running so it might be used to locate a program to run. Whether it is safe to implement it that way is questionable.
-
I am developing the program with Lazarus and the commands are executed with the TProcess object. The directory is set via TProcess.CurrentDirectory and the command via TProcess.CommandLine– vfclistsMay 21, 2015 at 14:02
Well a quick workaround would be creating an alias. This creation can be added to the .bashrc to have it added during startup.
The Unix/Linux/*BSD kernel has always kept track of working directory. The chdir()
and fchdir()
system calls have been around for as long as the C language has been around.
If you write a C language program and use execve()
, it's up to your program to specify the filename
argument of execve()
. The kernel will find executables in the current working directory, or relative to it, if you give a filename argument like "some_executable" or "subdir/some_executable", the kernel will find the executable (if it exists and is executable).
I realizes the modern shells (zsh at least) blur the current working directory a bit so as to be able to cd
into a symbolic link, and cd ..
back up to the directory containing the symbolic link, but the kernel-based tracking of current working directory is still present. On linux machines you can ls -l /proc/$$/cwd
to see exactly what the kernel thinks that directory is named.