username / id

2003-10-30 Thread Vu Pham
Hi all,

Is there any function to get the user name if uid is known, and vice versa ?
Similar for group.

Thanks,

Vu

___
Linux-users mailing list
[EMAIL PROTECTED]
Unsubscribe/Suspend/Etc - http://smtp.linux-sxs.org/mailman/listinfo/linux-users


Re: username / id

2003-10-30 Thread James McDonald
Vu Pham wrote:
Hi all,

Is there any function to get the user name if uid is known, and vice versa ?
Similar for group.
Thanks,

cat /etc/passwd | grep ^.*:x:500: | awk -F: '{print $1}'
cat /etc/passwd | grep ^.*:x:[0-9]*:500 | awk -F: '{print $1}'
This is probably not what you want but it does return a name from uid or 
gid...

--
James McDonald
Singleton Australia
61+ (0)2 65712401
61+ 0428 320 219
If you stand on your head, you will get footprints in your hair.

Linux 2.6.0-james2 #3 Wed Oct 29 10:25:46 EST 2003 athlon i386 GNU/Linux
22:50:00 up 1 day, 10:54, 2 users, load average: 0.07, 0.24, 0.44
___
Linux-users mailing list
[EMAIL PROTECTED]
Unsubscribe/Suspend/Etc - http://smtp.linux-sxs.org/mailman/listinfo/linux-users


Re: username / id

2003-10-30 Thread Vu Pham

