TIPI wrote:

>     i am doing a mail distributor program. It is a little program that
> reads /var/spool/mail/%USERNAME% and write its mails in differents files
> according to a rules file (generally it look at the Sender to know the
> distribution list). I have a skeleton like this:

[snip]

>     Here is my questions:
> 
>     1. I want to know how can i read a message of the Inbox. Actually a
> read by line with fgets and i look for the "From " line, but when i get
> the next "From  " line I know it is a new email. My problem is that file
> pointer of read is in the next line and i don't want it. How can I
> obtain the different messages in a optimal form?.
> 
> 
>         fgets(line,512,fdInbox);
>         strcat(email, line);
>         while(IsNotFromLine(fgets(line,512,fdInbox))) {
>             strcat(email,line);
>         }
> 
>         /* this ends with the first line of the next email in the line
> variable. Then when i invoke next time the function, it not read the
> >From line of the email. */

You can't determine when you've reached the end of a message without
reading the first line (or at least the first 5 bytes) of the next
message. If you don't want the file pointer to be changed, use ftell()
and fseek(), e.g.

        int IsFromLine(FILE *fp)
        {
                char buff[1024];
                long pos;
                int result;

                pos = ftell(fp);

                if (pos < 0)
                {
                        perror("IsFromLine: ftell");
                        return 0;
                }

                if (!fgets(buff, sizeof(buff), fp))
                {
                        fprintf(stderr, "IsFromLine: fgets error");
                        return 0;
                }

                result = (strncmp(buff, "From ", 5) == 0);

                fseek(fp, pos, SEEK_SET);

                return result;
        }

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to