What do I do wrong?
Kartik Subbarao
subbarao at phoenix.Princeton.EDU
Thu Dec 13 14:32:31 AEST 1990
In article <1990Dec13.023035.19793 at IRO.UMontreal.CA> quennevi at IRO.UMontreal.CA (Charles Quenneville) writes:
>Hello!
Hi! Say, didnt you crosspost the article before this before? I thought we
went over that....
>Could you tell me what is wrong with this program? All I'm trying to do
>is simply to print the username.
>#include <pwd.h>
>#include <stdio.h>
Ah. I see you're going to use the right routines at least.
>main ()
> {
> struct passwd *pwd;
> struct passwd *getpwent();
>
> setpwent("/etc/passwd");
/* setpwent() doesn't take any arguments.
* And if you were going to use fsetpwent() -- this is the default unless
* you're using yellow pages and for some reason only want to search
* "/etc/passwd".
*/
> while ((pwd = getpwent()) != 0)
> {
> printf("\n", pwd->pw_name);
/* err..... all this does is print newlines. Shouldn't there be a
* %s somewhere ? Or better yet, replace the printf() with puts().
*/
> fflush(stdout);
/* Another needless thing */
> }
> endpwent("/etc/passwd");
/* likewise needless -- endpwent() takes no arguments either btw. */
> }
This is all you need:
# include <stdio.h>
# include <pwd.h>
main() { struct passwd *pwd; while (pwd = getpwent()) puts(pw->pw_name); }
/* a one liner, minus includes :-) */
> Charles Quenneville
> quennevi at kovic.iro.umontreal.ca
>
>"Ventrebleu, de par ma chandelle verte, j'aime mieux etre gueux"
Don't know what this means, but:
"Shouldn't you read man pages and check code before you post it to the net?" */
-Kartik
--
(I need a new .signature -- any suggestions?)
subbarao@{phoenix or gauguin}.Princeton.EDU -|Internet
kartik at silvertone.Princeton.EDU (NeXT mail) -|
SUBBARAO at PUCC.BITNET - Bitnet
More information about the Comp.unix.questions
mailing list