linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* struct passwd usage
@ 2003-04-30 20:49 Fabio Miranda Hamburger
  2003-04-30 22:37 ` Rafael Santos
  2003-05-01  0:55 ` Glynn Clements
  0 siblings, 2 replies; 4+ messages in thread
From: Fabio Miranda Hamburger @ 2003-04-30 20:49 UTC (permalink / raw)
  To: linux-c-programming

what is wrong with this code:

main(){
struct passwd *pwd;
char *user;
puts("username:");
pwd=getpwnam(user);
puts(pwd->pw_name);
}


---
Fabio Andres Miranda
Ingenieria de sistemas informaticos
Universidad Latina - Costa Rica




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: struct passwd usage
  2003-04-30 20:49 struct passwd usage Fabio Miranda Hamburger
@ 2003-04-30 22:37 ` Rafael Santos
  2003-05-01  0:55 ` Glynn Clements
  1 sibling, 0 replies; 4+ messages in thread
From: Rafael Santos @ 2003-04-30 22:37 UTC (permalink / raw)
  To: Fabio Miranda Hamburger; +Cc: linux-c-programming

4/30/03 5:49:13 PM, Fabio Miranda Hamburger <fabmirha@ns.isi.ulatina.ac.cr> wrote:

>what is wrong with this code:
>
>main(){
>struct passwd *pwd;
>char *user;

Your string (user) does not have a defined size, so it won't work.

>puts("username:");

The parameter for puts() is the array that you want to recieve, but this is not good, because it can 
cause memory overflow, when you have an array smaller then the entry.

>pwd=getpwnam(user);
>puts(pwd->pw_name);
>}
>

#define USERNAME_MAX 100

main()
{
	struct passwd* pwd;
	// What is between [] defines the size of the string + 1 (the last one indicates the
	// of the string (\0)
	char user[USERNAME_MAX+1];

	printf("username: ");
	// Gets the string of maximum size USERNAME_MAX, from the keyboard (STDIN)
	fgets(user,USERNAME_MAX,STDIN);

	pwd=getpwnam(user);
	puts(pwd->pw_name);
}

Maybe you will have to allocate memory for structure passwd. I don't know that for sure, because I have 
never used that before.

>
>---
>Fabio Andres Miranda
>Ingenieria de sistemas informaticos
>Universidad Latina - Costa Rica
>
>
>
>-
>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>
Rafael Costa dos Santos
ThinkFreak Comércio e Soluções em Hardware e Software
Rio de Janeiro / RJ / Brazil
rafael@thinkfreak.com.br
+ 55 21 9432-9266


-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: struct passwd usage
  2003-04-30 20:49 struct passwd usage Fabio Miranda Hamburger
  2003-04-30 22:37 ` Rafael Santos
@ 2003-05-01  0:55 ` Glynn Clements
  1 sibling, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2003-05-01  0:55 UTC (permalink / raw)
  To: Fabio Miranda Hamburger; +Cc: linux-c-programming


Fabio Miranda Hamburger wrote:

> what is wrong with this code:
> 
> main(){
> struct passwd *pwd;
> char *user;
> puts("username:");
> pwd=getpwnam(user);

The variable "user" points to a random memory location at this point. 
getpwnam() expects its argument to point at a valid string, which is
the username for which to retrieve the passwd entry.

> puts(pwd->pw_name);
> }

If you meant to prompt for the username, you need to do something like
this:

int main(void)
{
	struct passwd *pwd;
	char user[64];

	puts("username:");
	gets(user);
	pwd=getpwnam(user);
	puts(pwd->pw_name);

	return 0;
}

Except that you should add error checking, use fgets() instead of
gets() etc.

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: struct passwd usage
@ 2003-05-01  2:10 Rafael Santos
  0 siblings, 0 replies; 4+ messages in thread
From: Rafael Santos @ 2003-05-01  2:10 UTC (permalink / raw)
  To: Fabio Miranda Hamburger; +Cc: linux-c-programming

A complete functional program solving your problem:

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

#include <stdio.h> //stdin

#define MAX_LOGIN_SIZE 100

int main(void)
{
        char name[MAX_LOGIN_SIZE+1];
        struct passwd* pwd;

        printf("Digite o Username: ");

        // Gets "Username" from the keyboard
        fgets(name,MAX_LOGIN_SIZE,stdin);

        // Exchanges the character '\n' for '\0'
        name[strlen(name) - 1] = '\0';

        printf("\n");

        // Reads from /etc/passwd
        if( (pwd = getpwnam(name)) != NULL )
        {
                printf("Username:%s\n", pwd->pw_name);
                printf("Passwrod:%s\n", pwd->pw_passwd);
                printf("Real Name:%s\n", pwd->pw_gecos);
                printf("Directory:%s\n", pwd->pw_dir);
                printf("Shell:%s\n",pwd->pw_shell);
                printf("UserID:%d\n",pwd->pw_uid);
        }
        else
                printf("The user was not found on the system.\n");
}


Rafael Costa dos Santos
ThinkFreak Comércio e Soluções em Hardware e Software
Rio de Janeiro / RJ / Brazil
rafael@thinkfreak.com.br
+ 55 21 9432-9266




-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-05-01  2:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-04-30 20:49 struct passwd usage Fabio Miranda Hamburger
2003-04-30 22:37 ` Rafael Santos
2003-05-01  0:55 ` Glynn Clements
  -- strict thread matches above, loose matches on Subject: below --
2003-05-01  2:10 Rafael Santos

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).