- Original Message - 
From: James McDonald [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Thursday, October 30, 2003 5:57 AM
Subject: Re: username / id


 Vu Pham wrote:
  Hi all,
 
  Is there any function to get the user name if uid is known, and vice
versa ?
  Similar for group.
 
  Thanks,
 

 cat /etc/passwd | grep ^.*:x:500: | awk -F: '{print $1}'
 cat /etc/passwd | grep ^.*:x:[0-9]*:500 | awk -F: '{print $1}'

 This is probably not what you want but it does return a name from uid or
 gid...

Oh, that works. I would like to know if there is an API so that I can use it
in my C app, or do I need to run some commands like your suggestion and
capture the output.

Thanks, James.

Vu

___
Linux-users mailing list
[EMAIL PROTECTED]
Unsubscribe/Suspend/Etc - http://smtp.linux-sxs.org/mailman/listinfo/linux-users


Re: username / id

2003-10-30 Thread Roger Oberholtzer
This may help if you are doing this from a program:

man getuid


On Thu, 30 Oct 2003 22:57:14 +1100
James McDonald [EMAIL PROTECTED] wrote:

 Vu Pham wrote:
  Hi all,
  
  Is there any function to get the user name if uid is known, and vice
  versa ? Similar for group.
  


-- 
++···+
· Roger Oberholtzer  ·   E-mail: [EMAIL PROTECTED]·
· OPQ Systems AB ·  WWW: http://www.opq.se/  ·
· Erik Dahlbergsgatan 41-43  ·Phone: Int + 46 8   314223 ·
· 115 34 Stockholm   ·   Mobile: Int + 46 733 621657 ·
· Sweden ·  Fax: Int + 46 8   302602 ·
++···+

___
Linux-users mailing list
[EMAIL PROTECTED]
Unsubscribe/Suspend/Etc - http://smtp.linux-sxs.org/mailman/listinfo/linux-users


Re: username / id

2003-10-30 Thread Roger Oberholtzer
On Thu, 30 Oct 2003 06:25:39 -0600
Vu Pham [EMAIL PROTECTED] wrote:

 
 - Original Message - 
 From: James McDonald [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 Sent: Thursday, October 30, 2003 5:57 AM
 Subject: Re: username / id
 
 
  Vu Pham wrote:
   Hi all,
  
   Is there any function to get the user name if uid is known, and vice
 versa ?
   Similar for group.
  
   Thanks,
  
 
  cat /etc/passwd | grep ^.*:x:500: | awk -F: '{print $1}'
  cat /etc/passwd | grep ^.*:x:[0-9]*:500 | awk -F: '{print $1}'
 
  This is probably not what you want but it does return a name from uid or
  gid...
 
 Oh, that works. I would like to know if there is an API so that I can use
 it in my C app, or do I need to run some commands like your suggestion and
 capture the output.

The closest to a C function I have seen is only for the current user:
getlogin or cuserid. For other users, I do not know. These functions use the
'utmp' function to get their information, and not /etc/passwd. 

So, if you are interested in currently logged in users, you can use the
utmp() function. Otherwise, fopen(/etc/passwd, ...)/fread() is the way to
go.



-- 
++···+
· Roger Oberholtzer  ·   E-mail: [EMAIL PROTECTED]·
· OPQ Systems AB ·  WWW: http://www.opq.se/  ·
· Erik Dahlbergsgatan 41-43  ·Phone: Int + 46 8   314223 ·
· 115 34 Stockholm   ·   Mobile: Int + 46 733 621657 ·
· Sweden ·  Fax: Int + 46 8   302602 ·
++···+

___
Linux-users mailing list
[EMAIL PROTECTED]
Unsubscribe/Suspend/Etc - http://smtp.linux-sxs.org/mailman/listinfo/linux-users


Re: username / id

2003-10-30 Thread vu pham
Thanks everybody who has replied. Perhaps I will go with one of the 
following suggestions :

- popen with the cat /etc/passwd | grep 

or

- read the /etc/passwd , /etc/group and parse them.

Thanks !

Vu

___
Linux-users mailing list
[EMAIL PROTECTED]
Unsubscribe/Suspend/Etc - http://smtp.linux-sxs.org/mailman/listinfo/linux-users


Re: username / id

2003-10-30 Thread Roger Oberholtzer
On Thu, 30 Oct 2003 07:53:35 -0600
Rick Sivernell [EMAIL PROTECTED] wrote:

 On Thu, 30 Oct 2003 06:25:39 -0600
 Vu Pham [EMAIL PROTECTED] wrote:
 
  
  - Original Message - 
  From: James McDonald [EMAIL PROTECTED]
  To: [EMAIL PROTECTED]
  Sent: Thursday, October 30, 2003 5:57 AM
  Subject: Re: username / id
  
  
   Vu Pham wrote:
Hi all,
   
Is there any function to get the user name if uid is known, and vice
  versa ?
Similar for group.
   
Thanks,
   
  
   cat /etc/passwd | grep ^.*:x:500: | awk -F: '{print $1}'
   cat /etc/passwd | grep ^.*:x:[0-9]*:500 | awk -F: '{print $1}'
  
   This is probably not what you want but it does return a name from uid
   or gid...
  
  Oh, that works. I would like to know if there is an API so that I can
  use it in my C app, or do I need to run some commands like your
  suggestion and capture the output.
  
  Thanks, James.
  
  Vu
  
  ___
  Linux-users mailing list
  [EMAIL PROTECTED]
  Unsubscribe/Suspend/Etc -
  http://smtp.linux-sxs.org/mailman/listinfo/linux-users
 Vu
 
use exec and put the cat  in a string. the exec will execute this
from
 code.

It really is not necessary to program via exec to read a file. Try the
attached code.

Put it in /tmp. Compile with 'make qq', Run as ./qq


-- 
++···+
· Roger Oberholtzer  ·   E-mail: [EMAIL PROTECTED]·
· OPQ Systems AB ·  WWW: http://www.opq.se/  ·
· Erik Dahlbergsgatan 41-43  ·Phone: Int + 46 8   314223 ·
· 115 34 Stockholm   ·   Mobile: Int + 46 733 621657 ·
· Sweden ·  Fax: Int + 46 8   302602 ·
++···+

#include stdio.h

char *myGetUserName(int target) {

FILE *pwdfile;
static char name[80];

if (!(pwdfile = fopen(/etc/passwd, r))) return(0);

while (1)  {

char aline[256];
int uid;

if (!fgets(aline, 200, pwdfile)) { name[0] = 0; break; }

sscanf(aline,   %*[^:]:   // login (skip)
%*[^:]:   // group (skip)
%d:   // uid
%*d:  // gid (skip)
%[^:],// name
uid, name);

if (uid == target) break;
}

fclose(pwdfile);
return(name);
}

main ()
{
printf(uid 500 is %s\n, myGetUserName(500));
printf(uid 200 is %s\n, myGetUserName(200));
}
___
Linux-users mailing list
[EMAIL PROTECTED]
Unsubscribe/Suspend/Etc - http://smtp.linux-sxs.org/mailman/listinfo/linux-users


Re: username / id

2003-10-30 Thread James McDonald
vu pham wrote:
Thanks everybody who has replied. Perhaps I will go with one of the 
following suggestions :

- popen with the cat /etc/passwd | grep 

or

- read the /etc/passwd , /etc/group and parse them.

Thanks !

Vu
Umm I was just playing with id and if you

[EMAIL PROTECTED] tmp]$ id apache
uid=48(apache) gid=48(apache) groups=48(apache)
[EMAIL PROTECTED] tmp]$ id apache -u
48
[EMAIL PROTECTED] tmp]$ id apache -g
48
[EMAIL PROTECTED] tmp]$ id apache -gu
id: cannot print only user and only grou
So forget about my earlier suggestion of cat'ing /etc/password and just 
use id

if you are trying to use the local uid then

[EMAIL PROTECTED] tmp]$ whoami | id
uid=500(james) gid=500(james) groups=500(james),10(wheel)
Well at least we aren't limited for choice using *nix

--
James McDonald
Singleton Australia
61+ (0)2 65712401
61+ 0428 320 219
Creativity is no substitute for knowing what you are doing.

Linux 2.6.0-james3 #4 Thu Oct 30 23:46:04 EST 2003 athlon i386 GNU/Linux
06:40:00 up 6:08, 3 users, load average: 0.44, 0.65, 0.44
___
Linux-users mailing list
[EMAIL PROTECTED]
Unsubscribe/Suspend/Etc - http://smtp.linux-sxs.org/mailman/listinfo/linux-users


Re: username / id

2003-10-30 Thread Rick Sivernell
Roger 

   That too will do the trick, my suggestion was using the cl stuff offered only.
cheers
-- 
Rick Sivernell
Dallas, Texas  75287
972 306-2296
[EMAIL PROTECTED]
Gentoo Linux 
Registered Linux User

   .~.
  / v \
 /( _ )\
   ^ ^
In Linux we trust!
___
Linux-users mailing list
[EMAIL PROTECTED]
Unsubscribe/Suspend/Etc - http://smtp.linux-sxs.org/mailman/listinfo/linux-users