Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getlogin() returns NULL?

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
Linux: gcc version 2.95.3
Linux 2.4.14 #2 SMP Wed Nov 21 14:43:43 EST 2001 i686

I have the following trivial program:
void revealAll(struct passwd *);

int main(int argc, char **argv) {
char *login;
struct passwd *nameinfo;

if ((login = getlogin()) == NULL) {
perror("Getlogin()");
}
nameinfo = getpwnam(argv[1]);
revealAll(nameinfo);
return 0;
}


void revealAll(struct passwd *passptr) {

if (passptr == NULL) {
printf("Error, no info derived from getpwnam()\n");
exit(1);
}

printf("name: %s\n passwd: %s\n uid: %d\n
gid: %d\n realname: %s\n homedir: %s\n, shell: %s\n",
passptr->pw_name,passptr->pw_passwd,passptr->pw_uid,
passptr->pw_gid,passptr->pw_gecos,passptr->pw_dir,passptr->pw_shell);
}

The getlogin() call always fails..any ideas?
For example:

for xx in $(awk -F":" ' $1 ~ /m.*/ {print $1}' /etc/passwd); do progname $xx; done

Will return:
Getlogin(): No such file or directory
name: amanda
passwd: x
uid: 37
gid: 6
realname: Amanda Admin
homedir: /var/lib/amanda
shell: /bin/bash
 
yes i also got troubles with 'getlogin()', but in your code
you don't need it !
on my solaris, assumed you call this prog with strings-args,
not user-id, i would write:
/* not tested, i am not on an unix-box at the moment :( */

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

typedef struct pwd PWD;

int main(int argc, char **argv)
{
int revealAll(PWD *);

for(++argv; *argv; ++argv){
/* reset the entry point */
setpwent(); /* ??? look for syntax in man pages */
if(revealAll(getpwnam(*argv)))
printf(&quot;%s: unknown user\n&quot;,*argv);
}
exit(0);
}
int revealAll(PWD *qqq)
{
if(!qqq) return(1);
printf(&quot;some data of qqq\n&quot;);
return(0);
}

i also believe, you don't use the old good 'vi' for editing:
-----------------
int qqq(...){
}
----------------- AND
int qqq(...)
{
}
----------------ARE really not the same in 'vi'
-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Don't like vi, or emacs for that matter.
I know it's blasphemy but I like xedit or
even kedit.

The program was just an example, it's not a real problem..I just wanted to demonstrate that getlogin() doesn't return
anything. The rest was superfluous.

It's good to know that the getlogin() problem is not
only me and that there are other ways around it however!

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top