linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* various doubts part 2
@ 2004-08-10  2:53 joy
  2004-08-10  7:04 ` Sascha Retzki
  0 siblings, 1 reply; 5+ messages in thread
From: joy @ 2004-08-10  2:53 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 439 bytes --]

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 =

 >From : .........

the only cahracter in buffer is F.
how do I work around this?



[-- Attachment #2: mailLDA.c --]
[-- Type: text/plain, Size: 1035 bytes --]

#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;
}

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

* Re: various doubts part 2
  2004-08-10  2:53 various doubts part 2 joy
@ 2004-08-10  7:04 ` Sascha Retzki
  2004-08-11  2:52   ` joy
  0 siblings, 1 reply; 5+ messages in thread
From: Sascha Retzki @ 2004-08-10  7:04 UTC (permalink / raw)
  To: linux-c-programming

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 :) ?


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

* Re: various doubts part 2
  2004-08-10  7:04 ` Sascha Retzki
@ 2004-08-11  2:52   ` joy
  2004-08-11 22:21     ` Luiz Fernando N. Capitulino
  0 siblings, 1 reply; 5+ messages in thread
From: joy @ 2004-08-11  2:52 UTC (permalink / raw)
  To: lantis, linux-c-programming

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.

Joy.M.M

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

* Re: various doubts part 2
  2004-08-11  2:52   ` joy
@ 2004-08-11 22:21     ` Luiz Fernando N. Capitulino
  2004-08-12 10:49       ` joy
  0 siblings, 1 reply; 5+ messages in thread
From: Luiz Fernando N. Capitulino @ 2004-08-11 22:21 UTC (permalink / raw)
  To: gracecott; +Cc: lantis, linux-c-programming

Em Wed, 11 Aug 2004 08:22:08 +0530
joy <gracecott@sancharnet.in> 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.


-- 
Luiz Fernando

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

* Re: various doubts part 2
  2004-08-11 22:21     ` Luiz Fernando N. Capitulino
@ 2004-08-12 10:49       ` joy
  0 siblings, 0 replies; 5+ messages in thread
From: joy @ 2004-08-12 10:49 UTC (permalink / raw)
  To: Luiz Fernando N. Capitulino; +Cc: lantis, linux-c-programming

Luiz Fernando N. Capitulino wrote:

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

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

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

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

end of thread, other threads:[~2004-08-12 10:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-08-10  2:53 various doubts part 2 joy
2004-08-10  7:04 ` Sascha Retzki
2004-08-11  2:52   ` joy
2004-08-11 22:21     ` Luiz Fernando N. Capitulino
2004-08-12 10:49       ` joy

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