LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-06-2007, 12:16 AM   #1
Hard_Working_
LQ Newbie
 
Registered: Apr 2007
Posts: 8

Rep: Reputation: 0
c++ function that returns the user ID


Hello everyone:

I need some help... 2 simple problems:


1) Is there any c++ function that tells you the current user ID in linux?

----------------------------------------------------------------

2) Is there any c++ function thats tells you the name of a job... like

myString = getTheNameOfTheJob(index or something);
myString now equals --> "Sleep 10"

The intention is to implement something similar to the bash "jobs" function

like:

$jobs

pid cmd

1233 sleep 10
1234 another process or command
----------------------------------------------------------------

Thank you in advance

HW
 
Old 04-06-2007, 01:17 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
The answers to these questions are the same for C and C++.

For getting the user ID of the current process:

Code:
man 2 getuid
For getting the command line of process XXX, where XXX is some process number, look at /proc/XXX/cmdline. Although not a real file, it acts like one, in that you can read it. It contains all parts of the command line. Each part is followed by a single NUL character.

Hope this helps.
 
Old 04-06-2007, 02:22 AM   #3
Hard_Working_
LQ Newbie
 
Registered: Apr 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Hi thank you for your help, the second one works fine, but the first one, cant make it work.

What i really need is the user name as a string, that fuction seems to return something like a int ... uid_t...

Any idea how to solve this issue?

Thank you once again

HW
 
Old 04-06-2007, 09:10 AM   #4
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
id -u
i dont know c++ very well but assume it is possible to use bash function - or is this a c function anyway?
 
Old 04-06-2007, 12:23 PM   #5
Kristofer
LQ Newbie
 
Registered: Nov 2005
Location: Sweden
Distribution: Slackware
Posts: 20

Rep: Reputation: 0
Using the passwd struct for getting the name

Try using the passwd struct for extracting the name as a string:
struct passwd *userpasswd;
userpasswd = getpwuid(getuid());

For more info on the struct:
http://www.mkssoftware.com/docs/man5...t_passwd.5.asp

/Kristofer
 
Old 04-06-2007, 06:26 PM   #6
Kristofer
LQ Newbie
 
Registered: Nov 2005
Location: Sweden
Distribution: Slackware
Posts: 20

Rep: Reputation: 0
Getting the name of a process by pid

The struct task_struct defined in include/linux/sched.h has char comm[TASK_COMM_LEN] which contains the name of the process, to retrieve the filled task_struct for a specific pid you should be able to use find_task_by_pid(int pid)

/Kristofer
 
  


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
Function that returns the size of a file in bytes in C kalamaraki Programming 2 01-07-2007 11:22 AM
How to pass array to a function in Java so that it returns changed? kornerr Programming 1 09-19-2006 12:16 PM
trunc function returns a double? :confused: zahadumy Programming 6 12-03-2005 10:20 PM
C function that returns $HOME cbranje Programming 1 02-12-2005 05:41 PM
open function returns error sudheernair Programming 2 05-07-2004 11:14 AM

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

All times are GMT -5. The time now is 05:46 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