From: Nikola <root.admin1@zg.t-com.hr>
To: linux-c-programming@vger.kernel.org
Subject: Pipes and passwd programm problem
Date: Thu, 29 Mar 2007 04:41:48 +0200 [thread overview]
Message-ID: <460B276C.3090103@zg.t-com.hr> (raw)
Hi,
I would like to interface with passwd program in Debian Linux using
pipes to be able to change password from a C program.
Problem is in reading output from passwd program.
I have observed interesting behavior with passwd program.
---------------------------
[nikola@host][~]$ passwd
Changing password for zip
(current) UNIX password:
[ENTER]
passwd: Authentication failure
[nikola@host][~]$
[nikola@host][~]$ passwd 1> /dev/null
(current) UNIX password:
[ENTER]
passwd: Authentication failure
[nikola@host][~]$
[nikola@host][~]$ passwd 2> /dev/null
Changing password for zip
[ENTER]
[nikola@host][~]$
---------------------------
Program is using both STDOUT and STDERR for output.
program.c
---------------------------
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#define BSIZE 500
int read_chars(FILE *rstream){
char ch;
while(!feof(rstream)){
ch = fgetc(rstream);
printf("%c",ch);
}
fflush(NULL);
return 0;
}
int main(int argc, char **argv){
int rfds[2];
int stdout_wfds[2];
int stderr_wfds[2];
pid_t pid;
if(argc < 3){
fprintf(stderr,"Error: In argument count\n");
exit(1);
}
pipe(rfds);
pipe(stdout_wfds);
pipe(stderr_wfds);
pid = fork();
if( pid == 0 ){ // child
close(rfds[1]);
close(stdout_wfds[0]);
close(stderr_wfds[0]);
dup2(rfds[0],STDIN_FILENO);
dup2(stdout_wfds[1],STDOUT_FILENO);
dup2(stderr_wfds[1],STDERR_FILENO);
execlp("passwd","passwd",0);
} else { // parent
FILE *rstream_out,*rstream_err,*wstream;
char buffer[BSIZE];
wstream = fdopen(rfds[1],"w");
rstream_out = fdopen(stdout_wfds[0],"r");
rstream_err = fdopen(stderr_wfds[0],"r");
// PROBLEM !!!!!!!!!!!!!!!!!!!
// this is supposed to read 2 lines of passwd output
// but seems it doesn't work
read_chars(rstream_out);
read_chars(rstream_err);
close(rfds[1]);
close(stdout_wfds[0]);
close(stderr_wfds[0]);
waitpid(pid,NULL,0);
}
return 0;
}
---------------------------
tnx in advance.
Nikola.
next reply other threads:[~2007-03-29 2:41 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-03-29 2:41 Nikola [this message]
2007-03-29 3:35 ` Pipes and passwd programm problem Glynn Clements
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=460B276C.3090103@zg.t-com.hr \
--to=root.admin1@zg.t-com.hr \
--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).