Username of Caller

T

TWalsh

I need to write a file to the bin directory of the user who is calling
my installer. I am running this on IBM's AIX OS with ksh. Is there a
way to do this without using system() to execute whoami?
 
V

Victor Bazarov

TWalsh said:
I need to write a file to the bin directory of the user who is calling
my installer. I am running this on IBM's AIX OS with ksh. Is there a
way to do this without using system() to execute whoami?

Huh? Don't you think that this is better asked in AIX OS newsgroup?
'system' function is platform-specific, not to mention that it's most
likely not the right tool for the job. There are probably mechanisms
in the OS to query the information about the user.

V
 
T

TWalsh

I am converting a shell script to C++. I already know how to find the
calling user's username in a shell script, which is just the whoami
command. I need to know if there is a way to find either the calling
user's username or the calling users home directory in C++.

I know one way to do this would be to call system() from stdlib.h and
save the result into a file but that is a horrible solution.
 
F

Frank Birbacher

Hi!
I am converting a shell script to C++. I already know how to find the
calling user's username in a shell script, which is just the whoami
command. I need to know if there is a way to find either the calling
user's username or the calling users home directory in C++.

I think you missed the point. This newsgroup is for C++. Altough you are
writing a C++ program, not all question go here. The C++ standard (which
this newsgroup is about) contains nothing about users home dirs. This is
why we cannot help you here.

Anyway, the operating system you are targeting may supply functions
which do what you want. The knowledge about these functions is not
covered here. You should look for a specific other newsgroup which
covers system calls of AIX OS. My guess goes to comp.unix.aix .

If nothing helps try to read the source code of "whoami".

My 2cents: I guess not all home dirs are "/home/username". I hope there
is a function to retrieve the complete correct path directly.

Frank
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

On 2007-08-10 19:55, TWalsh wrote:

First of all, please quote the text you are replying to.
I am converting a shell script to C++. I already know how to find the
calling user's username in a shell script, which is just the whoami
command. I need to know if there is a way to find either the calling
user's username or the calling users home directory in C++.

The concept of user does not exist in standard C++ since not all
platforms where you can use C++ on have users (does not really make much
sense when programming the injection-system of a combustion-engine does
it?).
I know one way to do this would be to call system() from stdlib.h and
save the result into a file but that is a horrible solution.

Take a look at the getenv() function from <cstdlib>, and use it to get
the path to the home directory, that's how your shell does it when you
do cd ~.
 
F

Fred Kleinschmidt

TWalsh said:
I am converting a shell script to C++. I already know how to find the
calling user's username in a shell script, which is just the whoami
command. I need to know if there is a way to find either the calling
user's username or the calling users home directory in C++.

I know one way to do this would be to call system() from stdlib.h and
save the result into a file but that is a horrible solution.
[OT}
On a Unix platform, try getuid()
With Motif, XmeGetHomeDirName()
or try getenv("HOME")
 
T

TWalsh

Take a look at the getenv() function from <cstdlib>, and use it to get
the path to the home directory, that's how your shell does it when you
do cd ~.

I had just found that unix defines $HOME as an environment variable
and was going to post for completeness. Thanks for the help.
 
G

Gianni Mariani

TWalsh said:
I need to write a file to the bin directory of the user who is calling
my installer. I am running this on IBM's AIX OS with ksh. Is there a
way to do this without using system() to execute whoami?

Still off topic, however, you may look at the environment variables like
PATH, USER, USERNAME, HOME etc. These or at least some are cross platform.

BTW - there is no "standard" bin directory for a particular user.

getenv *IS* standard c++.
 
J

James Kanze

Huh? Don't you think that this is better asked in AIX OS newsgroup?
'system' function is platform-specific, not to mention that it's most
likely not the right tool for the job. There are probably mechanisms
in the OS to query the information about the user.

A general Unix group would be OK as well. (But as Gianni said,
there is no bin directory of a user under Unix, unless that user
has decided to create one.)
 
L

liam_herron

I know this is off topic but I thought I would help. I have
a multi-platform solution that works on both Windows and
Redhat Linux. See below code:


#ifdef WIN32
#include <stdlib.h>
#else //LINUX
#include <pwd.h>
#include <unistd.h>
#endif

std::string getUsername()
{
std::string username;
#ifdef WIN32
const char* getenvStr = ::getenv("Username");
if (getenvStr != 0)
{
username = getenvStr;
}
#else //LINUX
struct passwd *userinfo;
userinfo = getpwuid(getuid() );
username = userinfo->pw_name;
#endif
return username;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top