quennevi@IRO.UMontreal.CA (Charles Quenneville) (12/13/90)
Hello! 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> main () { struct passwd *pwd; struct passwd *getpwent(); setpwent("/etc/passwd"); while ((pwd = getpwent()) != 0) { printf("\n", pwd->pw_name); fflush(stdout); } endpwent("/etc/passwd"); } -- Charles Quenneville quennevi@kovic.iro.umontreal.ca "Ventrebleu, de par ma chandelle verte, j'aime mieux etre gueux
subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (12/13/90)
In article <1990Dec13.023035.19793@IRO.UMontreal.CA> quennevi@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@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@silvertone.Princeton.EDU (NeXT mail) -| SUBBARAO@PUCC.BITNET - Bitnet
tchrist@convex.COM (Tom Christiansen) (12/14/90)
In article <1990Dec13.023035.19793@IRO.UMontreal.CA> quennevi@IRO.UMontreal.CA (Charles Quenneville) writes:
:Hello!
:Could you tell me what is wrong with this program? All I'm trying to do
:is simply to print the username.
You didn't lint it. I'll leave the flames to someone else.
--tom
--
Tom Christiansen tchrist@convex.com convex!tchrist
"With a kernel dive, all things are possible, but it sure makes it hard
to look at yourself in the mirror the next morning." -me
krader@crg8.sequent.com (Kurtis D. Rader) (12/14/90)
In article <1990Dec13.023035.19793@IRO.UMontreal.CA> quennevi@IRO.UMontreal.CA (Charles Quenneville) writes:
%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>
%
%main ()
% {
% struct passwd *pwd;
% struct passwd *getpwent();
Doesn't pwd.h include a declaration for getpwent(3)?
%
% setpwent("/etc/passwd");
At least in BSD 4.2 setpwent(3) doesn't take any arguments, so this
should be
setpwent();
% while ((pwd = getpwent()) != 0)
% {
% printf("\n", pwd->pw_name);
Try
printf ("%s\n", pwd->pw_name);
% fflush(stdout);
% }
% endpwent("/etc/passwd");
Likewise, endpwent(3) doesn't take any arguments either.
% }
--
Kurtis D. Rader, Technical Support Engineer voice: 503/578-3714
Service Hotline, Sequent Computer Systems fax: 503/578-3731
15450 SW Koll Parkway, M/S UMP2-502 UUCP: ...uunet!sequent!krader
Beaverton, OR 97006-6063 internet: krader@sequent.com
jik@athena.mit.edu (Jonathan I. Kamens) (12/14/90)
(Note the Followup-To. This is not a wizard-level question, by any stretch of the imagination.) In article <1990Dec13.023035.19793@IRO.UMontreal.CA>, quennevi@IRO.UMontreal.CA (Charles Quenneville) writes: |> #include <pwd.h> |> #include <stdio.h> |> |> main () |> { |> struct passwd *pwd; |> struct passwd *getpwent(); |> |> setpwent("/etc/passwd"); This should either be setpwent(); or setpwfile("/etc/passwd"); setpwent(); Although your program will work the way you have it, it's still wrong. |> while ((pwd = getpwent()) != 0) |> { |> printf("\n", pwd->pw_name); This should be printf ("%s\n", pwd->pw_name); This is the bug that's causing your program to fail. |> fflush(stdout); This is unnecessary, as long as your stdout is line-buffered, and it almost certainly is. Once again, your program will work with this line, but you probably don't need it. |> } |> endpwent("/etc/passwd"); This should be endpwent(); Once again, not a required change, but it's still wrong the way you have it. |> } \begin{small-simmering-flame} Don't you think that all of these problems are just a bit too basic for you to be asking the entire Usenet about them? Isn't there anybody at your site with whom you could have consulted before sending a message to the entire net? \end{small-simmering-flame} -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710
markt@polari.UUCP (Mark Tapper) (12/17/90)
In article <1990Dec13.023035.19793@IRO.UMontreal.CA> quennevi@IRO.UMontreal.CA (Charles Quenneville) writes: >Hello! > >Could you tell me what is wrong with this program? All I'm trying to do >is simply to print the username. > Deleted most of small program to read names from the passwd file. > printf("\n", pwd->pw_name); Here is your problem, you forgot to put a %s in the printf. it should look like this. printf("%s\n", pwd->pw_name); >-- > Charles Quenneville > quennevi@kovic.iro.umontreal.ca > >"Ventrebleu, de par ma chandelle verte, j'aime mieux etre gueux