Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux C++ Detect user shell (csh,bash,etc)

I have a C++ application that needs to make shell specific commands using system calls. Is there a way to detect which shell the user is running? (Csh/Bash/etc).

Thanks


Elaborate

I'm trying to work with some code that forks off via system a rsh call that has a sequence of commands that are using setenv which doesnt work in bash. I want to detect whether the system is csh or bash and rewrite the calls accordingly.

like image 616
Jeef Avatar asked Oct 22 '25 17:10

Jeef


1 Answers

Get the userid with geteuid, get the user database entry for that ID getpwuid containing the shell and must not be freed. So it breaks down to

getpwuid(geteuid())->pw_shell

Minimal working example:

#include <pwd.h>
#include <unistd.h>
#include <stdio.h>

int main (int argc, const char* argv[]) {
    printf("%s\n", getpwuid(geteuid())->pw_shell);
    return 0;
}
like image 103
ManuelSchneid3r Avatar answered Oct 24 '25 07:10

ManuelSchneid3r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!