various doubts part 2
Heres the next installment of the great series (please keep this series alive by donating to the ABsolutely Crappy tv company (ABC).)
1) I'm trying to simulate what sendmail does when it has a command as a user, so I run my program like this: a.out<text
However , When it read()s , only one character is copied into the
buffer.
if text =
Quoted 1 line
From : .........
the only cahracter in buffer is F.
how do I work around this?
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<sys/timeb.h>
#include<ctype.h>
#include<fcntl.h>
#define MAX_FILENAME 20
#define MAX_BUFF 1024
#define FIFO_NAME "/tmp/Vpim_messenger"
char filename[MAX_FILENAME];
int main()
{
struct timeb tp;
int outfile , fifo ,bytes_read,bytes_written, locked=1;
char buff[MAX_BUFF];
char message[MAX_BUFF] ;
ftime( &tp);
sprintf(filename,"/tmp/%d.%d",tp.time,getpid());
sprintf(message,"INFILE: %s",filename);
outfile = open(filename,O_WRONLY|O_CREAT,S_IRWXU|S_IRWXO);
while((bytes_read = read(STDIN_FILENO,buff,sizeof(buff))>0))
{
if((bytes_written = write(outfile , buff ,bytes_read))!=bytes_read)
{
fprintf(stderr,"write error");
exit(1);
}
}
fifo = open(FIFO_NAME,O_WRONLY,S_IWUSR);
while(locked)
{
if(!(lockf(fifo,F_TEST,0)))
{
lockf(fifo,F_LOCK,0);
locked = 0;
}
}
if(bytes_written = write(fifo , message , sizeof(message))!= sizeof(message))
{
fprintf(stderr,"write error");
exit(1);
}
return 1;
}Am Di, 2004-08-10 um 04.53 schrieb joy:
Quoted 9 lines
outfile = open(filename,O_WRONLY|O_CREAT,S_IRWXU|S_IRWXO);
while((bytes_read = read(STDIN_FILENO,buff,sizeof(buff))>0))
{
if((bytes_written = write(outfile , buff ,bytes_read))!=bytes_read)
{
fprintf(stderr,"write error");
exit(1);
}
}Why not reading from 'stdin' directly ? That FD is defined in stdio.h iirc... What is STDIN_FILENO anyway :) ?
Quoted 15 lines · Sascha Retzki
Am Di, 2004-08-10 um 04.53 schrieb joy:
Quoted 11 lines
outfile = open(filename,O_WRONLY|O_CREAT,S_IRWXU|S_IRWXO); while((bytes_read = read(STDIN_FILENO,buff,sizeof(buff))>0)) { if((bytes_written = write(outfile , buff ,bytes_read))!=bytes_read) { fprintf(stderr,"write error"); exit(1); } }
Quoted 4 lines
Why not reading from 'stdin' directly ? That FD is defined in
stdio.hiirc... What isSTDIN_FILENOanyway :) ?
Also a defined contant, and corresponds to the fd of stdin (0).(Stevens, adv., prog in unix env.,) Anyways ,I used fread and fwrite and the fellow seems to work.
guess you have to treat stdin as streams only.
Joy.M.M
Em Wed, 11 Aug 2004 08:22:08 +0530
joy <[email protected]> screveu:
| Sascha Retzki wrote:
|
| >Am Di, 2004-08-10 um 04.53 schrieb joy:
| >
| >
| >> outfile = open(filename,O_WRONLY|O_CREAT,S_IRWXU|S_IRWXO);
| >> while((bytes_read = read(STDIN_FILENO,buff,sizeof(buff))>0))
| >> {
| >> if((bytes_written = write(outfile , buff ,bytes_read))!=bytes_read)
| >> {
| >> fprintf(stderr,"write error");
| >> exit(1);
| >> }
| >> }
| >>
| >>
| >
| >Why not reading from 'stdin' directly ? That FD is defined in stdio.h
| >iirc... What is STDIN_FILENO anyway :) ?
| >
| >
| Also a defined contant, and corresponds to the fd of stdin
| (0).(Stevens, adv., prog in unix env.,)
| Anyways ,I used fread and fwrite and the fellow seems to work.
| guess you have to treat stdin as streams only.Don't, I found the bug:
while((bytes_read = read(STDIN_FILENO,buff,sizeof(buff))>0))
look where ">0" is inside... If you change to:
while((bytes_read = read(STDIN_FILENO,buff,sizeof(buff)))>0)
it will work... man, this bug needed about 20 min to be catch up...
every time I did run the program I tought "it _must_ work, all is
right"... Only saw the bug when I started to write it again myself. :)
and I didn't played with the second part of the code, because
I didn't understand very well what do you want.
PS(0): I'm not a C expert, but I advice you: *always* try to write clean code.
PS(1): I worked in this because I can't sleep when something which should work don't work.
Signature · 1 line
Luiz Fernando
Quoted 10 lines · Luiz Fernando N. Capitulino
Don't, I found the bug:
while((bytes_read = read(STDIN_FILENO,buff,sizeof(buff))>0))look where ">0" is inside... If you change to:
while((bytes_read = read(STDIN_FILENO,buff,sizeof(buff)))>0)
Ouch as usual, the retard in me begins to show up.........
Quoted 7 lines
it will work... man, this bug needed about 20 min to be catch up...
every time I did run the program I tought "it _must_ work, all is right"... Only saw the bug when I started to write it again myself. :)
You DO have some kind of obsessive compulsive disorder ( You remind me of myself :-)
Quoted 12 lines
and I didn't played with the second part of the code, because I didn't understand very well what do you want.
PS(0): I'm not a C expert, but I advice you: *always* try to write clean code.
PS(1): I worked in this because I can't sleep when something which should work don't work.
Thanks a lot, anyway I had rewritten the code using frread and fwrite, forgot to do an fflush, got empty output files , almost broke my computer , added an fclose just after the fwrite, got nice output files, cried in happiness, and swore to write clean code an hour before I read this mail ;-).
regards,
Joy.