LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2004, 02:36 AM   #1
rjstephens
LQ Newbie
 
Registered: Jun 2004
Posts: 11

Rep: Reputation: 0
Getting uid and/or username of logged in user


Hi
I am writing a C++ app and i need a way to get the the user ID and the user name of the currently logged in user. Any ideas?

(BTW, this is on linux)
 
Old 09-22-2004, 03:34 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
man utmp
 
Old 09-22-2004, 03:38 AM   #3
rjstephens
LQ Newbie
 
Registered: Jun 2004
Posts: 11

Original Poster
Rep: Reputation: 0
thanks.
 
Old 09-22-2004, 03:41 AM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
If you mean that you want the username and ID of the user who is running the program, you can use
Code:
#include <unistd.h>
#include <sys/types.h>

uid_t uid getuid(void);
You can pass this uid value to getpwdnam to return a structure representing the password file entry:
Code:
#include <pwd.h>
#include <sys/types.h>

struct passwd pw *getpwuid(uid);
And then read out the username with the pw_name field pointed to by pw.

You can also get the username of the current user (which may or may not be either the logged in user or the user running the process; see su -m) using
Code:
#include <stdlib.h>

char * uname = getenv("USER");
I hope that's legible,

— Robert J. Lee
 
Old 09-22-2004, 03:50 AM   #5
rjstephens
LQ Newbie
 
Registered: Jun 2004
Posts: 11

Original Poster
Rep: Reputation: 0
thankyou rjiee, I am using:
Code:
#include <unistd.h>

int uid = getuid();
That appears to work. "void" gives a parser error for some reason (I'm using g++). I'll get to the user name stuff later, I don't need that just yet.


Cedrik, thanks but I was just searching on google for details of how to get that to work. Couldn't find any, then I saw rjiee suggestion. Thanks anyway.
 
Old 09-23-2004, 05:02 PM   #6
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
void is only allowed in function prototypes in C or C++. It just means that no parameter is allowed (and can sometimes be omitted).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add a user with UID =500 KWTm Ubuntu 1 10-29-2005 11:47 AM
FREEBSD5.3 platform: PHP setcookie() successful, but $uid = $_COOKIE['uid']; fail tclwp Programming 1 03-30-2005 08:05 PM
Multi-user and 3D sharing, 3D only works for the first logged in user foxy123 SUSE / openSUSE 0 02-20-2005 04:31 AM
Changing User IDs (UID) totalcommand Linux - Newbie 5 06-06-2003 06:47 PM
mozilla works fine when logged in as a user but crashes when logged in as root jimi Linux - General 6 04-02-2003 08:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration