Re: Disappearing emails in bsd-mailx 8.1.2-0.200909
Debian Bug report logs - #642940


Hello,

Nobody replied to this bug report. Am I the only one
with this problem (or maybe the only one still using
mailx)?

Anyhow, I attach a temporary fix. The attached program
fixmail.c will automatically fix your mailbox. I run
my e-mail via the mailx script (also attached) that
will automatically run fixmail.

There are certainly better ways to do this... but the
most important is not losing e-mails!

Cheers,
Franco


/*   Fixes corrupt files due to buggy bsd-mailx   */
/*   that "forgets" line feed between messages    */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>


#define MAXLINE 10000


void error(const char *fmt,...)
{
        va_list ap;

        va_start(ap,fmt);
        fprintf(stderr, "fixmail: ");
        vfprintf(stderr,fmt,ap);
        putc('\n',stderr);
        va_end(ap);
        exit(EXIT_FAILURE);
}


int fgetline(FILE *flp,char buffer[],const int max,const int sklead)
{
        int i,c;

        i = 0;
        while ((c=getc(flp))!='\n' && c!=EOF)
                if ((i<max-1 || max==-1) && !(sklead && i==0 && isspace(c)))
                        buffer[i++] = c;
        if (i<=max-1 || max==-1)
                buffer[i]='\0';
        if (c==EOF && i==0)
                return -1;
        if (c==EOF)
                ungetc(c,flp);
        return (i+1);
}


FILE *ffopen(const char *path,const char *fln, const char *mode)
{
        char buffer[MAXLINE];
        FILE *flp;

        sprintf(buffer,"%s%s",path,fln);
        if ((flp=fopen(buffer,mode))==NULL)
                error("ffopen(): Couldn't open file %s.",buffer);
        return flp;
}


int ssystem(const char *string)
{
        int excode;

        if ((excode=system(string))!=0 && string!=NULL)
                error("ssystem(\"%s\"): exit code %d.", string, excode);
        return(excode);
}


int main(int n, char *p[])
{
        FILE *in, *out;
        char *infln, outfln[MAXLINE], tmpfln[MAXLINE];
        char buffer[MAXLINE];
        int l, old_l = 1, fix=0;

        if (TMP_MAX<2)
                error("Can't make temporary files: this system has TMP_MAX<2");

        if (n > 1)
                infln = p[1];
        else
                if ((infln = getenv("MAIL"))==NULL)
                        error("Undefined $MAIL");
        in = ffopen("", infln, "r");

        tmpnam(outfln);
        out = ffopen("", outfln, "w");

        while ((l = fgetline(in, buffer, MAXLINE, 0)) > 0)
                {
                if (l >= MAXLINE)
                        error("%s: Long buffer: %100.100s", infln, buffer);
                if (strncmp(buffer, "From ", 5)==0 && old_l > 1)
                        {
                        putc('\n', out);
                        if (!fix)
                                printf("Fixing %s:\n", infln);
                        printf("  %s\n", buffer);
                        ++fix;
                        }
                fprintf(out, "%s\n", buffer);
                old_l = l;
                }

        fclose(out);
        fclose(in);

        if (fix > 0)
                {
                tmpnam(tmpfln);
                sprintf(buffer, "cp %s %s", infln, tmpfln);
                ssystem(buffer);
                sprintf(buffer, "cp %s %s", outfln, infln);
                ssystem(buffer);
                printf("fixmail: %d fixes applied (%s backed up as %s).\n",
                        fix, infln, tmpfln);
                }
}
exec mail $*
fixmail

Reply via email to