Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: any efficient way to get user's full name?

by PrakashK (Pilgrim)
on Jul 26, 2001 at 01:12 UTC ( #99806=note: print w/replies, xml ) Need Help??

in reply to any efficient way to get user's full name?

How about:
$user_name = (getpwuid($<))[5];
This saves in $user_name whatever is in the comment field of the user's entry in the password file (assuming comments are in the sixth field).

Update: Due to the way different systems handle the comments field, the above may not work on all systems. I tested the above on Solaris 2.6 and it worked. mdillon alerted me that the name is available from the GCOS field on his system which means the index 6 should be used.

When I looked again on the Solaris system, I found that the name was available in both comment and GCOS fields. On my Linux (Debian) box it is in the GCOS field.

So, it seems that using index 6 seems to be working on both Solaris and Debian Linux systems. Here's the updated code:

$user_name = (getpwuid($<))[6];
Thanks to mdillon for the correction.

PS: Does anyone know the story behind the GCOS field? What does it stand for? Why do both comment and GCOS fields both exist?

Replies are listed 'Best First'.
Re: Re: any efficient way to get user's full name?
by ChemBoy (Priest) on Jul 26, 2001 at 02:27 UTC

    Or, alternatively,

    $username = (getpwnam(getlogin))[5];
    which is probably infinitesimally slower and infinitesimally more self-documenting. But I happen to have used that particular idiom, so I thought I'd throw it in. :-)

    Update: in the spirit of actually adding something to the discussion, I should point out that there is an entire class of built-in functions to deal with users and groups, named things like getpwnam, getgrent, getgrgid and the like, which you should look at if you ever so much as think of directly accessing /etc/passwd directly (you can find the complete list under "Fetching user and group info" here).

    Update 2: fields 5 and 6 are identical here, too (SGI), FWIW. And I hadn't considered su, but it seems like that will cause issues not matter what you do, yes?

    If God had meant us to fly, he would *never* have give us the railroads.
        --Michael Flanders

      Danger Will Robinson!

      This code will fail if the user has employed su(1) to change who they are on the system. If you su to root and run this code, you retrieve the original name. Which may or may not be what you wanted...

      g r i n d e r
The story behind GECOS
by grinder (Bishop) on Jul 26, 2001 at 12:50 UTC
    GECOS is/was an operating system developed by General Electric in the early sixties. It stands for GE Comprehensive Operating System. It was designed to run on some sort of GE mainframe.

    In the late sixties, a newer version of the same mainframe was selected by Bell Laboratories to use as the platform for the Multics project. You can see where this is headed.

    GE was then bought out by Honeywell, who promptly dropped the E in the name, and it became GCOS (for General Comprehensive...). There was a war between the GCOS and Multics crews, and GCOS won, which led to the demise of Multics.

    When the Multics project got canned, a couple of bright engineers thought it would be fun to hack some rudimentary OS tools on a discarded PDP-7 minicomputer... Some early Unix systems running at Bell ran on GCOS hardware, or else they talked to them, I'm not sure which. In any event the information needed to connect to them was stored in what was referred to as the GECOS field. And there you have it.

    g r i n d e r

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://99806]
[shmem]: your scanner is pre-loaded ;-)
[shmem]: anyways, do as is your wont. I won't fight western hypocresy anymore - for today. Let's call that a day, and done.
[erix]: yeah, scanning pre-loaded. I wouldn't have it any other way. As I say, my attention is a rare commodity, jealously guarded.
[erix]: ok, ok. happy pipe-dreams :P

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2018-03-19 21:12 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.