linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "J." <mailing-lists@xs4all.nl>
To: linux-c-programming@vger.kernel.org
Subject: Re: getlogin(), prints ((NULL)) solved.. thnkx.. glen.
Date: Thu, 13 Mar 2003 01:18:39 +0100 (CET)	[thread overview]
Message-ID: <Pine.LNX.4.21.0303130054060.15029-100000@hestia> (raw)
In-Reply-To: <15983.48975.608147.30390@cerise.nosuchdomain.co.uk>

On Wed, 12 Mar 2003, Glynn Clements wrote:

> 
> Jeroen Reynders. wrote:
> 
> > But, Everytime when I execute the following C code (bottom) from within a
> > shell loop as in:
> > 
> > while : ; do ./program ; done
> > 
> > it prints: ((NULL))
> > 
> > But when I excute it in the normal way:
> > ./program
> > It prints perfectly ok: myusername
> > 
> > C Code.
> > #include <stdio.h>
> > #include <unistd.h>
> > #include <sys/types.h>
> > 
> > int main(void) {
> >  char *username = NULL;
> > 
> >  username = getlogin();
> >  /* for(;;) */
> >   printf("%s\n", username);
> > 
> >  return 0;
> > }
> > 
> > When I call the printf from a for loop in the native C code it also
> > starts to print ((NULL)) after a couple of loops.
> 
> Odd; for me, it's consistent: it either always prints "glynn" or
> always prints "(null)".

Cool thankx for trying...

> But is getlogin() (i.e. the name from the utmp entry for the
> controlling tty) really the information that you want? Or would you be
> better off with:
> 
> 	struct passwd *pw = getpwuid(getuid());
> 	pw ? pw->pw_name : "<none>"
> 
> ?
> 
> Bear in mind that not all processes have a controlling terminal, and
> even for those that do, there may not be an associated utmp entry.

I havent thought of that one, it's indeed a good one to remember...

I have searched all the documentation I could find this evening and
eventually going back to the getlogin() manual page. It seems that I have
overlooked the `BUGS' section in the end of the manual page.

BUGS
    Unfortunately, it is often rather easy to fool getlogin().  Sometimes
    it does not work at all, because some program messed up the utmp
    file. Often, it gives only the first 8 characters of the login
    name. The user currently logged in on the controlling tty of our
    program need not be the user who started it.  Avoid getlogin() for
    security-related purposes.

Hmmm... Again a type of lesson I am not going to forgett easily ... :)

Anyway I solved the problem the way you suggested, the following works
fine on my system.

#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(void) {
 uid_t uid;
 struct passwd *pw;

 uid = getuid();

 pw = getpwuid(uid);
 printf("my name is: %s\n", pw->pw_name);

 return 0;
}

Thank you for helping me out. 

> -- 
> Glynn Clements <glynn.clements@virgin.net>

Jeroen Reynders


      reply	other threads:[~2003-03-13  0:18 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-05  7:02 structure size Mohammed Khalid Ansari
2003-03-05  8:27 ` Helmut Djurkin
2003-03-25 10:00   ` strange SIGSEGV Alexi Jordanov
2003-03-05  9:06 ` (F)structure size Steven Smith
2003-03-12 22:49 ` getlogin(), prints ((NULL)) when printing multiple times in a row J.
2003-03-12 23:14   ` Glynn Clements
2003-03-13  0:18     ` J. [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.21.0303130054060.15029-100000@hestia \
    --to=mailing-lists@xs4all.nl \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).