USERID

USERID

Post by PA Ro » Thu, 24 Nov 1994 00:39:53



Dear all,

Is there a nice way of obtaining the user's id from within a unix C program ?
I know that I can do it with:

system("whoami > temp");
/* Read in file temp */

But this is not a very nice solution. Anyone else got any ideas ?

Paul

 
 
 

USERID

Post by Russell Edwar » Thu, 24 Nov 1994 13:35:50



>Dear all,
>Is there a nice way of obtaining the user's id from within a unix C program ?
>I know that I can do it with:
>system("whoami > temp");
>/* Read in file temp */

 How about getuid()? Or geteuid() depending if you need the real id or
the effective user id.

Russell
--

-----------------------------------------------------------------------
  You can't have everything -- where you you put it?
                                                   -- Steven Wright

 
 
 

USERID

Post by Axel-St├ęphane C. Sm?rgr » Thu, 24 Nov 1994 17:36:24



Quote:>hi,
>you can use the environment function like getenv.  do this.

>char *userid;

>userid = getenv("USER");

This is definitively *NOT* a good idea since anyone can change the
environment before starting your program.

Unix provides functions for retrieving the user id of a process. Each
process has to kinds of user ids: real user id and effective user id.
The real uid is the one of the user who created the process, whereas
the effective uid gives the process the privileges of a possibly
different user. This is somewhat simplified, but I think I may put it
that way...

Read the getuid(2) man-page.

-ascs

 
 
 

USERID

Post by GH Stims » Thu, 24 Nov 1994 19:48:38


|>
|> Dear all,
|>
|> Is there a nice way of obtaining the user's id from within a unix C program ?
|> I know that I can do it with:
|>
|> system("whoami > temp");
|> /* Read in file temp */
|>
|> But this is not a very nice solution. Anyone else got any ideas ?
|>
|> Paul
|>

Aghh! Don't you ever read the news?? I posted this on uwe.general about a week
ago and so did ac-gavin, but incase you missed it, here it is again..

#include <pwd.h>

struct passwd *my_passwd;

my_passwd=getpwuid(getuid());

printf(%s\n",my_passwd->pw_name);

endpwent();

Don't post this again....

Geraint

--
*******************************************************************************
* Without going outside, you may know the whole world.                        *
* Without looking through the window, you may see the ways of heaven.         *
* The farther you go, the less you know.                                      *
*                                                                             *
* Thus the sage knows without traveling;                                      *
* He sees without looking;                                                    *
* He works without doing.                                                     *
*        (Lao Tsu) - 'Tao te ching'                                           *
*******************************************************************************

 
 
 

USERID

Post by PA Ro » Fri, 25 Nov 1994 00:46:25


Quote:>Aghh! Don't you ever read the news?? I posted this on uwe.general about a week
>ago and so did ac-gavin, but incase you missed it, here it is again..
>#include <pwd.h>
[STUFF DELETED]
>endpwent();

Problem is it didn't work!
 
 
 

USERID

Post by GH Stims » Fri, 25 Nov 1994 01:41:04


|> >Aghh! Don't you ever read the news?? I posted this on uwe.general about a week
|> >ago and so did ac-gavin, but incase you missed it, here it is again..
|> >#include <pwd.h>
|> [STUFF DELETED]
|> >endpwent();
|>
|> Problem is it didn't work!
|>

I have tested this code and it DOES work.

Here it is as a full runnable piece of code:

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

int main()
        {
        struct passwd *my_passwd;

        my_passwd=getpwuid(getuid());

        printf("%s\n",my_passwd->pw_name);

        endpwent();
        }

Both myself and Andy have tested this, infact we both replied with the same
code.. (Andy finished with endpwent(), but I forgot.. :) )
If you wanted to copy the returned username to another string, use this code
fragment...

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

int main()
        {
        char *my_name;
        struct passwd *my_passwd;

        my_passwd=getpwuid(getuid());

        my_name=(char *)malloc(strlen(my_passwd->pw_name));

        strcpy(my_name,my_passwd->pw_name);

        /* String my_name now contains the username */
        }

Geraint...

--
*******************************************************************************
* Without going outside, you may know the whole world.                        *
* Without looking through the window, you may see the ways of heaven.         *
* The farther you go, the less you know.                                      *
*                                                                             *
* Thus the sage knows without traveling;                                      *
* He sees without looking;                                                    *
* He works without doing.                                                     *
*        (Lao Tsu) - 'Tao te ching'                                           *
*******************************************************************************

 
 
 

USERID

Post by Richard L. Jon » Fri, 25 Nov 1994 23:15:53



   |> >Aghh! Don't you ever read the news?? I posted this on uwe.general about a week
   |> >ago and so did ac-gavin, but incase you missed it, here it is again..
   |> >#include <pwd.h>
   |> [STUFF DELETED]
   |> >endpwent();
   |>
   |> Problem is it didn't work!
   |>

   I have tested this code and it DOES work.

[first example deleted]

   If you wanted to copy the returned username to another string, use this code
   fragment...

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

   int main()
           {
           char *my_name;
           struct passwd *my_passwd;

           my_passwd=getpwuid(getuid());

           my_name=(char *)malloc(strlen(my_passwd->pw_name));

           strcpy(my_name,my_passwd->pw_name);

           /* String my_name now contains the username */
           }

   Geraint...

or more succinctly...

   char *my_name = strdup (getpwuid (getuid ())->pw_name);

remembering of course to use free(). strdup is jolly useful
if you are tired of typing malloc ...  strlen... strcpy.

rlj

 
 
 

1. >int userids

Does anybody know (or would anyone like to make a guess) as to what might
happen if a user has a userid of >32767?  To be more precise, I popped an
Oracle CD-ROM in my drive, mounted it without problem.  I then NFS exported
it to a Sun.  That sun had couldn't do a `pwd` without getting permission
problems inside any directory on the CD.  I moved the CD to another Sun and
didn't have a problem.  I also didn't have a problem looking at it on my
Linux box locally (it's a 486/66 running 1.2.1).

Just curious.
-Cliff

2. Compiling whole system and /etc/mk.conf

3. URL with userid and passwort?

4. netstat hangs on name resolution

5. Setting umask for ftp userid

6. sendmail tellme: we do not relay

7. Need help: IP routing based on userid

8. Is LinuxPPC for Macs *only*?

9. Setting up /~USERID for ftp/http access

10. Signoff a userid

11. userid change to upper case

12. Getting a userid on server side, generating a unique number, etc.

13. Changing userid of a process